python 中实现按照 fasta文件的scaffold进行排序
作者:互联网
001、 方法1
root@PC1:/home/test# ls a.fasta test.py root@PC1:/home/test# cat test.py ## 测试程序 #!/usr/bin/python in_file = open("a.fasta", "r") dict1 = dict() for i in in_file: i = i.strip() if i[0] == ">": key = i dict1[key] = [] else: dict1[key].append(i) dict1 = dict(sorted(dict1.items(), key = lambda x: x[0])) for i,j in dict1.items(): print(i) for k in j: print(k) in_file.close() root@PC1:/home/test# cat a.fasta ## 测试fasta文件 >gene2 myc AGCTGCCTAAGC GGCATAGCTAATCG >gene3 jun ACCGAATCGGAGCGATG GGCATTAAAGATCTAGCT >gene1 malat1 AGGCTAGCGAG GCGCGAG GATTAGGCG root@PC1:/home/test# python test.py ## 执行程序 >gene1 malat1 AGGCTAGCGAG GCGCGAG GATTAGGCG >gene2 myc AGCTGCCTAAGC GGCATAGCTAATCG >gene3 jun ACCGAATCGGAGCGATG GGCATTAAAGATCTAGCT
002、方法2
root@PC1:/home/test# ls a.fasta test.py root@PC1:/home/test# cat test.py ## 测试程序 #!/usr/bin/python in_file = open("a.fasta", "r") dict1 = dict() for i in in_file: i = i.strip() if i[0] == ">": key = i dict1[key] = [] else: dict1[key].append(i) for i in sorted(dict1): print(i) for k in dict1[i]: print(k) in_file.close() root@PC1:/home/test# cat a.fasta ## 测试fasta文件 >gene2 myc AGCTGCCTAAGC GGCATAGCTAATCG >gene3 jun ACCGAATCGGAGCGATG GGCATTAAAGATCTAGCT >gene1 malat1 AGGCTAGCGAG GCGCGAG GATTAGGCG root@PC1:/home/test# python test.py ## 执行程序 >gene1 malat1 AGGCTAGCGAG GCGCGAG GATTAGGCG >gene2 myc AGCTGCCTAAGC GGCATAGCTAATCG >gene3 jun ACCGAATCGGAGCGATG GGCATTAAAGATCTAGCT
标签:dict1,python,scaffold,PC1,test,home,fasta,root 来源: https://www.cnblogs.com/liujiaxin2018/p/16589247.html