首页 > 其他 > 详细

统计给定序列的AT-content ,并设置小数点位数

时间:2015-09-17 21:14:37      阅读:187      评论:0      收藏:0      [点我收藏+]

习题来源: Python for Biologists: A complete programming course for beginner

 1 #!/bin/python
 2 # calculate the AT content of a DNA seq
 3 
 4 def get_at_content(dna, sig_figs = 2): # sig_figs=2 为默认参数 sig_figs 
 5     length = len(dna)
 6     a_count = dna.upper().count(A)   # use str.upper() and str.count() method
 7     t_count = dna.upper().count(T)
 8     at_content = (a_count + t_count) / length
 9     return round(at_content, sig_figs)  # 使用return 在很多情况下比 print 好
10                                         # use round() function ,就是设置数值的小数点位数
11 
12 assert get_at_content("ATCG") == 0.5   # assert 语句用来测试。非常有用  
13 test_dna = "ATGCATGCAACTGTAGC"
14 print(get_at_content(test_dna, 1))  # 此时,将sig_figs 赋值为 1
15 print(get_at_content(test_dna))     # 当没有明确的赋值时, 即调用默认参数值
16 print(get_at_content(test_dna, 3))
17 print(get_at_content(dna = "ATCGGTAGTCGTAGCGTAGCAGT", sig_figs = 2))  # 比较完整的函数调用

 运行结果:

uubuntu$ python3 chapter5_1_py3.py 
0.5
0.53
0.529
0.48

 

统计给定序列的AT-content ,并设置小数点位数

原文:http://www.cnblogs.com/OA-maque/p/4817420.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!