Question: parse of file multifasta in python
gravatar for erick_rc93
23 months ago by
erick_rc9310 wrote:

I'm trying to parse a multifasta file with python with the next script

import re

def loadFasta(filename):
    if (filename.endswith(".gz")):
        fp =, 'rb')
        fp = open(filename, 'rb')
    # split at headers
    data =">")
    # ignore whatever appears before the 1st header
    headers = []
    sequences = []
    for sequence in data:
        lines = sequence.split('\n')
        # add an extra "+" to make string "1-referenced"
        sequences.append('+' + ''.join(lines))
    return (headers, sequences)

header, seq = loadFasta("/path/to/fasta/all_chromosomes.fasta")

for i in xrange(len(header)):
    print (header[i])
    print (len(seq[i])-1, "bases", seq[i][:30], "...", seq[i][-30:])

genome = seq[0]

But when I try to run the above script I get the next message error

Traceback (most recent call last):
  File "", line 23, in <module>
    header, seq = loadFasta("/path/to/fasta/all_chromosomes.fasta")
  File "", line 10, in loadFasta
    data =">")
TypeError: a bytes-like object is required, not 'str'
sequence • 841 views
ADD COMMENTlink modified 23 months ago by finswimmer14k • written 23 months ago by erick_rc9310

any reason you didn't try

ADD REPLYlink written 23 months ago by Kevin640
gravatar for finswimmer
23 months ago by
finswimmer14k wrote:

You open your file in binary mode with rb. This isn't necessary, just use r or omit this parameter, because that's the default.

  • What also works is to use data =">")
  • When working with files, it is good practice to use the withstatement because this takes care about closing you file correct
  • As Kevin said before, I would highly recommend using an existing module for handling fasta files like biopython.

fin swimmer

ADD COMMENTlink written 23 months ago by finswimmer14k
Please log in to add an answer.


Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1755 users visited in the last hour