Transitions between A <-> G and C <-> T
Transversions between A <-> C and G <-> TAssignment:
- Write a function transition that takes two nucleotides. The function must return a Boolean value that indicates whether or not replacing the first nucleotide by the second nucleotide leads to a transition.
问题:我认为函数无法识别or
后面的语句。
代码在某些情况下不起作用,例如:transition('A', 'G')
为True,而对于我的代码,他给出False
- Write a function ratio that takes two DNA sequences s1 and s2
The function may assume that both sequences have the same length (the function does not need to check this explicitly). The function must return the transition/transversion ratio R(s 1 ,s 2 )∈R of the two given sequences as a floating point number. In case there are no transversions between the two sequences, R(s 1 ,s 2 )=0 by definition.
问题:代码不起作用
def transition(letter1, letter2):
"""
>>> transition('G', 'A')
True
>>> transition('t', 'g')
False
>>> transition('C', 'c')
False
"""
return True if letter1.lower() == 'gt' and letter2.lower() == 'ac' or letter1.lower() == 'ac' and letter2.lower() == 'gt' else False
def transversion(letter1, letter2):
"""
>>> transversion('G', 'A')
False
>>> transversion('t', 'g')
True
>>> transversion('C', 'c')
False
"""
return True if letter1.lower() == 'ct' and letter2.lower() == 'ag' or letter1.lower() == 'ag' and letter2.lower() == 'ct' else False
def ratio(seq1, seq2):
"""
>>> ratio('ATTAGCATTATCATC', 'AAATAGGATATATGG')
0.2222222222222222
>>> seq1 = 'GCAACGCACAACGAAAACCCTTAGGGACTGGATTATTTCGTGATCGTTGTAGTTATTGGAAGTACGGGCATCAACCCAGTT'
>>> seq2 = 'ttatctgacaaagaaagccgtcaacggctggataatttcgcgatcgtgctggttactggcggtacgagtgttcctttgggt'
>>> ratio(seq1, seq2)
1.2142857142857142
"""
count = 0
tel = 0
for i in range(len(seq1)):
if transition(seq1[i], seq2[i]):
count += 1
for i in range(len(seq1)):
if transversion(seq1[i], seq2[i]):
tel += 1
if tel != 0:
return float(count / tel)
else:
return 0
if __name__ == '__main__':
import doctest
doctest.testmod()
我认为相同碱基的^{} 或^{} 必须返回False
(即
transition('A','A')
==False)您可以简化使用简单的命名谓词:https://repl.it/N4TC/4
其他谓词:
把线改成这样
签署人:
一般来说,如果有多个条件,可以先将它们赋给变量。可能不是您想要的答案,但可能可以帮助您编写更易于阅读的代码。
像这样:
相关问题 更多 >
编程相关推荐