#!/bin/bash
RUN_ID=SIYS018_NSs_RNA
WORKING_DIR="/home/shichino/data/${RUN_ID}"

SAMPLE_LIST="pool_rna.csv"
SAMPLE_ID=( $(cut -d ',' -f1 ${SAMPLE_LIST} ) )

SAMPLE_LIST_vec="pool_vec.csv"
SAMPLE_ID_vec=( $(cut -d ',' -f1 ${SAMPLE_LIST_vec} ) )
SAMPLE_LIST_NSs="pool_NSs.csv"
SAMPLE_ID_NSs=( $(cut -d ',' -f1 ${SAMPLE_LIST_NSs} ) )
SAMPLE_LIST_mut="pool_mut.csv"
SAMPLE_ID_mut=( $(cut -d ',' -f1 ${SAMPLE_LIST_mut} ) )

GENOMEINDEX_SOURCE="/home/genome/hg38/hg38_gencode32_STAR"
DEPLETION_SOURCE="/home/genome/hg38/hg19_depletion_RNAs2_STAR"
BED_SOURCE="/home/genome/hg38/hg38_gencode32_nuc.bed"

GENOMEINDEX_SOURCE_vec="/home/shichino/data/SIYS017_NSs_Ribo/plasmid/1136/"
GENOMEINDEX_SOURCE_NSs="/home/shichino/data/SIYS017_NSs_Ribo/plasmid/1137/"
GENOMEINDEX_SOURCE_mut="/home/shichino/data/SIYS017_NSs_Ribo/plasmid/1138/"

BED_SOURCE_vec="/home/shichino/data/SIYS017_NSs_Ribo/plasmid/1136.bed"
BED_SOURCE_NSs="/home/shichino/data/SIYS017_NSs_Ribo/plasmid/1137.bed"
BED_SOURCE_mut="/home/shichino/data/SIYS017_NSs_Ribo/plasmid/1138.bed"


RAWFASTQ_SOURCE="${WORKING_DIR}/RawFastq/"

CLIPPED_OUT="${WORKING_DIR}/clipped"
UNALIGNED_OUT="${WORKING_DIR}/unaligned"
MAPPING_OUT="${WORKING_DIR}/mapped"
FPCOUNT_OUT="${WORKING_DIR}/fpcount"
SHELL_IN_OUT="${WORKING_DIR}/shell_in_out"

ADAPTER_SEQ=AGATCGGAAGAGCACACGTCTGAACTCCAGTCAC

echo "beginning riboscript"

mkdir ${CLIPPED_OUT} ${UNALIGNED_OUT} ${MAPPING_OUT} ${FPCOUNT_OUT} ${SHELL_IN_OUT}


# adapter removal
echo "task: adapter removal"
date +"%Y/%m/%d %H:%M:%S"

for id in ${SAMPLE_ID[@]}
do
	echo "(nohup fastp -i ${RAWFASTQ_SOURCE}/${id}*.fastq.gz -o ${CLIPPED_OUT}/${id}_clipped.fastq -a ${ADAPTER_SEQ} -w 12 -h ${CLIPPED_OUT}/${id}.html) &" >> filtercommands.sh
	echo "wait">> filtercommands.sh
done
source filtercommands.sh


# ncRNA depletion
echo "task: ncRNA depletion"
date +"%Y/%m/%d %H:%M:%S"

for id in ${SAMPLE_ID[@]}
do
		echo "(nohup mkdir ${UNALIGNED_OUT}/${id})" >> ncrnadepletioncommands.sh
        echo "(nohup STAR --genomeDir ${DEPLETION_SOURCE} --readFilesIn ${CLIPPED_OUT}/${id}_clipped.fastq --runThreadN 12 --outFilterMultimapNmax 2000 --outSAMtype BAM SortedByCoordinate --outFileNamePrefix ${UNALIGNED_OUT}/${id}/ --outReadsUnmapped Fastx --limitBAMsortRAM 10000000000) &" >> ncrnadepletioncommands.sh
		echo "wait">> ncrnadepletioncommands.sh
done
source ncrnadepletioncommands.sh


# Map rtRNA-depleted reads to human genome
echo "task: mapping"
date +"%Y/%m/%d %H:%M:%S"

for id in ${SAMPLE_ID[@]}
do
		echo "(nohup mkdir ${MAPPING_OUT}/${id})" >> starcommands.sh
        echo "(nohup STAR --genomeDir ${GENOMEINDEX_SOURCE} --readFilesIn ${UNALIGNED_OUT}/${id}/Unmapped.out.mate1 --runThreadN 12 --outSAMtype BAM SortedByCoordinate --outFileNamePrefix ${MAPPING_OUT}/${id}/ --outReadsUnmapped Fastx --limitBAMsortRAM 10000000000) &" >> starcommands.sh
		echo "wait">> starcommands.sh
done
source starcommands.sh


# Map rtRNA-depleted reads to plasmid
echo "task: mapping plasmid"
date +"%Y/%m/%d %H:%M:%S"

for id in ${SAMPLE_ID_vec[@]}
do
		echo "(nohup mkdir ${MAPPING_OUT}/${id}_plasmid)" >> plasmidstarcommands.sh
        echo "(nohup STAR --genomeDir ${GENOMEINDEX_SOURCE_vec} --readFilesIn ${MAPPING_OUT}/${id}/Unmapped.out.mate1 --runThreadN 6 --outSAMtype BAM SortedByCoordinate --outFileNamePrefix ${MAPPING_OUT}/${id}_plasmid/ --outReadsUnmapped Fastx --limitBAMsortRAM 10000000000) &" >> plasmidstarcommands.sh
		echo "wait">> plasmidstarcommands.sh
done

for id in ${SAMPLE_ID_NSs[@]}
do
		echo "(nohup mkdir ${MAPPING_OUT}/${id}_plasmid)" >> plasmidstarcommands.sh
        echo "(nohup STAR --genomeDir ${GENOMEINDEX_SOURCE_NSs} --readFilesIn ${MAPPING_OUT}/${id}/Unmapped.out.mate1 --runThreadN 6 --outSAMtype BAM SortedByCoordinate --outFileNamePrefix ${MAPPING_OUT}/${id}_plasmid/ --outReadsUnmapped Fastx --limitBAMsortRAM 10000000000) &" >> plasmidstarcommands.sh
		echo "wait">> plasmidstarcommands.sh
done

for id in ${SAMPLE_ID_mut[@]}
do
		echo "(nohup mkdir ${MAPPING_OUT}/${id}_plasmid)" >> plasmidstarcommands.sh
        echo "(nohup STAR --genomeDir ${GENOMEINDEX_SOURCE_mut} --readFilesIn ${MAPPING_OUT}/${id}/Unmapped.out.mate1 --runThreadN 6 --outSAMtype BAM SortedByCoordinate --outFileNamePrefix ${MAPPING_OUT}/${id}_plasmid/ --outReadsUnmapped Fastx --limitBAMsortRAM 10000000000) &" >> plasmidstarcommands.sh
		echo "wait">> plasmidstarcommands.sh
done


source plasmidstarcommands.sh


#Extract mapped reads
echo "task: extract mapped reads"
date +"%Y/%m/%d %H:%M:%S"

for id in ${SAMPLE_ID[@]}
do
        echo "(nohup samtools view -b ${MAPPING_OUT}/${id}/Aligned.sortedByCoord.out.bam > ${MAPPING_OUT}/${id}.bam ) &" >> renamingcommands.sh
        echo "(nohup samtools view -b ${MAPPING_OUT}/${id}_plasmid/Aligned.sortedByCoord.out.bam > ${MAPPING_OUT}/${id}_plasmid.bam ) &" >> renamingcommands.sh
done
echo "wait">> renamingcommands.sh
source renamingcommands.sh


#Index bam files
echo "task: indexing bam file"
date +"%Y/%m/%d %H:%M:%S"

for id in ${SAMPLE_ID[@]}
do
	echo "(nohup samtools index ${MAPPING_OUT}/${id}.bam ) &" >> indexingcommands.sh
	echo "(nohup samtools index ${MAPPING_OUT}/${id}_plasmid.bam ) &" >> indexingcommands.sh
done
echo "wait">>indexingcommands.sh
source indexingcommands.sh


#fp-count
echo "task: fp-count"
date +"%Y/%m/%d %H:%M:%S"

for id in ${SAMPLE_ID[@]}
do
       echo "(nohup fp-count -r -c5,5 -o ${FPCOUNT_OUT}/${id}.txt -b ${BED_SOURCE} -a mrna.txt ${MAPPING_OUT}/${id}.bam) &" >> fpcountcommands.sh
done
echo "wait" >> fpcountcommands.sh

for id in ${SAMPLE_ID_vec[@]}
do
       echo "(nohup fp-count -r -c5,5 -o ${FPCOUNT_OUT}/${id}.plasmid.txt -b ${BED_SOURCE_vec} -a mrna.txt ${MAPPING_OUT}/${id}_plasmid.bam) &" >> fpcountcommands.sh
done

for id in ${SAMPLE_ID_NSs[@]}
do
       echo "(nohup fp-count -r -c5,5 -o ${FPCOUNT_OUT}/${id}.plasmid.txt -b ${BED_SOURCE_NSs} -a mrna.txt ${MAPPING_OUT}/${id}_plasmid.bam) &" >> fpcountcommands.sh
done

for id in ${SAMPLE_ID_mut[@]}
do
       echo "(nohup fp-count -r -c5,5 -o ${FPCOUNT_OUT}/${id}.plasmid.txt -b ${BED_SOURCE_mut} -a mrna.txt ${MAPPING_OUT}/${id}_plasmid.bam) &" >> fpcountcommands.sh
done

echo "wait" >> fpcountcommands.sh

source fpcountcommands.sh


cat filtercommands.sh >> list_of_commands_${RUN_ID}
cat ncrnadepletioncommands.sh >> list_of_commands_${RUN_ID}
cat starcommands.sh >> list_of_commands_${RUN_ID}
cat renamingcommands.sh >> list_of_commands_${RUN_ID}
cat indexingcommands.sh >> list_of_commands_${RUN_ID}
cat fpcountcommands.sh >>  list_of_commands_${RUN_ID}


rm *commands.sh
mv list_of_commands_${RUN_ID} ${SHELL_IN_OUT}
cp rnascript.sh ${SHELL_IN_OUT}


echo "exiting riboscript"
date +"%Y/%m/%d %H:%M:%S"

mv nohup.out ${SHELL_IN_OUT}

