#!/bin/bash
#
# goby_transcriptomic_unix_code.sh
#
# Written by: Blinded
#
# This script documents the steps undertaken to first characterize expressed transcripts 
# in goby liver tissue using a de novo assembly approach implemented using Trinity, 
# then multi-mapping of reads on to the assembled transcriptome using Bowtie2 and
# processing of BAM files to generate gene level counts of mapped reads using Corset.
# The code given below performs these steps for round goby (RG). Identical parameters 
# were used for tubenose goby (TNG).
#

# Trinity assembly

cat RG-C02_R1.fastq RG-C03_R1.fastq RG-C04_R1.fastq RG-H01_R1.fastq RG-H03_R1.fastq RG-H04_R1.fastq RG-L01_R1.fastq RG-L02_R1.fastq RG-L03_R1.fastq > reads_1.fq
cat RG-C02_R2.fastq RG-C03_R2.fastq RG-C04_R2.fastq RG-H01_R2.fastq RG-H03_R2.fastq RG-H04_R2.fastq RG-L01_R2.fastq RG-L02_R2.fastq RG-L03_R2.fastq > reads_2.fq

trinityrnaseq-2.0.3/Trinity --seqType fq --max_memory 30G --left reads_1.fq --right reads_2.fq --SS_lib_type RF --CPU 4 --trimmomatic --normalize_reads > run.log 2>&1 &



# Bowtie2 multi-mapping of reads

FILES=`ls *_R1.fastq.gz | sed 's/_R1.fastq.gz//g'`
for F in $FILES ; do
        R1=${F}_R1.fastq.gz
        R2=${F}_R2.fastq.gz
        P1=${F}_P1.fq
        U1=${F}_U1.fq
        P2=${F}_P2.fq
        U2=${F}_U2.fq
        java -jar /home/wellban/src/Trimmomatic-0.33/trimmomatic-0.33.jar PE -threads 2 $R1 $R2 $P1 $U1 $P2 $U2 ILLUMINACLIP:/home/wellban/src/Trimmomatic-0.33/adapters/TruSeq3-PE.fa:2:30:10 LEADING:3 TRAILING:3 SLIDINGWINDOW:4:15 MINLEN:36
    	rm $R1 $R2 $U1 $U2
        bowtie2 -x RG_Trinity --end-to-end --sensitive --no-mixed --no-discordant --score-min L,-0.6,-0.6 -p 2 -q -1 $P1 -2 $P2 > ${F}.sam  
        rm $P1 $P2
        samtools view -b ${F}.sam > ${F}.bam
        rm ${F}.sam
done


# Corset processing of BAM files to generate counts file used for data analysis

corset -g 1,1,1,2,2,2,3,3,3 RG-C02.bam RG-C03.bam RG-C04.bam RG-L01.bam RG-L02.bam RG-L03.bam RG-H01.bam RG-H03.bam RG-H04.bam
