#!/bin/bash

#SBATCH --job-name=dDocent_make_inputfiles_RegioDiv_07_batch22
#SBATCH --mail-type=BEGIN,END,FAIL,TIME_LIMIT

#SBATCH --time=8:00:00
#SBATCH --mem-per-cpu=8G
#SBATCH --cpus-per-task=1

# output files
#SBATCH --output=/work/durka/%x-%j.out
#SBATCH --error=/work/durka/%x-%j.err

# $1 = batch
# $2 = index #platename, z.B. "RegioDiv_70" 
# $3 = 0 = standard; 1 = disable radcheck 

batch=$1
index=$2
radcheck=$3

#########################################################################################################
# Paths and filenames for this analysis
HOME_DIR="/gpfs1/schlecker/home/durka/"
WORK_DIR="/work/durka/dDocent/RegioDiv"
#WORK_SUBDIR="/work/durka/dDocent/RegioDiv/RegioDiv.all.samples"
 #     /gpfs1/data/Popgen/2021_RegioDiv/RegioDiv01/RegioDiv1_1/RegioDiv1_1_EKDL210003282-1a-1_H7KV3DSX2_L4_1.fq.gz
DATA="/gpfs1/data/Popgen/2021_RegioDiv/RegioDiv"
DATA="/gpfs1/work/durka/a/RegioDiv"
#DATA="/gpfs1/work/durka/temp/RD12-18/batch"
#BCODES="/gpfs1/data/Popgen/2021_RegioDiv/barcodes/RegioDiv_barcode_1_"
BCODES="/gpfs1/data/Popgen/2021_RegioDiv/barcodes/RegioDiv_barcode_"
#BCODESraw_01="barcodes.RegioDiv_01_8bpRC1_raw.txt"

if [ $batch -eq 22 ] 
then
  case $index in 
     1) fileP1='Unknown_A1B'; spec='LAT.PRA';;
     2) fileP1='Unknown_A2B'; spec='LAT.PRA';;
     3) fileP1='Unknown_A3B'; spec='LAT.PRA';;
     4) fileP1='Unknown_A4B'; spec='LAT.PRA';;
     5) fileP1='Unknown_A5B'; spec='LAT.PRA';;
     6) fileP1='Unknown_A6B'; spec='LAT.PRA';;
     7) fileP1='Unknown_A7B'; spec='ANT.DIO';;
     8) fileP1='Unknown_A8B'; spec='ANT.DIO';;
     9) fileP1='Unknown_A9B'; spec='ANT.DIO';;
     10) fileP1='Unknown_A10B'; spec='ANT.DIO';;
     11) fileP1='Unknown_A11B'; spec='ANT.DIO';;
     12) fileP1='Unknown_A12B'; spec='ANT.DIO';;
  esac
  #Unknown_ddRAD220BMK230829-BO304-ZX01-010001-01_1.fq.gz
  fileP2=MK230920-BP420-ZX01-010001-01_good
  fileP31=_1.fq.gz
  fileP32=_2.fq.gz
fi

radcheck=$3 # 0 = standard; 1 = disable radcheck
if [ $radcheck -eq 0 ] 
then
  DATA_DEMUXtemp="/work/durka/dDocent/RegioDiv/xdemux/data_demux_temp_""$batch""_"
else
  DATA_DEMUXtemp="/work/durka/dDocent/RegioDiv/xdemux_disable_radcheck/data_demux_temp_""$batch""_"
fi



#file1="RegioDiv1_1_EKDL210003282-1a-1_H7KV3DSX2_L4_1.fq.gz"
#file2="RegioDiv1_1_EKDL210003282-1a-1_H7KV3DSX2_L4_2.fq.gz"

# "/gpfs1/work/durka/temp/RD12-18/batch12/RegioDiv_70_EKDL220018307-1A_HLWWYDSX5_L3_1.fq.gz"

#fileP1=$1 #"RegioDiv1_"
#fileP2="_EKDL220018307-1A_HLWWYDSX5"
#fileP31="_L3_1.fq.gz"
#fileP32="_L3_2.fq.gz"

#########################################################################################################

module load Anaconda3
source $(dirname $(which conda))/../etc/profile.d/conda.sh 
conda activate /data/Popgen/programs/stacks-2.53
#conda activate /data/Popgen/programs/stacks-2.65

cd $WORK_DIR || exit

#for i in $1 # 2 3 4 5 6 7 8 9 10 11 12 #1
#do
  mkdir "$DATA_DEMUXtemp"$index
  if [ $radcheck -eq 0 ] 
    then
    #process_radtags --paired -1 "$DATA"$i/"$fileP1"$i"$fileP2"$i"$fileP31" -2 "$DATA"$i/"$fileP1"$i"$fileP2"$i"$fileP32" -o "$DATA_DEMUXtemp"$i/  -i gzfastq -y gzfastq -b $BCODES"$i".txt --barcode_dist_1 2 --barcode_dist_2 2 --inline_index --renz_1 'ecoRI' --renz_2 'mspI' --retain_header
    bash "$HOME_DIR"dDocent/dDocent_make_inputfiles_command_line.sh  "$DATA"$batch/"$fileP1""$fileP2""$fileP31" "$DATA"$batch/"$fileP1""$fileP2""$fileP32" "$DATA_DEMUXtemp"$index  $BCODES"$batch"_"$index".txt 
    cd "$DATA_DEMUXtemp"$index/
    mv *.log process_radtags.RegioDiv"$batch"_"$index".log
    cp *.log /gpfs1/data/Popgen/2021_RegioDiv/Logfiles/process_radtags_logs/
    cp *.gz /gpfs1/data/Popgen/2021_RegioDiv/Spec28.fq/"$spec".disable_radcheck/
    else
    #process_radtags --paired -1 "$DATA"$i/"$fileP1"$i"$fileP2"$i"$fileP31" -2 "$DATA"$i/"$fileP1"$i"$fileP2"$i"$fileP32" -o "$DATA_DEMUXtemp"$i/  -i gzfastq -y gzfastq -b $BCODES"$i".txt --barcode_dist_1 2 --barcode_dist_2 2 --inline_index --renz_1 'ecoRI' --renz_2 'mspI' --retain_header
    bash "$HOME_DIR"dDocent/dDocent_make_inputfiles_command_line_disable_radcheck.sh "$DATA"$batch/"$fileP1""$fileP2""$fileP31" "$DATA"$batch/"$fileP1""$fileP2""$fileP32" "$DATA_DEMUXtemp"$index  $BCODES"$batch"_"$index".txt 
    cd "$DATA_DEMUXtemp"$index/
    mv *.log process_radtags.RegioDiv"$batch"_"$index".log
    cp *.log /gpfs1/data/Popgen/2021_RegioDiv/Logfiles/process_radtags_logs/disable_radcheck/
    cp *.gz /gpfs1/data/Popgen/2021_RegioDiv/Spec28.fq/"$spec".disable_radcheck/
  fi

#done


conda deactivate

