haiferry的个人博客分享 http://blog.sciencenet.cn/u/haiferry

博文

DNA 的反向互补

已有 3805 次阅读 2015-10-29 08:57 |系统分类:科研笔记

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)  #列表连接成字符串且中间不带有任何空格




https://wap.sciencenet.cn/blog-2887147-931825.html

上一篇:python 计算Counting DNA nucleotides
下一篇:Counting GC content
收藏 IP: 159.226.67.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-5-18 00:16

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部