#!/bin/bash  

echo "X X X X X X The process takes around a day 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*.gpff.gz &
sleep 5
wget -q ftp://ftp.ncbi.nlm.nih.gov/refseq/release/vertebrate_mammalian/*protein*.gpff.gz &
sleep 5
wget -q ftp://ftp.ncbi.nlm.nih.gov/refseq/release/invertebrate/*protein*.gpff.gz &
sleep 5
wget -q ftp://ftp.ncbi.nlm.nih.gov/refseq/H_sapiens/mRNA_Prot/*protein*.gpff.gz &
sleep 5
wget -q ftp://ftp.ncbi.nlm.nih.gov/refseq/M_musculus/mRNA_Prot/*protein*.gpff.gz &
sleep 5
wget -q ftp://ftp.ncbi.nlm.nih.gov/refseq/R_norvegicus/mRNA_Prot/*protein*.gpff.gz &
sleep 5
wget -q ftp://ftp.ncbi.nlm.nih.gov/refseq/S_scrofa/mRNA_Prot/*protein*.gpff.gz &
sleep 5
wget -q ftp://ftp.ncbi.nlm.nih.gov/refseq/B_taurus/mRNA_Prot/*protein*.gpff.gz &
sleep 5
wget -q ftp://ftp.ncbi.nlm.nih.gov/refseq/release/viral/*protein*.gpff.gz &
sleep 5
wget -q ftp://ftp.ncbi.nlm.nih.gov/refseq/release/fungi/*protein*.gpff.gz &
sleep 5
wget -q ftp://ftp.ncbi.nlm.nih.gov/refseq/release/protozoa/*protein*.gpff.gz &
sleep 5
wget -q ftp://ftp.ncbi.nlm.nih.gov/refseq/release/plant/*protein*.gpff.gz &
sleep 5

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

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

# Wait until all files are unzipped
wait
echo "Unzipping completed, renaming files..."

# merge and rename files
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 vertebrate_mammalian.*.protein.gpff > refseq_protein_all_mammalian.gpff
cat plant.*.protein.gpff > refseq_protein_all_plants.gpff

wait 
echo "Renaming files completed, extracting species from multiple species files..."
# 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/ 

wait
echo "folder permissions changed, deleting unused files..."

# delete old files from the download folder that are not used

find /data/shared/glygen/downloads/$resource/$new_dir ! -name refseq_protein_\* ! -type d -exec rm -f {} \;

wait

echo "Download is ready, process completed."