#!/bin/bash -x 

paired_flag=0
while getopts ":r:P:p" opt; do
   case $opt in
      r )
        run_id="$OPTARG"
        ;;
      P )
        proj="$OPTARG"
        ;;
      p )
        paired_flag=1
        ;;
    esac
done
shift "$((OPTIND -1))"
#cat runlist | xargs -I {} fastq-dump --fasta --split-3 {} &

hbasepath=out/hits
cbasepath=out/counts
#proj=SRP050499
hitpath="${hbasepath}/${proj}"
cpath="${cbasepath}/${proj}"

if [ ${paired_flag} -eq  1 ]; then
echo -n "(hisat2 -f -x GCA_000001405.15_GRCh38_no_alt_analysis_set -1 in/${proj}/${run_id}_1.fasta -2 in/${proj}/${run_id}_2.fasta  -S ${hitpath}/${run_id} ) 2> ${hitpath}/${run_id}.hisat_log"
(/usr/local/hisat2/2-2.0.5/bin/hisat2 -f -x GCA_000001405.15_GRCh38_no_alt_analysis_set -1 in/${proj}/${run_id}_1.fasta -2 in/${proj}/${run_id}_2.fasta  -S ${hitpath}/${run_id}.sam ) 2> ${hitpath}/${run_id}.hisat_log
else
(/usr/local/hisat2/2-2.0.5/bin/hisat2 -f -x GCA_000001405.15_GRCh38_no_alt_analysis_set -U in/${proj}/${run_id}.fasta  -S ${hitpath}/${run_id}.sam ) 2> ${hitpath}/${run_id}.hisat_log
fi
echo -n "samtools sort ${hitpath}/${run_id}.sam > ${hitpath}/${run_id}.bam" 
/usr/local/samtools/1.9/bin/samtools sort ${hitpath}/${run_id}.sam > ${hitpath}/${run_id}.bam 
echo -n "~/packgs/subread-1.6.4-source/bin/featureCounts -t exon -g gene_id -M -O --fraction -a ../GCA_000001405.15_GRCh38_noalt.gtf ${hitpath}/${run_id}.bam -o ${run_id}.counts "
~/packgs/subread-1.6.4-source/bin/featureCounts -t exon -g gene_id -M -O --fraction -a GCA_000001405.15_GRCh38_noalt.gtf ${hitpath}/${run_id}.bam -o ${cpath}/${run_id}.counts 
