0

我正在尝试从输入文件中获取 DNA 序列,并使用循环计算单个 A 的 T、C 和 G 的数量,如果有非“ATCG”字母我需要打印“错误”例如我的输入文件是:

Seq1 AAAGCGT Seq2 aa tGcGt t Seq3 af GtgA cCTg

我想出的代码是:

acount = 0
ccount = 0
gcount = 0
tcount = 0
for line in input:
         line=line.strip('\n')
         if line[0] == ">":
                print line + "\n"
                output.write(line+"\n")
         else:
                line=line.upper()
                list=line.split()
                for list in line:

                        if list == "A":
                                acount = acount +
                                #print acount
                        elif list == "C":
                                ccount = ccount +
                                #print ccount 

                        elif list == "T":
                                tcount = tcount +
                                #print tcount 
                        elif list == "G":
                                gcount=gcount +1
                                #print gcount 
                        elif list != 'A'or 'T' or 'G' or 'C':
                                break

所以我需要每一行的总数,但我的代码给了我整个文件的 A 和 T 等的总数。我希望我的输出类似于

Seq1: Total A's: 3 Total C's: 每个序列以此类推。

关于我可以做些什么来修复我的代码以实现这一目标的任何想法?

4

1 回答 1

0

我会建议一些类似的东西:

import re

def countNucleotides(filePath):
    aCount = []
    gCount = []
    cCount = []
    tCount = []
    with open(filePath, 'rb') as data:
        for line in data:
            if not re.match(r'[agctAGCT]+',line):
                break
            aCount.append(notCount(line,'a'))
            gCount.append(notCount(line,'g'))
            cCount.append(notCount(line,'c'))
            tCount.append(notCount(line,'t'))

def notCount(line, character):
    appearances = 0
    for item in line:
        if item == character:
            appearances += 1
    return appearances

之后您可以随意打印它们。

于 2013-04-01T04:21:55.460 回答