I was wondering if there is a tutorial or a small code snippet to understand how to write bioinformatics pipeline using python, for example

  1. use a aligner (say hisat)
  2. 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',
  • 4
    $\begingroup$ If you ever need more elaborate pipelines in python, then I would strongly encourage you to look into snakemake, which is pretty commonly used for this purpose. $\endgroup$
    – Devon Ryan
    Commented Jul 6, 2017 at 6:53
  • 1
    $\begingroup$ I agree that snakemake is worth learning. If you want to use subprocess, I found the following tutorial just yesterday, which looked quite clear: crashcourse.housegordon.org/python-subprocess.html $\endgroup$
    – bli
    Commented Jul 6, 2017 at 11:58
  • $\begingroup$ @bli this was useful $\endgroup$ Commented Jul 6, 2017 at 12:22
  • $\begingroup$ @DevonRyan I looked snakemake it is available only in python3 is that correct? $\endgroup$ Commented Jul 6, 2017 at 12:28
  • $\begingroup$ @novicebioinforesearcher Yes, that's correct. $\endgroup$
    – Devon Ryan
    Commented Jul 6, 2017 at 12:29

5 Answers 5


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'


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.


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.


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')
  • 1
    $\begingroup$ edited code snippet based on OP comments about executing on multiple fastq files $\endgroup$
    – Bioathlete
    Commented Jul 6, 2017 at 2:36

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
  • $\begingroup$ thank you @gringer, I am familiar with unix pipe. I am trying to learn to create a pipeline where when I give path of a directory which has fastqs I intend to submit .sh files to cluster and get the desired results . for example python my_pipeline.py -dir /pathtofastq would submit jobs to cluster here i will use bunch of argparse and subprocess and predefinec bsub commands. $\endgroup$ Commented Jul 6, 2017 at 1:38
  • 1
    $\begingroup$ Look into python's os.path.walk. This will allow you to take in the dir and then iterate over all the fastq files in the directory. pythonforbeginners.com/fnmatch/os-walk-and-fnmatch-in-python $\endgroup$
    – Bioathlete
    Commented Jul 6, 2017 at 2:31

Have a look at luigi for writing pipelines in python. You can found BioLuigi if you like :-P

  • $\begingroup$ could you please share link for BioLuigi I could not find it online $\endgroup$ Commented Jul 6, 2017 at 12:27
  • 2
    $\begingroup$ You need to start it ;-) $\endgroup$
    – Dan
    Commented Jul 7, 2017 at 8:46
  • $\begingroup$ haha good one!! $\endgroup$ Commented Jul 7, 2017 at 13:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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