#!/bin/bash
#SBATCH --time=120:00:00
#SBATCH --mem-per-cpu=8G
#SBATCH --cpus-per-task=16
#SBATCH --mail-type=ALL
#SBATCH --mail-user=linda.procell@nyulangone.org
#SBATCH --output=serial_test_%j.log

module load sratoolkit/2.10.8
module load fastqc/0.11.7
module load trimgalore/0.5.0
module load python/cpu/2.7.15-ES
module load samtools/1.9
module load bedtools/2.27.1
module load star/2.5.0c
module load igenome/1.0
module load subread/1.6.3

# make directory for each of the SRRs ($1 is each variable = each SRR)

 trim_galore --paired gpfs/data/yanailab/public/linda_share/for_debbie/GSE109972/$1_1.fastq.gz /gpfs/data/yanailab/public/linda_share/for_debbie/GSE109972/$1_2.fastq.gz --fastqc --fastqc_args "-o /gpfs/data/yanailab/public/linda_share/for_debbie/GSE109972/" --q 30

cd /gpfs/data/yanailab/public/linda_share/for_debbie/GSE109972/alignment_again

# aligning mm10 bulk star genome
STAR --genomeDir /gpfs/data/yanailab/projects/lp2700/data_raw/references/Mus_musculus/UCSC/mm10/STARIndex \
--runThreadN 6 \
--readFilesIn /gpfs/data/yanailab/public/linda_share/for_debbie/GSE109972/$1_1_trimmed.fq /gpfs/data/yanailab/public/linda_share/for_debbie/GSE109972/$1_2_trimmed.fq \
--readFilesCommand zcat \
--outFileNamePrefix /gpfs/data/yanailab/public/linda_share/for_debbie/GSE109972/alignment_again/$1 \

# sorting BAM file by chromosome etc.
# takes the sam and makes it a bam, then sorts it and indexes it
cd /gpfs/data/yanailab/public/linda_share/for_debbie/GSE109972/alignment_again
samtools view -bS -o $1Aligned.out.bam $1Aligned.out.sam
samtools sort -o $1.sorted.bam $1Aligned.out.bam
samtools index $1.sorted.bam

# counts
mkdir /gpfs/data/yanailab/public/linda_share/for_debbie/GSE109972/featurecounts/$1
cd /gpfs/data/yanailab/public/linda_share/for_debbie/GSE109972/featurecounts/$1

featureCounts -p -B -a /gpfs/data/yanailab/projects/lp2700/data_raw/references/Mus_musculus/UCSC/mm10/STARIndex/genes.gtf -o /gpfs/data/yanailab/public/linda_share/for_debbie/GSE109972/featurecounts/$1/$1 /gpfs/data/yanailab/public/linda_share/for_debbie/GSE109972/alignment_again/$1.sorted.bam

featureCounts -p -B -a /gpfs/data/yanailab/projects/dl4564/GSE109972/fastq/STARIndex/genes.gtf -o /gpfs/data/yanailab/projects/dl4564/GSE109972/featurecounts/$1/$1 /gpfs/data/yanailab/projects/dl4564/GSE109972/aligned/$1.sorted.bam