使用“Biopython” - 我如何改进我的代码

时间:2012-08-05 15:09:04

标签: python bioinformatics biopython

我有以下代码:

from Bio import AlignIO
import itertools

out=open("test.csv","a")
align = AlignIO.read("HPV16_CG.aln.fas", "fasta")
n=0
def SNP(line):  
    result=[]
    result.append(str(n+1))
    result.append(line[0])
    result.append(align[y].id.rsplit("|")[3])
    result.append(x)
    return result



while n<len(align[0]):
  line = align[:,n]
  y=0
  for x in line:
    if line[0]!=x:
      print >> out, ','.join(map(str,SNP(line)))
      y=y+1
    else:
      y=y+1
  y=0
  n=n+1
out.close()

f=open("test.csv","rU")

out=open("test_2.csv","a")
lines=f.read().split()

for key, group in itertools.groupby(lines, lambda line: line.partition(',')[0]):
    print >>out, ','.join(group)

out.close()
f.close()

如您所见,我目前正在撰写两个文件。我真的只需要第二个文件。 有没有人有任何建议将“下标”合二为一?

输入文件“HPV16_CG.aln.fas”如下所示:

>gi|333031|lcl|HPV16REF.1| Alpha-9 - Human Papillomavirus 16, complete genome. 
ACTACAATAATTCATGTATAAAACTAAGGGCGTAACCGAAATCGGTTGAACCGAAACCGG

>gi|333031|gb|K02718.1|PPH16 Human papillomavirus type 16 (HPV16), complete genome
ACTACAATAATTCATGTATAAAACTAAGGGCGTAACCGAAATCGGTTGAACCGAAACCGG

>gi|196170262|gb|FJ006723.1| Human papillomavirus type 16, complete genome
ACTACAATAATTCATGTATAAAACTAAGGGCGTAACCGAAATCGGTTGAACCGAAACCGG

我非常感谢帮助我改善这一切的所有帮助/建议!

1 个答案:

答案 0 :(得分:1)

最简单的方法是将文件的行保留在内存中,但我怀疑这不会起作用,因为任何有用的生物信息学文件都可能非常大。

这是尝试通过删除全局变量使用并添加生成器函数来清理脚本,以便以流式方式创建从SNP函数返回的行,该行应与itertools.groupby调用兼容

from Bio import AlignIO
import itertools

n=0
align = AlignIO.read("HPV16_CG.aln.fas", "fasta")

def SNP(line, y, x):
    """Pass y as a parameter rather than relying on a global"""  
    result=[]
    result.append(str(n+1))
    result.append(line[0])
    result.append(align[y].id.rsplit("|")[3])
    result.append(x)
    return result

def generate_snp_lines(align, n):
    """this is a function generator that'll produce lines without writing them to a file"""
    while n<len(align[0]):
        line = align[:,n]
        y=0
        for x in line:
            if line[0]!=x:
                yield ','.join(map(str,SNP(line, y, x)))
            y+=1
        n+=1

def main():

    # let's use a context manager to open and cleanup this file for us:
    with open("test.csv","a") as out:
        # construct the generator:
        lines = generate_snp_lines(align, n)
        # pass it to itertools.groupby like we'd pass any iterable:
        for key, group in itertools.groupby(lines, lambda line: line.partition(',')[0]):
            print >>out, ','.join(group)

if __name__=="__main__":
    main()