Elsa镇魂女孩
2019-06-27 21:38
采纳率: 62.5%
浏览 1.8k
已采纳

如何用python写一个循环,或者其他方式,完成批量选取内容并保存。

下面是将文件 split__1.fasta中选取一段序列,如下421至480. 存于selected_split_1.fasta文件中__。
如何写一个循环,可以从split__1.fasta 至 split__68.fasta,中分别选取421至480. 并存于selected_split_1.fasta至selected_split_68.fasta文件中文件中

from Bio import SeqIO

fin = open('split_1.fasta', 'r')
fout = open('selected_split_1.fasta', 'w')

with open("selected_split_1.fasta","w") as f:
        for seq_record in SeqIO.parse("split_1.fasta", "fasta"):
                f.write(">")
                f.write(str(seq_record.id) + "\n")
                f.write(str(seq_record.seq[421:480]) + "\n")  #start 421 to end 480 base positions

fin.close()
fout.close()
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • threenewbee 2019-06-28 05:18
    已采纳

    from Bio import SeqIO

    for xx in range(1, 68):
    xn = "split__" + str(xx) + ".fasta"
    yn = "selected_split_" + str(xx) + ".fasta"

    fin = open(xn, 'r')
    fout = open(yn, 'w')

    with open(yn,"w") as f:
    for seq_record in SeqIO.parse(xn, "fasta"):
    f.write(">")
    f.write(str(seq_record.id) + "\n")
    f.write(str(seq_record.seq[421:480]) + "\n") #start 421 to end 480 base positions

    fin.close()
    fout.close()

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • 吃鸡王者 2019-06-28 10:01

    for i in range(1,69):
    inf="split_%s.fasta" % i
    outf="selected_"+inf
    with open(inf,'r') as infp:
    data=infp.readlines()
    with open(outf,'w') as outfp:
    outfp.writelines(data[421:481])

    评论
    解决 无用
    打赏 举报
  • Elsa镇魂女孩 2019-07-01 23:10
    # -*- coding:utf-8 -*-
    import os
    from Bio import SeqIO
    
    # root_dir为要读取文件的根目录
    root_dir = r"C:\Users\2350586L\PycharmProjects\split\splitE"
    # 读取批量文件后要写入的文件
    with open("FANCE1020_1080.fasta", "w") as f:
    
        # 依次读取根目录下的每一个文件
        for file in os.listdir(root_dir):
            file_name = root_dir + "\\" + file
            filein = open(file_name, "r")
            # 按行读取每个文件中的内容
            for seq_record in SeqIO.parse(file_name, "fasta"):
                    f.write(">")
                    f.write(str(seq_record.id) + "\n")
                    f.write(str(seq_record.seq[1020:1080]) + "\n")  #start 481 to end 540 base positions
    
            filein.close()
    print("FINISHED")
    
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题