#!/bin/bash


script_name="split_study.sh"

#Help function
usage() {
  echo "-h  --Help documentation for $script_name"
  echo "-r  --Study_RID."
  echo "-t  --Tag of pipelne"
  echo "-e  --Email on failure"
  echo "-o  --Path to output directory"
  echo "Example: $script_name -r 16-1ZP0 -t 0.1.0 -e 'first.last@utsouthwestern.edu' -o '/path/to/output/dir/'"
  exit 1
}

main(){

    # Load required modules
    module load python/3.6.4-anaconda
    module load nextflow/20.01.0
    module load singularity/3.5.3

    # Parsing options
    OPTIND=1 # Reset OPTIND
    while getopts :r:t:e:o:h opt
        do
            case $opt in
                r) rid=$OPTARG;;
                t) tag=$OPTARG;;
                e) email=$OPTARG;;
                o) out=$OPTARG;;
                h) usage;;
            esac
        done

    shift $(($OPTIND -1))

    # Check for mandatory options
    if [[ -z ${rid} ]] || [[ -z ${tag} ]] || [[ -z ${email} ]] || [[ -z ${out} ]]; then
        usage
    fi

    # query GUDMAP/RBK study RID for experiment RIDs
    echo "curl --location --request GET 'https://www.gudmap.org/ermrest/catalog/2/entity/RNASeq:Experiment/Sequencing_Type=mRNA-Seq/Study_RID="${rid}"'" | bash > ${rid}\_experimentRIDs.json
    # extract experiment RIDs
    >${rid}\_experimentRIDs.csv
    python3 ./scripts/extract_RIDs.py -f ${rid}\_experimentRIDs
    expRIDs=$(cat ${rid}\_experimentRIDs.csv)

    > ${rid}\_studyRID.csv
    for expRID in ${expRIDs}
    do
      # query GUDMAP/RBK experiment RIDs for replicate RIDs
      > temp\_${expRID}\_repRIDs.json
      echo "curl --location --request GET 'https://www.gudmap.org/ermrest/catalog/2/entity/RNASeq:Replicate/Experiment_RID="${expRID}"'" | bash >> temp\_${expRID}\_repRIDs.json
      # extract replicate RIDs
      > temp\_${expRID}\_repRIDs.csv
      python3 ./scripts/extract_RIDs.py -f temp\_${expRID}\_repRIDs
      cat temp\_${expRID}\_repRIDs.csv >> ${rid}\_studyRID.csv
    done

    # cleanup study RID files
    rm -f temp*
    rm ${rid}\_experimentRIDs.json
    rm ${rid}\_experimentRIDs.csv

    # make dir for study
    mkdir $out/$rid
    mv $rid\_studyRID.csv $out/$rid
    cd $out/$rid

    # download version of RNA-seq pipelne
    existed_in_remote=$( git ls-remote --tags git@git.biohpc.swmed.edu:gudmap_rbk/rna-seq.git ${tag})

    if [[ -z ${existed_in_remote} ]]; then
        echo "The tag ${tag} doesn't exist."
        exit 1
    fi

    git clone -b $tag --depth 1 git@git.biohpc.swmed.edu:gudmap_rbk/rna-seq.git
    cd rna-seq

    # run pipeline on replicate RIDs in parallel
    while read repRID; do echo ${repRID}; sleep 30; done < "../${rid}_studyRID.csv" | xargs -P 5 -I {} nextflow -q run workflow/rna-seq.nf --repRID {} --source dev --deriva /project/BICF/BICF_Core/shared/gudmap/test_data/auth/credential.json --bdbag /project/BICF/BICF_Core/shared/gudmap/test_data/auth/cookies.txt --dev false --upload true --track true --email ${email} -with-report ./output/{}_report.html -with-timeline ./output/{}_timeline.html

}

main "$@"
