TITLE: NGS Protocol Automation
AUTHORS: erick.rios. 

[DESCRIPTION]
<div class = "text-blocks"><div class = "text-block">Automation_v1_2.sh</div><div class = "text-block">	Written by Erick (02/05/2021)</div><div class = "text-block">--------------------------------------------</div><div class = "text-block">Hello to "v1_2".</div><div class = "text-block">The current protocol:</div><div class = "text-block">"A simplified workflow for the analysis of whole-genome sequencing data from mutant lines</div><div class = "text-block">		with an application to the nematode Pristionchus pacificus"</div><div class = "text-block"> 			written by Christian Rödelsperger.</div><div class = "text-block">Uses BWA, samtools, and bcftools which are tools or packages that can be readily downloaded</div><div class = "text-block">and installed. These tools are "linux/Mac based" to put it simply.</div><div class = "text-block">Windows users need to download an "emulator" of linux terminal such as "ubuntu 20.04" from </div><div class = "text-block">sources such as the Microsoft store. (I use ubuntu 20.04.)</div><div class = "text-block">---------------------------------------------</div><div class = "text-block">Installing Tools</div><div class = "text-block">For Windows 10:</div><div class = "text-block">1.	Install “ubuntu 20.04 LTS” (free) from the Microsoft store</div><div class = "text-block">2.	Once installed correctly</div><div class = "text-block">3.	Open windows command shell and type “bash”; it may take a few moments for first time running.</div><div class = "text-block">4.	$ sudo apt-get install bwa</div><div class = "text-block">5.	$ sudo apt-get install samtools</div><div class = "text-block">6.	$ sudo apt-get install bcftools</div><div class = "text-block">7.	You are now free to continue.</div><div class = "text-block">For Mac users:</div><div class = "text-block">1.	Open terminal/console</div><div class = "text-block">2.	$ brew install bwa</div><div class = "text-block">3.	$ brew install samtools</div><div class = "text-block">4.	$ brew install bcftools</div><div class = "text-block">5.	You are now free to continue.</div><div class = "text-block">-----------------------------------------------</div><div class = "text-block">Some computer systems may require admin or even root permissions in order to run the bash script.</div><div class = "text-block">The simplest way ensure the script does not fail mid-way:</div><div class = "text-block">>sudo -i sudo -s</div><div class = "text-block">Bash script can now be ran with full permissions.</div><div class = "text-block">------------------------------------------------</div><div class = "text-block">Initiating the bash script</div><div class = "text-block">>./Automation_v1_2.sh</div></div>

[STEPS]
?. ENTIRE=n
 ALIGNMENTS=n
 REGIONS=n
 MUTATIONS=n
 INDEX=n
 echo -n "Perform ENTIRE Protocol? (Y/n): "read ENTIREif [[ “$ENTIRE” == Y ]]; then
 echo “WILL perform ENTIRE protocol.”
 fiif [ $ENTIRE != n -a $ENTIRE != Y ]; then
 echo “You said $ENTIRE, assuming ‘NO’”
 ENTIRE=n
 fi
 ###################################
 if [[ “$ENTIRE” == n ]]; then
 echo -n "Perform candidate regions protocol? (Y/n): "
 read REGIONSif [ $REGIONS != n -a $REGIONS != Y ]; then
 echo “You said $REGIONS, assuming ‘NO’”
 REGIONS=n
 fiecho -n "Perform candidate mutations protocol? (Y/n): "
 read MUTATIONS
 if [ $MUTATIONS != n -a $MUTATIONS != Y ]; then
 echo “You said $MUTATIONS, assuming ‘NO’”
 MUTATIONS=n
 fi
 fi
 ####################################
 if [ $ENTIRE == Y -o $ALIGNMENTS == Y -o $REGIONS == Y -o $MUTATIONS == Y ]; then
 echo -n "Enter representative genome (include file extension): "
 read GENOME
 echo -n "Index your representative genome? (Y/n): "
 read INDEX
 if [ $INDEX != Y -a $INDEX != n ]; then
 echo “You said $INDEX, assuming ‘NO’”
 echo “Will NOT index genome.”
 INDEX=n
 fi
 fi
 ####################################
 if [ “$ENTIRE” == Y -o $MUTATIONS == Y -o $ALIGNMENTS == Y ]; then
 echo -n "Enter suppressor read 1 (include file extension): "
 read SUPP_1
 echo -n "Enter suppressor read 2 (include file extension): "
 read SUPP_2echo -n "Enter mutant read 1 (include file extension): "
 read MUT_1
 echo -n "Enter mutant read 2 (include file extension): "
 read MUT_2
 fi
 if [ “$ENTIRE” == Y -o $MUTATIONS == Y ]; then
 echo -n "Candidate mutations; Enter annotation file (include file extension): "
 read ANNOTATION
 fi
 if [ “$ENTIRE” == Y -o $REGIONS == Y -o $ALIGNMENTS == Y ]; then
 echo -n "Enter wild read 1 (include file extension): "
 read WILD_1
 echo -n "Enter wild read 2 (include file extension): "
 read WILD_2echo -n "Enter map read 1 (include file extension): "
 read MAP_1
 echo -n "Enter map read 2 (include file extension): "
 read MAP_2if [ “$ENTIRE” == Y -o $REGIONS == Y ]; then
 echo -n "Candidate regions; Enter ‘Source Code 1’ perl script (include file extension): "
 read $PERL
 fi
 fi
 ####################################
 if [ $ENTIRE == Y -o $ALIGNMENTS == Y -o $REGIONS == Y -o $MUTATIONS == Y ]; then
 echo -n "How many CPU threads to dedicate for alignments? (Minimum: 1; Max: 5): "
 read THREAD
 fi
 ######################################################################################
 if [[ “$INDEX” == Y ]];then
 bwa index $GENOME
 fi
 ####################### REGIONS ######################################################
 if [ “$ENTIRE” == Y -o $REGIONS == Y ]; then
 bwa mem -t THREAD -o WILD.sam $GENOME $WILD_1 $WILD_2
 bwa mem -t THREAD -o MAP.sam $GENOME $MAP_1 $MAP_2samtools view -S -b MAP.sam > MAP_unsorted.bam
 samtools view -S -b WILD.sam > WILD_unsorted.bamsamtools sort -o MAP.bam MAP_unsorted.bam
 samtools sort -o WILD.bam WILD_unsorted.bamsamtools index MAP.bam
 samtools index WILD.bamrm MAP_unsorted.bam MAP.sam
 rm WILD_unsorted.bam WILD.sambcftools mpileup -Ou -f $GENOME WILD.bam | bcftools call -mv -Ov -o WILD.vcfawk ‘{if($6>100) print}’ WILD.vcf | grep -v INDEL |awk ‘{print $1 “\t” $2}’ > positions.txtsamtools mpileup -f $GENOME -l positions.txt MAP.bam > pileup_data.txtperl count_allele_frequencies.pl pileup_data.txt > mapping_data.txt
 echo “Candidate regions finished; Look for ‘mapping_data.txt’ file.”
 fi
 ############################### MUTATIONS ############################################
 if [ “$ENTIRE” == Y -o $MUTATIONS == Y ]; then
 bwa mem -t THREAD -o SUPP.sam $GENOME $SUPP_1 $SUPP_2
 bwa mem -t THREAD -o MUT.sam $GENOME $MUT_1 $MUT_2samtools view -S -b SUPP.sam > SUPP_unsorted.bam
 samtools view -S -b MUT.sam > MUT_unsorted.bamsamtools sort -o SUPP.bam SUPP_unsorted.bam
 samtools sort -o MUT.bam MUT_unsorted.bamsamtools index SUPP.bam
 samtools index MUT.bamrm SUPP_unsorted.bam SUPP.sam
 rm MUT_unsorted.bam MUT.sambcftools mpileup -Ou -f $GENOME SUPP.bam | bcftools call -mv -Ou | bcftools view -i ‘%QUAL>=20’ -Oz > SUPP.vcf.gzbcftools mpileup -Ou -f $GENOME MUT.bam | bcftools call -mv -Ou | bcftools view -i ‘%QUAL>=20’ -Oz > MUT.vcf.gzbcftools index SUPP.vcf.gzbcftools index MUT.vcf.gzbcftools filter -O z SUPP.vcf.gz > SUPP_filtered1.vcf.gzbcftools index SUPP_filtered1.vcf.gzbcftools isec -C SUPP_filtered1.vcf.gz MUT.vcf.gz > pos.txtbcftools filter -O z -R pos.txt SUPP_filtered1.vcf.gz > SUPP_filtered2.vcf.gzbcftools sort -O z SUPP_filtered2.vcf.gz > SUPP_filtered2_sorted.vcf.gzbcftools index SUPP_filtered2_sorted.vcf.gzbcftools csq -p s -f $GENOME -g $ANNOTATION SUPP_filtered2_sorted.vcf.gz >   SUPP_variants_annotated.vcfgrep missense SUPP_variants_annotated.vcf > Candidate_Mut.txt
 grep stop_gained SUPP_variants_annotated.vcf >> Candidate_Mut.txt
 grep synonymous SUPP_variants_annotated.vcf >> Candidate_Mut.txt
 grep INDEL SUPP_variants_annotated.vcf >> Candidate_Mut.txt
 echo “Candidate mutations protocol finished; Look for ‘Candidate_Mut.txt’ file”
 fi
 ######################################################################################
 if [[ $ENTIRE == n ]]; thenif [[ “$REGIONS” == n ]]; then
 echo “Candidate regions protocol was not performed.”
 fi
 if [[ “$MUTATIONS” == n ]]; then
 echo “Candidate mutations protocol was not performed.”
 fi
 fi
 if [ $ENTIRE == n -a $REGIONS == n -a $MUTATIONS == n -a $INDEX == n -a $ALIGNMENTS == n ];then
 echo “Nothing was accomplished this day.”
 else
 echo “Something was accomplished!”
 fi