かなり複雑なタスクを実行できるコードがあります(少なくとも私にとっては):
import csv
import os.path
#open files + readlines
with open("C:/Users/Ivan Wong/Desktop/Placement/Lists of targets/Mouse/UCSC to Ensembl.csv", "r") as f:
reader = csv.reader(f, delimiter = ',')
#find files with the name in 1st row
for row in reader:
graph_filename = os.path.join("C:/Python27/Scripts/My scripts/Top targets",row[0]+"_nt_counts.txt.png")
if os.path.exists(graph_filename):
y = row[0]+'_nt_counts.txt'
r = open('C:/Users/Ivan Wong/Desktop/Placement/fp_mesc_nochx/'+y, 'r')
k = r.readlines()
r.close
del k[:1]
k = map(lambda s: s.strip(), k)
interger = map(int, k)
import itertools
#adding the numbers for every 3 rows
def grouper(n, iterable, fillvalue=None):
"grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
args = [iter(iterable)] * n
return itertools.izip_longest(*args, fillvalue=fillvalue)
result = map(sum, grouper(3, interger, 0))
e = row[0]
print e
cDNA = open('C:/Users/Ivan Wong/Desktop/Placement/Downloaded seq/Mouse/MOUSE_mRNAs.txt', 'r')
seq = cDNA.readlines()
# get all lines that have a gene name
lineNum = 0;
lineGenes = []
for line in seq:
lineNum = lineNum +1
if '>' in line:
lineGenes.append(str(lineNum))
if '>'+e in line:
lineBegin = lineNum
cDNA.close
# which gene is this
index1 = lineGenes.index(str(lineBegin))
lineEnd = lineGenes[index1+1]
# linebegin and lineEnd now give you, where to look for your sequence, all that
# you have to do is to read the lines between lineBegin and lineEnd in the file
# and make it into a single string.
lineEnd = lineGenes[index1+1]
Lastline = int(lineEnd) -1
# in your code you have already made a list with all the lines (q), first delete
# \n and other symbols, then combine all lines into a big string of nucleotides (like this)
qq = seq[lineBegin:Lastline]
qq = map(lambda s: s.strip(), qq)
string = ''
for i in range(len(qq)):
string = string + qq[i]
# now you want to get a list of triplets, again you can use the for loop:
# first get the length of the string
lenString = len(string);
# this is your list codons
listCodon = []
for i in range(0,lenString/3):
listCodon.append(string[0+i*3:3+i*3])
proper_result = '\n'.join('%s, %s' % (nr, codon) for nr, codon in zip(result, listCodon))
with open(e+'.csv','wb') as outfile:
outfile.writelines(proper_result)
これらのコードは、.csv からファイルを読み取り、同じ名前のファイルを含むフォルダーから識別します。それらが存在する場合は、いくつかのデータを処理し、それらを .csv に書き込みます。私の出力ファイルは次のようになります。
完全に問題ないように見えますが、1 つの問題があり、データから (さまざまな方法で確認しました)、2 番目の列は取得したものよりも長くする必要があることがわかりました。結果(数字)とlistCodon(文字)の両方が存在するときにコードがファイルを書き込んでいるためだと思うので、何かが欠けています。どうすれば修正できますか?
ファイルが書き込まれる直前にlistCodonを印刷しようとしましたが、すべてのトリプレットがまだそこにあることがわかったので、問題はここにあると推測しています:
proper_result = '\n'.join('%s, %s' % (nr, codon) for nr, codon in zip(result, listCodon))