This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to ignore truncation error with PySam?

I want to extract some info from these sam files and ignore the truncation errors. I tried using ignore_truncation=True but it didn't work.

How can I catch this error and let it continue?

In [4]:
   ...: path_input = "./bbmap_output/1065.1/output.sam"
   ...: with pysam.AlignmentFile(path_input, ignore_truncation=True) as f:
   ...:     for i, segment in enumerate(f):
   ...:         try:
   ...:             pass
   ...:         except OSError:
   ...:             print(i, segment)
   ...:
[E::sam_parse1] SEQ and QUAL are of different length
[W::sam_read1] Parse error at line 7555317
---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
<ipython-input-4-f8c90b65a160> in <module>()
      2 path_input = "./bbmap_output/1065.1/output.sam"
      3 with pysam.AlignmentFile(path_input, ignore_truncation=True) as f:
----> 4     for i, segment in enumerate(f):
      5         try:
      6             pass

pysam/libcalignmentfile.pyx in pysam.libcalignmentfile.AlignmentFile.__next__()

OSError: truncated file
pysam sam alignment mapping python

Hello O.rka ,

  • Why do you want to do this?
  • The ignore_truncation isn't do what you are thinking. From the manual: Issue a warning, instead of raising an error if the current file appears to be truncated due to a missing EOF marker. Only applies to bgzipped formats.
  • The error arises during the for statement. So the try/except block inside the loop is to late. It must be outside.

fin swimmer

0 answers

No answers yet.

Log in to answer this question.