我正在尝试创建一个函数,它接受 2 个 DNA 序列作为参数并检查 3 件事(按此顺序):
我对编码很陌生,所以我不确定我的错误在哪里。我的代码没有得到任何输出,当我得到输出时,这是一个错误。有什么建议吗?
这是我到目前为止所得到的:
dna_bases = ['ACGT']
ct = ['CT']
ag = ['AG']
def dna_comparison(seq1, seq2):
if len(seq1) != len(seq2):
print("Error: The sequences are different lengths.")
else:
for i in range(len(seq1)):
for ii in range(len(seq2)):
if seq1[i] or seq2[ii] not in dna_bases:
return False
break
else:
print("Valid sequences. The program will continue")
transitions = 0
transversions = 0
for i in range(len(seq1)):
if seq1[i] != seq2[i]:
if seq1[i] in ag and seq2[i] in ag:
transitions =+ 1
elif seq1[i] in ct and seq2[i] in ct:
transitions =+ 1
else:
transversion += 1
print (transitions)
print (transversions)
IIUC,你可以这样做:
def check(seq1, seq2):
if len(seq1) != len(seq2):
raise ValueError("Sequences not the same length")
if set(seq1).union(seq2).difference("ACGT"):
raise ValueError("Sequences contain invalid character")
transitions = 0
transversions = 0
for s1, s2 in zip(seq1, seq2):
if s1 == s2:
continue
if (s1 in "CT" and s2 in "CT") or (s1 in "AG" and s2 in "AG"):
transitions += 1
else:
transversions += 1
return transitions, transversions
seq1 = "ACGTACGTCCACGT"
seq2 = "TTTACCGACGTACG"
transitions, transversions = check(seq1, seq2)
print(f"{transitions=} {transversions=}")
打印:
transitions=1 transversions=10