#!/bin/bash

set -e

# define resource
resource=unilectin

# add the time_stamped_folder
cd /data/projects/glygen/downloads/$resource/

new_dir=$(date +%m_%d_%Y)

mkdir -p "$new_dir" && echo "Created new directory /data/projects/glygen/downloads/$resource/$new_dir"

# move into new folder
cd /data/projects/glygen/downloads/$resource/$new_dir

echo "Downloading UniLectin data..."

# download UniLectin dataset
curl -X POST \
    -H "Content-Type: application/json" \
    -d ' { "getcolumns": "lectin.lectin_id,uniprot,lectin.pdb,species,class,family,origin,fold,doi_list,pubmed_list", 
    "wherecolumn": "uniprot", 
    "isvalue": "%%", 
    "limit": "-1"}  ' \
    https://unilectin.unige.ch/api/getlectins \
    -o UniLectin3D.json

sleep 5

echo "Downloading Human Lectome data..."

# download full human lectome response
curl -X POST \
    -H "Content-Type: application/json" \
    -d '  { "getcolumns": "id,UniProt_ID,PDB_ID,infer_class,family,infer_fold,lectinStatus,PubMed_ID_UniProtCanonical,pubmed_NCBI_inTxt_NCBIdata", 
    "wherecolumn": "UniProt_ID", 
    "isvalue": "%%", 
    "limit": "-1"}    ' \
    https://unilectin.unige.ch/api/gethumanlectome \
    -o HumanLectome.json

sleep 5

# echo "Filtering curated Human Lectome entries..."

# # keep only curated rows
# jq '
#     [.[0]] +                # keep header row
#     [.[1:][] | select(.[6] == "Curated")]
# ' HumanLectome.json > HumanLectome_curated.json

# # optionally remove unfiltered file
# rm -f HumanLectome.json

# update new folder permissions
chmod -R 775 /data/projects/glygen/downloads/$resource/$new_dir/

# create symbolic link
cd /data/projects/glygen/downloads/$resource/

rm -f current
ln -s "$new_dir" current

echo "UniLectin download complete."