text: user926: I was wondering if there is a tutorial or a small code snippet to understand how to write bioinformatics pipeline using python, for example
  
  use a aligner (say hisat) 
  get the output and process it using samtools 
  
  I was able to use subprocess from python2.7 for this purpose using samtools but i am not able to link both the processes.i.e given path(which I can use argparse) for directory with fastq files output would be processed bam. 
  sample code for samtools sam to bam :
  import subprocess
  subprocess.run(['samtools','view', '-bS',
                  '../some.sam',
                  '>',
                  '../some.bam'])
  
  
  user73: If you're just doing alignment and conversion to a sorted BAM file, there's no need to run it through python. A simple pipe on the unix command line works just as well (and probably runs faster):
  hisat2 -x genome.index -1 reads_R1.fq.gz -2 reads_R2.fq.gz | 
    samtools sort > reads_vs_genome.bam
  
  
  user191: I agree that using a specialized tool is probably a good idea.
  Nevertheless if you want to stick with Python, I suggest using plumbum instead of subprocess. It has a very nice syntax for this kind of problems.
  from plumbum import local
  
  # load a command
  samtools = local['samtools']
  
  # you can then easily redirect
  (samtools['view', '-bS', '../some.sam'] > '../some.bam')()
  
  # it is also easy to pipe 
  hisat2 = local['hisat2']
  chain = hisat2['-x', 'genome.index', '-1', 'reads_R1.fq.gz', '-2', 'reads_R2.fq.gz'] | samtools['sort'] > 'reads_vs_genome.bam'
  
  chain()
  
  
  user964: BioPython has some good tools for processing reads and alignments.
  http://biopython.org/DIST/docs/tutorial/Tutorial.html
  There is a python library wrapping samtools so many of the samtools calls can be used directly as python objects and calls
  https://pysam.readthedocs.io/en/latest/
  I would use subprocess to call the aligner and specify the output to a bam file that you have named and then read that bam file with pysam to do the analysis that you are interested in.  
  If you provide more specifics about the analysis or aligners I can help you more but you are in the right track just need to connect the two calls together.
  example:
  import subprocess
  import pysam
  import os 
  
  for root, dirs, files in os.walk(rootPath):
      for filename in fnmatch.filter(files, ".fastq"):
  
          subprocess.run([`hisat`, `-f`, filename `-o`, `some.bam`])
  
          #get depth
          depth = pysam.depth('some.bam')
  
  
  user377: Taking a different tack from other answers, there's lots of tools for pipelines in Python. Note: there was a time when people would use "pipeline" to refer to a shell script. I'm talking about something more sophisticated that helps you decompose an analysis into parts and  runs it robustly.
  
  Snakemake is my favourite. It's (nearly) pure Python and can generate reports.
  Nextflow is growing in popularity and is pretty straightforward
  Ruffus used to be reasonably popular, seemed fine when I used it
  Bpipe is for bioinformatics
  Airflow is a more industrial "big" solution but in wide use
  See this big list of pipeline systems
  
  I'm sure you can find something there.
  
original_text: 926: <p>I was wondering if there is a tutorial or a small code snippet to understand how to write bioinformatics pipeline using python, for example</p>
  
  <ol>
  <li>use a aligner (say hisat) </li>
  <li>get the output and process it using samtools </li>
  </ol>
  
  <p>I was able to use subprocess from python2.7 for this purpose using samtools but i am not able to link both the processes.i.e given path(which I can use argparse) for directory with fastq files output would be processed bam. </p>
  
  <p>sample code for samtools sam to bam :</p>
  
  <pre><code>import subprocess
  subprocess.run(['samtools','view', '-bS',
                  '../some.sam',
                  '&gt;',
                  '../some.bam'])
  </code></pre>
   73: <p>If you're just doing alignment and conversion to a sorted BAM file, there's no need to run it through python. A simple pipe on the unix command line works just as well (and probably runs faster):</p>
  
  <pre><code>hisat2 -x genome.index -1 reads_R1.fq.gz -2 reads_R2.fq.gz | 
    samtools sort &gt; reads_vs_genome.bam
  </code></pre>
   191: <p>I <a href="https://bioinformatics.stackexchange.com/a/1008/191">agree</a> that using a specialized tool is probably a good idea.</p>
  
  <p>Nevertheless if you want to stick with Python, I suggest using <a href="https://plumbum.readthedocs.io/en/latest/" rel="nofollow noreferrer"><code>plumbum</code></a> instead of <code>subprocess</code>. It has a very nice syntax for this kind of problems.</p>
  
  <pre class="lang-python prettyprint-override"><code>from plumbum import local
  
  # load a command
  samtools = local['samtools']
  
  # you can then easily redirect
  (samtools['view', '-bS', '../some.sam'] &gt; '../some.bam')()
  
  # it is also easy to pipe 
  hisat2 = local['hisat2']
  chain = hisat2['-x', 'genome.index', '-1', 'reads_R1.fq.gz', '-2', 'reads_R2.fq.gz'] | samtools['sort'] &gt; 'reads_vs_genome.bam'
  
  chain()
  </code></pre>
   964: <p>BioPython has some good tools for processing reads and alignments.
  <a href="http://biopython.org/DIST/docs/tutorial/Tutorial.html" rel="nofollow noreferrer">http://biopython.org/DIST/docs/tutorial/Tutorial.html</a></p>
  
  <p>There is a python library wrapping samtools so many of the samtools calls can be used directly as python objects and calls
  <a href="https://pysam.readthedocs.io/en/latest/" rel="nofollow noreferrer">https://pysam.readthedocs.io/en/latest/</a></p>
  
  <p>I would use subprocess to call the aligner and specify the output to a bam file that you have named and then read that bam file with pysam to do the analysis that you are interested in.  </p>
  
  <p>If you provide more specifics about the analysis or aligners I can help you more but you are in the right track just need to connect the two calls together.</p>
  
  <p>example:</p>
  
  <pre><code>import subprocess
  import pysam
  import os 
  
  for root, dirs, files in os.walk(rootPath):
      for filename in fnmatch.filter(files, ".fastq"):
  
          subprocess.run([`hisat`, `-f`, filename `-o`, `some.bam`])
  
          #get depth
          depth = pysam.depth('some.bam')
  </code></pre>
   377: <p>Taking a different tack from other answers, there's lots of tools for pipelines in Python. Note: there was a time when people would use "pipeline" to refer to a shell script. I'm talking about something more sophisticated that helps you decompose an analysis into parts and  runs it robustly.</p>
  
  <ul>
  <li><strong>Snakemake</strong> is my favourite. It's (nearly) pure Python and can generate reports.</li>
  <li><strong>Nextflow</strong> is growing in popularity and is pretty straightforward</li>
  <li><strong>Ruffus</strong> used to be reasonably popular, seemed fine when I used it</li>
  <li><strong>Bpipe</strong> is for bioinformatics</li>
  <li><strong>Airflow</strong> is a more industrial "big" solution but in wide use</li>
  <li>See this <a href="https://github.com/pditommaso/awesome-pipeline" rel="noreferrer">big list of pipeline systems</a></li>
  </ul>
  
  <p>I'm sure you can find something there.</p>
  