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(" <ul>\n"); struct trackDb *track = tdb; for ( ; track; track = track->next ) { + struct slName *el = slNameNew(track->track); + slAddHead(&retList, el); char *bigDataUrl = hashFindVal(track->settingsHash, "bigDataUrl"); char *compositeTrack = hashFindVal(track->settingsHash, "compositeTrack"); char *superTrack = hashFindVal(track->settingsHash, "superTrack"); boolean depthSearch = cartUsualBoolean(cart, "depthSearch", FALSE); if (compositeTrack) hashIncInt(countTracks, "composite container"); else if (superTrack) hashIncInt(countTracks, "superTrack container"); else if (isEmpty(track->type)) hashIncInt(countTracks, "no type specified"); else hashIncInt(countTracks, track->type); if (depthSearch && bigDataUrl) { char *bigDataIndex = NULL; @@ -410,104 +378,118 @@ { hPrintf(" <ul>\n"); struct hashEl *hel; struct hashCookie hc = hashFirst(countTracks); while ((hel = hashNext(&hc)) != NULL) { int prevCount = ptToInt(hashFindVal(trackCounter, hel->name)); totalTracks += ptToInt(hel->val); hashReplace(trackCounter, hel->name, intToPt(prevCount + ptToInt(hel->val))); hPrintf(" <li>%d - %s</li>\n", ptToInt(hel->val), hel->name); } hPrintf(" </ul>\n"); } hPrintf(" </ul>\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(" <ul>\n"); struct hashEl *hel; struct hashCookie hc = hashFirst(genome->settingsHash); while ((hel = hashNext(&hc)) != NULL) { 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); - trackList(tdb, genome); + retList = trackList(tdb, genome); } if (timeOutReached()) break; } hPrintf(" </ul>\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("<h4>genome sequences (and tracks) present in this track hub</h4>\n"); hPrintf("<ul>\n"); long lastTime = clock1000(); for ( ; genome; genome = genome->next ) { + if (selectGenome) /* is only one genome requested ? */ + { + if ( differentStringNullOk(selectGenome, genome->name) ) + continue; + } ++totalAssemblyCount; struct slName *el = slNameNew(genome->name); slAddHead(&retList, el); if (genome->organism) { hPrintf("<li>%s - %s - %s</li>\n", genome->organism, genome->name, genome->description); } else { /* can there be a description when organism is empty ? */ hPrintf("<li>%s</li>\n", genome->name); } if (genome->settingsHash) - assemblySettings(genome); + { + struct slName *trackList = assemblySettings(genome); + if (dbTrackList) + *dbTrackList = trackList; + } if (measureTiming) { long thisTime = clock1000(); hPrintf("<em>processing time %s: %ld millis</em><br>\n", genome->name, thisTime - lastTime); } if (timeOutReached()) break; } if (trackCounter->elCount) { - hPrintf(" <li>total assembly count: %ld</li>\n", totalAssemblyCount); + hPrintf(" <li>total genome assembly count: %ld</li>\n", totalAssemblyCount); hPrintf(" <li>%ld total tracks counted, %d different track types:</li>\n", totalTracks, trackCounter->elCount); hPrintf(" <ul>\n"); struct hashEl *hel; struct hashCookie hc = hashFirst(trackCounter); while ((hel = hashNext(&hc)) != NULL) { hPrintf(" <li>%d - %s - total</li>\n", ptToInt(hel->val), hel->name); } hPrintf(" </ul>\n"); } hPrintf("</ul>\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("<h2>Example URLs to return json data structures:</h2>\n"); -hPrintf("<ul>\n"); -hPrintf("<li><a href='/cgi-bin/hubApi/list/publicHubs'>list public hubs</a> <em>/cgi-bin/hubApi/list/publicHubs</em></li>\n"); -hPrintf("<li><a href='/cgi-bin/hubApi/list/genomes?hubUrl=%s'>list genomes from specified hub</a> <em>/cgi-bin/hubApi/list/genomes?hubUrl='%s'</em></li>\n", urlInput, urlInput); -hPrintf("</ul>\n"); long lastTime = clock1000(); struct trackHub *hub = trackHubOpen(urlInput, ""); if (measureTiming) { long thisTime = clock1000(); hPrintf("<em>hub open time: %ld millis</em><br>\n", thisTime - lastTime); } +struct trackHubGenome *hubGenome = hub->genomeList; + +hPrintf("<h2>Example URLs to return json data structures:</h2>\n"); +hPrintf("<ul>\n"); +hPrintf("<li><a href='/cgi-bin/hubApi/list/publicHubs'>list public hubs</a> <em>/cgi-bin/hubApi/list/publicHubs</em></li>\n"); +hPrintf("<li><a href='/cgi-bin/hubApi/list/genomes?hubUrl=%s'>list genomes from specified hub</a> <em>/cgi-bin/hubApi/list/genomes?hubUrl='%s'</em></li>\n", urlInput, urlInput); +hPrintf("<li><a href='/cgi-bin/hubApi/list/tracks?hubUrl=%s&genome=%s'>list tracks from specified hub and genome</a> <em>/cgi-bin/hubApi/list/genomes?hubUrl='%s&genome=%s'</em></li>\n", urlInput, hubGenome->name, urlInput, hubGenome->name); +hPrintf("</ul>\n"); + hPrintf("<h4>cart dump</h4>"); hPrintf("<pre>\n"); cartDump(cart); hPrintf("</pre>\n"); hPrintf("<form action='%s' name='hubApiUrl' id='hubApiUrl' method='GET'>\n\n", "../cgi-bin/hubApi"); hPrintf("<b>Select public hub: </b>"); #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<br>\n", allTrackSettings ? "TRUE" : "FALSE"); hPrintf("<br>\n</form>\n"); hPrintf("<p>URL: %s - %s<br>\n", urlInput, sameWord("go",goPublicHub) ? "public hub" : "other hub"); hPrintf("name: %s<br>\n", hub->shortLabel); hPrintf("description: %s<br>\n", hub->longLabel); hPrintf("default db: '%s'<br>\n", isEmpty(hub->defaultDb) ? "(none available)" : hub->defaultDb); printf("docRoot:'%s'<br>\n", docRoot); if (hub->genomeList) - (void) genomeList(hub); /* ignore returned list */ + (void) genomeList(hub, NULL, NULL); /* ignore returned list */ hPrintf("</p>\n"); if (timedOut) hPrintf("<h1>Reached time out %ld seconds</h1>", timeOutSeconds); if (measureTiming) hPrintf("<em>Overall total time: %ld millis</em><br>\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,};