main(){
    PYTHON_PATH=python3.4
    READEMPTION_PATH=../bin/reademption
    RSCRIPT_PATH=Rscript
    SEGEMEHL_PATH=../bin/segemehl.x
    LACK_PATH=../bin/lack.x
    FASTQ_DUMP_PATH=fastq-dump
    FASTQ_TRIMMER_PATH=../bin/fastq_quality_trimmer
    FASTQ_TO_FASTA_PATH=../bin/fastq_to_fasta

    SRA_FOLDER=Libs_as_SRA
    FASTQ_FOLDER=Libs_as_FASTQ
    READEMPTION_FOLDER=READemption_analysis
    ACCURACY=90
    MIN_MAPPING_LEN=20
    EDGER_SALMONELLA_FOLDER=edgeR_Salmonella
    PROCESSORS_MAPPING=40
    PROCESSORS_GENE_QUANTI=40
    PLOT_FOLDER=plots

    create_folders
    set_up_analysis_folder
    link_reference_sequences
    link_annotations
    download_sra
    convert_to_fastq
    convert_to_quality_trimmed_fasta
    run_read_alignment
    run_gene_quantification
    run_coverage_file_generation
    run_salmonella_edger
}

create_folders(){
    mkdir -p bin $SRA_FOLDER $FASTQ_FOLDER $EDGER_SALMONELLA_FOLDER $PLOT_FOLDER 
}

set_up_analysis_folder(){
    $PYTHON_PATH $READEMPTION_PATH create $READEMPTION_FOLDER
}

download_sra(){
    BASE_URL="ftp://ftp-trace.ncbi.nlm.nih.gov/sra/sra-instant/reads/ByExp/sra/SRX/"
    for LIB in \
	GSM1967917,SRX147/SRX1470903/SRR2981449/SRR2981449.sra,in_vitro:WT:00_h:R1 \
	GSM1967920,SRX147/SRX1470906/SRR2981452/SRR2981452.sra,in_vitro:WT:01_h:R1 \
	GSM1967911,SRX147/SRX1470897/SRR2981443/SRR2981443.sra,in_vitro:DeltapinT:00_h:R1 \
	GSM1967914,SRX147/SRX1470900/SRR2981446/SRR2981446.sra,in_vitro:DeltapinT:01_h:R1 \
	GSM1967905,SRX147/SRX1470891/SRR2981437/SRR2981437.sra,in_vitro:ComppinT:00_h:R1 \
	GSM1967908,SRX147/SRX1470894/SRR2981440/SRR2981440.sra,in_vitro:ComppinT:01_h:R1 \
	GSM1967918,SRX147/SRX1470904/SRR2981450/SRR2981450.sra,in_vitro:WT:00_h:R2 \
	GSM1967921,SRX147/SRX1470907/SRR2981453/SRR2981453.sra,in_vitro:WT:01_h:R2 \
	GSM1967912,SRX147/SRX1470898/SRR2981444/SRR2981444.sra,in_vitro:DeltapinT:00_h:R2 \
	GSM1967915,SRX147/SRX1470901/SRR2981447/SRR2981447.sra,in_vitro:DeltapinT:01_h:R2 \
	GSM1967906,SRX147/SRX1470892/SRR2981438/SRR2981438.sra,in_vitro:ComppinT:00_h:R2 \
	GSM1967909,SRX147/SRX1470895/SRR2981441/SRR2981441.sra,in_vitro:ComppinT:01_h:R2 \
	GSM1967919,SRX147/SRX1470905/SRR2981451/SRR2981451.sra,in_vitro:WT:00_h:R3 \
	GSM1967922,SRX147/SRX1470908/SRR2981454/SRR2981454.sra,in_vitro:WT:01_h:R3 \
	GSM1967913,SRX147/SRX1470899/SRR2981445/SRR2981445.sra,in_vitro:DeltapinT:00_h:R3 \
	GSM1967916,SRX147/SRX1470902/SRR2981448/SRR2981448.sra,in_vitro:DeltapinT:01_h:R3 \
	GSM1967907,SRX147/SRX1470893/SRR2981439/SRR2981439.sra,in_vitro:ComppinT:00_h:R3 \
	GSM1967910,SRX147/SRX1470896/SRR2981442/SRR2981442.sra,in_vitro:ComppinT:01_h:R3
    do
	GEO_ACCESSION=$(echo $LIB | cut -f 1 -d",")
	FILE_PATH=$(echo $LIB | cut -f 2 -d",")
	LIB_NAME=$(echo $LIB | cut -f 3 -d",")

	wget \
	    -O $SRA_FOLDER/${GEO_ACCESSION}_${LIB_NAME}.sra \
	    -c \
	    ${BASE_URL}/${FILE_PATH}
    done
}

convert_to_fastq(){
    for SRA_FILE in $(ls $SRA_FOLDER)
    do
	COMPRESSED_FASTQ_FILE=$(echo $SRA_FILE \
	    | sed -e "s/GSM......._//" -e "s/.sra/.fq.bz2/")
	$FASTQ_DUMP_PATH \
	    --stdout \
	    $SRA_FOLDER/$SRA_FILE \
	    | bzip2 \
	    --stdout - \
	    > ${FASTQ_FOLDER}/$COMPRESSED_FASTQ_FILE &
    done
    wait
}


convert_to_quality_trimmed_fasta(){
    for FASTQ_FILE in $(ls $FASTQ_FOLDER)
    do
	FASTA_FILE=$(echo $FASTQ_FILE | sed "s/.fq.bz2/.fa.bz2/")
	bunzip2 --stdout $FASTQ_FOLDER/$FASTQ_FILE \
	    | $FASTQ_TRIMMER_PATH -t 20 -Q33 -l 1 \
	    | $FASTQ_TO_FASTA_PATH -Q33 \
	    | bzip2 --stdout - > $READEMPTION_FOLDER/input/reads/$FASTA_FILE &
    done
    wait
}

link_reference_sequences(){
    ln -s ../../../../reference_sequences_and_annotations/output/NC_016810.fa \
	$READEMPTION_FOLDER/input/reference_sequences/
    ln -s ../../../../reference_sequences_and_annotations/output/NC_017718.fa \
	$READEMPTION_FOLDER/input/reference_sequences/
    ln -s ../../../../reference_sequences_and_annotations/output/NC_017719.fa \
	$READEMPTION_FOLDER/input/reference_sequences/
    ln -s ../../../../reference_sequences_and_annotations/output/NC_017720.fa \
	$READEMPTION_FOLDER/input/reference_sequences/
}

link_annotations(){
    ln -s ../../../../reference_sequences_and_annotations/output/Salmonella.gff \
	$READEMPTION_FOLDER/input/annotations/
}

run_read_alignment(){
    $PYTHON_PATH $READEMPTION_PATH \
        align \
	--split \
	-r \
        -p ${PROCESSORS_MAPPING} \
        -a $ACCURACY \
        -l $MIN_MAPPING_LEN \
	--poly_a_clipping \
	--progress \
        $READEMPTION_FOLDER
}

run_gene_quantification(){
    $PYTHON_PATH $READEMPTION_PATH \
        gene_quanti \
        -p ${PROCESSORS_GENE_QUANTI} \
    	--unique_only \
    	--no_count_splitting_by_gene_no \
    	--no_count_split_by_alignment_no \
        $READEMPTION_FOLDER
}

run_coverage_file_generation(){
    $PYTHON_PATH $READEMPTION_PATH \
    coverage \
        -p ${PROCESSORS_MAPPING} \
        $READEMPTION_FOLDER
}

change_reademption_folders_permission(){
    chmod -R a-w $READEMPTION_FOLDER
}

run_salmonella_edger(){
    # Extract gene names and locus tag colums
    grep -Pv "\trRNA\t|\ttRNA\t" ../reference_sequences_and_annotations/output/Salmonella.csv \
    	| cut -f 10,11 \
    	> tmp_names_and_locus_tags

    # Extract Salmonella countings for non-mock libraries
    grep -P "^Orienta|\tNC_01" $READEMPTION_FOLDER/output/gene_quanti/gene_quanti_combined/gene_wise_quantifications_combined.csv \
    	| grep -v "^anti-sense" \
    	| grep -Pv "\trRNA\t|\ttRNA\t" \
    	> tmp

    paste tmp tmp_names_and_locus_tags \
    	> $EDGER_SALMONELLA_FOLDER/Salmonella_gene_wise_read_countings.csv

    rm tmp tmp_names_and_locus_tags

    $RSCRIPT_PATH bin/edgeR_Salmonella.R \
    	$EDGER_SALMONELLA_FOLDER/Salmonella_gene_wise_read_countings.csv \
    	$EDGER_SALMONELLA_FOLDER/
}


main
