0
$\begingroup$

I am a beginner and trying to read a bam file in Python.

The lines below throw the error

OSError: [Errno 8] could not open alignment file `mybam.bam`: Exec format error

What could be the possible solution?

user@server:~/Documents$ python3
Python 3.8.10 (default, Jun  2 2021, 10:49:15)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> import pysam
>>> samfile = pysam.AlignmentFile("mybam.bam", "rb")

[E::hts_hopen] Failed to open file mybam.bam
[E::hts_open_format] Failed to open file "mybam.bam" : Exec format error
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "pysam/libcalignmentfile.pyx", line 742, in pysam.libcalignmentfile.AlignmentFile.__cinit__
  File "pysam/libcalignmentfile.pyx", line 941, in pysam.libcalignmentfile.AlignmentFile._open
OSError: [Errno 8] could not open alignment file `mybam.bam`: Exec format error
$\endgroup$
3
  • 3
    $\begingroup$ this is something related to the file not with python. Check the file with the linux command file mybam.bam just to know that you have a bam instead of a sam, text format. If you have a compressed file in bam format, check the head with samtools view mybam.bam | head -100, if this fails check the mapping software again where you got the alignment from. $\endgroup$
    – zorbax
    Oct 15 at 18:25
  • $\begingroup$ biostars.org/p/284417 $\endgroup$ Oct 15 at 22:02
  • $\begingroup$ Thank you for the help. samtools view mybam.bam | head -100 outputed a hundred lines, which have no index. As there is no index, I was able to retrieve the content with: for read in samfile.fetch(until_eof=True): print(read) $\endgroup$
    – monade
    Oct 16 at 9:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.