Answers.Body: <p>It's a bit hard to say with certainty, though I would suspect that offloading the BAM decompression by using a pipe will be very slightly faster. Note that decompressing and parsing the BAM file will not be the bottleneck in your processing, rather the python script itself will be.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-12-11T21:51:34.613Z
Answers.LastActivityDate: 2019-12-11T21:51:34.613Z
Answers.UserDisplayName: Devon Ryan
Body: <p>Is it faster to use the PySam package to run a python script on a bam</p>
  
  <pre><code>for read in samfile.fetch('chr1', 100, 120):
   print read
  </code></pre>
  
  <p>compared to using a pipe and reading line by line from stdin in a python script. </p>
  
  <p><code>samtools view example.bam | python script.py.
  </code></p>
  
  <pre><code>for line in tqdm(sys.stdin, total=number_of_lines)
  </code></pre>
  
Tags: <python><sam><samtools><pysam>
Title: pysam or piping samtools view to a python script
ThreadText: # pysam or piping samtools view to a python script
  
  (Asked by: username_0 on 2019-12-11)
  
  Is it faster to use the PySam package to run a python script on a bam
  
  ```
  for read in samfile.fetch('chr1', 100, 120):
   print read
  
  ```
  
  compared to using a pipe and reading line by line from stdin in a python script.
  
  `samtools view example.bam | python script.py.`
  
  ```
  for line in tqdm(sys.stdin, total=number_of_lines)
  
  ```
  
  **Tags:** python, sam, samtools, pysam
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-12-11. Score: 5
  
  It's a bit hard to say with certainty, though I would suspect that offloading the BAM decompression by using a pipe will be very slightly faster. Note that decompressing and parsing the BAM file will not be the bottleneck in your processing, rather the python script itself will be.
  
  
  ---
  
  