#!/bin/bash

#SBATCH --nodes=1
#SBATCH --ntasks-per-node=32
#SBATCH --mem=32GB
#SBATCH --time=24:00:00
#SBATCH --output=slurm-Bowtie2-%j.log


module load Bowtie2/2.4.5-GCC-11.3.0
module load SAMtools/1.17-GCC-12.2.0
module load Biopython/1.81-foss-2022b

# Global parameters needed for this script
# setting global parameters
cpu=$SLURM_JOB_CPUS_PER_NODE
SOFTWAREDIR=/scratch/public/genomes/AnneTools




# =============================================================================================================================================================
# ==                                                          Functions                                                                                      ==
# =============================================================================================================================================================
function my_bowtie_SE {  # Map the reads (from the fastq file) to the reference genome ( .fna ) this will generate a SAM file (Sequence Alignment Map)
	name=$1
	forward=$2
	printf "\n\n========= Bowtie2 SE: "$name" =================================\n" >> $resultsdir/02.$name.bowtie2.log
	(bowtie2 --local -U $fastqdir/$forward -p $cpu -S $resultsdir/$name.sam -x $genome) 2>>$resultsdir/02.$name.bowtie2.log 
	SAM_2_BAM $name
}
function my_bowtie_SE_2_fastq {  # Map the reads (from the fastq file) to the reference genome ( .fna ) this will generate a SAM file (Sequence Alignment Map)
	name=$1
	printf "\n\n========= Bowtie2 SE: "$name" =================================\n" >> $resultsdir/02.$name.bowtie2.log
	(bowtie2 --local -U $fastqdir/$2,$fastqdir/$3 -p $cpu -S $resultsdir/$name.sam -x $genome) 2>>$resultsdir/02.$name.bowtie2.log 
	SAM_2_BAM $name
}


function SAM_2_BAM {  # convert SAM to sorted BAM (binairy SAM) and remove unmapped reads with option -F 4
	name=$1
	printf "\n========== Convert "$name" ======================================\n"	
	samtools view -S -b -F 4 -o $resultsdir/$name.bam $resultsdir/$name.sam
	samtools sort -@ $cpu $resultsdir/$name.bam > $resultsdir/$name.sorted.bam
	samtools index $resultsdir/$name.sorted.bam
	rm $resultsdir/$name.sam  # save space by removing the SAM file 
	rm $resultsdir/$name.bam  # save space by removing the non sorted BAM file                                                                          # 3) save space by removing the SAM file 
}



#genome=/scratch/p127804/PROJECTS/Douwe/genomes/Bifidobacterium_longum_subsp_longum_NCIMB8809_ASM144625v1_genomic
genome=/scratch/p127804/PROJECTS/Douwe/genomes/Bifidobacterium_longum_subsp_longum_NCIMB8809_pFREMII_pBM44
bowtie2-build $genome.fna $genome

fastqdir=/scratch/hb-molgen/NGSdata/20251027_026
resultsdir=/scratch/p127804/PROJECTS/Douwe/LisaFries_2025_026/results

mkdir -p $resultsdir


my_bowtie_SE LF026_02 LF026_02_S30_L001_R1_001.fastq.gz
my_bowtie_SE LF026_03 LF026_03_S31_L001_R1_001.fastq.gz
my_bowtie_SE LF026_04 LF026_04_S32_L001_R1_001.fastq.gz
my_bowtie_SE LF026_05 LF026_05_S33_L001_R1_001.fastq.gz
my_bowtie_SE LF026_06 LF026_06_S34_L001_R1_001.fastq.gz
my_bowtie_SE LF026_07 LF026_07_S35_L001_R1_001.fastq.gz
my_bowtie_SE LF026_08 LF026_08_S36_L001_R1_001.fastq.gz
my_bowtie_SE LF026_09 LF026_09_S37_L001_R1_001.fastq.gz
my_bowtie_SE LF026_10 LF026_10_S38_L001_R1_001.fastq.gz
my_bowtie_SE LF026_11 LF026_11_S39_L001_R1_001.fastq.gz
my_bowtie_SE LF026_12 LF026_12_S40_L001_R1_001.fastq.gz
my_bowtie_SE LF026_13 LF026_13_S41_L001_R1_001.fastq.gz
my_bowtie_SE LF026_14 LF026_14_S42_L001_R1_001.fastq.gz
my_bowtie_SE LF026_15 LF026_15_S43_L001_R1_001.fastq.gz
my_bowtie_SE LF026_16 LF026_16_S44_L001_R1_001.fastq.gz



# # # Make a report summary
cat $resultsdir/02.*.bowtie2.log > $resultsdir/02.bowtie2_all.log
python3 $SOFTWAREDIR/Bowtie2LogTable.py -query $resultsdir/02.bowtie2_all.log > $resultsdir/02.bowtie2_all.log.table



