#!/bin/bash/
fcid=$1
#####################################################
########### preparo genoma di riferimento ###########
cat /home/vm-gen-ngs/Scrivania/CORSO_NGS/GENOME_REF/*.fa > /home/vm-gen-ngs/Scrivania/CORSO_NGS/GENOME_REF/reference.fa
/home/vm-gen-ngs/Scrivania/PROGRAMMI/bwa-0.7.10/bwa index -p /home/vm-gen-ngs/Scrivania/CORSO_NGS/GENOME_REF/reference.fa -a bwtsw /home/vm-gen-ngs/Scrivania/CORSO_NGS/GENOME_REF/reference.fa
######################################################
########### creo files bam indicizzati ###########
creo SAM FILES con BWA mem
######################################################
/home/vm-gen-ngs/Scrivania/PROGRAMMI/bwa-0.7.10/bwa mem -R "@RG\tID:IDa\tSM:SM\tPL:Illumina" -t 1 /home/vm-gen-ngs/Scrivania/CORSO_NGS/GENOME_REF/reference.fa /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/EXAMPLE_1/EXAMPLE_1_R1_001.fastq.gz /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/EXAMPLE_1/EXAMPLE_1_R2_001.fastq.gz > /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/EXAMPLE_1/EXAMPLE_1_001.sam
######################################################
## ALTERNATIVA creo SAM FILES con BWA aln
# ####################################################
/home/vm-gen-ngs/Scrivania/PROGRAMMI/bwa-0.7.10/bwa aln -q 20 -t 4 /home/vm-gen-ngs/Scrivania/CORSO_NGS/GENOME_REF/reference.fa /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/EXAMPLE_1/EXAMPLE_1_R1_001.fastq.gz > /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/EXAMPLE_1/EXAMPLE_1_R1_001.sai
/home/vm-gen-ngs/Scrivania/PROGRAMMI/bwa-0.7.10/bwa aln -q 20 -t 4 /home/vm-gen-ngs/Scrivania/CORSO_NGS/GENOME_REF/reference.fa /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/EXAMPLE_1/EXAMPLE_1_R2_001.fastq.gz > /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/EXAMPLE_1/EXAMPLE_1_R2_001.sai
/home/vm-gen-ngs/Scrivania/PROGRAMMI/bwa-0.7.10/bwa sampe -r "@RG\tID:IDa\tSM:SM\tPL:Illumina" /home/vm-gen-ngs/Scrivania/CORSO_NGS/GENOME_REF/reference.fa /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/EXAMPLE_1/EXAMPLE_1_R1_001.sai /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/EXAMPLE_1/EXAMPLE_1_R2_001.sai /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/EXAMPLE_1/EXAMPLE_1_R1_001.fastq.gz /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/EXAMPLE_1/EXAMPLE_1_R2_001.fastq.gz > /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/EXAMPLE_1/EXAMPLE_1_001.sam
#####################################################
# CONVERTO SAM in BAM
#####################################################
samtools view -bt /home/vm-gen-ngs/Scrivania/CORSO_NGS/GENOME_REF/reference.fa -o /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/EXAMPLE_1/EXAMPLE_1.bam /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/EXAMPLE_1/EXAMPLE_1_001.sam
samtools sort /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/EXAMPLE_1/EXAMPLE_1.bam /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/EXAMPLE_1/EXAMPLE_1
samtools index /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/EXAMPLE_1/EXAMPLE_1.bam
#########################################################################
### ALTERNATIVA: CONVERTO SAM in BAM sorted con picard-tools
#########################################################################
java -jar -Xms4g -Xmx4g /home/vm-gen-ngs/Scrivania/PROGRAMMI/picard-tools-1.119/SortSam.jar INPUT= /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/EXAMPLE_1/EXAMPLE_1_001.sam OUTPUT= /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/EXAMPLE_1/EXAMPLE_1.sorted.bam SORT_ORDER=coordinate
java -jar -Xms4g -Xmx4g /home/vm-gen-ngs/Scrivania/PROGRAMMI/picard-tools-1.119/BuildBamIndex.jar INPUT= /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/EXAMPLE_1/EXAMPLE_1.sorted.bam
#####################################################
### riallineamento locale
#####################################################
samtools faidx /home/vm-gen-ngs/Scrivania/CORSO_NGS/GENOME_REF/reference.fa
picard-tools CreateSequenceDictionary R= /home/vm-gen-ngs/Scrivania/CORSO_NGS/GENOME_REF/reference.fa O= /home/vm-gen-ngs/Scrivania/CORSO_NGS/GENOME_REF/reference.dict
java -d64 -Xms4g -Xmx4g -jar /home/vm-gen-ngs/Scrivania/PROGRAMMI/GenomeAnalysisTK.jar -T RealignerTargetCreator -R /home/vm-gen-ngs/Scrivania/CORSO_NGS/GENOME_REF/reference.fa -I /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/EXAMPLE_1/EXAMPLE_1.sorted.bam -L:capture,BED /home/vm-gen-ngs/Scrivania/CORSO_NGS/TARGETS_AND_DATABASES/targets.interval_list -o /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/EXAMPLE_1/EXAMPLE_1.sorted.targets.list
java -d64 -Xms4g -Xmx4g -jar /home/vm-gen-ngs/Scrivania/PROGRAMMI/GenomeAnalysisTK.jar -T IndelRealigner -I /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/EXAMPLE_1/EXAMPLE_1.sorted.bam -targetIntervals /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/EXAMPLE_1/EXAMPLE_1.sorted.targets.list -R /home/vm-gen-ngs/Scrivania/CORSO_NGS/GENOME_REF/reference.fa -known /home/vm-gen-ngs/Scrivania/CORSO_NGS/TARGETS_AND_DATABASES/1000G_phase1.indels.hg19.vcf -known /home/vm-gen-ngs/Scrivania/CORSO_NGS/TARGETS_AND_DATABASES/dbsnp_135.hg19.vcf --consensusDeterminationModel KNOWNS_ONLY -LOD 0.4 -o /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/EXAMPLE_1/EXAMPLE_1.realigned.bam
#####################################################
### RICALIBRAZIONE
#####################################################
java -d64 -Xms4g -Xmx4g -jar /home/vm-gen-ngs/Scrivania/PROGRAMMI/GenomeAnalysisTK.jar -T BaseRecalibrator --maximum_cycle_value 600 -I /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/EXAMPLE_1/EXAMPLE_1.realigned.bam -R /home/vm-gen-ngs/Scrivania/CORSO_NGS/GENOME_REF/reference.fa -knownSites /home/vm-gen-ngs/Scrivania/CORSO_NGS/TARGETS_AND_DATABASES/dbsnp_135.hg19.vcf -cov QualityScoreCovariate -cov CycleCovariate -o /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/EXAMPLE_1/EXAMPLE_1.realigned.grp
java -d64 -Xms4g -Xmx4g -jar /home/vm-gen-ngs/Scrivania/PROGRAMMI/GenomeAnalysisTK.jar -T PrintReads -I /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/EXAMPLE_1/EXAMPLE_1.realigned.bam -R /home/vm-gen-ngs/Scrivania/CORSO_NGS/GENOME_REF/reference.fa -BQSR /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/EXAMPLE_1/EXAMPLE_1.realigned.grp -o /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/EXAMPLE_1/EXAMPLE_1.bam
rm -f, yes | rm /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/EXAMPLE_1/*.realigned*
rm -f, yes | rm /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/EXAMPLE_1/*.sorted*
#####################################################
########### chiamata varianti GATK ###########
#####################################################
java -jar /home/vm-gen-ngs/Scrivania/PROGRAMMI/GenomeAnalysisTK.jar -T UnifiedGenotyper -I /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/EXAMPLE_1/EXAMPLE_1.bam -R /home/vm-gen-ngs/Scrivania/CORSO_NGS/GENOME_REF/reference.fa -o /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/EXAMPLE_1/EXAMPLE_1_UNI.vcf -nct 200 -L:capture,BED /home/vm-gen-ngs/Scrivania/CORSO_NGS/GENOME_REF/targets.interval_list -glm BOTH -dt NONE -stand_emit_conf 20
java -jar /home/vm-gen-ngs/Scrivania/PROGRAMMI/GenomeAnalysisTK.jar -T HaplotypeCaller -I /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/EXAMPLE_1/EXAMPLE_1.bam -R /home/vm-gen-ngs/Scrivania/CORSO_NGS/GENOME_REF/reference.fa -o /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/EXAMPLE_1/EXAMPLE_1_HAPLO.vcf -nct 200 -L:capture,BED /home/vm-gen-ngs/Scrivania/CORSO_NGS/GENOME_REF/targets.interval_list -stand_emit_conf 20
#####################################################
########### chiamata varianti VARSCAN ##########
#####################################################
samtools mpileup -B -q 1 -f /home/vm-gen-ngs/Scrivania/CORSO_NGS/GENOME_REF/reference.fa /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/EXAMPLE_1/EXAMPLE_1.bam > /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/EXAMPLE_1/EXAMPLE_1.pileup
java -jar /home/vm-gen-ngs/Scrivania/CORSO_NGS/PROGRAMMI/VarScan.v2.3.9.jar mpileup2snp /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/EXAMPLE_1/EXAMPLE_1.pileup --min-coverage 4 --min-reads2 4 --min-avg-qual 20 --min-var-freq 0.05 --p-value 0.05 --output-vcf 1 > /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/EXAMPLE_1/EXAMPLE_1_varscan_snp.vcf
java -jar /home/vm-gen-ngs/Scrivania/CORSO_NGS/PROGRAMMI/VarScan.v2.3.9.jar mpileup2indel /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/EXAMPLE_1/EXAMPLE_1.pileup --min-coverage 4 --min-reads2 4 --min-avg-qual 20 --min-var-freq 0.05 --p-value 0.05 --output-vcf 1 > /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/EXAMPLE_1/EXAMPLE_1_varscan_indel.vcf
java -jar /home/vm-gen-ngs/Scrivania/CORSO_NGS/PROGRAMMI/VarScan.v2.3.9.jar mpileup2cns /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/EXAMPLE_1/EXAMPLE_1.pileup --min-coverage 4 --min-reads2 4 --min-avg-qual 20 --min-var-freq 0.05 --p-value 0.05 --output-vcf 1 > /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/EXAMPLE_1/EXAMPLE_1_varscan_cns.vcf
#####################################################################
########### chiamata varianti SOMATICHE VARSCAN ###########
#####################################################################
### Passaggio Fastq BAM saltando per ragioni di tempo la parte della ricalibrazione e allineamento locale
/home/vm-gen-ngs/Scrivania/PROGRAMMI/bwa-0.7.10/bwa mem -R "@RG\tID:IDa\tSM:SM\tPL:Illumina" -t 1 /home/vm-gen-ngs/Scrivania/CORSO_NGS/GENOME_REF/reference.fa /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/SOMATIC/PT_05_CK19p_S12_L001_R1_001.fastq.gz /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/SOMATIC/PT_05_CK19p_S12_L001_R2_001.fastq.gz > /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/SOMATIC/SOMATIC_SANE.sam
######
/home/vm-gen-ngs/Scrivania/PROGRAMMI/bwa-0.7.10/bwa mem -R "@RG\tID:IDa\tSM:SM\tPL:Illumina" -t 1 /home/vm-gen-ngs/Scrivania/CORSO_NGS/GENOME_REF/reference.fa /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/SOMATIC/T_05_CK19p_S1_L001_R1_001.fastq.gz /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/SOMATIC/T_05_CK19p_S1_L001_R2_001.fastq.gz > /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/SOMATIC/SOMATIC_TUMORAL.sam
######
java -jar -Xms4g -Xmx4g /home/vm-gen-ngs/Scrivania/PROGRAMMI/picard-tools-1.119/SortSam.jar INPUT= /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/SOMATIC/SOMATIC_TUMORAL.sam OUTPUT= /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/SOMATIC/SOMATIC_TUMORAL.bam SORT_ORDER=coordinate
######
java -jar -Xms4g -Xmx4g /home/vm-gen-ngs/Scrivania/PROGRAMMI/picard-tools-1.119/BuildBamIndex.jar INPUT= /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/SOMATIC/SOMATIC_TUMORAL.bam
######
java -jar -Xms4g -Xmx4g /home/vm-gen-ngs/Scrivania/PROGRAMMI/picard-tools-1.119/SortSam.jar INPUT= /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/SOMATIC/SOMATIC_SANE.sam OUTPUT= /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/SOMATIC/SOMATIC_SANE.bam SORT_ORDER=coordinate
######
java -jar -Xms4g -Xmx4g /home/vm-gen-ngs/Scrivania/PROGRAMMI/picard-tools-1.119/BuildBamIndex.jar INPUT= /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/SOMATIC/SOMATIC_SANE.bam
### Generazione files Pileup
samtools mpileup -B -q 1 -f /home/vm-gen-ngs/Scrivania/CORSO_NGS/GENOME_REF/reference.fa /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/SOMATIC/SOMATIC_TUMORAL.bam > /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/SOMATIC/SOMATIC_TUMORAL.pileup
######
samtools mpileup -B -q 1 -f /home/vm-gen-ngs/Scrivania/CORSO_NGS/GENOME_REF/reference.fa /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/SOMATIC/SOMATIC_SANE.bam > /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/SOMATIC/SOMATIC_SANE.pileup
### analisi con varscan
java -jar /home/vm-gen-ngs/Scrivania/CORSO_NGS/PROGRAMMI/VarScan.v2.3.9.jar somatic /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/SOMATIC/SOMATIC_SANE.pileup /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/SOMATIC/SOMATIC_TUMORAL.pileup /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/SOMATIC/SOMATIC.output -min-coverage 10 -min-var-freq 0.08 -somatic-p-value 0.05
######
java -jar /home/vm-gen-ngs/Scrivania/CORSO_NGS/PROGRAMMI/VarScan.v2.3.9.jar processSomatic /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/SOMATIC/SOMATIC.output.snp
java -jar /home/vm-gen-ngs/Scrivania/CORSO_NGS/PROGRAMMI/VarScan.v2.3.9.jar processSomatic /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/SOMATIC/SOMATIC.output.indel
### calcolo delle COPERTURE
#
mkdir /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/EXAMPLE_1/DEPTH/
samtools depth /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/EXAMPLE_1/EXAMPLE_1.bam > /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/EXAMPLE_1/DEPTH/EXAMPLE_1.deep.txt
bamToBed -i /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/EXAMPLE_1/EXAMPLE_1.bam > /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/EXAMPLE_1/DEPTH/EXAMPLE_1.TOT.bed
awk -F"\t" '$3 < '20' { print $1"\t"$2"\t"$2 }' /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/EXAMPLE_1/EXAMPLE_1.deep.txt > /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/EXAMPLE_1/DEPTH/EXAMPLE_1.underT.bed
subtractBed -a /home/vm-gen-ngs/Scrivania/CORSO_NGS/TARGETS_AND_DATABASES/targets.interval_list.bed -b /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/EXAMPLE_1/DEPTH/EXAMPLE_1.TOT.bed > /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/EXAMPLE_1/DEPTH/EXAMPLE_1.zero.bed
intersectBed -a /home/vm-gen-ngs/Scrivania/CORSO_NGS/TARGETS_AND_DATABASES/targets.interval_list.bed -b /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/EXAMPLE_1/DEPTH/EXAMPLE_1.underT.bed > /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/EXAMPLE_1/DEPTH/EXAMPLE_1.underTHRESHOLD.bed
cat /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/EXAMPLE_1/DEPTH/EXAMPLE_1.underTHRESHOLD.bed /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/EXAMPLE_1/DEPTH/EXAMPLE_1.zero.bed > /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/EXAMPLE_1/DEPTH/EXAMPLE_1.RECOVER.bed
sortBed -i /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/EXAMPLE_1/DEPTH/EXAMPLE_1.RECOVER.bed > /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/EXAMPLE_1/DEPTH/EXAMPLE_1.SORT.RECOVER.bed
mergeBed -i /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/EXAMPLE_1/DEPTH/EXAMPLE_1.SORT.RECOVER.bed > /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/EXAMPLE_1/DEPTH/EXAMPLE_1.MERGED.RECOVER.bed
#####################################################
########### ANALISI COPERTURE con NGSRICH ###########
#####################################################
java -Xms2g -Xmx4g -cp /home/vm-gen-ngs/Scrivania/PROGRAMMI/NGSRICH/NGSrich_0.7.8/bin NGSrich evaluate -r /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/EXAMPLE_1/EXAMPLE_1.bam -u hg19 -a /home/vm-gen-ngs/Scrivania/CORSO_NGS/GENOME_REF/refGene.txt -t /home/vm-gen-ngs/Scrivania/CORSO_NGS/GENOME_REF/targets.interval_list.bed -o /home/vm-gen-ngs/Scrivania/CORSO_NGS/FASTQ_FILES/EXAMPLE_1/output.coverage -p 20 -h 200 --no-details