#!/bin/bash

echo ""
echo "Alignment of fastq files using Hisat2 has been started."
echo "--------------------------------------------------------------------------------"

for i in *.fastq
do
    #get basename without .fastq extension
    x="${i%%.*}" 

    #print the file that is currently processed
    echo "$i is currently processed..."
    echo "aligning $i..."
    echo "$i" >> hisat2.log

    #set file name for hisat2 output.sam
    filename="$x""_aligned.sam"  

    #run hisat2 with selected parameters and redirect summary output from str_error(2) to hisat2.log
    ~/Downloads/bioinformatics_tools/hisat2-2.1.0/hisat2 --score-min L,0,-0.36 -k 2 --max-seeds 20 --rna-strandness F -p 6 -x /media/user/hdd6/Reference_genomes_and_indexes/hisat_2_indexes/mm10/genome -U $i -S $filename 2>> hisat2.log

    #leave empty line in the log file
    echo "" >> hisat2.log

    #set file name for the sorted bam file
    filename2="$x"".bam"

    #print sorting message
    echo "sorting $x.sam..."
    
    #samtools to sort sam file based on coordinates, store the sorted version in bam format, delete sam file
    /home/user/Downloads/bioinformatics_tools/samtools-1.9/samtools view -uh $filename | /home/user/Downloads/bioinformatics_tools/samtools-1.9/samtools sort -o $filename2 &>> samtools.log
    rm $filename

    echo "$x.sam has been sorted and stored in bam format."
    echo "--------------------------------------------------------------------------------"
done

echo "Operation has been completed!"
echo ""
