4cb561203efce23398fe8328be64e15dbb06e98d hiram Tue Jan 29 15:22:55 2019 -0800 now with /list/tracks function example refs #18869 diff --git src/hg/hubApi/hubApi.c src/hg/hubApi/hubApi.c index ef705a8..9ae29b3 100644 --- src/hg/hubApi/hubApi.c +++ src/hg/hubApi/hubApi.c @@ -11,30 +11,31 @@ #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" #ifdef USE_HAL #include "halBlockViz.h" #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 | | @@ -61,101 +62,65 @@ 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 */ static int publicHubCount = 0; static struct hubPublic *publicHubList = NULL; static char *defaultHub = "Plants"; 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; /* ######################################################################### */ -/* json output needs to encode special characters in strings: - " - quotation mark - / - forward slash - \ - back slash - \n - new line - \r - carriage return - \t - tab -*/ - -static char* jsonEscape(char *jsonString) -/* escape any of the special characters in the string for json output */ -{ -if (NULL == jsonString) - return NULL; -/* going to alternate the result string between a and b so the returned - * string from replaceChars() can be freemem'ed - * returned result from here should also be freemem'ed - */ -static char *a = NULL; -static char *b = NULL; -/* replace back slash first since the other encodings will be adding - * the back slash - */ -a = replaceChars(jsonString, "\\", "\\\\"); /* \ -> \\ */ -b = replaceChars(a, "\"", "\\\""); /* " -> \" */ -freeMem(a); -a = replaceChars(b, "/", "\\/"); /* / -> \/ */ -freeMem(b); -b = replaceChars(a, "\n", "\\\n"); /* \n -> \\n */ -freeMem(a); -a = replaceChars(b, "\r", "\\\r"); /* \r -> \\r */ -freeMem(b); -b = replaceChars(a, "\t", "\\\t"); /* \t -> \\t */ -return b; -} - static void jsonInteger(FILE *f, char *tag, int value) /* output one json interger: "tag":value appropriately quoted and encoded */ { fprintf(f,"\"%s\":%d",tag, value); } -static void jsonString(FILE *f, char *tag, char *value) +static void jsonStringOut(FILE *f, char *tag, char *value) /* output one json string: "tag":"value" appropriately quoted and encoded */ { fprintf(f,"\"%s\":",tag); -char *a = jsonEscape(value); +char *a = jsonStringEscape(value); if (isEmpty(a)) fprintf(f, "%s", "null"); else fprintf(f, "\"%s\"", a); freeMem(a); } static void hubPublicJsonOutput(struct hubPublic *el, FILE *f) /* Print out hubPublic element in JSON format. */ { fputc('{',f); -jsonString(f, "hubUrl", el->hubUrl); +jsonStringOut(f, "hubUrl", el->hubUrl); fputc(',',f); -jsonString(f, "shortLabel", el->shortLabel); +jsonStringOut(f, "shortLabel", el->shortLabel); fputc(',',f); -jsonString(f, "longLabel", el->longLabel); +jsonStringOut(f, "longLabel", el->longLabel); fputc(',',f); -jsonString(f, "registrationTime", el->registrationTime); +jsonStringOut(f, "registrationTime", el->registrationTime); fputc(',',f); jsonInteger(f, "dbCount", el->dbCount); fputc(',',f); -jsonString(f, "dbList", el->dbList); +jsonStringOut(f, "dbList", el->dbList); fputc(',',f); -jsonString(f, "descriptionUrl", el->descriptionUrl); +jsonStringOut(f, "descriptionUrl", el->descriptionUrl); fputc('}',f); } 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); @@ -330,40 +295,43 @@ retVal = 1; } } errCatchEnd(errCatch); if (errCatch->gotError) { retVal = 1; dyStringPrintf(errors, "%s", errCatch->message->string); } errCatchFree(&errCatch); return retVal; } /* static int bbiBriefMeasure() */ -static void trackList(struct trackDb *tdb, struct trackHubGenome *genome) +static struct slName *trackList(struct trackDb *tdb, struct trackHubGenome *genome) /* process the track list to show all tracks */ { +struct slName *retList = NULL; /* for return of track list for 'genome' */ if (tdb) { struct hash *countTracks = hashNew(0); hPrintf(" \n"); } -} /* static void trackList(struct trackDb *tdb) */ +return retList; +} /* static struct slName *trackList() */ -static void assemblySettings(struct trackHubGenome *genome) +static struct slName *assemblySettings(struct trackHubGenome *genome) /* display all the assembly 'settingsHash' */ { +struct slName *retList = NULL; hPrintf(" \n"); +return retList; } -static struct slName *genomeList(struct trackHub *hubTop) +static struct slName *genomeList(struct trackHub *hubTop, struct slName **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("

genome sequences (and tracks) present in this track hub

\n"); hPrintf("\n"); return retList; -} /* static struct slName *genomeList (struct trackHub *hubTop) */ +} /* static struct slName *genomeList () */ static char *urlFromShortLabel(char *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); } @@ -521,76 +503,112 @@ if (el->next) printf(","); } printf("]}\n"); } #define MAX_PATH_INFO 32 static void apiList(char *words[MAX_PATH_INFO]) /* 'list' function */ { if (sameWord("publicHubs", words[1])) jsonPublicHubs(); else if (sameWord("genomes", words[1])) { char *hubUrl = cgiOptionalString("hubUrl"); - if (isNotEmpty(hubUrl)) - { + if (isEmpty(hubUrl)) + errAbort("# must supply hubUrl='http:...' some URL to a hub for /list/genomes\n"); + struct trackHub *hub = trackHubOpen(hubUrl, ""); if (hub->genomeList) { fputc('{',stdout); - jsonString(stdout, "hubUrl", hubUrl); + jsonStringOut(stdout, "hubUrl", hubUrl); fputc(',',stdout); printf("\"genomes\":["); - struct slName *theList = genomeList(hub); + struct slName *theList = genomeList(hub, NULL, NULL); slNameSort(&theList); struct slName *el = theList; for ( ; el ; el = el->next ) { - char *n = jsonEscape(el->name); - printf("\"%s\"", n); + char *a = jsonStringEscape(el->name); + printf("\"%s\"", a); + freeMem(a); if (el->next) fputc(',',stdout); } printf("]}\n"); } } - else - errAbort("# must supply hubUrl='http:...' some URL to a hub for /list/genomes\n"); +else if (sameWord("tracks", words[1])) + { + char *hubUrl = cgiOptionalString("hubUrl"); + char *genome = cgiOptionalString("genome"); + if (isEmpty(genome) || isEmpty(hubUrl)) + { + if (isEmpty(genome)) + warn("# must supply genome='someName' the name of a genome in a hub for /list/tracks\n"); + if (isEmpty(hubUrl)) + warn("# must supply hubUrl='http:...' some URL to a hub for /list/genomes\n"); + errAbort("# ERROR exit"); + } + struct trackHub *hub = trackHubOpen(hubUrl, ""); + if (hub->genomeList) + { + struct slName *dbTrackList = NULL; + (void) genomeList(hub, &dbTrackList, genome); + fputc('{',stdout); + jsonStringOut(stdout, "hubUrl", hubUrl); + fputc(',',stdout); + jsonStringOut(stdout, "genome", genome); + fputc(',',stdout); + printf("\"tracks\":["); + slNameSort(&dbTrackList); + struct slName *el = dbTrackList; + for ( ; el ; el = el->next ) + { + char *a = jsonStringEscape(el->name); + printf("\"%s\"", a); + freeMem(a); + if (el->next) + fputc(',',stdout); + } + printf("]}\n"); + } } else - errAbort("# ERROR: do not recognize '%s' for 'list' function\n", words[1]); + errAbort("# ERROR: do not recognize command '%s' for 'list' function\n", words[1]); } static struct hash *apiFunctionHash = NULL; static void setupFunctionHash() +/* initialize the apiFunctionHash */ { if (apiFunctionHash) return; apiFunctionHash = hashNew(0); hashAdd(apiFunctionHash, "list", &apiList); } static void apiFunctionSwitch(char *pathInfo) /* given a pathInfo string: /command/subCommand/etc... * parse that and decide on which function to acll */ { -hPrintDisable(); /* turn off all normal HTML output */ +hPrintDisable(); /* turn off all normal HTML output, doing JSON output */ /* the leading slash has been removed from the pathInfo, therefore, the * chop will have the first word in words[0] */ char *words[MAX_PATH_INFO];/*expect no more than MAX_PATH_INFO number of words*/ int wordCount = chopByChar(pathInfo, '/', words, ArraySize(words)); if (wordCount < 2) errAbort("ERROR: no commands found in path info\n"); void (*apiFunction)(char **) = hashMustFindVal(apiFunctionHash, words[0]); (*apiFunction)(words); } @@ -625,43 +643,47 @@ apiFunctionSwitch(pathInfo); return; } cartWebStart(cart, database, "access mechanism to hub data resources"); char *goOtherHub = cartUsualString(cart, "goOtherHub", defaultHub); char *otherHubUrl = cartUsualString(cart, "urlHub", defaultHub); char *goPublicHub = cartUsualString(cart, "goPublicHub", defaultHub); char *hubDropDown = cartUsualString(cart, "publicHubs", defaultHub); char *urlDropDown = urlFromShortLabel(hubDropDown); char *urlInput = urlDropDown; /* assume public hub */ if (sameWord("go", goOtherHub)) /* requested other hub URL */ urlInput = otherHubUrl; -hPrintf("

Example URLs to return json data structures:

\n"); -hPrintf("\n"); long lastTime = clock1000(); struct trackHub *hub = trackHubOpen(urlInput, ""); if (measureTiming) { long thisTime = clock1000(); hPrintf("hub open time: %ld millis
\n", thisTime - lastTime); } +struct trackHubGenome *hubGenome = hub->genomeList; + +hPrintf("

Example URLs to return json data structures:

\n"); +hPrintf("\n"); + hPrintf("

cart dump

"); hPrintf("
\n");
 cartDump(cart);
 hPrintf("
\n"); hPrintf("
\n\n", "../cgi-bin/hubApi"); hPrintf("Select public hub: "); #define JBUFSIZE 2048 char javascript[JBUFSIZE]; struct slPair *events = NULL; safef(javascript, sizeof(javascript), "this.lastIndex=this.selectedIndex;"); slPairAdd(&events, "focus", cloneString(javascript)); #define SMALLBUF 256 // char class[SMALLBUF]; @@ -685,31 +707,31 @@ hPrintf("\n  "); allTrackSettings = cartUsualBoolean(cart, "allTrackSettings", FALSE); hCheckBox("allTrackSettings", allTrackSettings); hPrintf(" display all track settings for each track : %s
\n", allTrackSettings ? "TRUE" : "FALSE"); hPrintf("
\n
\n"); hPrintf("

URL: %s - %s
\n", urlInput, sameWord("go",goPublicHub) ? "public hub" : "other hub"); hPrintf("name: %s
\n", hub->shortLabel); hPrintf("description: %s
\n", hub->longLabel); hPrintf("default db: '%s'
\n", isEmpty(hub->defaultDb) ? "(none available)" : hub->defaultDb); printf("docRoot:'%s'
\n", docRoot); if (hub->genomeList) - (void) genomeList(hub); /* ignore returned list */ + (void) genomeList(hub, NULL, NULL); /* ignore returned list */ hPrintf("

\n"); if (timedOut) hPrintf("

Reached time out %ld seconds

", timeOutSeconds); if (measureTiming) hPrintf("Overall total time: %ld millis
\n", clock1000() - enteredMainTime); cartWebEnd(); } /* void doMiddle(struct cart *theCart) */ /* Null terminated list of CGI Variables we don't want to save * permanently. */ char *excludeVars[] = {"Submit", "submit", NULL,};