Search code examples
pipegnugnu-parallelsamtools

Piping commands withing GNU parallel (samtools)


I'm trying to run a command in parallel while piping. The reason is that the intermediate files are too big to keep, so I could discard them

I have the following codes, that do work separately:

#fixmate and convert to bam
parallel --verbose --link -j 4  'samtools fixmate -O bam,level=1 {1} /home/Teste1/samtools/unsorted/{/.}.bam -@ 8' ::: /home/Teste1/star/twopassoverhang/*.fqAligned.out.sam 

#sort according to coordinate
parallel --verbose --link -j 4  'samtools sort {1} -o /home/Teste1/samtools/sorted/{/.}-sorted.bam -@ 8' ::: /home/Teste1/samtools/unsorted/*.bam

#index bam files
parallel --verbose --link -j 4  'samtools index {1} -@ 8' ::: /home/Teste1/samtools/sorted/*-sorted.bam

I've tried:

parallel --verbose --link -j 4  'samtools fixmate -O bam,level=1 {1} - -@ 4 | / #fixmate and output bam to nextcommand
samtools sort - - -@ 4 | / #sort bams
tee /home/Teste1/samtools/sorted/{/.}-sorted.bam | / #save sorted bam file to disk
samtools index - -@ 4' ::: /home/Teste1/star/twopassoverhang/*.fqAligned.out.sam  #creates index

But I get the following with --dry-run:

 samtools fixmate -O bam,level=1 '/homeTeste1/star/twopassoverhang/101_FRAS202421991-1a_1.fqAligned.out.sam' - -@ 4 |
 samtools sort - - -@ 4 |
 tee /home/Teste1/samtools/sorted/101_FRAS202421991-1a_1.fqAligned.out-sorted.bam |
 samtools index - -@ 4

and running gives me:

samtools index: "-" is in a format that cannot be usefully indexed
samtools fixmate -O bam,level=1 '/home/Teste1/star/twopassoverhang/12_FRAS202372578-2r_1.fqAligned.out.sam' - -@ 4 | samtools sort - - -@ 4 | tee /home/Teste1/samtools/sorted/12_FRAS202372578-2r_1.fqAligned.out-sorted.bam | samtools index - -@ 4
[bam_sort] Use -T PREFIX / -o FILE to specify temporary and final output files
Usage: samtools sort [options...] [in.bam]

I've tried some other variations, but without success. Any ideas?


Solution

  • This might work:

    do_one() {
      sam="$1"
      sorted="$2"
      #fixmate and convert to bam
      samtools fixmate -O bam,level=1 "$sam" - -@ 8 |
        #sort according to coordinate
        samtools sort -T /home/Teste1/samtools/ - -o "$sorted" -@ 8
    
      #index bam files
      samtools index "$sorted" -@ 8
    }
    export -f do_one
    
    parallel do_one {} /home/Teste1/samtools/sorted/{/.}-sorted.bam ::: /home/Teste1/star/twopassoverhang/*.fqAligned.out.sam 
    

    If you cannot get samtools to work directly on pipes, then you might want to remove the intermediate files as soon as you are done with them:

    do_one() {
      sam="$1"
      bam="$2"
      sorted="$3"
      #fixmate and convert to bam
      samtools fixmate -O bam,level=1 "$sam" "$bam" -@ 8
    
      #sort according to coordinate
      samtools sort "$bam" -o "$sorted" -@ 8
    
      #index bam files
      samtools index "$sorted" -@ 8
    
      # remove unsorted
      rm "$bam" 
    }
    export -f do_one
    
    parallel do_one {} /home/Teste1/samtools/unsorted/{/.}.bam /home/Teste1/samtools/sorted/{/.}-sorted.bam ::: /home/Teste1/star/twopassoverhang/*.fqAligned.out.sam