python实现fasta文件碱基序列每行按照指定数目输出

发布时间 2023-10-14 08:59:51作者: 小鲨鱼2018

 

001、

(base) [root@pc1 test1]# ls
a.fa  test.py
(base) [root@pc1 test1]# cat a.fa          ## 测试fasta
>chr1
tttcccggg
>chr2
tttgggjjj
cccjjjjjj
>chr3
ccc
>chr4
aaaaatt
(base) [root@pc1 test1]# cat test.py      ## 程序
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

dict1 = dict()
with open("a.fa", "r") as in_put:
        for i in in_put:
                i = i.strip()
                if i[0] == ">":
                        key = i
                        dict1[key] = ""
                else:
                        dict1[key] += i
        in_put.close()

        for i,j in dict1.items():
                print(i)
                for k in range(0, len(j), 5):
                        print(j[k:k+5])

 

(base) [root@pc1 test1]# ls
a.fa  test.py
(base) [root@pc1 test1]# python3 test.py   ## 运算结果
>chr1
tttcc
cggg
>chr2
tttgg
gjjjc
ccjjj
jjj
>chr3
ccc
>chr4
aaaaa
tt

 。