首页 > 代码库 > BioPython-3

BioPython-3

有关于 Seq 对象MutbableSeq 对象

 

# -*- coding: utf-8 -*-
"""
Created on Sun Jun 08 23:59:14 2014

@author: cpycpp
"""

"""
Seq 对象的很多行为都像字符串,所以许多字符串的操作都可以用于 Seq 对象
"""
from Bio.Seq import Seq
import Bio.Alphabet

dna_seq = Seq(CCGGGTTAACGTA, Bio.Alphabet.IUPAC.unambiguous_dna )

# 切片,求序列的前5个字母
print dna_seq[ : 5 ]
# Output: CCGGG

# 返回序列的长度
print len( dna_seq )
# Output: 13

# 打印整个序列
print dna_seq
# Output:CCGGGTTAACGTA

"""
Seq 对象是不可变的,这对于想在其中保存数据而不像做改变的情况来说是有用的。
Seq 对象的不可变性与Python的字符串的不可变性刚好匹配
如果想修改Seq对象,那么会抛出一个异常
dna_seq[ 0 ] = ‘T‘
#########################################################################
Output:
Traceback (most recent call last):
  File "D:\Python\BioPython\SeqObjectsAsString.py", in <module>
    dna_seq[ 0 ] = ‘T‘
TypeError: ‘Seq‘ object does not support item assignment
#########################################################################
"""

"""
要想得到一个可变的序列,可以采用Seq 对象的tomutable()生成一个 MutableSeq 对象
"""
mut_dna_seq = dna_seq.tomutable()
print mut_dna_seq
# Output; CCGGGTTAACGTA

# 改变下第一个字母进行测试 
mut_dna_seq[ 0 ] = T
print mut_dna_seq
# Output: TCGGGTTAACGTA 
# OK!修改成功!
print

"""
可以将MutableSeq 对象看做一个 List,进行修改
方法展示:append(), insert(), pop(), remove()
"""
print "mut_dna_seq: ", mut_dna_seq
# Output; mut_dna_seq:  TCGGGTTAACGTA

mut_dna_seq.append( "T" )
print "After appended with T, mut_dna_seq: ", mut_dna_seq
# Output: After appended with T, mut_dna_seq:  TCGGGTTAACGTAT


print "\nmut_dna_seq: ", mut_dna_seq
# Output: mut_dna_seq:  TCGGGTTAACGTAT
   
mut_dna_seq.insert( 1, "G" )
print "After inserted G at the second letter, mut_dna_seq: ", mut_dna_seq
# Output: After inserted G at the second letter, mut_dna_seq:  TGCGGGTTAACGTAT 


print "\nmut_dna_seq: ", mut_dna_seq
# Output:  mut_dna_seq:  TGCGGGTTAACGTAT

mut_dna_seq.pop()
print "After popped, mut_dna_seq: ", mut_dna_seq
# Output: After popped, mut_dna_seq:  TGCGGGTTAACGTA


print "\nmut_dna_seq: ", mut_dna_seq
# Output:mut_dna_seq:  TGCGGGTTAACGTA

mut_dna_seq.remove("G")
print "After removed first G, mut_dna_seq: ", mut_dna_seq
# Output: After removed first G, mut_dna_seq:  TCGGGTTAACGTA


"""
对于DNA 的序列的处理还有一些特别的方法
"""
print "\nmut_dna_seq: ", mut_dna_seq
# Output:mut_dna_seq:  TCGGGTTAACGTA

# 反转序列
mut_dna_seq.reverse()
print "After reversed, mut_dna_seq: ", mut_dna_seq
# Output: After reversed, mut_dna_seq:  ATGCAATTGGGCT


print "\nmut_dna_seq: ", mut_dna_seq
# Output:ATGCAATTGGGCT

# 求互补序列
mut_dna_seq.complement()
print "The complement strand of mut_dna_seq: ", mut_dna_seq
# Output: The complement strand of mut_dna_seq:  TACGTTAACCCGA


print "\nmut_dna_seq: ", mut_dna_seq
# Output:mut_dna_seq:  TACGTTAACCCGA

# 求反向互补序列
mut_dna_seq.reverse_complement()
print "The reversed and complement strand of mut_dna_seq: ", mut_dna_seq
# Output: The reversed and complement strand of mut_dna_seq:  TCGGGTTAACGTA