Answers.Body: <p>Oh you silly sausage, <code>pysam.index</code> takes a bam file name, not a python object.</p>
  <pre><code>import pysam
  pysam.index(&quot;regular_bwamem_mapping.bam&quot;)
  </code></pre>
  <p>will index your <code>.bam</code> file.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-02-14T14:20:34.283Z
Answers.Text: I should print "Oh you silly sausage" and tape it above my monitor.
Answers.UserDisplayName: Daniel Standage
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-02-13T16:14:29.463Z
Answers.LastActivityDate: 2021-05-17T10:58:45.903Z
Answers.LastEditDate: 2021-05-17T10:58:45.903Z
Answers.UserDisplayName: Kamil S Jaron
Body: <p>(How) can you index a BAM file using <code>pysam</code>?</p>
  
  <p>When I tried the intuitive <code>pysam.index</code> I got:</p>
  
  <pre><code>import pysam
  my_bam = pysam.AlignmentFile("regular_bwamem_mapping.bam", "rb")
  pysam.index(my_bam)
  
  ---------------------------------------------------------------------------
  TypeError                                 Traceback (most recent call last)
  &lt;ipython-input-5-a0b3a04ecb2c&gt; in &lt;module&gt;
  ----&gt; 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),
  ---&gt; 61             save_stdout=kwargs.get("save_stdout", None))
       62
       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()
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-02-13T16:15:30.840Z
Comments.Text: disclaimer: when I post a question and answer, it's because I figure the answer while I write the question.
Comments.UserDisplayName: Kamil S Jaron
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-11-26T02:28:43.727Z
Comments.Text: I have tried same command...
  error
  [E::hts_idx_push] Unsorted positions on sequence #8: 3411049 followed by 3378665
  [E::sam_index] Read 'A00404:40:HFGYNDSXX:4:1258:24623:27633' with ref_name='chr8', ref_length=145138636, flags=1107, pos=3378665 cannot be indexed
  Traceback (most recent call last): File "Insertx.py", line 283, in <module> main() File "Insertx.py", line 263, in main pysam.index(str(''.join( data))) File "/usr/local/lib/python3.8/dist-packages/pysam/utils.py", line 69, in __call__ raise SamtoolsError(
  pysam.utils.SamtoolsError: 'samtools returned with error 1: stdout=, stderr=samt
Comments.UserDisplayName: anju
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-12-15T14:14:38.073Z
Comments.Text: @anju there must have been something that gone wrong with sorting the file. Otherwise it would not complain about it.
Comments.UserDisplayName: Kamil S Jaron
Tags: <sam><indexing>
Title: Index a BAM file using pysam
ThreadText: # Index a BAM file using pysam
  
  (Asked by: username_0 on 2020-02-13)
  
  (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")
  pysam.index(my_bam)
  
  ---------------------------------------------------------------------------
  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))
       62
       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()
  
  ```
  
  **Tags:** sam, indexing
  
  > **Comments:**
  > **username_0**: disclaimer: when I post a question and answer, it's because I figure the answer while I write the question.  
  > **username_1**: I have tried same command... error [E::hts_idx_push] Unsorted positions on sequence #8: 3411049 followed by 3378665 [E::sam_index] Read 'A00404:40:HFGYNDSXX:4:1258:24623:27633' with ref_name='chr8', ref_length=145138636, flags=1107, pos=3378665 cannot be indexed Traceback (most recent call last): File "Insertx.py", line 283, in <module> main() File "Insertx.py", line 263, in main pysam.index(str(''.join( data))) File "/usr/local/lib/python3.8/dist-packages/pysam/utils.py", line 69, in __call__ raise SamtoolsError( pysam.utils.SamtoolsError: 'samtools returned with error 1: stdout=, stderr=samt  
  > **username_0**: @username_1 there must have been something that gone wrong with sorting the file. Otherwise it would not complain about it.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2020-02-13. Score: 13 | Accepted answer
  
  Oh you silly sausage, `pysam.index` takes a bam file name, not a python object.
  
  ```
  import pysam
  pysam.index("regular_bwamem_mapping.bam")
  
  ```
  
  will index your `.bam` file.
  
  > **Comments:**
  > **username_2**: I should print "Oh you silly sausage" and tape it above my monitor.  
  
  ---
  
  