Hello, I am new to the world of Biopython and Python in general. I am try to parse a fasta file using class. I have got the following code so far:
from itertools import groupby
class OpenFastaFile:
def __init__(self, path):
self.path = path
self._map = {}
__fasta_sequences = self.__fasta_iter()
def __str__(self):
return self._map.__str__()
def __fasta_iter(self):
fh = open(self.path)
faiter = (x[1] for x in groupby(fh, lambda line: line[0] == ">"))
for header in faiter:
header = header.__next__()[1:].strip()
seq = "".join(s.strip() for s in faiter.__next__())
self._map[header] = seq
of = OpenFastaFile("sample.fa")
print(of)
However, I receive this output:
Traceback (most recent call last):
File "UniProtFile.py", line 25, in <module>
of = OpenFastaFile("sample.fa")
File "UniProtFile.py", line 12, in __init__
__fasta_sequences = self.__fasta_iter()
File "UniProtFile.py", line 21, in __fasta_iter
header = header.__next__()[1:].strip()
AttributeError: 'itertools._grouper' object has no attribute '__next__'
Process finished with exit code 1
My expected output was something along the lines of a dictionary like this: {'name' : 'ACCAGT' , 'name1' : 'ACGGCTA', etc}
Can someone please show me the error of my ways?
I would guess that
faiter
in the following line should beheader
:Thanks for replying! Unfortunately, it still gives the same error message.
Is there any reason that you're trying to implement this as a class?