#!/bin/bash  

echo "X X X X X X The process takes around 45mins to complete X X X X X X"
# define resource
resource=ncbi/refseq

# add the time stamped folder
cd /data/projects/glygen/downloads/$resource/

new_dir=$(date +%Y_%m_%d)/
mkdir $new_dir

# create symbolic link
# this step is required for filter-refseq.py to run correctly
cd /data/projects/glygen/downloads/$resource/
rm current
ln -s $new_dir current

# download files to new folder
cd /data/projects/glygen/downloads/$resource/$new_dir

echo "Begin downloading files at $(date)"

# the commands followed by & are run in the background
wget -q ftp://ftp.ncbi.nlm.nih.gov/refseq/release/vertebrate_other/*protein* &
sleep 5
wget -q ftp://ftp.ncbi.nlm.nih.gov/refseq/release/vertebrate_mammalian/*protein* &
sleep 5
wget -q ftp://ftp.ncbi.nlm.nih.gov/refseq/release/invertebrate/*protein* &
sleep 5
wget -q ftp://ftp.ncbi.nlm.nih.gov/refseq/H_sapiens/mRNA_Prot/*protein* &
sleep 5
wget -q ftp://ftp.ncbi.nlm.nih.gov/refseq/M_musculus/mRNA_Prot/*protein* &
sleep 5
wget -q ftp://ftp.ncbi.nlm.nih.gov/refseq/R_norvegicus/mRNA_Prot/*protein* &
sleep 5
wget -q ftp://ftp.ncbi.nlm.nih.gov/refseq/S_scrofa/mRNA_Prot/*protein* &
sleep 5
wget -q ftp://ftp.ncbi.nlm.nih.gov/refseq/B_taurus/mRNA_Prot/*protein* &
sleep 5
wget -q ftp://ftp.ncbi.nlm.nih.gov/refseq/release/viral/*protein* &
sleep 5
wget -q ftp://ftp.ncbi.nlm.nih.gov/refseq/release/fungi/*protein* &
sleep 5
wget -q ftp://ftp.ncbi.nlm.nih.gov/refseq/release/protozoa/*protein* &
sleep 5
wget -q ftp://ftp.ncbi.nlm.nih.gov/refseq/release/plant/*protein* &
sleep 5

# Wait until all downloads are complete
wait 
echo "Download completed, unzipping files..."

# unzip the downloaded folder
gunzip *.gpff.gz &
wait

echo "Unzipping completed, renaming files..."
# renaming files with species-specific folders in refseq
cat human.*.protein.gpff > refseq_protein_all_9606.gpff
cat mouse.*.protein.gpff > refseq_protein_all_10090.gpff
cat rat.*.protein.gpff > refseq_protein_all_10116.gpff
cat pig.*.protein.gpff > refseq_protein_all_9823.gpff
cat cow.*.protein.gpff > refseq_protein_all_9913.gpff
cat viral.*.protein.gpff > refseq_protein_all_viral.gpff
cat invertebrate.*.protein.gpff > refseq_protein_all_insects.gpff
cat fungi.*.protein.gpff > refseq_protein_all_fungi.gpff
cat protozoa.*.protein.gpff > refseq_protein_all_amoebozoa.gpff
cat vertebrate_other.*.protein.gpff > refseq_protein_all_vertebrates.gpff
cat plant.*.protein.gpff > refseq_protein_all_plants.gpff

# extracting other organisms from gpff files
python3 /software/glygen/filter-refseq.py

echo "Files renamed, changing folder permissions..."

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

echo "Download is ready, process completed."
