码迷,mamicode.com
首页 > 编程语言 > 详细

BioPython-3

时间:2014-06-09 20:25:02      阅读:253      评论:0      收藏:0      [点我收藏+]

标签:c   style   class   blog   code   java   

有关于 Seq 对象MutbableSeq 对象

 

bubuko.com,布布扣
# -*- 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
bubuko.com,布布扣

 

 

 

BioPython-3,布布扣,bubuko.com

BioPython-3

标签:c   style   class   blog   code   java   

原文地址:http://www.cnblogs.com/tmmuyb/p/3777093.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!