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

博文

计算CDS中密码子的数量

已有 3514 次阅读 2017-4-10 22:03 |系统分类:科研笔记| CDS, 密码子

看到一个现金求助的题目:

http://www.timedoo.com/task-id-1194.html

代码如下:

1
   #!/usr/bin/env python
2
   # -*- coding: utf-8 -*-
3
   __author__ = "Sheng-Wei Ma"
4
   
5
   from Bio import SeqIO
6
   from collections import OrderedDict
7
   
8
   records = (r for r in SeqIO.parse('cds.fa', "fasta"))
9
   count_file = open('cds.count.txt', 'w')
10
   fre_file = open('cds.frequency.txt', 'w')
11
   
12
   count_file.writelines('%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%sn' % ('name', 'TTT', 'TTC', 'TTA', 'TTG', 'CTT', 'CTC', 'CTA', 'CTG', 'ATT', 'ATC', 'ATA', 'ATG', 'GTT', 'GTC', 'GTA',
13
         'GTG', 'TAT', 'TAC', 'TAA', 'TAG', 'CAT', 'CAC', 'CAA', 'CAG', 'AAT', 'AAC', 'AAA', 'AAG', 'GAT', 'GAC', 'GAA',
14
         'GAG', 'TCT', 'TCC', 'TCA', 'TCG', 'CCT', 'CCC', 'CCA', 'CCG', 'ACT', 'ACC', 'ACA', 'ACG', 'GCT', 'GCC', 'GCA',
15
         'GCG', 'TGT', 'TGC', 'TGA', 'TGG', 'CGT', 'CGC', 'CGA', 'CGG', 'AGT','AGC', 'AGA', 'AGG', 'GGT', 'GGC', 'GGA', 'GGG'))
16
   fre_file.writelines(
17
       '%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%st%sn' % (
18
       'name', 'TTT', 'TTC', 'TTA', 'TTG', 'CTT', 'CTC', 'CTA', 'CTG', 'ATT', 'ATC', 'ATA', 'ATG', 'GTT', 'GTC', 'GTA',
19
       'GTG', 'TAT', 'TAC', 'TAA', 'TAG', 'CAT', 'CAC', 'CAA', 'CAG', 'AAT', 'AAC', 'AAA', 'AAG', 'GAT', 'GAC', 'GAA',
20
       'GAG', 'TCT', 'TCC', 'TCA', 'TCG', 'CCT', 'CCC', 'CCA', 'CCG', 'ACT', 'ACC', 'ACA', 'ACG', 'GCT', 'GCC', 'GCA',
21
       'GCG', 'TGT', 'TGC', 'TGA', 'TGG', 'CGT', 'CGC', 'CGA', 'CGG', 'AGT', 'AGC', 'AGA', 'AGG', 'GGT', 'GGC', 'GGA',
22
       'GGG'))
23
   
24
   for i in records:
25
       CodonsDict = OrderedDict([('TTT', 0), ('TTC', 0), ('TTA', 0), ('TTG', 0), ('CTT', 0), ('CTC', 0), ('CTA', 0),
26
                                ('CTG', 0), ('ATT', 0), ('ATC', 0),
27
                                ('ATA', 0), ('ATG', 0), ('GTT', 0), ('GTC', 0), ('GTA', 0), ('GTG', 0), ('TAT', 0),
28
                                ('TAC', 0), ('TAA', 0), ('TAG', 0),
29
                                ('CAT', 0), ('CAC', 0), ('CAA', 0), ('CAG', 0), ('AAT', 0), ('AAC', 0), ('AAA', 0),
30
                                ('AAG', 0), ('GAT', 0), ('GAC', 0),
31
                                ('GAA', 0), ('GAG', 0), ('TCT', 0), ('TCC', 0), ('TCA', 0), ('TCG', 0), ('CCT', 0),
32
                                ('CCC', 0), ('CCA', 0), ('CCG', 0),
33
                                ('ACT', 0), ('ACC', 0), ('ACA', 0), ('ACG', 0), ('GCT', 0), ('GCC', 0), ('GCA', 0),
34
                                ('GCG', 0), ('TGT', 0), ('TGC', 0),
35
                                ('TGA', 0), ('TGG', 0), ('CGT', 0), ('CGC', 0), ('CGA', 0), ('CGG', 0), ('AGT', 0),
36
                                ('AGC', 0), ('AGA', 0), ('AGG', 0),
37
                                ('GGT', 0), ('GGC', 0), ('GGA', 0), ('GGG', 0)])
38
       if i.seq.startswith('ATG') and 'N' not in i.seq and 'Y' not in i.seq and 'K' not in i.seq and 'R' not in i.seq and 'M' not in i.seq and 'S' not in i.seq and 'W' not in i.seq:
39
           for j in range(0, len(str(i.seq)), 3):
40
               codon = str(i.seq)[j:j+3]
41
               if codon in CodonsDict.keys():
42
                   CodonsDict[codon] +=1
43
           count_file.writelines('%st' % i.id)
44
           fre_file.writelines('%st' % i.id)
45
           total = sum([CodonsDict[key] for key in CodonsDict.keys()])
46
           print total,
47
           for key in CodonsDict.keys():
48
               count_file.writelines('%st' % CodonsDict[key])
49
               fre_file.writelines('%.2ft' % (CodonsDict[key]/float(total)))
50
           count_file.writelines('n')
51
           fre_file.writelines('n')
52
   count_file.close()
53
   fre_file.close()
54




https://wap.sciencenet.cn/blog-1094241-1048023.html

上一篇:给GFF3格式文件添加fasta格式
下一篇:修改文章那些事
收藏 IP: 58.213.93.*| 热度|

0

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

数据加载中...
扫一扫,分享此博文

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

GMT+8, 2024-5-1 21:37

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部