#PBS -q xl230g9q
#PBS -l select=1:ncpus=5:mem=20g
#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/scratch/ws/psbelokopytova/202208081122polya_data/4c_plasmid/2022-06-20/
cd $WORK_DIR; 
[ -d stat ] || mkdir align; 
cd align;
# Declare a string array with type
declare -a files=("K21" "K22")

for file in "${files[@]}"
do
echo ${file}
[ -d stat ] || mkdir ${file};
## align pr_plasmid1_cutTaiI
bwa mem -t 5 -M -R '@RG\tID:pr_plasmid1_woTaiI_withNlaIII' /mnt/storage/home/psbelokopytova/scratch/202208081122polya_data/4c_plasmid/genome/mm10_hg38_plasmid.fa $WORK_DIR/cutadapt/${file}_pr_plasmid1_empty_cutTaiI.fq | samtools view -bS -@ 5 | samtools sort > $WORK_DIR/align/${file}/${file}_pr_plasmid1_empty_to_empty_cutTaiI.sorted.bam;
bwa mem -t 5 -M -R '@RG\tID:pr_plasmid1_woTaiI_withNlaIII' /mnt/storage/home/psbelokopytova/scratch/202208081122polya_data/4c_plasmid/genome/mm10_hg38_plasmid_cmv_oldseq.fa $WORK_DIR/cutadapt/${file}_pr_plasmid1_cmv_cutTaiI.fq | samtools view -bS -@ 5 | samtools sort > $WORK_DIR/align/${file}/${file}_pr_plasmid1_cmv_to_cmv_cutTaiI.sorted.bam
##calculate stat
samtools idxstats $WORK_DIR/align/${file}/${file}_pr_plasmid1_empty_to_empty_cutTaiI.sorted.bam > $WORK_DIR/align/${file}/${file}_pr_plasmid1_empty_to_empty_cutTaiI.sorted.stat.txt
samtools idxstats $WORK_DIR/align/${file}/${file}_pr_plasmid1_cmv_to_cmv_cutTaiI.sorted.bam > $WORK_DIR/align/${file}/${file}_pr_plasmid1_cmv_to_cmv_cutTaiI.sorted.stat.txt
##create index
samtools index $WORK_DIR/align/${file}/${file}_pr_plasmid1_empty_to_empty_cutTaiI.sorted.bam > $WORK_DIR/align/${file}/${file}_pr_plasmid1_empty_to_empty_cutTaiI.sorted.bam.bai;
samtools index $WORK_DIR/align/${file}/${file}_pr_plasmid1_cmv_to_cmv_cutTaiI.sorted.bam > $WORK_DIR/align/${file}/${file}_pr_plasmid1_cmv_to_cmv_cutTaiI.sorted.bam.bai;
## create sam
samtools view $WORK_DIR/align/${file}/${file}_pr_plasmid1_cmv_to_cmv_cutTaiI.sorted.bam > $WORK_DIR/align/${file}/${file}_pr_plasmid1_cmv_to_cmv_cutTaiI.sorted.sam;
samtools view $WORK_DIR/align/${file}/${file}_pr_plasmid1_empty_to_empty_cutTaiI.sorted.bam > $WORK_DIR/align/${file}/${file}_pr_plasmid1_empty_to_empty_cutTaiI.sorted.sam

done
