Loops Python使用for循环计算核苷酸
我试图从输入文件中提取DNA序列,并使用循环计数单个A、T、C和G的数量,如果存在非“ATCG”字母,我需要打印“错误”,例如,我的输入文件是: 序号1 AAAGCGT 序号2 aa-tGcGt 序号3 af-GtgA-cCTg 我想到的代码是:Loops Python使用for循环计算核苷酸,loops,count,dna-sequence,Loops,Count,Dna Sequence,我试图从输入文件中提取DNA序列,并使用循环计数单个A、T、C和G的数量,如果存在非“ATCG”字母,我需要打印“错误”,例如,我的输入文件是: 序号1 AAAGCGT 序号2 aa-tGcGt 序号3 af-GtgA-cCTg 我想到的代码是: acount = 0 ccount = 0 gcount = 0 tcount = 0 for line in input: line=line.strip('\n') if line[0] == ">":
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's T's等的总数。我希望我的输出像这样
序号1:
总A分:3分
C类总计:
对于每个序列,依此类推
关于如何修复代码以实现这一点,我有什么想法吗?我建议如下:
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
之后,您可以随意打印它们。在循环迭代的每个
开始时重置a帐户。
我喜欢这里的@Slater Tyranus,唯一的问题是(如果您不知道的话)这是学校的作业,如果我使用.count函数,我会得到分数。如果问题是家庭作业,请使用家庭作业标签。堆栈溢出不是真正的家庭作业,但我将更新问题,使其不使用count函数。