#!/bin/bash

#$ -N convert_data_run_midas_species        #-- set the name of the job; this will appear in the job listing
#$ -l h_vmem=2G                      #-- set the maximum memory usage (per slot). on scg3 the default is 1GB of h_vmem per slot
#$ -l h_rt=168:00:00                 #-- set the maximum run time, hh:mm:ss, default is 6hrs on scg3
#$ -w e                              #-- check for errors in the job submission options
#$ -j y                              #-- join the stdout and stderr streams into one file
#$ -o ~/output_error_directory       #-- output directory 
#$ -e ~/output_error_directory       #-- error directory   
#$ -t 1-19                           #-- specify the number of tasks 

readarray files < ~/snyder_project/timepts/timepts.txt
files=(null ${files[@]}) # this pads the file with an extra line in the beginning.
timept=${files[$SGE_TASK_ID]}

module load python/2.7.9 

export PYTHONPATH=$PYTHONPATH:/home/ngarud/snyder_project/software/MIDAS
export PATH=$PATH:/home/ngarud/snyder_project/software/MIDAS/scripts
export MIDAS_DB=/home/ngarud/snyder_project/software/midas_db_v1.2

echo $timept

indir=/srv/gsfs0/projects/snyder/hayanlee/projects/meta-gut/contigs/IDBA_UD
barcode_indir=/srv/gsfs0/projects/snyder/ngarud/fastq_with_barcode
mkdir=${barcode_indir}/${timept}

echo 'converting fastq files...'

cat $indir/${timept}/demux.${timept}.rmbc.tl20.trim.clean.r1.fastq | python /srv/gsfs0/projects/snyder/bhgood/barcode_readnames.py > ${barcode_indir}/${timept}/demux.${timept}.rmbc.tl20.trim.clean.r1.barcode.fastq

cat $indir/${timept}/demux.${timept}.rmbc.tl25.trim.clean.r2.fastq | python /srv/gsfs0/projects/snyder/bhgood/barcode_readnames.py > ${barcode_indir}/${timept}/demux.${timept}.rmbc.tl25.trim.clean.r2.barcode.fastq


OUTDIR=/srv/gsfs0/projects/snyder/ngarud/MIDAS_output/${timept}
mkdir $OUTDIR

echo 'running species...'

run_midas.py species $OUTDIR -1 ${barcode_indir}/${timept}/demux.${timept}.rmbc.tl20.trim.clean.r1.barcode.fastq -2 ${barcode_indir}/${timept}/demux.${timept}.rmbc.tl25.trim.clean.r2.barcode.fastq -t 10


echo 'done!'