#PBS -q bl2x220g7q
#PBS -l select=1:ncpus=5:mem=10g
#PBS -l walltime=01:00:00
#PBS -l cput=01:00:00

export PATH=$PATH:~/programms/bwa-0.7.17;
source activate nn_Polina_tf2
WORK_DIR=/mnt/storage/home/psbelokopytova/scratch/202112281307data_Polya/4c_plasmid/2021-11-22/
cd $WORK_DIR; 
[ -d stat ] || mkdir align; 
cd align;
# Declare a string array with type
declare -a files=("P4C5" "P4C25")

for file in "${files[@]}"
do
echo ${file}
[ -d stat ] || mkdir ${file}
# cat $WORK_DIR/cutadapt/${file}_1.fq | grep pr_plasmid1 -A 4 >> $WORK_DIR/cutadapt/${file}_pr_plasmid1.fq
# cat $WORK_DIR/cutadapt/${file}_2.fq | grep pr_plasmid1 -A 4 >> $WORK_DIR/cutadapt/${file}_pr_plasmid1.fq
# cat $WORK_DIR/cutadapt/${file}_1.fq | grep pr_plasmid2 -A 4 >> $WORK_DIR/cutadapt/${file}_pr_plasmid2.fq
# cat $WORK_DIR/cutadapt/${file}_2.fq | grep pr_plasmid2 -A 4 >> $WORK_DIR/cutadapt/${file}_pr_plasmid2.fq
# ## align pr_plasmid1
# bwa mem -t 5 -M -R '@RG\tID:${file}_pr_plasmid1' /mnt/storage/home/psbelokopytova/scratch/202112281307data_Polya/4c_plasmid/genome/hg38_plasmid $WORK_DIR/cutadapt/${file}_pr_plasmid1.fq | samtools view -bS -@ 5 | samtools sort > $WORK_DIR/align/${file}/${file}_pr_plasmid1.sorted.bam;
# samtools index $WORK_DIR/align/${file}/${file}_pr_plasmid1.sorted.bam > $WORK_DIR/align/${file}/${file}_pr_plasmid1.sorted.bam.bai;
# ## align pr_plasmid2
# bwa mem -t 5 -M -R '@RG\tID:${file}_pr_plasmid2' /mnt/storage/home/psbelokopytova/scratch/202112281307data_Polya/4c_plasmid/genome/hg38_plasmid $WORK_DIR/cutadapt/${file}_pr_plasmid2.fq | samtools view -bS -@ 5 | samtools sort > $WORK_DIR/align/${file}/${file}_pr_plasmid2.sorted.bam;
# samtools index $WORK_DIR/align/${file}/${file}_pr_plasmid2.sorted.bam > $WORK_DIR/align/${file}/${file}_pr_plasmid2.sorted.bam.bai;
# ## merge files
# samtools merge $WORK_DIR/align/${file}/${file}_pr_plasmid1_pr_plasmid2.bam $WORK_DIR/align/${file}/${file}_pr_plasmid1.sorted.bam $WORK_DIR/align/${file}/${file}_pr_plasmid2.sorted.bam;
# samtools index $WORK_DIR/align/${file}/${file}_pr_plasmid1_pr_plasmid2.bam > $WORK_DIR/align/${file}/${file}_pr_plasmid1_pr_plasmid2.bam.bai;
# ## align paired reads
# bwa mem -t 5 -M /mnt/storage/home/psbelokopytova/scratch/202112281307data_Polya/4c_plasmid/genome/hg38_plasmid $WORK_DIR/cutadapt/${file}_1.fq $WORK_DIR/cutadapt/${file}_2.fq | samtools view -bS -@ 5 | samtools sort > $WORK_DIR/align/${file}/${file}.sorted.bam
# samtools index $WORK_DIR/align/${file}/${file}.sorted.bam > $WORK_DIR/align/${file}/${file}.sorted.bam.bai
## create coverage file
~/programms/bedtools2/bin/bedtools genomecov -ibam $WORK_DIR/align/${file}/${file}.sorted.bam -bg > $WORK_DIR/align/${file}/${file}.bedgraph;
python /mnt/storage/home/psbelokopytova/scratch/202112281307data_Polya/4c_plasmid/modify_bedgraph.py $WORK_DIR $file
## create sam
samtools view $WORK_DIR/align/${file}/${file}.sorted.bam > $WORK_DIR/align/${file}/${file}.sorted.sam
## claculatite mapping statistics
samtools idxstats $WORK_DIR/align/${file}/${file}.sorted.bam > $WORK_DIR/align/${file}/${file}_mapping_stat.txt
done
