# These are some tools required for running the analyses. Please adapt
# the installation procedures depending on your environment. Make sure
# that Python (3.4 or above), R and Perl are installed. Also get
# Ontologizer from https://charite.github.io/software-ontologizer.html
# and put the "file Ontologizer.jar" into the bin folder.

main(){
    mkdir -p bin
    install_r_packages
    install_python_libs
    install_reademption
    install_selected_sra_tools
    install_selected_fastx_tools
    install_segemehl
}

install_r_packages(){
    # Originally used versions:
    # - RUVSeq: 1.2.0
    # - edgeR 3.10.5 
    # - ggplot2: 1.0.1
    # - plyr: 1.8.1
    # - reshape: 0.8.5
    # - scales: 0.2.4
    R --slave --vanilla --quiet --no-save <<EOF
source("http://bioconductor.org/biocLite.R")
biocLite("RUVSeq")
biocLite("edgeR")
install.packages("ggplot2")
install.packages("plyr")
install.packages("reshape")
install.packages("scales")
EOF
}

install_python_libs(){
    pip3 install --user pandas biopython Cython matplotlib
}

install_reademption(){
    pip3 install --user reademption==0.3.5 
    ln -s ~/.local/bin/reademption bin
}

install_selected_sra_tools(){
    wget  http://ftp-trace.ncbi.nlm.nih.gov/sra/sdk/2.1.7/sratoolkit.2.1.7-ubuntu32.tar.gz
    tar xzf sratoolkit.2.1.7-ubuntu32.tar.gz
    cp sratoolkit.2.1.7-ubuntu32/fastq-dump.2.1.7 bin/fastq-dump
    rm -rf sratoolkit.2.1.7-ubuntu32*
}

install_selected_fastx_tools(){
    wget http://hannonlab.cshl.edu/fastx_toolkit/fastx_toolkit_0.0.13_binaries_Linux_2.6_amd64.tar.bz2
    mkdir tmp
    mv fastx_toolkit_0.0.13_binaries_Linux_2.6_amd64.tar.bz2 tmp
    cd tmp ; tar xfj fastx_toolkit_0.0.13_binaries_Linux_2.6_amd64.tar.bz2 \
	&& mv bin/fastq_to_fasta ../bin \
	&& mv bin/fastq_quality_trimmer ../bin \
	&& cd ..
    rm -rf tmp
}

install_segemehl(){
    wget http://www.bioinf.uni-leipzig.de/Software/segemehl/segemehl_0_2_0.tar.gz
    tar xzf segemehl_0_2_0.tar.gz
    cd segemehl_0_2_0/segemehl && make && cd ../..
    mv segemehl_0_2_0/segemehl/lack.x segemehl_0_2_0/segemehl/segemehl.x bin/
    rm -rf segemehl_0_2_0*
}

main
