#!/bin/sh -x
#
# Script name: ScriptC_InsertSize.sh
#
# Purpose of script: Analysis of insert size disrtibution of mapped reads
#
# Date Created: 2022/12/29
#
# Written by Keita Hasegawa and Kazuhiko Nakabayashi for the following study:
#
# Keita Hasegawa et al.
# "A capture methyl-seq protocol with improved efficiency and cost-effectiveness using pre-pooling and enzymatic conversion"
# 
#
# Contact information:
# Kazuhiko Nakabayashi, Ph.D.
# nakabaya-k@ncchd.go.jp
# Laboratory of Developmental Genomics, Research Institute, National Center for Child Health and Development, Tokyo, Japan
#
#
# Required Files in same working directory
# #1: this script
# #2: bam files (*_final_mapped.bam)
#
#
# Usage
# ./this_script
#
# Output 6 types of Files in Directory
# File1: EM_all.bam, BSH_all.bam, BSM_all.bam, BSL_all.bam
# File2: EM_all_sort.bam, BSH_all_sort.bam, BSM_all_sort.bam, BSL_all_sort.bam
# File3: EM.sam.insertsizemetrics, BSH.sam.insertsizemetrics, BSM.sam.insertsizemetrics, BSL.sam.insertsizemetrics,
# File4: EM.sam.hist.pdf, BSH.sam.hist.pdf, BSM.sam.hist.pdf, BSL.sam.hist.pdf
# File5: EM.sam.insertsizefreq, BSH.sam.insertsizefreq, BSM.sam.insertsizefreq, BSL.sam.insertsizefreq
# File6: EM_mod_insertsizefreq, BSH_mod_insertsizefreq, BSM_mod_insertsizefreq, BSL_mod_insertsizefreq
#
#
#
# This script requires the following tools:
#export PATH=/home/xxx/yyyy/samtools-1.12/bin/:$PATH   # Add the path to samtools
#
#
## Script starts hereafter ###

export PATH=/XXX/YYYY/samtools-1.12/bin/:$PATH


# Step1: Merge all samples' bam files by samtools
samtools merge EM_all.bam EMCap1_final_mapped.bam EMCap2_final_mapped.bam EMCap3_final_mapped.bam EMCap4_final_mapped.bam
samtools merge BSH_all.bam SRR12063108_final_mapped.bam SRR12063109_final_mapped.bam SRR12063110_final_mapped.bam SRR12063111_final_mapped.bam
samtools merge BSM_all.bam SRR12063112_final_mapped.bam SRR12063113_final_mapped.bam SRR12063114_final_mapped.bam SRR12063115_final_mapped.bam
samtools merge BSL_all.bam SRR12063116_final_mapped.bam SRR12063117_final_mapped.bam SRR12063118_final_mapped.bam SRR12063119_final_mapped.bam


# Step2: Sort *_all.bam by samtools
samtools sort EM_all.bam > EM_all_sort.bam
samtools sort BSH_all.bam > BSH_all_sort.bam
samtools sort BSM_all.bam > BSM_all_sort.bam
samtools sort BSL_all.bam > BSL_all_sort.bam


# Step3: install and make files below
conda install -c bioconda -y picard

picard CollectInsertSizeMetrics INPUT=EM_all_sort.bam o=EM.sam.insertsizemetrics H=EM.sam.hist.pdf
picard CollectInsertSizeMetrics INPUT=BSH_all_sort.bam o=BSH.sam.insertsizemetrics H=BSH.sam.hist.pdf
picard CollectInsertSizeMetrics INPUT=BSM_all_sort.bam o=BSM.sam.insertsizemetrics H=BSM.sam.hist.pdf
picard CollectInsertSizeMetrics INPUT=BSL_all_sort.bam o=BSL.sam.insertsizemetrics H=BSL.sam.hist.pdf


# Step4: Make *.sam.insertsizefreq 
sed -n -e  '/^insert_size/,$p' EM.sam.insertsizemetrics > EM.sam.insertsizefreq
sed -n -e  '/^insert_size/,$p' BSH.sam.insertsizemetrics > BSH.sam.insertsizefreq
sed -n -e  '/^insert_size/,$p' BSM.sam.insertsizemetrics > BSM.sam.insertsizefreq
sed -n -e  '/^insert_size/,$p' BSL.sam.insertsizemetrics > BSL.sam.insertsizefreq


# Step5: Modify sum of read counts = 100
ModEM=`cat EM.sam.insertsizefreq | sed -e '1d' | awk '{a+=$2} END{print a;}' | cut -f1`
cat EM.sam.insertsizefreq | sed -e '1d' | awk '{print $1,$2/'$ModEM'}' | sed 's/ /\t/g' > EM_mod_insertsizefreq

ModBSH=`cat BSH.sam.insertsizefreq | sed -e '1d' | awk '{a+=$2} END{print a;}' | cut -f1`
cat BSH.sam.insertsizefreq | sed -e '1d' | awk '{print $1,$2/'$ModBSH'}' | sed 's/ /\t/g' > BSH_mod_insertsizefreq

ModBSM=`cat BSM.sam.insertsizefreq | sed -e '1d' | awk '{a+=$2} END{print a;}' | cut -f1`
cat BSM.sam.insertsizefreq | sed -e '1d' | awk '{print $1,$2/'$ModBSM'}' | sed 's/ /\t/g' > BSM_mod_insertsizefreq

ModBSL=`cat BSL.sam.insertsizefreq | sed -e '1d' | awk '{a+=$2} END{print a;}' | cut -f1`
cat BSL.sam.insertsizefreq | sed -e '1d' | awk '{print $1,$2/'$ModBSL'}' | sed 's/ /\t/g' > BSL_mod_insertsizefreq


# Step6: Make Figure of Insert Size by R
EM_mod_INS <- read.table("EM_mod_insertsizefreq", sep = "\t", head=F)
BShigh_mod_INS <- read.table("BSH_mod_insertsizefreq", sep = "\t", head=F)
BSmid_mod_INS <- read.table("BSM_mod_insertsizefreq", sep = "\t", head=F)
BSlow_mod_INS <- read.table("BSL_mod_insertsizefreq", sep = "\t", head=F)

plot(EM_mod_INS)
plot(BShigh_mod_INS)
plot(BSlow_mod_INS)
plot(BSmid_mod_INS)

exit

