python实现DNA序列字符串转换,互补链,反向链,反向互补链

在生物信息学分析中,经常对DNA序列进行一系列操作,包括子序列截取,互补序列获取,反向序列获取,反向互补序列获取。在python语言中,可编写如下函数完成这些简单功能。

子序列截取

python中对序列截取使用字符串切片功能就可以完成,例如:

>>> seq="ATGATATAGtatatatgCAAGAGg"
>>> subseq = seq[1:6]
>>> subseq
"TGATA"

注意,切片操作是“0-base”的,包左不包右。

互补序列获取

比较常见的做法是定义一个碱基替换字典,如下所示:

def complement(s):
    basecomplemt = {
         "A":"T",
          "T":"A",
          "G":"C",
          "C":G",
          "a":"t",
          "t":"a",
          "g":"c",
          "c":"g",
          }
    letters = list(s)
    letters = [basecomplement[base] for base in letters]
    return ‘‘.join(letters)

使用python3字符串使用的translate方法

def complement(seq):
    return seq.translate(str.maketrans(‘ACGTacgtRYMKrymkVBHDvbhd‘, ‘TGCAtgcaYRKMyrkmBVDHbvdh‘))

python2 string包中的maketrans方法

from string import maketrans

def complement(seq):
    return seq.translate(maketrans(‘ACGTacgtRYMKrymkVBHDvbhd‘, ‘TGCAtgcaYRKMyrkmBVDHbvdh‘))

反向互补序列获取

def revcomp(seq):
    return complement(seq)[::-1]

参考资料

DNA反向互补序列获取

原文地址:https://www.cnblogs.com/yahengwang/p/9332561.html

时间: 2024-08-02 14:57:38

python实现DNA序列字符串转换,互补链,反向链,反向互补链的相关文章

python ip整数与字符串转换

import socket,struct,sys,getopt,string def doAnalysis(ifile, ofile): infile = open(ifile, 'r') onfile = open(ofile, 'w') for line in infile.readlines(): info = line.split('\t') if len(info) >= 2: info[1] = socket.inet_ntoa(struct.pack('I', socket.hto

Python基础系列----序列(列表、元组、字符串)

1.定义                                                                                               1.数据结构:通过某种方式组织在一起的数据元素的集合.这些数据元素可以说数字或者字符,甚至可以是其他数据结构.在python中,最基本的数据结构是序列. 2.索引:序列中的每个元素被分配一个序号.注意索引默认从0开始. 在python中,内建了6中序列:列表.元组.字符串.unicode字符串.buf

Python第六天(字符串转换)

Python字符串转换 int函数将字符串转换为数字 str函数将数字转换为字符串 >>> int('100') 100 >>> str(100) '100' repr函数可以将一个对象转换为其他字符串形式,然后这些返回的 对象将作为代码的字符串 内置的函数可以把浮点数转换为字符串 或者把字符串转化为浮点数 >>> str(1.1) '1.1' >>> float('1.1') 1.1000000000000001 字符串代码转换 o

利用Python【Orange】结合DNA序列进行人种预测

http://blog.csdn.net/jj12345jj198999/article/details/8951120 coursera上 web intelligence and big data 终于布置了HW7,这一次的要求是对一系列DNA序列进行预测,具体说明如下: Data Analytics Assignment (for HW7) Predict the Ethnicity of Individuals from their Genes =====================

python学习第二天:数字与字符串转换及逻辑值

1.数字与字符串的转化 #1.数字转字符,使用格式化字符串: *1.demo = ‘%d’  %  source *2.%d整型:%f 浮点型 :%e科学计数  *3.int('source') #转化为int型 #2.字符串转化为数字 *1.导入string :import string *2.demo = atoi(source)  #转换为整型’ atof()    #转为浮点型 2.逻辑值: and  #与 or  #或 not #非 python学习第二天:数字与字符串转换及逻辑值

python 数字和字符串转换问题

一.python中字符串转换成数字 (1)import string tt='555' ts=string.atoi(tt) ts即为tt转换成的数字 转换为浮点数 string.atof(tt) (2)直接int int(tt)即可. 二.数字转换成字符串 tt=322 tem='%d' %tt tem即为tt转换成的字符串

python中文和unicode字符串之间的互相转换

首先:中文->unicode字符串 import chardet import codecs >>> a = "我是一个中国人">>> a'\xce\xd2\xca\xc7\xd2\xbb\xb8\xf6\xd6\xd0\xb9\xfa\xc8\xcb' >>> chardet.detect(a){'confidence': 0.99, 'encoding': 'GB2312'}>>> b = a.decod

python中的list,字符串转换

在Python的编程中,经常会涉及到字符串与list之间的转换问题,下面就将两者之间的转换做一个梳理.1.list转换成字符串 命令:list() 例子: 2.字符串转换成list命令:"".join(list) 其中,引号中是字符之间的分割符,如",",";","\t"等等 [python] view plain copy['1', '2', '3', '4', '5'] ['123', 'sjhid', 'dhi'] ['

python如何将字符串转换成json的几种办法

python如何将字符串转换成json的几种办法 最近在工作中遇到了一个小问题,如果要将字符串型的数据转换成dict类型,我第一时间就想到了使用json函数.但是里面出现了一些问题 1.通过json来转换: In [1]: import json In [2]: mes = '{"InsId": 2, "name": "lege-happy", "CreationTime": "2019-04-23T03:18:02