#!/usr/bin/env bash

### Created: Apr 14, 2020
### Author: Anastasiya Boersch
### Company: Zavolan Group, Biozentrum, University of Basel

### Mouse
# Download mouse GTF
wget ftp://ftp.ensembl.org/pub/release-92/gtf/mus_musculus/Mus_musculus.GRCm38.92.gtf.gz
gunzip Mus_musculus.GRCm38.92.gtf.gz

# Keep only genome elements that are annotated to chromosomes 1-19, MT, X and Y
awk '($1 == "1") || ($1 == "2") || ($1 == "3") || ($1 == "4") || ($1 == "5") || ($1 == "6") || ($1 == "7") || ($1 == "8") || ($1 == "9") || ($1 == "10") || ($1 == "11") || ($1 == "12") || ($1 == "13") || ($1 == "14") || ($1 == "15") || ($1 == "16") || ($1 == "17") || ($1 == "18") || ($1 == "19") || ($1 == "MT") || ($1 == "X")  || ($1 == "Y") { print $0}' Mus_musculus.GRCm38.92.gtf > tmp2

# Keep only genome elements annotated as "gene"
awk '($3 == "gene") { print $10"\t"$14}' tmp2 > tmp3

# Get gene Ensembl IDs and corresponding gene symbols
sed 's/\"//g' tmp3 > tmp4
sed 's/;//g' tmp4 > GeneIdTOGeneName_mouse.txt

# Remove temporary variables
rm Mus_musculus.GRCm38.92.gtf
rm tmp2
rm tmp3
rm tmp4

### Rat
# Download rat GTF
wget ftp://ftp.ensembl.org/pub//release-88/gtf/rattus_norvegicus/Rattus_norvegicus.Rnor_6.0.88.gtf.gz
gunzip Rattus_norvegicus.Rnor_6.0.88.gtf.gz

# Keep only genome elements that are annotated to chromosomes 
awk '($1 == "1") || ($1 == "2") || ($1 == "3") || ($1 == "4") || ($1 == "5") || ($1 == "6") || ($1 == "7") || ($1 == "8") || ($1 == "9") || ($1 == "10") || ($1 == "11") || ($1 == "12") || ($1 == "13") || ($1 == "14")  || ($1 == "15") || ($1 == "16") || ($1 == "17") || ($1 == "18") || ($1 == "19") || ($1 == "20") || ($1 == "MT") || ($1 == "X")  || ($1 == "Y") { print $0}' Rattus_norvegicus.Rnor_6.0.88.gtf > tmp2

# Keep only genome elements annotated as "gene"
awk '($3 == "gene") { print $10"\t"$14}' tmp2 > tmp3

# Get gene Ensembl IDs and corresponding gene symbols
sed 's/\"//g' tmp3 > tmp4
sed 's/;//g' tmp4 > GeneIdTOGeneName_rat.txt

# Remove temporary variables
rm Rattus_norvegicus.Rnor_6.0.88.gtf
rm tmp2
rm tmp3
rm tmp4

### Human
wget ftp://ftp.ensembl.org/pub/release-96/gtf/homo_sapiens/Homo_sapiens.GRCh38.96.gtf.gz
gunzip Homo_sapiens.GRCh38.96.gtf.gz

# Get genes associated with chromosomes
awk '($1 == "1") || ($1 == "2") || ($1 == "3") || ($1 == "4") || ($1 == "5") || ($1 == "6") || ($1 == "7") || ($1 == "8") || ($1 == "9") || ($1 == "10") || ($1 == "11") || ($1 == "12") || ($1 == "13") || ($1 == "14") || ($1 == "15") || ($1 == "16") || ($1 == "17") || ($1 == "18") || ($1 == "19") || ($1 == "20")|| ($1 == "21") || ($1 == "22") || ($1 == "23") || ($1 == "MT") || ($1 == "X")  || ($1 == "Y") { print $0}' Homo_sapiens.GRCh38.96.gtf > tmp2
awk '($3 == "gene") { print $10"\t"$14}' tmp2 > tmp3

# Assign Ensembl ids to gene symbols
sed 's/\"//g' tmp3 > tmp4
sed 's/;//g' tmp4 > GeneIdTOGeneName_human.txt

# Remove temporary variables
rm Homo_sapiens.GRCh38.96.gtf
rm tmp2
rm tmp3
rm tmp4
