python 中实现按照 fasta文件的scaffold进行排序

2022/8/16 14:55:45

本文主要是介绍python 中实现按照 fasta文件的scaffold进行排序,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

 

001、 方法1

[email protected]:/home/test# ls
a.fasta  test.py
[email protected]:/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()
[email protected]:/home/test# cat a.fasta                          ## 测试fasta文件
>gene2 myc
AGCTGCCTAAGC
GGCATAGCTAATCG
>gene3 jun
ACCGAATCGGAGCGATG
GGCATTAAAGATCTAGCT
>gene1 malat1
AGGCTAGCGAG
GCGCGAG
GATTAGGCG
[email protected]:/home/test# python test.py                        ## 执行程序
>gene1 malat1
AGGCTAGCGAG
GCGCGAG
GATTAGGCG
>gene2 myc
AGCTGCCTAAGC
GGCATAGCTAATCG
>gene3 jun
ACCGAATCGGAGCGATG
GGCATTAAAGATCTAGCT

 

002、方法2

[email protected]:/home/test# ls
a.fasta  test.py
[email protected]:/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()
[email protected]:/home/test# cat a.fasta                                ## 测试fasta文件
>gene2 myc
AGCTGCCTAAGC
GGCATAGCTAATCG
>gene3 jun
ACCGAATCGGAGCGATG
GGCATTAAAGATCTAGCT
>gene1 malat1
AGGCTAGCGAG
GCGCGAG
GATTAGGCG
[email protected]:/home/test# python test.py                              ## 执行程序
>gene1 malat1
AGGCTAGCGAG
GCGCGAG
GATTAGGCG
>gene2 myc
AGCTGCCTAAGC
GGCATAGCTAATCG
>gene3 jun
ACCGAATCGGAGCGATG
GGCATTAAAGATCTAGCT

 



这篇关于python 中实现按照 fasta文件的scaffold进行排序的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程