(How) can you index a BAM file using pysam?

When I tried the intuitive pysam.index I got:

import pysam
my_bam = pysam.AlignmentFile("regular_bwamem_mapping.bam", "rb")

TypeError                                 Traceback (most recent call last)
<ipython-input-5-a0b3a04ecb2c> in <module>
----> 1 pysam.index(L_bam)

~/miniconda3/lib/python3.7/site-packages/pysam/utils.py in __call__(self, *args, **kwargs)
     59             args,
     60             catch_stdout=kwargs.get("catch_stdout", True),
---> 61             save_stdout=kwargs.get("save_stdout", None))
     63         if kwargs.get("split_lines", False):

pysam/libcutils.pyx in pysam.libcutils._pysam_dispatch()

TypeError: object of type 'pysam.libcalignmentfile.AlignmentFile' has no len()
  • 1
    $\begingroup$ disclaimer: when I post a question and answer, it's because I figure the answer while I write the question. $\endgroup$
    – Kamil S Jaron
    Feb 13 '20 at 16:15

Oh you silly sausage, pysam.index takes a bam file name, not a python object.

import pysam

will index your .bam file.

  • 2
    $\begingroup$ I should print "Oh you silly sausage" and tape it above my monitor. $\endgroup$ Feb 14 '20 at 14:20

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.