#!/bin/bash
#
# This script is continuation of 00_preparation.sh


# please, log in to the metacentrum if you are not already logged in !

ssh user@skirit.ics.muni.cz


# Lets perform quality control of the raw data

# Lets don't forget we are in front end, in the metacentrum "lobby" , we need to ask for a machine to work on! 
qsub -l walltime=3:0:0 -q default -l select=1:ncpus=4:mem=10gb:scratch_local=50gb -I

# Our directory with data has this format: /storage/brno2/home/*username*/RNAseq/data/raw/

USERNAME="bartonv"

#lets make directories in the scratch
mkdir -p $SCRATCH/RNAseq/data/raw
mkdir -p $SCRATCH/RNAseq/scripts
mkdir -p $SCRATCH/RNAseq/results/qc

# Primary QC
#

############################################################################################
### Variables, need to be updated according to actual path to the input and output folder 

# DONT FORGET WE ARE AT A DIFFERENT MACHINE, hence need to put a complete path (please, dont forget to change the username)
INPUT_DIR=/storage/brno2/home/$USERNAME/RNAseq/data/raw

APPENDIX="fastq.gz"

ADAPTERS=/storage/brno2/home/$USERNAME/RNAseq/data/raw/adapters_merge.txt # Adapters to scan for in fasta format

THREADS=$PBS_NUM_PPN # we will do the analysis on all the available processors
# how many processors we have?
echo $THREADS


# The analysis requires FastQC, Kraken (minion, swan), multiQC
# Kraken with minion and swan are returning the positions of the adapters
module add fastQC-0.11.5
FASTQC=$(which fastqc)

module add kraken-15-065
MINION=$(which minion)
SWAN=$(which swan)

module add python27-modules-gcc #adding multiQC tool


#system variables needed to properly run MultiQC tool
export LC_ALL=C.UTF-8
export LANG=C.UTF-8
MULTIQC=$(which multiqc)


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

#enter the scratch
cd $SCRATCH/


### Copy inputs to the scratch so that we can work there
cp $INPUT_DIR/*$APPENDIX RNAseq/data/raw/
cp $ADAPTERS RNAseq/data/raw/

ADAPTERS=$(basename $ADAPTERS)
echo $ADAPTERS

ls RNAseq/data/raw/

############################################################################################
### FastQC QC

mkdir -p RNAseq/results/qc/fastqc/raw

#run quality control on raw reads
$FASTQC -t $THREADS -f fastq -o RNAseq/results/qc/fastqc/raw RNAseq/data/raw/*$APPENDIX
$MULTIQC -o RNAseq/results/qc/fastqc/raw RNAseq/results/qc/fastqc/raw/


### Minion adapter scan
mkdir -p RNAseq/results/qc/minion

for i in RNAseq/data/raw/*$APPENDIX
do
	fi=basename -s $APPENDIX ${i}
	#find all possible overrerpesented seqeunces in a sample (possible adapters)
	$MINION search-adapter -i $i -show 3 -write-fasta RNAseq/results/qc/minion/${fi}.minion.fasta
	#compare found overrepresented sequences to all known adapter sequences
	$SWAN -r RNAseq/data/raw/$ADAPTERS -q RNAseq/results/qc/minion/${fi}.minion.fasta >  RNAseq/results/qc/minion/${fi}.minion.compare
done 

############################################################################################
### Copy results

cp -r RNAseq/results/ /storage/brno2/home/$USERNAME/RNAseq/
rm -r $SCRATCH/*

# control if everything cleaned

ls $SCRATCH

# and logout from the machine

logout

#alternatively use command exit

# and logout from the front end

logout
#alternatively use command exit
