774479fda06428280150d609bdf4052f5f35d571
hiram
  Wed Feb 13 14:17:16 2019 -0800
list functions moved to list.c refs #18869

diff --git src/hg/hubApi/hubApi.c src/hg/hubApi/hubApi.c
index cb478e5..850fb5f 100644
--- src/hg/hubApi/hubApi.c
+++ src/hg/hubApi/hubApi.c
@@ -1,161 +1,101 @@
 /* hubApi - access mechanism to hub data resources. */
 #include "dataApi.h"
-#ifdef NOT
-#include "common.h"
-#include "linefile.h"
-#include "hash.h"
-#include "options.h"
-#include "jksql.h"
-#include "htmshell.h"
-#include "web.h"
-#include "cheapcgi.h"
-#include "cart.h"
-#include "hui.h"
-#include "udc.h"
-#include "knetUdc.h"
-#include "genbank.h"
-#include "trackHub.h"
-#include "hgConfig.h"
-#include "hCommon.h"
-#include "hPrint.h"
-#include "bigWig.h"
-#include "hubConnect.h"
-#include "obscure.h"
-#include "errCatch.h"
-#include "vcf.h"
-#include "bedTabix.h"
-#include "bamFile.h"
-#include "jsonParse.h"
-#include "jsonWrite.h"
-#include "chromInfo.h"
-
-#ifdef USE_HAL
-#include "halBlockViz.h"
-#endif
-
-#endif
 
 /*
 +------------------+------------------+------+-----+---------+-------+
 | Field            | Type             | Null | Key | Default | Extra |
 +------------------+------------------+------+-----+---------+-------+
 | hubUrl           | longblob         | NO   | PRI | NULL    |       |
 | shortLabel       | varchar(255)     | NO   |     | NULL    |       |
 | longLabel        | varchar(255)     | NO   |     | NULL    |       |
 | registrationTime | varchar(255)     | NO   |     | NULL    |       |
 | dbCount          | int(10) unsigned | NO   |     | NULL    |       |
 | dbList           | blob             | YES  |     | NULL    |       |
 | descriptionUrl   | longblob         | YES  |     | NULL    |       |
 +------------------+------------------+------+-----+---------+-------+
 */
 
+#ifdef NOT
+/*this definition should be over in hg/inc/hubPublic.h but that does not exist*/
 struct hubPublic
 /* Table of public track data hub connections. */
     {
     struct hubPublic *next;  /* Next in singly linked list. */
     char *hubUrl;	/* URL to hub.ra file */
     char *shortLabel;	/* Hub short label. */
     char *longLabel;	/* Hub long label. */
     char *registrationTime;	/* Time first registered */
     unsigned dbCount;	/* Number of databases hub has data for. */
     char *dbList;	/* Comma separated list of databases. */
     char *descriptionUrl;	/* URL to description HTML */
     };
+#endif
 
 /* Global Variables */
 static struct cart *cart;             /* CGI and other variables */
 static struct hash *oldVars = NULL;
 static struct hash *trackCounter = NULL;
 static long totalTracks = 0;
 static boolean measureTiming = FALSE;	/* set by CGI parameters */
 static boolean allTrackSettings = FALSE;	/* checkbox setting */
 static char **shortLabels = NULL;	/* public hub short labels in array */
 // struct hubPublic *publicHubList = NULL;
 static int publicHubCount = 0;
 static char *defaultHub = "Plants";
 static char *defaultDb = "ce11";
 static long enteredMainTime = 0;	/* will become = clock1000() on entry */
 		/* to allow calculation of when to bail out, taking too long */
 static long timeOutSeconds = 100;
 static boolean timedOut = FALSE;
 
 #ifdef NOT
-/* ######################################################################### */
-static struct jsonWrite *jsonStartOutput()
-/* begin json output with standard header information for all requests */
-{
-time_t timeNow = time(NULL);
-// struct tm tm;
-// gmtime_r(&timeNow, &tm);
-struct jsonWrite *jw = jsonWriteNew();
-jsonWriteObjectStart(jw, NULL);
-jsonWriteString(jw, "apiVersion", "0.1");
-jsonWriteString(jw, "source", "UCSantaCruz");
-jsonWriteDateFromUnix(jw, "downloadTime", (long long) timeNow);
-jsonWriteNumber(jw, "downloadTimeStamp", (long long) timeNow);
-return jw;
-}
-
-static void jsonErrAbort(char *format, ...)
-/* Issue an error message in json format, and exit(0) */
-{
-char errMsg[2048];
-va_list args;
-va_start(args, format);
-vsnprintf(errMsg, sizeof(errMsg), format, args);
-struct jsonWrite *jw = jsonStartOutput();
-jsonWriteString(jw, "error", errMsg);
-jsonWriteObjectEnd(jw);
-fputs(jw->dy->string,stdout);
-exit(0);
-}
-#endif
-
 static void hubPublicJsonData(struct jsonWrite *jw, struct hubPublic *el)
 /* Print array data for one row from hubPublic table, order here
  * must be same as was stated in the columnName header element
  *  TODO: need to figure out how to use the order of the columns as
  *        they are in the 'desc' request
  */
 {
 jsonWriteListStart(jw, NULL);
 jsonWriteString(jw, NULL, el->hubUrl);
 jsonWriteString(jw, NULL, el->shortLabel);
 jsonWriteString(jw, NULL, el->longLabel);
 jsonWriteString(jw, NULL, el->registrationTime);
 jsonWriteNumber(jw, NULL, (long long)el->dbCount);
 jsonWriteString(jw, NULL, el->dbList);
 jsonWriteString(jw, NULL, el->descriptionUrl);
 jsonWriteListEnd(jw);
 }
 
-int trackDbTrackCmp(const void *va, const void *vb)
+static int trackDbTrackCmp(const void *va, const void *vb)
 /* Compare to sort based on 'track' name; use shortLabel as secondary sort key.
  * Note: parallel code to hgTracks.c:tgCmpPriority */
 {
 const struct trackDb *a = *((struct trackDb **)va);
 const struct trackDb *b = *((struct trackDb **)vb);
 int dif = strcmp(a->track, b->track);
 if (dif < 0)
    return -1;
 else if (dif == 0.0)
    return strcasecmp(a->shortLabel, b->shortLabel);
 else
    return 1;
 }
 
+#endif
+
 static int publicHubCmpCase(const void *va, const void *vb)
 /* Compare two slNames, ignore case. */
 {
 const struct hubPublic *a = *((struct hubPublic **)va);
 const struct hubPublic *b = *((struct hubPublic **)vb);
 return strcasecmp(a->shortLabel, b->shortLabel);
 }
 
 static void publicHubSortCase(struct hubPublic **pList)
 /* Sort slName list, ignore case. */
 {
 slSort(pList, publicHubCmpCase);
 }
 
 static struct hubPublic *hubPublicLoad(char **row)
@@ -166,31 +106,31 @@
 
 AllocVar(ret);
 ret->hubUrl = cloneString(row[0]);
 ret->shortLabel = cloneString(row[1]);
 ret->longLabel = cloneString(row[2]);
 ret->registrationTime = cloneString(row[3]);
 ret->dbCount = sqlUnsigned(row[4]);
 ret->dbList = cloneString(row[5]);
 // if (row[6])
     ret->descriptionUrl = cloneString(row[6]);
 // else
 //     ret->descriptionUrl = cloneString("");
 return ret;
 }
 
-static struct hubPublic *hubPublicLoadAll()
+struct hubPublic *hubPublicLoadAll()
 {
 char query[1024];
 struct hubPublic *list = NULL;
 struct sqlConnection *conn = hConnectCentral();
 sqlSafef(query, sizeof(query), "select * from %s", hubPublicTableName());
 struct sqlResult *sr = sqlGetResult(conn, query);
 char **row;
 while ((row = sqlNextRow(sr)) != NULL)
     {
     struct hubPublic *el = hubPublicLoad(row);
     slAddHead(&list, el);
     }
 sqlFreeResult(&sr);
 hDisconnectCentral(&conn);
 publicHubSortCase(&list);
@@ -446,31 +386,31 @@
     {
     hPrintf("    <li>%s : %s</li>\n", hel->name, (char *)hel->val);
     if (sameWord("trackDb", hel->name))	/* examine the trackDb structure */
 	{
         struct trackDb *tdb = trackHubTracksForGenome(genome->trackHub, genome);
 	retTbd = tdb;
 	hubTrackList(tdb, genome);
         }
     if (timeOutReached())
 	break;
     }
 hPrintf("    </ul>\n");
 return retTbd;
 }
 
-static struct slName *genomeList(struct trackHub *hubTop, struct trackDb **dbTrackList, char *selectGenome)
+struct slName *genomeList(struct trackHub *hubTop, struct trackDb **dbTrackList, char *selectGenome)
 /* follow the pointers from the trackHub to trackHubGenome and around
  * in a circle from one to the other to find all hub resources
  * return slName list of the genomes in this track hub
  * optionally, return the trackList from this hub for the specified genome
  */
 {
 struct slName *retList = NULL;
 
 long totalAssemblyCount = 0;
 struct trackHubGenome *genome = hubTop->genomeList;
 
 hPrintf("<h4>genome sequences (and tracks) present in this track hub</h4>\n");
 hPrintf("<ul>\n");
 long lastTime = clock1000();
 for ( ; genome; genome = genome->next )
@@ -526,55 +466,57 @@
  * and then get the shortLabel
  */
 {
 char hubUrl[1024];
 char query[1024];
 struct sqlConnection *conn = hConnectCentral();
 // Build a query to select the hubUrl for the given shortLabel
 sqlSafef(query, sizeof(query), "select hubUrl from %s where shortLabel='%s'",
       hubPublicTableName(), shortLabel);
 if (! sqlQuickQuery(conn, query, hubUrl, sizeof(hubUrl)))
     hubUrl[0] = 0;
 hDisconnectCentral(&conn);
 return cloneString(hubUrl);
 }
 
+#ifdef NOT
 static void dbDbJsonData(struct jsonWrite *jw, struct dbDb *el)
 /* Print out dbDb table element in JSON format.
  * must be same as was stated in the columnName header element
  *  TODO: need to figure out how to use the order of the columns as
  *        they are in the 'desc' request
  */
 {
 jsonWriteListStart(jw, NULL);
 jsonWriteString(jw, NULL, el->name);
 jsonWriteString(jw, NULL, el->description);
 jsonWriteString(jw, NULL, el->nibPath);
 jsonWriteString(jw, NULL, el->organism);
 jsonWriteString(jw, NULL, el->defaultPos);
 jsonWriteNumber(jw, NULL, (long long)el->active);
 jsonWriteNumber(jw, NULL, (long long)el->orderKey);
 jsonWriteString(jw, NULL, el->genome);
 jsonWriteString(jw, NULL, el->scientificName);
 jsonWriteString(jw, NULL, el->htmlPath);
 jsonWriteNumber(jw, NULL, (long long)el->hgNearOk);
 jsonWriteNumber(jw, NULL, (long long)el->hgPbOk);
 jsonWriteString(jw, NULL, el->sourceName);
 jsonWriteNumber(jw, NULL, (long long)el->taxId);
 jsonWriteListEnd(jw);
 }
 
+
 static boolean tableColumns(struct jsonWrite *jw, char *table)
 /* output the column names for the given table
  * return: TRUE on error, FALSE on success
  */
 {
 jsonWriteListStart(jw, "columnNames");
 char query[1024];
 struct sqlConnection *conn = hConnectCentral();
 sqlSafef(query, sizeof(query), "desc %s", table);
 struct sqlResult *sr = sqlGetResult(conn, query);
 char **row;
 row = sqlNextRow(sr);
 if (NULL == row)
     {
     jsonErrAbort("ERROR: can not 'desc' table '%s'", table);
@@ -600,59 +542,61 @@
 struct jsonWrite *jw = jsonStartOutput();
 jsonWriteString(jw, "dataTime", dataTime);
 jsonWriteNumber(jw, "dataTimeStamp", (long long)dataTimeStamp);
 freeMem(dataTime);
 jsonWriteString(jw, "tableName", hubPublicTableName());
 tableColumns(jw, hubPublicTableName());
 jsonWriteListStart(jw, "publicHubData");
 for ( ; el != NULL; el = el->next )
     {
     hubPublicJsonData(jw, el);
     }
 jsonWriteListEnd(jw);
 jsonWriteObjectEnd(jw);
 fputs(jw->dy->string,stdout);
 }
+#endif
 
 static int dbDbCmpName(const void *va, const void *vb)
 /* Compare two dbDb elements: name, ignore case. */
 {
 const struct dbDb *a = *((struct dbDb **)va);
 const struct dbDb *b = *((struct dbDb **)vb);
 return strcasecmp(a->name, b->name);
 }
 
-static struct dbDb *ucscDbDb()
+struct dbDb *ucscDbDb()
 /* return the dbDb table as an slList */
 {
 char query[1024];
 struct sqlConnection *conn = hConnectCentral();
 sqlSafef(query, sizeof(query), "select * from dbDb");
 struct dbDb *dbList = NULL, *el = NULL;
 struct sqlResult *sr = sqlGetResult(conn, query);
 char **row;
 while ((row = sqlNextRow(sr)) != NULL)
     {
     el = dbDbLoad(row);
     slAddHead(&dbList, el);
     }
 sqlFreeResult(&sr);
 hDisconnectCentral(&conn);
 slSort(&dbList, dbDbCmpName);
 return dbList;
 }
 
+#ifdef NOT
 static void jsonDbDb()
 /* output the dbDb SQL table */
 {
 struct sqlConnection *conn = hConnectCentral();
 char *dataTime = sqlTableUpdate(conn, "dbDb");
 hDisconnectCentral(&conn);
 time_t dataTimeStamp = sqlDateToUnixTime(dataTime);
 replaceChar(dataTime, ' ', 'T');
 struct dbDb *dbList = ucscDbDb();
 struct dbDb *el;
 struct jsonWrite *jw = jsonStartOutput();
 jsonWriteString(jw, "dataTime", dataTime);
 jsonWriteNumber(jw, "dataTimeStamp", (long long)dataTimeStamp);
 freeMem(dataTime);
 jsonWriteString(jw, "tableName", "dbDb");
@@ -771,92 +715,30 @@
 char *dataTime = sqlTableUpdate(conn, "trackDb");
 time_t dataTimeStamp = sqlDateToUnixTime(dataTime);
 replaceChar(dataTime, ' ', 'T');
 hFreeConn(&conn);
 struct trackDb *tdbList = hTrackDb(db);
 struct jsonWrite *jw = jsonStartOutput();
 jsonWriteString(jw, "db", db);
 jsonWriteString(jw, "dataTime", dataTime);
 jsonWriteNumber(jw, "dataTimeStamp", (long long)dataTimeStamp);
 freeMem(dataTime);
 recursiveTrackList(jw, tdbList, "tracks");
 jsonWriteObjectEnd(jw);
 fputs(jw->dy->string,stdout);
 }	/*	static void trackDbJsonOutput(char *db, FILE *f)	*/
 
-#ifdef NOT
-static void getTrackData()
-{
-}
-
-static void getSequenceData()
-/* given at least a db=name and chrom=chr, optionally start and end  */
-{
-char *db = cgiOptionalString("db");
-char *chrom = cgiOptionalString("chrom");
-char *start = cgiOptionalString("start");
-char *end = cgiOptionalString("end");
-
-if (isEmpty(db))
-    jsonErrAbort("missing URL db=<ucscDb> name for endpoint '/getData/sequence");
-if (isEmpty(chrom))
-    jsonErrAbort("missing URL chrom=<name> for endpoint '/getData/sequence?db=%s", db);
-if (chromSeqFileExists(db, chrom))
-    {
-    struct chromInfo *ci = hGetChromInfo(db, chrom);
-    struct dnaSeq *seq = NULL;
-    if (isEmpty(start) || isEmpty(end))
-	seq = hChromSeqMixed(db, chrom, 0, 0);
-    else
-	seq = hChromSeqMixed(db, chrom, sqlSigned(start), sqlSigned(end));
-    if (NULL == seq)
-        jsonErrAbort("can not find sequence for chrom=%s for endpoint '/getData/sequence?db=%s&chrom=%s", chrom, db, chrom);
-    struct jsonWrite *jw = jsonStartOutput();
-    jsonWriteString(jw, "db", db);
-    jsonWriteString(jw, "chrom", chrom);
-    if (isEmpty(start) || isEmpty(end))
-	{
-        jsonWriteNumber(jw, "start", (long long)0);
-        jsonWriteNumber(jw, "end", (long long)ci->size);
-	}
-    else
-	{
-        jsonWriteNumber(jw, "start", (long long)sqlSigned(start));
-        jsonWriteNumber(jw, "end", (long long)sqlSigned(end));
-	}
-    jsonWriteString(jw, "dna", seq->dna);
-    jsonWriteObjectEnd(jw);
-    fputs(jw->dy->string,stdout);
-    freeDnaSeq(&seq);
-    }
-else
-    jsonErrAbort("can not find specified chrom=%s in sequence for endpoint '/getData/sequence?db=%s&chrom=%s", chrom, db, chrom);
-}
-#endif
-
-#define MAX_PATH_INFO 32
-static void apiGetData(char *words[MAX_PATH_INFO])
-/* 'getData' function, words[1] is the subCommand */
-{
-if (sameWord("track", words[1]))
-    getTrackData();
-else if (sameWord("sequence", words[1]))
-    getSequenceData();
-else
-    jsonErrAbort("do not recognize endpoint function: '/%s/%s'", words[0], words[1]);
-}
-
 static void apiList(char *words[MAX_PATH_INFO])
 /* 'list' function words[1] is the subCommand */
 {
 if (sameWord("publicHubs", words[1]))
     jsonPublicHubs();
 else if (sameWord("ucscGenomes", words[1]))
     jsonDbDb();
 else if (sameWord("hubGenomes", words[1]))
     {
     char *hubUrl = cgiOptionalString("hubUrl");
     if (isEmpty(hubUrl))
 	jsonErrAbort("must supply hubUrl='http:...' some URL to a hub for /list/hubGenomes");
 
     struct trackHub *hub = NULL;
     struct errCatch *errCatch = errCatchNew();
@@ -925,30 +807,31 @@
     char *hubUrl = cgiOptionalString("hubUrl");
 //    char *genome = cgiOptionalString("genome");
     char *db = cgiOptionalString("db");
     if (isEmpty(hubUrl) && isEmpty(db))
         jsonErrAbort("ERROR: must supply hubUrl or db name to return chromosome list");
 
     if (isEmpty(hubUrl))	// missing hubUrl implies UCSC database
 	{
         chromInfoJsonOutput(stdout, db);
 	return;
 	}
     }
 else
     jsonErrAbort("do not recognize endpoint function: '/%s/%s'", words[0], words[1]);
 }
+#endif
 
 static struct hash *apiFunctionHash = NULL;
 
 static void setupFunctionHash()
 /* initialize the apiFunctionHash */
 {
 if (apiFunctionHash)
     return;	/* already done */
 
 apiFunctionHash = hashNew(0);
 hashAdd(apiFunctionHash, "list", &apiList);
 hashAdd(apiFunctionHash, "getData", &apiGetData);
 }
 
 static void apiFunctionSwitch(char *pathInfo)