#!/bin/bash

ANNOTDIR=/mnt/nas05/togovar/public/downloads/release/20241203/grch38/annotation
NEW_HEADER="tgv_id\trs\tchr\tposition_grch38\tref\talt\tsymbol\ttranscript_id\tconsequence\tsift_qualitative_prediction\tsift_score\tpolyphen2_qualitative_prediction\tpolyphen2_score\talphamissense_pathogenicity\talphamissense_score\tCLNSIG\tCLNDISDB\tCLNDN\tCLNREVSTAT\tCLNSIGSCV"

for chr in $(seq 1 22) X Y MT; do
  printf "Processing chr$chr ..."
  annot=$ANNOTDIR/chr_${chr}_molecular_annotation.tsv.gz 

  awk -F '\t' -v NEW_HEADER="tgv_id\trs\tchr\tposition_grch38\tref\talt\tsymbol\ttranscript_id\tconsequence\tsift_qualitative_prediction\tsift_score\tpolyphen2_qualitative_prediction\tpolyphen2_score\talphamissense_pathogenicity\talphamissense_score\tCLNSIG\tCLNDISDB\tCLNDN\tCLNREVSTAT\tCLNSIGSCV" '
NR==FNR { key = $1 FS $2 FS $3 FS $4; a[key] = $5 FS $6 FS $7 FS $8 FS $9; next }
FNR==1 { print NEW_HEADER; next }
{
  key = $3 FS $4 FS $5 FS $6;
  print $0, "\t", (key in a ? a[key] : "-\t-\t-\t-\t-") 
}' clinvar.txt <(gunzip -c $annot) | gzip > ../chr_${chr}_molecular_annoation_plus_clinvar.tsv.gz
  echo "Finished"
done

