python 统计fasta文件每条scalfold的碱基长度
001、
root@PC1:/home/test# cat test.fasta ## 测试数据 >scaffold_1 CCCGGGTAAAACGGGTCTTCAAGAAAACGCTCCTCCGTTAATGCCGGCCGATTCAAATAA CGCTGATTCTGATTCAGGATATACAATCTGACATGATGAACAGGTTTTCCAATTGGAATC CGTT >scaffold_2 CACGCCGCCAGCGTTCGTCCTGAGCCAGGATCAAACTCTCCGATAAATGGATCACAGGTT AAGTTCACCGCATCCTGCGGCGACACCTGTGTGGCCTGCGTCGTGCAGGCCCTAGTTTGA >scaffold_3 TTGATCCAGTGGCTCCGGTTACTCCAGTTGATCCTGTTGCGCCTGTTGCTCCAGTTTCTC CGGTTGGTCCGGTTGATCCGGTTGCACCTGTTACTCCAGTGGCTCCGGTTACTCCCGTCG CACCAGTTTCTCCTGTCGCACCAGTTGATCCTGTTGCGCCTGTTGGTCCTGTATCTCCAG >scaffold_4 CCTGAGCCAGGATCAAACTCTCCGATA root@PC1:/home/test# cat test.py ## 测试脚本 #!/usr/bin/python in_file = open("test.fasta", "r") out_file = open("result.txt", "w") dict1 = dict() for i in in_file: i = i.strip() if i[0] == ">": key = i dict1[key] = "" else: dict1[key] = dict1[key] + i print("id", "length", file = out_file, sep = "\t") for i in dict1: print(i, len(dict1[i]), file = out_file, sep = "\t") in_file.close() out_file.close() root@PC1:/home/test# python test.py root@PC1:/home/test# ls result.txt test.fasta test.py root@PC1:/home/test# cat result.txt ## 结果文件 id length >scaffold_1 124 >scaffold_2 120 >scaffold_3 180 >scaffold_4 27