001、测试序列,碱基序列保存只a.fa文件中,统计下面这段序列中A、C、G、T碱基的个数
[root@PC1 test01]# ls a.fa [root@PC1 test01]# cat a.fa ## 测试fasta文件 AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC
002、利用基本循环统计
[root@PC1 test01]# ls a.fa count.py [root@PC1 test01]# cat count.py ## 统计程序 #!/usr/bin/env python # -*- coding: utf-8 -*- in_file = open("a.fa", "r") a = 0; c = 0; g = 0; t = 0 for i in in_file: i = i.strip() for j in i: if j == "A": a += 1 elif j == "C": c += 1 elif j == "G": g += 1 elif j == "T": t += 1 else: print("anomanous letter!" + j) break #in_file.close() print(a, c, g, t)
执行程序:
[root@PC1 test01]# ls a.fa count.py [root@PC1 test01]# cat a.fa ## 测试文件 AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC [root@PC1 test01]# python3 count.py ## 执行程序 20 12 17 21
003、利用字符串计数函数实现
[root@PC1 test01]# ls a.fa count.py [root@PC1 test01]# cat a.fa ## 测试数据 AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC [root@PC1 test01]# cat count.py ## 统计程序 #!/usr/bin/env python # -*- coding: utf-8 -*- in_file = open("a.fa", "r") file = in_file.read() print(file.count("A"), file.count("C"), file.count("G"), file.count("T")) [root@PC1 test01]# python3 count.py ## 执行程序 20 12 17 21
004、借助函数实现
标签:练习题,count,01,python,PC1,fa,file,test01,root From: https://www.cnblogs.com/liujiaxin2018/p/17657831.html