title: Linking subprocesses in Python
body: <p>Hi I had a question about linking input and output with sub-processes in python. I am trying to simplify the program by skipping the output of one step by passing it to another subprocess rather than output it to a file. Then open another process to run on that file.</p>
  
  <p>E.g. First process uses SAMTOOLS to output a specific chromosome from a large bam file.
  So...
  bigfile.bam is read in and outputs chromosome22.bam</p>
  
  <p>The next subprocess uses BEDTOOLS to convert that chromosome22.bam to chromosome22.bed
  So...
  chromosome22.bam is read in and outputs chromosome22.bed</p>
  
  <p>What I want to do is pass the stdout of the first process into the second so there is no need for the intermediate file.</p>
  
  <p>So far I have this...</p>
  
  <pre><code>for x in 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,'X','Y':
     subprocess.call("%s view -bh %s %s &gt; %s/%s/%s.bam" % (samtools,bam,x,bampath,out,x), shell=True)
  </code></pre>
  
  <p>This makes the chromosome[1-22,X,Y].bam files. But can I avoid this and put another subprocess command in the same loop to convert them to bed files?</p>
  
  <p>The command for bed conversion is:</p>
  
  <pre><code>bedpath/bedtools bamtobed -i [bamfile] &gt; [bedfile]
  </code></pre>