Chinaunix首页 | 论坛 | 博客
  • 博客访问: 367472
  • 博文数量: 97
  • 博客积分: 2846
  • 博客等级: 少校
  • 技术积分: 1000
  • 用 户 组: 普通用户
  • 注册时间: 2007-03-19 20:00
文章分类

全部博文(97)

文章存档

2017年(1)

2013年(2)

2012年(6)

2011年(17)

2010年(12)

2009年(41)

2007年(18)

我的朋友

分类: Python/Ruby

2009-04-04 20:41:20

利用python的random模块,随机生成DNA,RNA或PROTEIN序列,长度为20.
 
 

# generate random sequences
# type: DNA, RNA, PROTEIN
# length is length

import random
def randomseq(type='DNA',length=20):
    seq = ''
    for i in xrange(length):
        if type == 'DNA':
            dicts = list('ATCG')
            seq += dicts[random.randrange(4)]
        if type == 'RNA':
            dicts = list('AUCG')
            seq += dicts[random.randrange(4)]
        if type == 'PROTEIN':
            dicts = list('ACDEFGHIKLMNPQRSTVWY')
            seq += dicts[random.randrange(4)]
    return seq

# another choice
def randomseq2(type='DNA',length=20):
    seq = []
    for i in xrange(length):
        if type == 'DNA':
            dicts = list('ATCG')
            seq.append(dicts[random.randrange(4)])
        if type == 'RNA':
            dicts = list('AUCG')
            seq.append(dicts[random.randrange(4)])
        if type == 'PROTEIN':
            dicts = list('ACDEFGHIKLMNPQRSTVWY')
            seq.append(dicts[random.randrange(4)])
    return ''.join(seq)
        

if __name__ == '__main__':
    print 'RNA: ',randomseq('RNA',20)
    print 'RNA: ',randomseq2('RNA',20)
    print 'DNA: ',randomseq2('DNA',20)
    print 'PROTEIN: ',randomseq('PROTEIN',20)

阅读(665) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~