#!/bin/bash
# sh extract.sh ../done data

DATA_DIR=$1
OUTPUT_DIR=$2
HERE=$(pwd)

OUTPUT_DIR=$(echo $OUTPUT_DIR | awk -v HERE=$HERE '{print (substr($1,1,1) == "/" ? $1 : HERE "/" $1)}')

[ -d $OUTPUT_DIR ] || mkdir $OUTPUT_DIR

#echo "reading $DATA_DIR/*, outputing in $OUTPUT_DIR"

VNTRS=$OUTPUT_DIR/vntrs
REFS=$OUTPUT_DIR/references
STATS=$OUTPUT_DIR/stats


[ -e $VNTRS ] && rm $VNTRS
[ -e $REFS ] && rm $REFS
[ -e $STATS ] && rm $STATS

cd $DATA_DIR

N=$(ls *.allwithsupport.span2.vcf | wc -l)
echo "total directories found=$N"

for file in *.allwithsupport.span2.vcf
do
  #wc -l $file/VNTRPIPE_*.allwithsupport.span2.vcf
  #wc -l $VNTRS
  name=$( echo $file | sed 's/_/\t/g' | sed 's/\./\t/g' | cut -f 2 | awk '{print toupper($1)}')
  total_vntrs=$(awk -F '\t' '{if(substr($1,1,1)!="#" && $7!="SC") print}' $file | grep -v 0/0 | grep -v 0/1/2 | grep -v 1/2/3 | wc -l )
  total_multis=$(awk -F '\t' '{if(substr($1,1,1)!="#" && $7!="SC") print}' $file | grep 0/1/2 | wc -l )
  tmp=$(awk -F '\t' '{if(substr($1,1,1)!="#" && $7!="SC") print}' $file | grep 1/2/3 | wc -l )
  total_multis=$(( $total_multis + tmp ))
  total_trs=$(awk -F '\t' '{if(substr($1,1,1)!="#" && $7!="SC") print}' $file | wc -l )
  read=$( grep '##referenceprofile' $file | sed 's/=/\t/g' | cut -f 2 | sed 's/"//g' | sed 's/\//\t/g' | awk '{print substr($NF,index($NF, "rl")+2,3)}')
  awk '{if (substr($1,1,1)!="#" && $7 == "PASS") print substr($3,3) "\t" $10}' $file |
    sed 's/:/\t/g' | grep -v '0/1/2' | grep -v '1/2/3' | cut -f 1,4 | sed 's/,/\t/g' |
    awk -v sample=$name -v R=$REFS -v V=$VNTRS '{if (NF==2) {print $1 "\t" sample >> R} else if(NF==3) {print $1 "\t" sample "\t" ($2=="." ? $3 "\t" $3 : $2 "\t" $3) >> V} else if(NF==4 && $2==".") {print $1 "\t" sample "\t" $3 "\t" $4  >> V}}'
  grep '##numreads' $file | sed 's/=/\t/g' | sed 's/"//g' |
    awk -v sample=$name -v L=$read -v T=$total_trs -v V=$total_vntrs -v M=$total_multis '{print sample "\t" L "\t" $2 "\t" T "\t" V "\t" M}' >> $STATS
done

cd $OUTPUT_DIR
ls -lh

