aa487ff67c04e2961d0ff4b5ba1d4568fa1d6f17 hiram Fri Mar 15 15:51:40 2019 -0700 now using source tree version tag and alpha install rule to cgi-bin-api and cleaned up html errors refs #18869 diff --git src/hg/hubApi/hubApi.c src/hg/hubApi/hubApi.c index 6f40732..4cbfffb 100644 --- src/hg/hubApi/hubApi.c +++ src/hg/hubApi/hubApi.c @@ -4,30 +4,31 @@ /* +------------------+------------------+------+-----+---------+-------+ | 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 | | +------------------+------------------+------+-----+---------+-------+ */ /* Global Variables */ +static boolean debug = TRUE; 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; @@ -97,36 +98,36 @@ * is appropriate at this time */ { long nowTime = clock1000(); timedOut = FALSE; if ((nowTime - enteredMainTime) > (1000 * timeOutSeconds)) timedOut= TRUE; return timedOut; } #ifdef NOT static void showCounts(struct hash *countTracks) { if (countTracks->elCount) { - hPrintf(" \n"); } } #endif static void hashCountTrack(struct trackDb *tdb, struct hash *countTracks) /* this is counting up track types into the hash countTracks */ { char *stripType = cloneString(tdb->type); if (startsWith("chain ", tdb->type)) stripType = cloneString("chain"); else if (startsWith("netAlign ", tdb->type)) stripType = cloneString("netAlign"); else if (startsWith("genePred ", tdb->type)) stripType = cloneString("genePred"); else if (startsWith("bigWig ", tdb->type)) @@ -154,58 +155,58 @@ } else if (isEmpty(tdb->type)) hashIncInt(countTracks, "no type specified"); else { hashIncInt(countTracks, stripType); hashIncInt(countTracks, "track count"); } freeMem(stripType); // showCounts(countTracks); } static void trackSettings(struct trackDb *tdb, struct hash *countTracks) /* process the settingsHash for a trackDb, recursive when subtracks */ { -hPrintf(" \n"); } static int bbiBriefMeasure(char *type, char *bigDataUrl, char *bigDataIndex, long *chromCount, long *itemCount, struct dyString *errors) /* check a bigDataUrl to find chrom count and item count */ { int retVal = 0; *chromCount = 0; *itemCount = 0; struct errCatch *errCatch = errCatchNew(); if (errCatchStart(errCatch)) { if (startsWithWord("bigNarrowPeak", type) || startsWithWord("bigBed", type) || startsWithWord("bigGenePred", type) || startsWithWord("bigPsl", type) @@ -326,99 +327,100 @@ else { if (compositeContainer) hPrintf("
  • %s : %s : composite track container
  • \n", tdb->track, tdb->type); else if (compositeView) hPrintf("
  • %s : %s : composite view of parent: %s
  • \n", tdb->track, tdb->type, tdb->parent->track); else if (superChild) hPrintf("
  • %s : %s : superTrack child of parent: %s
  • \n", tdb->track, tdb->type, tdb->parent->track); else if (! depthSearch) hPrintf("
  • %s : %s : %s
  • \n", tdb->track, tdb->type, bigDataUrl); else hPrintf("
  • %s : %s
  • \n", tdb->track, tdb->type); } if (allTrackSettings) { - hPrintf(" \n"); } return; } /* static void countOneTdb(struct trackDb *tdb, * char *bigDataIndex, struct hash *countTracks) */ static void hubTrackList(struct trackDb *topTrackDb, struct trackHubGenome *genome) /* process the track list in a hub to show all tracks */ { +hPrintf("
  • \n"); } /* static struct trackDb *hubTrackList() */ static struct trackDb *assemblySettings(struct trackHubGenome *genome) /* display all the assembly 'settingsHash' */ { struct trackDb *tdb = obtainTdb(genome, NULL); -hPrintf("
  • \n"); return tdb; } 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; @@ -437,47 +439,47 @@ slAddHead(&retList, el); if (genome->organism) { hPrintf("
  • %s - %s - %s
  • \n", genome->organism, genome->name, genome->description); } else { /* can there be a description when organism is empty ? */ hPrintf("
  • %s
  • \n", genome->name); } struct trackDb *tdb = assemblySettings(genome); if (dbTrackList) *dbTrackList = tdb; if (measureTiming) { long thisTime = clock1000(); - hPrintf("processing time %s: %ld millis
    \n", genome->name, thisTime - lastTime); + hPrintf("
  • processing time %s: %ld millis
  • \n", genome->name, thisTime - lastTime); } if (timeOutReached()) break; } if (trackCounter->elCount) { hPrintf("
  • total genome assembly count: %ld
  • \n", totalAssemblyCount); hPrintf("
  • %ld total tracks counted, %d different track types:
  • \n", totalTracks, trackCounter->elCount); - hPrintf("
      \n"); + hPrintf("
      1. \n"); struct hashEl *hel, *helList = hashElListHash(trackCounter); slSort(&helList, hashElCmpIntValDesc); for (hel = helList; hel; hel = hel->next) { hPrintf("
      2. %d - %s - total
      3. \n", ptToInt(hel->val), hel->name); } - hPrintf("
      \n"); + hPrintf("
    \n"); } hPrintf("\n"); return retList; } /* static struct slName *genomeList () */ static char *urlFromShortLabel(char *shortLabel) /* this is not a fair way to get the URL since shortLabel's are not * necessarily unique. This is temporary. TBD: need to always use URL * 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 @@ -518,54 +520,75 @@ } 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) +static struct hashEl *parsePathInfo(char *pathInfo, char *words[MAX_PATH_INFO]) +/* given a pathInfo string: /command/subCommand/etc... + * parse that and return a function pointer and the parsed words + * Returns NULL if not recognized + */ +{ +char *tmp = cloneString(pathInfo); +/* skip the first leading slash to simplify chopByChar parsing */ +tmp += 1; +int wordCount = chopByChar(tmp, '/', words, MAX_PATH_INFO); +if (wordCount < 1) + return NULL; + +struct hashEl *hel = hashLookup(apiFunctionHash, words[0]); +return hel; +} + +#ifdef NOT +static void apiFunctionSwitch(struct hashEl *hel, char *words[MAX_PATH_INFO]) /* given a pathInfo string: /command/subCommand/etc... * parse that and decide on which function to acll */ { 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] */ +#ifdef NOT 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) apiErrAbort("unknown endpoint command: '/%s'", pathInfo); struct hashEl *hel = hashLookup(apiFunctionHash, words[0]); if (hel == NULL) apiErrAbort("no such command: '%s' for endpoint '/%s'", words[0], pathInfo); +#endif void (*apiFunction)(char **) = hel->val; // void (*apiFunction)(char **) = hashMustFindVal(apiFunctionHash, words[0]); (*apiFunction)(words); } /* static void apiFunctionSwitch(char *pathInfo) */ +#endif static void tracksForUcscDb(char *db) /* scan the specified database for all tracks */ { struct hash *countTracks = hashNew(0); hPrintf("

    Tracks in UCSC genome: '%s'
    \n", db); struct trackDb *tdbList = obtainTdb(NULL, db); struct trackDb *tdb; hPrintf("

    \n"); hPrintf("

    \n"); } // static void tracksForUcscDb(char * db) static void showExamples(char *url, struct trackHubGenome *hubGenome, char *ucscDb) { hPrintf("

    Example URLs to return json data structures:

    \n"); hPrintf("

    listing functions

    \n"); hPrintf("
      \n"); -hPrintf("
    1. list public hubs /cgi-bin/hubApi/list/publicHubs
    2. \n"); -hPrintf("
    3. list database genomes /cgi-bin/hubApi/list/ucscGenomes
    4. \n"); -hPrintf("
    5. list genomes from specified hub /cgi-bin/hubApi/list/hubGenomes?hubUrl=%s
    6. \n", url, url); -hPrintf("
    7. list tracks from specified hub and genome /cgi-bin/hubApi/list/tracks?hubUrl=%s&genome=%s
    8. \n", url, url, hubGenome->name, url, hubGenome->name); -hPrintf("
    9. list tracks from specified UCSC database /cgi-bin/hubApi/list/tracks?db=%s
    10. \n", ucscDb, ucscDb); -hPrintf("
    11. list chromosomes from specified UCSC database /cgi-bin/hubApi/list/chromosomes?db=%s
    12. \n", ucscDb, ucscDb); -hPrintf("
    13. list chromosomes from specified track from UCSC databaset /cgi-bin/hubApi/list/chromosomes?db=%s&track=gap
    14. \n", ucscDb, ucscDb); +hPrintf("
    15. list public hubs /list/publicHubs
    16. \n"); +hPrintf("
    17. list database genomes /list/ucscGenomes
    18. \n"); +hPrintf("
    19. list genomes from specified hub /list/hubGenomes?hubUrl=%s
    20. \n", url, url); +hPrintf("
    21. list tracks from specified hub and genome /list/tracks?hubUrl=%s&genome=%s
    22. \n", url, url, hubGenome->name, url, hubGenome->name); +hPrintf("
    23. list tracks from specified UCSC database /list/tracks?db=%s
    24. \n", ucscDb, ucscDb); +hPrintf("
    25. list chromosomes from specified UCSC database /list/chromosomes?db=%s
    26. \n", ucscDb, ucscDb); +hPrintf("
    27. list chromosomes from specified track from UCSC databaset /list/chromosomes?db=%s&track=gap
    28. \n", ucscDb, ucscDb); hPrintf("
    \n"); hPrintf("

    getData functions

    \n"); hPrintf("
      \n"); -hPrintf("
    1. get sequence from specified database and chromosome /cgi-bin/hubApi/getData/sequence?db=%s&chrom=chrM
    2. \n", ucscDb, ucscDb); -hPrintf("
    3. get sequence from specified database, chromosome with start,end coordinates /cgi-bin/hubApi/getData/sequence?db=%s&chrom=chrM&start=0&end=128
    4. \n", ucscDb, ucscDb); -hPrintf("
    5. get entire track data from specified database and track name (gold == Assembly) /cgi-bin/hubApi/getData/track?db=%s&track=gold
    6. \n", ucscDb, ucscDb); -hPrintf("
    7. get track data from specified database, chromosome and track name (gold == Assembly) /cgi-bin/hubApi/getData/track?db=%s&chrom=chrM&track=gold
    8. \n", ucscDb, ucscDb); -hPrintf("
    9. get track data from specified database, chromosome, track name, start and end coordinates /cgi-bin/hubApi/getData/track?db=%s&chrom=chrI&track=gold&start=107680&end=186148
    10. \n", defaultDb, defaultDb); -hPrintf("
    11. get entire track data from specified hub and track name /cgi-bin/hubApi/getData/track?hubUrl=http://genome-test.gi.ucsc.edu/~hiram/hubs/GillBejerano/hub.txt&genome=hg19&track=ultraConserved
    12. \n"); -hPrintf("
    13. get track data from specified hub, chromosome and track name (full chromosome) /cgi-bin/hubApi/getData/track?hubUrl=http://genome-test.gi.ucsc.edu/~hiram/hubs/Plants/hub.txt&genome=_araTha1&chrom=chrCp&track=assembly_
    14. \n"); -hPrintf("
    15. get track data from specified hub, chromosome, track name, start and end coordinates /cgi-bin/hubApi/getData/track?hubUrl=http://genome-test.gi.ucsc.edu/~hiram/hubs/Plants/hub.txt&genome=_araTha1&chrom=chr1&track=assembly_&start=0&end=14309681
    16. \n"); -hPrintf("
    17. get all track data from specified hub and track name /cgi-bin/hubApi/getData/track?hubUrl=http://genome-test.gi.ucsc.edu/~hiram/hubs/Plants/hub.txt&genome=_araTha1&track=gc5Base
    18. \n"); -hPrintf("
    19. get track data from specified hub, chromosome, track name, start and end coordinates /cgi-bin/hubApi/getData/track?hubUrl=http://genome-test.gi.ucsc.edu/~hiram/hubs/Plants/hub.txt&genome=_araTha1&chrom=chrMt&track=gc5Base&start=143600&end=143685
    20. \n"); -hPrintf("
    21. get bigWig track data from specified database, chromosome, track name, start and end coordinates /cgi-bin/hubApi/getData/track?db=%s&chrom=chrI&track=gc5BaseBw&start=107680&end=186148
    22. \n", defaultDb, defaultDb); -hPrintf("
    23. get bigBed track data from specified database, chromosome, track name, start and end coordinates /cgi-bin/hubApi/getData/track?db=%s&chrom=chrII&track=ncbiRefSeqOther&start=14334626&end=14979625
    24. \n", defaultDb, defaultDb); +hPrintf("
    25. get sequence from specified database and chromosome /getData/sequence?db=%s&chrom=chrM
    26. \n", ucscDb, ucscDb); +hPrintf("
    27. get sequence from specified database, chromosome with start,end coordinates /getData/sequence?db=%s&chrom=chrM&start=0&end=128
    28. \n", ucscDb, ucscDb); +hPrintf("
    29. get entire track data from specified database and track name (gold == Assembly) /getData/track?db=%s&track=gold
    30. \n", ucscDb, ucscDb); +hPrintf("
    31. get track data from specified database, chromosome and track name (gold == Assembly) /getData/track?db=%s&chrom=chrM&track=gold
    32. \n", ucscDb, ucscDb); +hPrintf("
    33. get track data from specified database, chromosome, track name, start and end coordinates /getData/track?db=%s&chrom=chrI&track=gold&start=107680&end=186148
    34. \n", defaultDb, defaultDb); +hPrintf("
    35. get entire track data from specified hub and track name /getData/track?hubUrl=http://genome-test.gi.ucsc.edu/~hiram/hubs/GillBejerano/hub.txt&genome=hg19&track=ultraConserved
    36. \n"); +hPrintf("
    37. get track data from specified hub, chromosome and track name (full chromosome) /getData/track?hubUrl=http://genome-test.gi.ucsc.edu/~hiram/hubs/Plants/hub.txt&genome=_araTha1&chrom=chrCp&track=assembly_
    38. \n"); +hPrintf("
    39. get track data from specified hub, chromosome, track name, start and end coordinates /getData/track?hubUrl=http://genome-test.gi.ucsc.edu/~hiram/hubs/Plants/hub.txt&genome=_araTha1&chrom=chr1&track=assembly_&start=0&end=14309681
    40. \n"); +hPrintf("
    41. get all track data from specified hub and track name /getData/track?hubUrl=http://genome-test.gi.ucsc.edu/~hiram/hubs/Plants/hub.txt&genome=_araTha1&track=gc5Base
    42. \n"); +hPrintf("
    43. get track data from specified hub, chromosome, track name, start and end coordinates /getData/track?hubUrl=http://genome-test.gi.ucsc.edu/~hiram/hubs/Plants/hub.txt&genome=_araTha1&chrom=chrMt&track=gc5Base&start=143600&end=143685
    44. \n"); +hPrintf("
    45. get bigWig track data from specified database, chromosome, track name, start and end coordinates /getData/track?db=%s&chrom=chrI&track=gc5BaseBw&start=107680&end=186148
    46. \n", defaultDb, defaultDb); +hPrintf("
    47. get bigBed track data from specified database, chromosome, track name, start and end coordinates /getData/track?db=%s&chrom=chrII&track=ncbiRefSeqOther&start=14334626&end=14979625
    48. \n", defaultDb, defaultDb); +hPrintf("
    49. get narrowPeak track data from specified database, chromosome, track name, start and end coordinates /getData/track?db=%s&chrom=chr1&track=wgEncodeAwgDnaseDuke8988tUniPk&start=14334626&end=14979625
    50. \n", "hg19", "hg19"); +hPrintf("
    51. get broadPeak track data from specified database, chromosome, track name, start and end coordinates /getData/track?db=%s&chrom=chr1&track=wgEncodeBroadHistoneOsteoP300kat3bPk&start=14334626&end=14979625
    52. \n", "hg19", "hg19"); hPrintf("
    \n"); hPrintf("

    Example URLs to generate errors:

    \n"); -hPrintf("
  • get track data from specified hub, chromosome, track name, start and end coordinates /cgi-bin/hubApi/getData/track?hubUrl=http://genome-test.gi.ucsc.edu/~hiram/hubs/Plants/hub.txt&genome=_araTha1&chrom=chrI&track=assembly_&start=0&end=14309681
  • \n"); hPrintf("
      \n"); +hPrintf("
    1. get track data from specified hub, chromosome, track name, start and end coordinates /getData/track?hubUrl=http://genome-test.gi.ucsc.edu/~hiram/hubs/Plants/hub.txt&genome=_araTha1&chrom=chrI&track=assembly_&start=0&end=14309681
    2. \n"); hPrintf("
    \n"); } /* static void showExamples() */ static void showCartDump() /* for information purposes only during development, will become obsolete */ { hPrintf("

    cart dump

    "); hPrintf("
    \n");
     cartDump(cart);
     hPrintf("
    \n"); } static void doMiddle(struct cart *theCart) /* Set up globals and make web page */ { @@ -644,88 +669,112 @@ char *genome = NULL; cgiVarSet("ignoreCookie", "1"); getDbAndGenome(cart, &database, &genome, oldVars); initGenbankTableNames(database); char *docRoot = cfgOptionDefault("browser.documentRoot", DOCUMENT_ROOT); int timeout = cartUsualInt(cart, "udcTimeout", 300); if (udcCacheTimeout() < timeout) udcSetCacheTimeout(timeout); knetUdcInstall(); char *pathInfo = getenv("PATH_INFO"); +/* nothing on incoming path, then display the WEB page instead */ +if (sameOk("/",pathInfo)) + pathInfo = NULL; + +boolean commandError = FALSE; +char *words[MAX_PATH_INFO]; if (isNotEmpty(pathInfo)) { + setupFunctionHash(); + /*expect no more than MAX_PATH_INFO number of words*/ + struct hashEl *hel = parsePathInfo(pathInfo, words); + /* verify valid API command */ + + if (hel) /* have valid command */ + { puts("Content-Type:application/json"); puts("\n"); /* skip the first leading slash to simplify chopByChar parsing */ - pathInfo += 1; - setupFunctionHash(); - apiFunctionSwitch(pathInfo); +// pathInfo += 1; +// apiFunctionSwitch(hel, words); + void (*apiFunction)(char **) = hel->val; + (*apiFunction)(words); return; } + else + commandError = TRUE; + } + puts("Content-Type:text/html"); puts("\n"); (void) hubPublicLoadAll(); struct dbDb *dbList = ucscDbDb(); char **ucscDbList = NULL; int listSize = slCount(dbList); AllocArray(ucscDbList, listSize); struct dbDb *el = dbList; int ucscDataBaseCount = 0; int maxDbNameWidth = 0; for ( ; el != NULL; el = el->next ) { ucscDbList[ucscDataBaseCount++] = el->name; if (strlen(el->name) > maxDbNameWidth) maxDbNameWidth = strlen(el->name); } maxDbNameWidth += 1; -cartWebStart(cart, database, "access mechanism to hub data resources"); +cartWebStart(cart, database, "UCSC API v"CGI_VERSION); char *goOtherHub = cartUsualString(cart, "goOtherHub", defaultHub); char *goUcscDb = cartUsualString(cart, "goUcscDb", ""); char *otherHubUrl = cartUsualString(cart, "urlHub", defaultHub); char *goPublicHub = cartUsualString(cart, "goPublicHub", defaultHub); char *hubDropDown = cartUsualString(cart, "publicHubs", defaultHub); char *urlDropDown = urlFromShortLabel(hubDropDown); char *ucscDb = cartUsualString(cart, "ucscGenomes", defaultDb); char *urlInput = urlDropDown; /* assume public hub */ if (sameWord("go", goOtherHub)) /* requested other hub URL */ urlInput = otherHubUrl; +if (commandError) + { + hPrintf("

    ERROR: no such command: '%s/%s' for endpoint '%s'

    ", words[0], words[1], pathInfo); + } + long lastTime = clock1000(); struct trackHub *hub = errCatchTrackHubOpen(urlInput); if (measureTiming) { long thisTime = clock1000(); hPrintf("hub open time: %ld millis
    \n", thisTime - lastTime); } // hPrintf("

    ucscDb: '%s'

    \n", ucscDb); struct trackHubGenome *hubGenome = hub->genomeList; showExamples(urlInput, hubGenome, ucscDb); +if (debug) showCartDump(); hPrintf("
    \n\n", "../cgi-bin/hubApi"); hPrintf("Select public hub: "); #define JBUFSIZE 2048 #define SMALLBUF 256 char javascript[JBUFSIZE]; struct slPair *events = NULL; safef(javascript, sizeof(javascript), "this.lastIndex=this.selectedIndex;"); slPairAdd(&events, "focus", cloneString(javascript)); cgiMakeDropListClassWithIdStyleAndJavascript("publicHubs", "publicHubs", shortLabels, publicHubCount, hubDropDown, NULL, "width: 400px", events);