#!/bin/sh

# ===========================================================================
#
#                            PUBLIC DOMAIN NOTICE
#            National Center for Biotechnology Information (NCBI)
#
#  This software/database is a "United States Government Work" under the
#  terms of the United States Copyright Act.  It was written as part of
#  the author's official duties as a United States Government employee and
#  thus cannot be copyrighted.  This software/database is freely available
#  to the public for use. The National Library of Medicine and the U.S.
#  Government do not place any restriction on its use or reproduction.
#  We would, however, appreciate having the NCBI and the author cited in
#  any work or product based on this material.
#
#  Although all reasonable efforts have been taken to ensure the accuracy
#  and reliability of the software and data, the NLM and the U.S.
#  Government do not and cannot warrant the performance or results that
#  may be obtained by using this software or data. The NLM and the U.S.
#  Government disclaim all warranties, express or implied, including
#  warranties of performance, merchantability or fitness for any particular
#  purpose.
#
# ===========================================================================
#
# File Name:  bt-srch
#
# Author:  Jonathan Kans
#
# Version Creation Date:   2/6/19
#
# ==========================================================================

#  Front-end for expanding Entrez Direct style of command-line navigation to
#  external resources maintained by BioThings.io.  See comments in xplore
#  script for credits and references.

if [ "$#" -lt 1 ]
then
  echo "Must supply target argument"
  exit 1
fi

dst="$1"
shift

DoWrap() {
  echo "<opt>"
  sort -n | uniq -i |
  while read uid
  do
    echo "<Id>$uid</Id>"
  done
  echo "</opt>"
}

DoLoad() {
  xtract -wrp ENTREZ_EXTEND -pattern opt -wrp Type -lbl "$dst" \
    -wrp Count -num Id -wrp Id -encode Id |
  xtract -format
}

DoSearch() {
  url="$1"
  que="$2"
  res=$(nquire -get -j2x -url "$url" query -q "$que" -fetch_all TRUE)
  sid=$(echo "$res" | xtract -pattern opt -element @_scroll_id)
  ttl=$(echo "$res" | xtract -pattern opt -element @total)
  ids=$(echo "$res" | xtract -pattern opt -block hits -tab "\n" -element @_id)
  while :
  do
    echo "$ids"
    nid=$(echo "$ids" | wc -l)
    ttl=$((ttl - nid))
    if [ $ttl -lt 1 ]
    then
      break
    fi
    res=$(nquire -get -j2x -url "http://mygene.info/v3" query -scroll_id "$sid")
    ids=$(echo "$res" | xtract -pattern opt -block hits -tab "\n" -element @_id)
  done
}

case "$dst" in
  ncbigene )
    DoSearch "http://mygene.info/v3" "$@" |
    DoWrap |
    DoLoad
    break
    ;;
  hgvs )
    DoSearch "http://myvariant.info/v1" "$@" |
    DoWrap |
    xtract -wrp ENTREZ_EXTEND -pattern opt -wrp Type -lbl hgvs \
      -wrp Count -num Id -wrp Id -encode Id |
    xtract -format
    break
    ;;
  inchikey )
    DoSearch "http://mychem.info/v1" "$@" |
    DoWrap |
    DoLoad
    break
    ;;
  * )
    exec >&2
    echo "$0: Unrecognized target $dst"
    exit 1
    ;;
esac
