#!/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.5.4-GCC-13.2.0
module load SAMtools/1.19.2-GCC-13.2.0
module load Biopython/1.84-foss-2024a

# Global parameters needed for this script
# setting global parameters
cpu=16
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 
}

function hits_summery {  # counts the multiple hits
	name=$1
	samtools view -F 4 $resultsdir/$name | awk '{print $1}' | sort | uniq -c | \
	awk '{count[$1]++; sum += $1; n++} END {for (i in count) print i, count[i]; print "\nAverage hits per mapped read:", sum/n}' | sort -n > $resultsdir/$name.hits_summary.txt
}


fastqdir=/scratch/hb-molgen/NGSdata/20251219_028
resultsdir=/scratch/p127804/PROJECTS/Douwe/Douwe_202512_028/results

mkdir -p $resultsdir



genome=/scratch/p127804/PROJECTS/Douwe/genomes/Bifidobacterium_breve_NRBB57
# bowtie2-build $genome.fna $genome
my_bowtie_SE DS027_29 DS027_29_S13_L001_R1_001.fastq.gz
my_bowtie_SE DS027_30 DS027_30_S14_L001_R1_001.fastq.gz

# 
# 
genome=/scratch/p127804/PROJECTS/Douwe/genomes/Bifidobacterium_breve_NRBB01
# bowtie2-build $genome.fna $genome
my_bowtie_SE DS027_31 DS027_31_S15_L001_R1_001.fastq.gz
my_bowtie_SE DS027_32 DS027_32_S16_L001_R1_001.fastq.gz
# 
# 
genome=/scratch/p127804/PROJECTS/Douwe/genomes/Bifidobacterium_bifidum_CNCM_I_4319
# bowtie2-build $genome.fna $genome
my_bowtie_SE DS027_33 DS027_33_S17_L001_R1_001.fastq.gz
my_bowtie_SE DS027_34 DS027_34_S18_L001_R1_001.fastq.gz


genome=/scratch/p127804/PROJECTS/Douwe/genomes/Bifidobacterium_longum_subsp_infantis_Bll190
bowtie2-build $genome.fna $genome
my_bowtie_SE DS027_35 DS027_35_S19_L001_R1_001.fastq.gz
my_bowtie_SE DS027_36 DS027_36_S20_L001_R1_001.fastq.gz
my_bowtie_SE DS027_37 DS027_37_S21_L001_R1_001.fastq.gz
my_bowtie_SE DS027_38 DS027_38_S22_L001_R1_001.fastq.gz


genome=/scratch/p127804/PROJECTS/Douwe/genomes/2025_Bifido_mix/Bifidobacterium_mixA
bowtie2-build $genome.fna $genome
my_bowtie_SE DS027_39 DS027_39_S23_L001_R1_001.fastq.gz
my_bowtie_SE DS027_40 DS027_40_S24_L001_R1_001.fastq.gz

genome=/scratch/p127804/PROJECTS/Douwe/genomes/2025_Bifido_mix/Bifidobacterium_mixB
bowtie2-build $genome.fna $genome
my_bowtie_SE DS027_41 DS027_41_S25_L001_R1_001.fastq.gz
my_bowtie_SE DS027_42 DS027_42_S26_L001_R1_001.fastq.gz
my_bowtie_SE DS027_43 DS027_43_S27_L001_R1_001.fastq.gz
my_bowtie_SE DS027_44 DS027_44_S28_L001_R1_001.fastq.gz

genome=/scratch/p127804/PROJECTS/Douwe/genomes/2025_Bifido_mix/Bifidobacterium_mixC
bowtie2-build $genome.fna $genome
my_bowtie_SE DS027_25 DS027_25_S9_L001_R1_001.fastq.gz
my_bowtie_SE DS027_26 DS027_26_S10_L001_R1_001.fastq.gz
my_bowtie_SE DS027_27 DS027_27_S11_L001_R1_001.fastq.gz
my_bowtie_SE DS027_28 DS027_28_S12_L001_R1_001.fastq.gz
my_bowtie_SE DS027_45 DS027_45_S29_L001_R1_001.fastq.gz
my_bowtie_SE DS027_46 DS027_46_S30_L001_R1_001.fastq.gz
my_bowtie_SE DS027_47 DS027_47_S31_L001_R1_001.fastq.gz
my_bowtie_SE DS027_48 DS027_48_S32_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



