jupyter-notebookbiopython

how to solve type Traceback (most recent call last) error in biopython?


I am trying to write a SeqRecord to a fasta file via SeqIO.Write() method. I import all requirments but I encounter an error.

this is my code :

from Bio.SeqRecord import SeqRecord
from Bio import SeqIO
sr = SeqRecord(seq='ATATAT',id = '1',description = 'test')
SeqIO.write(sr,'d:myfile.fasta','fasta')

and the error is this :


TypeError                                 Traceback (most recent call last)
File ~\anaconda3\lib\site-packages\Bio\File.py:73, in as_handle(handleish, mode, **kwargs)
 72     with open(handleish, mode, **kwargs) as fp:
 ---> 73         yield fp
 74 except TypeError:

File ~\anaconda3\lib\site-packages\Bio\SeqIO\__init__.py:518, in write(sequences, handle, format)
517 for record in sequences:
--> 518     fp.write(format_function(record))
519     count += 1`
   File ~\anaconda3\lib\site-packages\Bio\SeqIO\FastaIO.py:429, in as_fasta(record)
427 lines = [f">{title}\n"]
--> 429 data = _get_seq_string(record)  # Catches sequence being None
430 assert "\n" not in data

File ~\anaconda3\lib\site-packages\Bio\SeqIO\Interfaces.py:107, in _get_seq_string(record)
106 elif not isinstance(record.seq, (Seq, MutableSeq)):
--> 107     raise TypeError(f"SeqRecord (id={record.id}) has an invalid sequence.")
108 return str(record.seq)

TypeError: SeqRecord (id=1) has an invalid sequence.

During handling of the above exception, another exception occurred:

RuntimeError                              Traceback (most recent call last)
Cell In[48], line 1
----> 1 SeqIO.write(sr,'d:myfile.fasta','fasta')

  File ~\anaconda3\lib\site-packages\Bio\SeqIO\__init__.py:516, in write(sequences, handle,            format)
514 if format_function is not None:
515     count = 0
--> 516     with as_handle(handle, "w") as fp:
517         for record in sequences:
518             fp.write(format_function(record))

File ~\anaconda3\lib\contextlib.py:185, in _GeneratorContextManager.__exit__(self, typ, value,                 traceback)
183         raise
184     return False
--> 185 raise RuntimeError("generator didn't stop after throw()")

RuntimeError: generator didn't stop after throw()

Solution

  • try:

    from Bio.SeqRecord import SeqRecord 
    
    from Bio import SeqIO
    
    from Bio.Seq import Seq
    
    sr = SeqRecord(Seq('ATATAT'),id = '1',description = 'test') 
    
    SeqIO.write(sr,'d:myfile.fasta','fasta')
    

    your output file will be named d:myfile.fasta though, its contents

    >1 test
    ATATAT