python - python 中更快的方法

标签 python bioinformatics biopython

我写了这段代码来获得大量的爆炸结果,但它看起来有点慢,因为我使用两个“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/

相关文章:

Python Tkinter 按钮回调

r - R 版本 4.0.4 中的匹配函数

python - 查找允许某些不匹配的子字符串的快速方法

python - Biopython 成对对齐在循环中运行时导致段错误

linux - 如何将我的数据拆分成足够小的 block 以提供给 Seq?

python - 通过python计算fasta中的20聚体数量

由于存在 'Not well-formed xml' 个字符,Python 给出 '&' 错误

python - 在 jupyter-notebook 上导入失败

python - 为什么 on_message() 停止命令工作?

regex - 使用 sed 删除两个空格字符之间的字符串