#!/bin/bash

file_base_name="GV1-LBC_RNA_S267.sorted";
samtools merge -n -@ 10 -O BAM ${file_base_name}.fp.bam ${file_base_name}.f_f.bam ${file_base_name}.m_r.bam;
samtools merge -n -@ 10 -O BAM ${file_base_name}.fn.bam ${file_base_name}.f_r.bam ${file_base_name}.m_f.bam;
samtools sort -n -o ${file_base_name}.fn.sorted.bam -@ 10 ${file_base_name}.fn.bam;
rm ${file_base_name}.fn.bam;
mv ${file_base_name}.fn.sorted.bam ${file_base_name}.fn.bam;
samtools fixmate -r -p -m -@ 10 ${file_base_name}.fn.bam ${file_base_name}.fn.fxmt.bam;
samtools sort -@ 10 -o ${file_base_name}.fn.fxmt.sorted.bam ${file_base_name}.fn.fxmt.bam;
rm ${file_base_name}.fn.bam ${file_base_name}.fn.fxmt.bam;
mv ${file_base_name}.fn.fxmt.sorted.bam ${file_base_name}.fn.bam;
samtools index -b -@ 10 ${file_base_name}.fn.bam ${file_base_name}.fn.bam.bai;
samtools markdup -r -l 152 -s -@ 10 ${file_base_name}.fn.bam ${file_base_name}.fn.dedup.bam;
samtools index -b -@ 10 ${file_base_name}.fn.dedup.bam ${file_base_name}.fn.dedup.bam.bai;
samtools sort -n -o ${file_base_name}.fp.sorted.bam -@ 10 ${file_base_name}.fp.bam;
rm ${file_base_name}.fp.bam;
mv ${file_base_name}.fp.sorted.bam ${file_base_name}.fp.bam;
samtools fixmate -r -p -m -@ 10 ${file_base_name}.fp.bam ${file_base_name}.fp.fxmt.bam;
samtools sort -@ 10 -o ${file_base_name}.fp.fxmt.sorted.bam ${file_base_name}.fp.fxmt.bam;
rm ${file_base_name}.fp.bam ${file_base_name}.fp.fxmt.bam;
mv ${file_base_name}.fp.fxmt.sorted.bam ${file_base_name}.fp.bam;
samtools index -b -@ 10 ${file_base_name}.fp.bam ${file_base_name}.fp.bam.bai;
samtools markdup -r -l 152 -s -@ 10 ${file_base_name}.fp.bam ${file_base_name}.fp.dedup.bam;
samtools index -b -@ 10 ${file_base_name}.fp.dedup.bam ${file_base_name}.fp.dedup.bam.bai;
c=1.5; s=4.0; f=0.02; a=10; j=3; m=1500; g=450;
stringtie ${file_base_name}.fp.dedup.bam -o ${file_base_name}.fp.dedup.gtf -l "REV" -p 10 -t -c ${c} -s ${s} -f ${f} -a ${a} -j ${j} -m ${m} -g ${g} --rf;
stringtie ${file_base_name}.fn.dedup.bam -o ${file_base_name}.fn.dedup.gtf -l "FORW" -p 10 -t -c ${c} -s ${s} -f ${f} -a ${a} -j ${j} -m ${m} -g ${g} --rf;
awk 'BEGIN {OFS=FS="\t"} FNR<=2 {print $0}' ${file_base_name}.fp.dedup.gtf ${file_base_name}.fn.dedup.gtf > ../${file_base_name}.all.dedup.gtf ; awk 'BEGIN {OFS=FS="\t"} FNR>2 {print $0}' ${file_base_name}.fp.dedup.gtf ${file_base_name}.fn.dedup.gtf >> ../${file_base_name}.all.dedup.gtf;