#!/bin/bash

### For debugging:
#set -x
#set -v

# exit if any statement returns a non-zero exit code
set -e

### If user specified help, or did not specify at least 4 args, then show usage
function usage
{
    echo "NAME"
    echo "	bior_rsid_to_tjson -- converts rsIDs into JSON as an additional column"
    echo ""
    echo "SYNOPSIS"
    echo "	bior_rsid_to_tjson [--log] [--help]"
    echo ""
    echo "DESCRIPTION"
    echo "	Takes rsIDs, one to a line, from STDIN and converts it into JSON as an additional"
    echo "	column that is output to STDOUT.  The added JSON column can then be utilized by other BioR"
    echo "	system commands."
    echo ""
    echo "	The options are as follows:"
    echo ""
    echo "	-d, --database <DATABASE>"
    echo "		The catalog file (bgzip format) containing the JSON data to lookup."
    echo ""
    echo "	-l, --log"
    echo "		Generate the log file 'bior.log'. By default, the log file is not generated."
    echo ""
    echo "	-h, --help"
    echo "		Print this message."
    echo ""
    echo "EXAMPLE"
    echo "	This is an example of converting an rsID into an additional JSON column.  The rsID"
    echo "	is a single Indel from dbSNP that has been trimmed for brevity and saved to a file named"
    echo "	rsids.txt, along with a header so the Column will have a valid and meaningful identifier."
    echo "	The bior_rsid_to_tjson command will parse the rsIDs piped into it and add an"
    echo "	extra JSON column onto the right that is then output to STDOUT."
    echo "	"
    echo "	cat rsids.txt"
    echo "	"
    echo "		#rsID"
    echo "		rs376643643"
    echo "	"
    echo "	cat rsids.txt | bior_rsid_to_tjson | bior_pretty_print"
    echo "	"
    echo "		#  COLUMN NAME    COLUMN VALUE"
    echo "		-  -----------    ------------"
    echo "		1  rsID           rs376643643"
    echo "		2  bior.dbSNP139  {"
    echo "		                    \"CHROM\": \"1\","
    echo "		                    \"POS\": \"10019\","
    echo "		                    \"ID\": \"rs376643643\","
    echo "		                    \"REF\": \"TA\","
    echo "		                    \"ALT\": \"T\","
    echo "		                    \"QUAL\": \".\","
    echo "		                    \"FILTER\": \".\","
    echo "		                    \"INFO\": {"
    echo "		                      \"RS\": 376643643,"
    echo "		                      \"RSPOS\": 10020,"
    echo "		                      \"dbSNPBuildID\": 138,"
    echo "		                      \"SSR\": 0,"
    echo "		                      \"SAO\": 0,"
    echo "		                      \"VP\": \"0x050000020001000002000200\","
    echo "		                      \"WGT\": 1,"
    echo "		                      \"VC\": \"DIV\","
    echo "		                      \"R5\": true,"
    echo "		                      \"OTHERKG\": true"
    echo "		                    },"
    echo "		                    \"_id\": \"rs376643643\","
    echo "		                    \"_type\": \"variant\","
    echo "		                    \"_landmark\": \"1\","
    echo "		                    \"_refAllele\": \"TA\","
    echo "		                    \"_altAlleles\": ["
    echo "		                      \"T\""
    echo "		                    ],"
    echo "		                    \"_minBP\": 10019,"
    echo "		                    \"_maxBP\": 10020"
    echo "		                  }"
    echo "		"
    echo ""	
    echo ""
}

# Resolve all of the parameters from the command line
log=""
catalog="$BIOR_CATALOG/dbSNP/142/00-All_GRCh37.tsv.bgz"
while [ "$1" != "" ] ; do
  case $1 in
    --help )
      usage
      exit 0
      ;;
    -h )
      usage
      exit 0
      ;;
    --log )
      log="--log "
      shift
      ;;
    -l )
      log="--log "
      shift
      ;;
    -d )
      shift
      catalog="$1"
      shift
      ;;
    --database )
      shift
      catalog="$1"
      shift
      ;;
   -* )
      echo "Unrecognized flag or option: $1"
      exit 1
      ;;
  esac
done

bior_lookup $log -p ID -d $catalog
