#!/bin/bash
#PBS -N 05_mapping_qc
#PBS -l select=1:ncpus=4:mem=16gb:scratch_local=10gb
#PBS -l walltime=12:00:00 
#PBS -m ae
# The 4 lines above are options for scheduling system: job will run 1 hour at maximum, 1 machine with 4 processors + 4gb RAM memory + 10gb scratch memory are requested, email notification will be sent when the job aborts (a) or ends (e)

#
############################################################################################

# if going by interactive job:
# qsub -l walltime=2:0:0 -q default -l select=1:ncpus=4:mem=12gb:scratch_local=10gb -I

### Variables

USERNAME="bartonv"
cd /storage/brno2/home/${USERNAME}/
THREADS=$PBS_NUM_PPN
INPUT_DIR=/storage/brno2/home/${USERNAME}/RNASeq/data/mapped
#INPUT_DIR=/storage/brno2/home/bartonv/E5444/mapped
OUTPUT_DIR_QC=/storage/brno2/home/${USERNAME}/RNASeq/results/qc/mapped


# if example data:
# cp /storage/brno2/home/bartonv/E5444/mapped $INPUT_DIR


mkdir -p $OUTPUT_DIR_QC

# Binaries
module add samtools

####################################################################################################
### QC
for file in $INPUT_DIR/*.Aligned.sortedByCoord.out.bam; do
    sample=$(basename $file)
    samtools flagstat $file > $OUTPUT_DIR_QC/$sample.falgstat.txt 
    samtools stats $file > $OUTPUT_DIR_QC/$sample.stats.txt
    samtools idxstats $file > $OUTPUT_DIR_QC/$sample.idxstats.txt
        
done

####################################################################################################
### multi QC


module add python27-modules-gcc # multiqc

multiqc -o $OUTPUT_DIR_QC $OUTPUT_DIR_QC

