main(){
    PYTHON_PATH=python3.4
    RSCRIPT_PATH=Rscript
    READEMPTION_PATH=../bin/reademption
    SEGEMEHL_PATH=../bin/segemehl.x
    LACK_PATH=../bin/lack.x
    FASTQ_DUMP_PATH=../bin/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=5
    PLOT_FOLDER=plots

    # 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
    # run_coverage_file_generation
    # change_reademption_folders_permission
    # 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_files(){
    BASE_URL="ftp://ftp-trace.ncbi.nlm.nih.gov/sra/sra-instant/reads/ByExp/sra/SRX/"
    for LIB in \
	GSM1967365,SRX146/SRX1469714/SRR2980133/SRR2980133.sra,3D4_31:WT:00_h:R1 \
	GSM1967350,SRX146/SRX1469699/SRR2980118/SRR2980118.sra,3D4_31:DeltapinT:00_h:R1 \
	GSM1967380,SRX146/SRX1469729/SRR2980148/SRR2980148.sra,3D4_31:mock:01_h:R1 \
	GSM1967368,SRX146/SRX1469717/SRR2980136/SRR2980136.sra,3D4_31:WT:01_h:R1 \
	GSM1967353,SRX146/SRX1469702/SRR2980121/SRR2980121.sra,3D4_31:DeltapinT:01_h:R1 \
	GSM1967371,SRX146/SRX1469720/SRR2980139/SRR2980139.sra,3D4_31:WT:02_h:R1 \
	GSM1967356,SRX146/SRX1469705/SRR2980124/SRR2980124.sra,3D4_31:DeltapinT:02_h:R1 \
	GSM1967374,SRX146/SRX1469723/SRR2980142/SRR2980142.sra,3D4_31:WT:06_h:R1 \
	GSM1967359,SRX146/SRX1469708/SRR2980127/SRR2980127.sra,3D4_31:DeltapinT:06_h:R1 \
	GSM1967377,SRX146/SRX1469726/SRR2980145/SRR2980145.sra,3D4_31:WT:16_h:R1 \
	GSM1967362,SRX146/SRX1469711/SRR2980130/SRR2980130.sra,3D4_31:DeltapinT:16_h:R1 \
	GSM1967366,SRX146/SRX1469715/SRR2980134/SRR2980134.sra,3D4_31:WT:00_h:R2 \
	GSM1967351,SRX146/SRX1469700/SRR2980119/SRR2980119.sra,3D4_31:DeltapinT:00_h:R2 \
	GSM1967381,SRX146/SRX1469730/SRR2980149/SRR2980149.sra,3D4_31:mock:01_h:R2 \
	GSM1967369,SRX146/SRX1469718/SRR2980137/SRR2980137.sra,3D4_31:WT:01_h:R2 \
	GSM1967354,SRX146/SRX1469703/SRR2980122/SRR2980122.sra,3D4_31:DeltapinT:01_h:R2 \
	GSM1967372,SRX146/SRX1469721/SRR2980140/SRR2980140.sra,3D4_31:WT:02_h:R2 \
	GSM1967357,SRX146/SRX1469706/SRR2980125/SRR2980125.sra,3D4_31:DeltapinT:02_h:R2 \
	GSM1967375,SRX146/SRX1469724/SRR2980143/SRR2980143.sra,3D4_31:WT:06_h:R2 \
	GSM1967360,SRX146/SRX1469709/SRR2980128/SRR2980128.sra,3D4_31:DeltapinT:06_h:R2 \
	GSM1967378,SRX146/SRX1469727/SRR2980146/SRR2980146.sra,3D4_31:WT:16_h:R2 \
	GSM1967363,SRX146/SRX1469712/SRR2980131/SRR2980131.sra,3D4_31:DeltapinT:16_h:R2 \
	GSM1967367,SRX146/SRX1469716/SRR2980135/SRR2980135.sra,3D4_31:WT:00_h:R3 \
	GSM1967352,SRX146/SRX1469701/SRR2980120/SRR2980120.sra,3D4_31:DeltapinT:00_h:R3 \
	GSM1967382,SRX146/SRX1469731/SRR2980150/SRR2980150.sra,3D4_31:mock:01_h:R3 \
	GSM1967370,SRX146/SRX1469719/SRR2980138/SRR2980138.sra,3D4_31:WT:01_h:R3 \
	GSM1967355,SRX146/SRX1469704/SRR2980123/SRR2980123.sra,3D4_31:DeltapinT:01_h:R3 \
	GSM1967373,SRX146/SRX1469722/SRR2980141/SRR2980141.sra,3D4_31:WT:02_h:R3 \
	GSM1967358,SRX146/SRX1469707/SRR2980126/SRR2980126.sra,3D4_31:DeltapinT:02_h:R3 \
	GSM1967376,SRX146/SRX1469725/SRR2980144/SRR2980144.sra,3D4_31:WT:06_h:R3 \
	GSM1967361,SRX146/SRX1469710/SRR2980129/SRR2980129.sra,3D4_31:DeltapinT:06_h:R3 \
	GSM1967379,SRX146/SRX1469728/SRR2980147/SRR2980147.sra,3D4_31:WT:16_h:R3 \
	GSM1967364,SRX146/SRX1469713/SRR2980132/SRR2980132.sra,3D4_31:DeltapinT:16_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/Pig.fa \
	$READEMPTION_FOLDER/input/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/Pig.gff \
	$READEMPTION_FOLDER/input/annotations/
    ln -s ../../../../reference_sequences_and_annotations/output/Salmonella.gff \
	$READEMPTION_FOLDER/input/annotations/
}

run_read_alignment(){
    $PYTHON_PATH $READEMPTION_PATH \
        align \
	--segemehl_bin=${SEGEMEHL_PATH} \
        --lack_bin=${LACK_PATH} \
	--split \
	-r \
        -p ${PROCESSORS_MAPPING} \
        -a $ACCURACY \
        -l $MIN_MAPPING_LEN \
	--poly_a_clipping \
	--progress \
	$CROSSMAPPING_PARAMETERS \
        $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" \
    	| cut -f 1-40 \
    	> 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
