main(){
    PYTHON_PATH=python3.4
    RSCRIPT_PATH=Rscript
    READEMPTION_PATH=../bin/reademption
    SEGEMEHL_PATH=../bin/segemehl.x
    LACK_PATH=../bin/lack.x
    FASTQ_TRIMMER_PATH=../bin/fastq_quality_trimmer
    FASTQ_TO_FASTA_PATH=../bin/fastq_to_fasta
    FASTQ_DUMP_PATH=../bin/fastq-dump
    SRA_FOLDER=Libs_as_SRA
    FASTQ_FOLDER=Libs_as_FASTQ
    READEMPTION_FOLDER=READemption_analysis
    ACCURACY=90
    MIN_MAPPING_LEN=20
    PROCESSORS_MAPPING=40
    PROCESSORS_GENE_QUANTI=5
    EDGER_SALMONELLA_FOLDER=edgeR_Salmonella

    create_folders
    set_up_analysis_folder
    link_reference_sequences
    link_annotations
    download_sra_files
    convert_to_fastq
    convert_to_quality_trimmed_fasta
    run_read_alignment
    run_gene_quantification
    change_reademption_folders_permission
    run_salmonella_edger
}

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

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

download_sra_files(){
    BASE_URL="ftp://ftp-trace.ncbi.nlm.nih.gov/sra/sra-instant/reads/ByExp/sra/SRX/"
    for LIB in \
	GSM1659495,SRX997/SRX997348/SRR1976379/SRR1976379.sra,in_vitro:SPI-1_empty_vector:05_min_ara:R1 \
	GSM1659496,SRX997/SRX997349/SRR1976380/SRR1976380.sra,in_vitro:SPI-1_pinT_vector:05_min_ara:R1 \
	GSM1659497,SRX997/SRX997350/SRR1976381/SRR1976381.sra,in_vitro:SPI-1_empty_vector:05_min_ara:R2 \
	GSM1659498,SRX997/SRX997351/SRR1976382/SRR1976382.sra,in_vitro:SPI-1_pinT_vector:05_min_ara:R2 \
	GSM1659499,SRX997/SRX997352/SRR1976383/SRR1976383.sra,in_vitro:SPI-2_empty_vector:05_min_ara:R1 \
	GSM1659500,SRX997/SRX997353/SRR1976384/SRR1976384.sra,in_vitro:SPI-2_pinT_vector:05_min_ara:R1 \
	GSM1659501,SRX997/SRX997354/SRR1976385/SRR1976385.sra,in_vitro:SPI-2_empty_vector:05_min_ara:R2 \
	GSM1659502,SRX997/SRX997356/SRR1976386/SRR1976386.sra,in_vitro:SPI-2_pinT_vector:05_min_ara:R2 \
	GSM1659503,SRX997/SRX997357/SRR1976387/SRR1976387.sra,HeLa-S3:SPI-2_empty_vector:05_min_ara:R1 \
	GSM1659504,SRX997/SRX997358/SRR1976388/SRR1976388.sra,HeLa-S3:SPI-2_pinT_vector:05_min_ara:R1 \
	GSM1659505,SRX997/SRX997359/SRR1976389/SRR1976389.sra,HeLa-S3:SPI-2_empty_vector:05_min_ara:R2 \
	GSM1659506,SRX997/SRX997360/SRR1976390/SRR1976390.sra,HeLa-S3:SPI-2_pinT_vector:05_min_ara:R2
    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
