
I read python basic codes and I am fimiliar with for loops, But not able to understand how to make a python programme to run hisat2 on multiple pair-end reads.

I want to run hisat2 mapping tool on mutiple pair end reads to map these on Genome.

I already have prepared genome index.


1)my input will be hisat2 location and  genome index, pair-end read file
2) How should I give multiple hisat2 arguments and how to store sam file in a separate folder for each pair-end reads.

Can you help to make this python script ?

    Location of hisat2:
     /home/yog/software/hisat2-2.0.4/hisat2

    Location of genome index:
    data/Analysis/Radish_index

    Location of pair end files:
    
     /data/Analysis/mapped/

    multiple pair end files like this:
    
    216_5W_Ca1_R1.fq
    
    216_5W_Ca1_R2.fq
    
    218_5W_Ca1_R1.fq
    
    218_5W_Ca1_R1.fq



    216_5W_Co1_R1.fq
    
    216_5W_Co1_R2.fq
    
    218_5W_Co1_R1.fq
    
    218_5W_Co1_R1.fq

Now I am running hisat2 on individual files by using this commonad:

    /home/yog/software/hisat2-2.0.4/hisat2 -p 8 -x /data/Analysis/Radish_index  --fr -1 /data/Analysis/mapped/216_5W_Ca1_R1.fq -2 /data/Analysis/mapped/216_5W_Ca1_R2.fq -S 216_5W_Ca1.sam


It would be easier to do this in bash:

    for r1 in  /data/Analysis/mapped/*R1.fq
    do 
      #replace R1 by R2
      r2=${r1/R1/R2}
      # snip off the fq and add sam to the output file name
      out=${r1%%_R1.fq}.sam
      /home/yog/software/hisat2-2.0.4/hisat2 -p 8 -x /data/Analysis/Radish_index  --fr -1 $r1 -2 $r2 -S $out
    done

In Python it's similar:

    import glob
    import os
    for r1 in glob.glob('/data/Analysis/mapped/*R1.fq'):
      r2 = r1.replace('R1', 'R2')
      out = r1.replace('_R1.fq', '.sam')
      os.popen('/home/yog/software/hisat2-2.0.4/hisat2 -p 8 -x /data/Analysis/Radish_index  --fr -1 %s -2 %s -S %s'%(r1, r2, out)) 

None of those are multithreaded - you can use the subprocess library instead of os.popen in Python so that each job runs in the background instead of it running each job one after the other, and you can end the command using & in bash to run all of the jobs at the same time, like this:

    /home/yog/software/hisat2-2.0.4/hisat2 -p 8 -x /data/Analysis/Radish_index  --fr -1 $r1 -2 $r2 -S $out &

Here I'm also not doing anything special to capture the standard error or the standard output, it would be better for you to have a look at all of the standard error.