DNA序列
简述其代码
原始序列上进行替换
利用upper()输出大写结果
结尾
DNA序列ACTGATCGATTACGTATAGTATTTGCTATCATACATATATATCGATGCGTTCAT
求其互补DNA序列。
在生物上DNA互补序列简述表达可以表示为:A与T,C与G互补,可以理解为将上述序列中现有的A用T代替,C用G代替,T用A代替,G用C代替,则其互补序列为:
TGACTAGCTAATGCATATCATAAACGATAGTATGTATATATAGCTACGCAAGTA
根据上述表述,我可以利用replace()函数进行替换,将A用T替换,T用A替换,C用G替换,G用C替换,
简述其代码my_dna = "ACTGATCGATTACGTATAGTATTTGCTATCATACATATATATCGATGCGTTCAT"
# replace A with T
sequence1 = my_dna.replace('A', 'T')
# replace T with A
sequence2 = sequence1.replace('T', 'A')
# replace C with G
sequence3 = sequence2.replace('C', 'G')
# replace G with C
sequence4 = sequence3.replace('G', 'C')
# print the result of the final replacement
print(sequence1)
print(sequence2)
print(sequence3)
print(sequence4)
其输出结果如下:
原始序列上进行替换TCTGTTCGTTTTCGTTTTGTTTTTGCTTTCTTTCTTTTTTTTCGTTGCGTTCTT
ACAGAACGAAAACGAAAAGAAAAAGCAAACAAACAAAAAAAACGAAGCGAACAA
AGAGAAGGAAAAGGAAAAGAAAAAGGAAAGAAAGAAAAAAAAGGAAGGGAAGAA
ACACAACCAAAACCAAAACAAAAACCAAACAAACAAAAAAAACCAACCCAACAA
显然结果是不正确的,我们在sequence1到sequence2中就已经出现错误,误把sequence1中A被替换之后变为T的序列,在sequence2中又被替换掉了,因此我们要转变思路,保持只替换原本的序列,不进行多次替换,避免错误,我们可以尝试每次只在原始序列上进行替换,尝试代码如下:
my_dna = "ACTGATCGATTACGTATAGTATTTGCTATCATACATATATATCGATGCGTTCAT"
# replace A with T
sequence = my_dna.replace('A', 'T')
# replace T with A
sequence2 = my_dna.replace('T', 'A')
# replace C with G
sequence3 = my_dna.replace('C', 'G')
# replace G with C
sequence4 = my_dna.replace('G', 'C')
print(sequence1)
print(sequence2)
print(sequence3)
print(sequence4)
其输出结果如下:
TCTGTTCGTTTTCGTTTTGTTTTTGCTTTCTTTCTTTTTTTTCGTTGCGTTCTT
ACAGAACGAAAACGAAAAGAAAAAGCAAACAAACAAAAAAAACGAAGCGAACAA
AGTGATGGATTAGGTATAGTATTTGGTATGATAGATATATATGGATGGGTTGAT
ACTCATCCATTACCTATACTATTTCCTATCATACATATATATCCATCCCTTCAT
显然结果也是不正确的,因此,我们要引入中间变量,最后再把它做一个回环,
也就是说引入四个临时字母,然后每个变换2次,最后把最终结果输出,其代码可以为:
my_dna = "ACTGATCGATTACGTATAGTATTTGCTATCATACATATATATCGATGCGTTCAT"
sequence1 = my_dna.replace('A', 'H')
sequence2 = sequence1.replace('T', 'J')
sequence3 = sequence2.replace('C', 'K')
sequence4 = sequence3.replace('G', 'L')
sequence5 = sequence4.replace('H', 'T')
sequence6 = sequence5.replace('J', 'A')
sequence7 = sequence6.replace('K', 'G')
sequence8 = sequence7.replace('L', 'C')
print(sequence8)
其结果为:
利用upper()输出大写结果TGACTAGCTAATGCATATCATAAACGATAGTATGTATATATAGCTACGCAAGTA
至此得到了我们想要的结果,但这种方法显然是有些复杂了,我们可以利用字符的大小写来完成我们的工作,也就是利用小写字母为临时变量,最终利用upper()输出大写的结果就行了,其代码和结果如下:
my_dna = "ACTGATCGATTACGTATAGTATTTGCTATCATACATATATATCGATGCGTTCAT"
sequence1 = my_dna.replace('A', 't')
print(sequence1)
sequence2 = sequence1.replace('T', 'a')
print(sequence2)
sequence3 = sequence2.replace('C', 'g')
print(sequence3)
sequence4 = sequence3.replace('G', 'c')
print(sequence4)
print(sequence4.upper())
其结果为:
tCTGtTCGtTTtCGTtTtGTtTTTGCTtTCtTtCtTtTtTtTCGtTGCGTTCtT
tCaGtaCGtaatCGatatGataaaGCataCtatCtatatataCGtaGCGaaCta
tgaGtagGtaatgGatatGataaaGgatagtatgtatatatagGtaGgGaagta
tgactagctaatgcatatcataaacgatagtatgtatatatagctacgcaagta
TGACTAGCTAATGCATATCATAAACGATAGTATGTATATATAGCTACGCAAGTA
至此我们的互补DNA序列得到了,也许有更好更简洁的代码。
结尾虽然这是个小小的计算程序,但对于初学者的我来说每一次对原代码的升级改造,哪怕是读懂后的注释都感觉是一次进步提升,总之代码虽小,动手最重要!希望更多学习Python的爱好者不要像我一样眼高手低,学习编程就是要,思考,敲码,思考,敲码,敲码,再敲码,更多关于python求DNA模板互补序列的资料请关注易知道(ezd.cc)其它相关文章!