|
Complementing a Strand of DNA
In DNA strings, symbols 'A' and 'T' arecomplements of each other, as are 'C' and 'G'.
The reverse complement of a DNA string s isthe string sc formed by reversing the symbols of s, then taking the complementof each symbol (e.g., the reverse complement of "GTCA" is"TGAC").
Given: A DNA string s of length at most1000 bp.
Return: The reverse complement sc of s.
Sample Dataset
AAAACCCGGT
Sample Output
ACCGGGTTTT
针对以上案例,我准备了三种解答方案:
#!/usr/bin/python
s='AAAACCCGGT'
b= s[::-1] #步长为-1的序列
c="" # 空字符串
for i in b:
if i=='A':
c=c+'T' # +号代表连接作用
elif i=='T':
c=c+'A'
elif i=='C':
c=c+'G'
else:
c=c+'C'
print c
第二种办法 利用字母的大小写差异:
#!/usr/bin/python
s='AAAACCCGGT'
complement_AT=s.replace("A","t")
complement_TA=complement_AT.replace("T","a")
complement_GC=complement_TA.replace("G","c")
complement_CG=complement_GC.replace("C","g")
complement_dna=complement_CG.upper() #转换成大写字母
reverse_complement_dna=complement_dna[::-1]
print s
print reverse_complement_dna
第三种办法:
利用字典键值对应的办法:
#!/usr/bin/python
s='AAAACCCGGT'
b= s[::-1]
c=[]
complement_dna={"A":"t","T":"a","C":"g","G":"c"}
for i in b:
c.append(complement_dna[i].upper()) #利用字典的键得到值,并且转换成大写字母
print s
print "".join(c) #列表连接成字符串且中间不带有任何空格
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-9-27 21:22
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社