我写了这段代码来获得大量的爆炸结果,但它看起来有点慢,因为我使用两个“for”循环来迭代两个文件。所以我想知道是否有一种更快、贪婪的方法来缩小范围迭代。
这是代码
for tf_line in SeqIO.parse('deneme2.txt','fasta'):
tf_line.description=tf_line.description.split()
tempfile=open('tempfile.txt','w')
for cd_line in SeqIO.parse('Mus_musculus.GRCm38.74.cdna.all.fa','fasta'):
if cd_line.id==tf_line.description[1]:
tempfile.write('>'+cd_line.id+'\n'+
str(cd_line.seq)[int(tf_line.description[2])-100:
int(tf_line.description[3])+100])
tempfile.close()
os.system('makeblastdb -in tempfile.txt -dbtype nucl '
'-out tempfile.db -title \'tempfile\'')
cline = NcbiblastnCommandline(query='SRR029153.fasta' ,
db="tempfile.db",
outfmt=7,
out=(tf_line.description[0]+' '+
tf_line.description[1]))
stdout,stderr=cline()
“deneme.txt”大小为 30 Mb,如下所示:
SRR029153.93098 ENSMUST00000103567 999 1147 TCAGGCCAAGTTTCTCTC
SRR029153.83280 ENSMUST00000181483 151 425 CAGGTTGAC
SRR029153.108993 ENSMUST00000184883 174 1415 TGGCACCTTTGC .....
“Mus_musculus.GRCm38.74.cdna.all.fa”文件大小为 170 Mb,如下所示:
ENSMUST00000181483 ACACTGAAGAT.....
ENSMUST00000184883 ATCTTTTTTCTTTCAGGG.....
“Mus_musculus.GRCm38.74.cdna.all.fa”文件有一些序列 ID(ENSMUST...)。我必须找到“deneme.txt”文件和“Mus_musculus.GRCm38.74.cdna”之间的匹配项。全部.fa.
应该需要 4-5 小时,但使用此代码至少需要 10 小时
任何帮助将不胜感激,因为我必须摆脱这样的残酷算法并变得更加贪婪。 谢谢
最佳答案
我认为这仍然会产生相同的爆炸,但应该更快。阅读代码中的注释以进行更多优化:
tf_data = {key: (int(val1), int(val2)) for key, val1, val2 in
(line.description.split() for line in
SeqIO.parse('deneme2.txt','fasta'))}
for cd_line in SeqIO.parse('Mus_musculus.GRCm38.74.cdna.all.fa','fasta'):
if cd_line.id in tf_data;
tempfile=open('tempfile.txt','w')
tf_val1, tf_va2 = tf_data[cd_line.id]
#If it is likely that the same tf_data-record is used many times
#move the math to the first line, if on the other hand it is
#very likely that most records won't be used in tf_data then
#move the int-casts back to the line below
tempfile.write('>{0}\n{1}'.format(
cd_line.id,
str(cd_line.seq)[tf_val1 - 100: tf_val2 + 100]))
tempfile.close()
os.system('makeblastdb -in tempfile.txt -dbtype nucl '
'-out tempfile.db -title \'tempfile\'')
cline = NcbiblastnCommandline(
query='SRR029153.fasta',
db="tempfile.db",
outfmt=7,
out=("{0} {1}".format(tf_val1, tf_val2)))
#Since not using stderr and stdout don't assign variables
cline()
关于python - python 中更快的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21890860/