parsing a fasta file using a generator ( python )

前端 未结 4 819
臣服心动
臣服心动 2020-11-28 14:14

I am trying to parse a large fasta file and I am encountering out of memory errors. Some suggestions to improve the data handling would be appreciated. Currently the program

4条回答
  •  醉梦人生
    2020-11-28 14:34

    def read_fasta(filename):
        name = None
        with open(filename) as file:
            for line in file:
                if line[0] == ">":
                    if name:
                        yield (name, seq)
                    name = line[1:-1].split("|")[0]
                    seq = ""
                else:
                    seq += line[:-1]
            yield (name, seq)
    

提交回复
热议问题