how to read a fasta file in python?

Viewed 32893

I'm trying to read a FASTA file and then find specific motif(string) and print out the sequence and number of times it occurs. A FASTA file is just series of sequences(strings) that starts with a header line and the signature for header or start of a new sequence is ">". in a new line immediately after the header is the sequence of letters.I'm not done with code but so far I have this and it gives me this error:

AttributeError: 'str' object has no attribute 'next'

I'm not sure what's wrong here.

import re

header=""
counts=0
newline=""

f1=open('fpprotein_fasta(2).txt','r')
f2=open('motifs.xls','w')
for line in f1:
    if line.startswith('>'):
        header=line
        #print header
        nextline=line.next()
        for i in nextline:
            motif="ML[A-Z][A-Z][IV]R"
            if re.findall(motif,nextline):
                counts+=1
                #print (header+'\t'+counts+'\t'+motif+'\n')
        fout.write(header+'\t'+counts+'\t'+motif+'\n')

f1.close()
f2.close()
4 Answers

This is how I load FASTA file to a dictionary:

motifs = dict()

with open('[path to FASTA file]\filename.fna') as f:
lines = f.readlines()
for i in range(0, len(lines)):
    s = lines[i].strip()
    if s[0] == '>':
        key = s[1:]
    else:
        motifs[key] = s

each line starting with '>' character contains the id(key) of the next line.

Related