예제 #1
0
static void writeTdbSimple(struct jsonWrite *jw, struct trackDb *tdb, struct hash *fieldHash)
/* Write JSON for the non-parent/child fields of tdb */
{
if (fieldOk("track", fieldHash))
    jsonWriteString(jw, "track", tdb->track);
if (fieldOk("table", fieldHash))
    jsonWriteString(jw, "table", tdb->table);
if (fieldOk("shortLabel", fieldHash))
    jsonWriteString(jw, "shortLabel", tdb->shortLabel);
if (fieldOk("longLabel", fieldHash))
    jsonWriteString(jw, "longLabel", tdb->longLabel);
if (fieldOk("type", fieldHash))
    jsonWriteString(jw, "type", tdb->type);
if (fieldOk("priority", fieldHash))
    jsonWriteDouble(jw, "priority", tdb->priority);
if (fieldOk("grp", fieldHash))
    jsonWriteString(jw, "grp", tdb->grp);
// NOTE: if you add a new field here, then also add it to nameIsTdbField above.
if (tdb->settingsHash)
    {
    struct hashEl *hel;
    struct hashCookie cookie = hashFirst(tdb->settingsHash);
    while ((hel = hashNext(&cookie)) != NULL)
        {
        if (! nameIsTdbField(hel->name) && fieldOk(hel->name, fieldHash))
            {
            //#*** TODO: move jsonStringEscape inside jsonWriteString
            char *encoded = jsonStringEscape((char *)hel->val);
            jsonWriteString(jw, hel->name, encoded);
            }
        }
    }
}
예제 #2
0
static void getVar(struct cartJson *cj, struct hash *paramHash)
/* Print out the requested cart var(s). varString may be a comma-separated list.
 * If a var is a list variable, prints out a list of values for that var. */
{
char *varString = cartJsonRequiredParam(paramHash, "var", cj->jw, "get");
if (! varString)
    return;
struct slName *varList = slNameListFromComma(varString), *var;
for (var = varList;  var != NULL;  var = var->next)
    {
    if (cartListVarExists(cj->cart, var->name))
	{
	// Use cartOptionalSlNameList and return a list:
	struct slName *valList = cartOptionalSlNameList(cj->cart, var->name);
	jsonWriteSlNameList(cj->jw, var->name, valList);
	slFreeList(&valList);
	}
    else
	{
	// Regular single-value variable (or not in the cart):
	char *val = cartOptionalString(cj->cart, var->name);
	//#*** TODO: move jsonStringEscape inside jsonWriteString
	char *encoded = jsonStringEscape(val);
	jsonWriteString(cj->jw, var->name, encoded);
	freeMem(encoded);
	}
    }
slFreeList(&varList);
}
예제 #3
0
static void cartJsonPrintWarnings(struct jsonWrite *jw)
/* If there are warnings, write them out as JSON: */
{
if (dyWarn && dyStringLen(dyWarn) > 0)
    {
    //#*** TODO: move jsonStringEscape inside jsonWriteString
    char *encoded = jsonStringEscape(dyWarn->string);
    jsonWriteString(jw, "warning", encoded);
    freeMem(encoded);
    }
}
예제 #4
0
void jsonErrPrintf(struct dyString *ds, char *format, ...)
//  Printf a json error to a dyString for communicating with ajax code; format is:
//  {"error": error message here}
{
va_list args;
va_start(args, format);
dyStringPrintf(ds, "{\"error\": \"");
struct dyString *buf = newDyString(1000);
dyStringVaPrintf(buf, format, args);
dyStringAppend(ds, jsonStringEscape(dyStringCannibalize(&buf)));
dyStringPrintf(ds, "\"}");
va_end(args);
}
예제 #5
0
static void getStaticHtml(struct cartJson *cj, struct hash *paramHash)
/* Read HTML text from a relative path under browser.documentRoot and
 * write it as an encoded JSON string */
{
char *tag = cartJsonOptionalParam(paramHash, "tag");
if (isEmpty(tag))
    tag = "html";
char *file = cartJsonRequiredParam(paramHash, "file", cj->jw, "getStaticHtml");
char *html = hFileContentsOrWarning(file);
//#*** TODO: move jsonStringEscape inside jsonWriteString
char *encoded = jsonStringEscape(html);
jsonWriteString(cj->jw, tag, encoded);
}
예제 #6
0
static void getAssemblyInfo(struct cartJson *cj, struct hash *paramHash)
/* Return useful things from dbDb (or track hub) and assembly description html (possibly NULL).
 * If db param is NULL, use db from cart. */
{
char *db = cartJsonOptionalParam(paramHash, "db");
if (db == NULL)
    db = cartString(cj->cart, "db");
jsonWriteString(cj->jw, "db", db);
jsonWriteString(cj->jw, "commonName", hGenome(db));
jsonWriteString(cj->jw, "scientificName", hScientificName(db));
jsonWriteString(cj->jw, "dbLabel", hFreezeDate(db));
//#*** TODO: move jsonStringEscape inside jsonWriteString
jsonWriteString(cj->jw, "assemblyDescription", jsonStringEscape(hAssemblyDescription(db)));
}
예제 #7
0
static void writeFindPositionInfo(struct jsonWrite *jw, char *db, int taxId, char *hubUrl,
                                  char *position)
/* Write JSON for the info needed to populate the 'Find Position' section. */
{
char *genome = hGenome(db);
if (isEmpty(genome))
    {
    jsonWriteStringf(jw, "error", "No genome for db '%s'", db);
    }
else
    {
    jsonWriteString(jw, "db", db);
    jsonWriteNumber(jw, "taxId", taxId);
    jsonWriteString(jw, "genome", genome);
    struct slPair *dbOptions = NULL;
    char genomeLabel[PATH_LEN*4];
    if (isNotEmpty(hubUrl))
        {
        struct trackHub *hub = hubConnectGetHub(hubUrl);
        if (hub == NULL)
            {
            jsonWriteStringf(jw, "error", "Can't connect to hub at '%s'", hubUrl);
            return;
            }
        struct dbDb *dbDbList = trackHubGetDbDbs(hub->name);
        dbOptions = trackHubDbDbToValueLabel(dbDbList);
        safecpy(genomeLabel, sizeof(genomeLabel), hub->shortLabel);
        jsonWriteString(jw, "hubUrl", hubUrl);
        }
    else
        {
        dbOptions = hGetDbOptionsForGenome(genome);
        safecpy(genomeLabel, sizeof(genomeLabel), genome);
        }
    jsonWriteValueLabelList(jw, "dbOptions", dbOptions);
    jsonWriteString(jw, "genomeLabel", genomeLabel);
    jsonWriteString(jw, "position", position);
    char *suggestTrack = NULL;
    if (! trackHubDatabase(db))
        suggestTrack = assemblyGeneSuggestTrack(db);
    jsonWriteString(jw, "suggestTrack", suggestTrack);
    char *description = maybeGetDescriptionText(db);
    //#*** TODO: move jsonStringEscape inside jsonWriteString
    char *encoded = jsonStringEscape(description);
    jsonWriteString(jw, "description", encoded);
    listAssemblyHubs(jw);
    }
}
예제 #8
0
파일: cartJson.c 프로젝트: davidhoover/kent
void cartJsonExecute(struct cartJson *cj)
/* Get commands from cgi, print Content-type, execute commands, print results as JSON. */
{
cartJsonPushErrHandlers();
puts("Content-Type:text/javascript\n");

// Initialize response JSON object:
jsonWriteObjectStart(cj->jw, NULL);
// Always send back hgsid:
jsonWriteString(cj->jw, cartSessionVarName(), cartSessionId(cj->cart));

char *commandJson = cgiOptionalString(CARTJSON_COMMAND);
if (commandJson)
    {
    struct errCatch *errCatch = errCatchNew();
    if (errCatchStart(errCatch))
        {
        struct jsonElement *commandObj = jsonParse(commandJson);
        struct hash *commandHash = jsonObjectVal(commandObj, "commandObj");
        // change* commands need to go first!  Really we need an ordered map type here...
        // for now, just make a list and sort to put change commands at the front.
        struct slPair *commandList = NULL, *cmd;
        struct hashCookie cookie = hashFirst(commandHash);
        struct hashEl *hel;
        while ((hel = hashNext(&cookie)) != NULL)
            slAddHead(&commandList, slPairNew(hel->name, hel->val));
        slSort(&commandList, commandCmp);
        for (cmd = commandList;  cmd != NULL;  cmd = cmd->next)
            doOneCommand(cj, cmd->name, (struct jsonElement *)cmd->val);
        }
    errCatchEnd(errCatch);
    if (errCatch->gotError)
        {
        jsonWritePopToLevel(cj->jw, 1);
        //#*** TODO: move jsonStringEscape inside jsonWriteString
        char *encoded = jsonStringEscape(errCatch->message->string);
        jsonWriteString(cj->jw, "error", encoded);
        }
    errCatchFree(&errCatch);
    }

cartJsonPrintWarnings(cj->jw);
jsonWriteObjectEnd(cj->jw);
puts(cj->jw->dy->string);
cartJsonPopErrHandlers();
}
예제 #9
0
static void doMainPage()
/* Send HTML with javascript to bootstrap the user interface. */
{
// Start web page with new banner
char *db = NULL, *genome = NULL, *clade = NULL;
getDbGenomeClade(cart, &db, &genome, &clade, oldVars);
// If CGI has &lastDbPos=..., handle that here and save position to cart so it's in place for
// future cartJson calls.
char *position = cartGetPosition(cart, db, NULL);
cartSetString(cart, "position", position);
webStartJWest(cart, db, "Genome Browser Gateway");

if (cgiIsOnWeb())
    checkForGeoMirrorRedirect(cart);

#define WARNING_BOX_START "<div id=\"previewWarningRow\" class=\"jwRow\">" \
         "<div id=\"previewWarningBox\" class=\"jwWarningBox\">"

#define UNDER_DEV "Data and tools on this site are under development, have not been reviewed " \
         "for quality, and are subject to change at any time. "

#define MAIN_SITE "The high-quality, reviewed public site of the UCSC Genome Browser is " \
         "available for use at <a href=\"http://genome.ucsc.edu/\">http://genome.ucsc.edu/</a>."

#define WARNING_BOX_END "</div></div>"

if (hIsPreviewHost())
    {
    puts(WARNING_BOX_START
         "WARNING: This is the UCSC Genome Browser preview site. "
         "This website is a weekly mirror of our internal development server for public access. "
         UNDER_DEV
         "We provide this site for early access, with the warning that it is less available "
         "and stable than our public site. "
         MAIN_SITE
         WARNING_BOX_END);
    }

if (hIsPrivateHost() && !hHostHasPrefix("hgwdev-demo6"))
    {
    puts(WARNING_BOX_START
         "WARNING: This is the UCSC Genome Browser development site. "
         "This website is used for testing purposes only and is not intended for general public "
         "use. "
         UNDER_DEV
         MAIN_SITE
         WARNING_BOX_END);
    }

// The visible page elements are all in ./hgGateway.html, which is transformed into a quoted .h
// file containing a string constant that we #include and print here (see makefile).
puts(
#include "hgGateway.html.h"
);

// Set global JS variables hgsid, activeGenomes, and survey* at page load time
// We can't just use "var hgsid = " or the other scripts won't see it -- it has to be
// "window.hgsid = ".
puts("<script>");
printf("window.%s = '%s';\n", cartSessionVarName(), cartSessionId(cart));
puts("window.activeGenomes =");
printActiveGenomes();
puts(";");
char *surveyLink = cfgOption("survey");
if (isNotEmpty(surveyLink) && !sameWord(surveyLink, "off"))
    {
    printf("window.surveyLink=\"%s\";\n", jsonStringEscape(surveyLink));
    char *surveyLabel = cfgOptionDefault("surveyLabel", "Please take our survey");
    printf("window.surveyLabel=\"%s\";\n", jsonStringEscape(surveyLabel));
    char *surveyLabelImage = cfgOption("surveyLabelImage");
    if (isNotEmpty(surveyLabelImage))
        printf("window.surveyLabelImage=\"%s\";\n", jsonStringEscape(surveyLabelImage));
    else
        puts("window.surveyLabelImage=null;");
    }
else
    {
    puts("window.surveyLink=null;");
    puts("window.surveyLabel=null;");
    puts("window.surveyLabelImage=null;");
    }
puts("</script>");

puts("<script src=\"../js/es5-shim.4.0.3.min.js\"></script>");
puts("<script src=\"../js/es5-sham.4.0.3.min.js\"></script>");
puts("<script src=\"../js/lodash.3.10.0.compat.min.js\"></script>");
puts("<script src=\"../js/cart.js\"></script>");

webIncludeResourceFile("jquery-ui.css");
jsIncludeFile("jquery-ui.js", NULL);
jsIncludeFile("jquery.watermarkinput.js", NULL);
jsIncludeFile("utils.js",NULL);

// Phylogenetic tree .js file, produced by dbDbTaxonomy.pl:
char *dbDbTree = cfgOptionDefault("hgGateway.dbDbTaxonomy", "../js/dbDbTaxonomy.js");
if (isNotEmpty(dbDbTree))
    printf("<script src=\"%s\"></script>\n", dbDbTree);

// Main JS for hgGateway:
puts("<script src=\"../js/hgGateway.js\"></script>");

webIncludeFile("inc/jWestFooter.html");

cartFlushHubWarnings();

webEndJWest();
}
예제 #10
0
static void jsonDyStringPrintRecurse(struct dyString *dy, struct jsonElement *ele, int indentLevel)
{
if (indentLevel >= -1) // Note that < -1 will result in no indenting
    indentLevel++;
char *tab = "\t";
char *nl = "\n";
if (indentLevel < 0)
    {
    tab = "";
    nl = "";
    }
char *indentBuf = makeIndentBuf(indentLevel);
switch (ele->type)
    {
    case jsonObject:
        {
        dyStringPrintf(dy,"{%s",nl);
        if(hashNumEntries(ele->val.jeHash))
            {
            struct hashEl *el, *list = hashElListHash(ele->val.jeHash);
            slSort(&list, hashElCmp);
            for (el = list; el != NULL; el = el->next)
                {
                struct jsonElement *val = el->val;
                dyStringPrintf(dy,"%s%s\"%s\": ", indentBuf, tab, el->name);
                jsonDyStringPrintRecurse(dy, val, indentLevel);
                dyStringPrintf(dy,"%s%s", el->next == NULL ? "" : ",",nl);
                }
            hashElFreeList(&list);
            }
        dyStringPrintf(dy,"%s}", indentBuf);
        break;
        }
    case jsonList:
        {
        struct slRef *el;
        dyStringPrintf(dy,"[%s",nl);
        if(ele->val.jeList)
            {
            for (el = ele->val.jeList; el != NULL; el = el->next)
                {
                struct jsonElement *val = el->val;
                dyStringPrintf(dy,"%s%s", indentBuf,tab);
                jsonDyStringPrintRecurse(dy, val, indentLevel);
                dyStringPrintf(dy,"%s%s", el->next == NULL ? "" : ",",nl);
                }
            }
        dyStringPrintf(dy,"%s]", indentBuf);
        break;
        }
    case jsonString:
        {
        dyStringPrintf(dy,"\"%s\"", jsonStringEscape(ele->val.jeString));
        break;
        }
    case jsonBoolean:
        {
        dyStringPrintf(dy,"%s", ele->val.jeBoolean ? "true" : "false");
        break;
        }
    case jsonNumber:
        {
        char buf[256];
        safef(buf, sizeof(buf), "%ld", ele->val.jeNumber);
        dyStringPrintf(dy,"%s", buf);
        break;
        }
    case jsonDouble:
        {
        char buf[256];
        safef(buf, sizeof(buf), "%g", ele->val.jeDouble);
        dyStringPrintf(dy,"%s", buf);
        break;
        }
    default:
        {
        errAbort("jsonPrintRecurse; invalid type: %d", ele->type);
        break;
        }
    }
if (indentLevel >= 0)
    freez(&indentBuf);
}
예제 #11
0
void suggestGene(char *database, char *table, char *prefix)
/* Print out a Javascript list of objects describing genes that start with prefix. */
{
struct dyString *str = newDyString(10000);
dyStringPrintf(str, "[\n");

int exact = cgiOptionalInt("exact", 0);
boolean hasKnownCanonical = sameString(table, "knownCanonical");
initGenbankTableNames(database);
char query[2048];
if(exact)
    {
    // NOTE that exact is no longer used by the UI as of v271, but there are still some robots
    // using it so we still support it.
    if(hasKnownCanonical)
        sqlSafef(query, sizeof(query),
                 "select x.geneSymbol, k.chrom, kg.txStart, kg.txEnd, x.kgID, x.description "
                 "from knownCanonical k, knownGene kg, kgXref x "
                 "where k.transcript = x.kgID and k.transcript = kg.name and x.geneSymbol = '%s' "
                 "order by x.geneSymbol, k.chrom, kg.txEnd - kg.txStart desc", prefix);
    else
        sqlSafef(query, sizeof(query),
                 "select r.name2, r.chrom, r.txStart, r.txEnd, r.name, d.name "
                 "from %s r, %s g, %s d "
                 "where r.name2 = '%s' and g.acc = r.name and g.description = d.id "
                 "order by r.name2, r.chrom, r.txEnd - r.txStart desc",
                 table, gbCdnaInfoTable, descriptionTable, prefix);
    }
else
    {
    // We use a LIKE query b/c it uses the geneSymbol index (substr queries do not use indices in mysql).
    // Also note that we take advantage of the fact that searches are case-insensitive in mysql.
    // Unfortunately, knownCanonical sometimes has multiple entries for a given gene (e.g. 2 TTn's in mm9 knownCanonical;
    // 3 POU5F1's in hg19); we return all of them (#5962).
    if(hasKnownCanonical)
        sqlSafef(query, sizeof(query),
                 "select x.geneSymbol, k.chrom, kg.txStart, kg.txEnd, x.kgID, x.description "
                 "from knownCanonical k, knownGene kg, kgXref x "
                 "where k.transcript = x.kgID and k.transcript = kg.name "
                 "and x.geneSymbol LIKE '%s%%' "
                 "order by x.geneSymbol, k.chrom, kg.txStart", prefix);
    else
        sqlSafef(query, sizeof(query), "select r.name2, r.chrom, r.txStart, r.txEnd, r.name, d.name "
                 "from %s r, %s g, %s d "
                 "where r.name2 LIKE '%s%%' and g.acc = r.name and g.description = d.id "
                 "order by r.name2, r.chrom, r.txStart",
                 table, gbCdnaInfoTable, descriptionTable, prefix);
    }
struct sqlConnection *conn = hAllocConn(database);
struct sqlResult *sr = sqlGetResult(conn, query);
char **row;
int count = 0;
while ((row = sqlNextRow(sr)) != NULL)
    {
    // ignore funny chroms (e.g. _hap chroms. See redmine #4257.
    if(!strchr(row[1], '_'))
        {
        // We have some very long descriptions, e.g. 4277 chars for hg38 CLOCK, so truncate:
        const int maxDesc = 120;
        char *description = row[5];
        if (strlen(description) > maxDesc + 4)
            strcpy(description + maxDesc, "...");
        count++;
        dyStringPrintf(str, "%s{\"value\": \"%s (%s)\", "
                       "\"id\": \"%s:%d-%s\", "
                       "\"geneSymbol\": \"%s\", "
                       "\"internalId\": \"%s\"}",
                       count == 1 ? "" : ",\n", row[0], jsonStringEscape(description),
                       row[1], atoi(row[2])+1, row[3],
                       jsonStringEscape(row[0]),
                       jsonStringEscape(row[4]));
        }
    }
hFreeConn(&conn);
dyStringPrintf(str, "\n]\n");
puts(dyStringContents(str));
}
예제 #12
0
int main(int argc, char *argv[])
{
long enteredMainTime = clock1000();

cgiSpoof(&argc, argv);
char *prefix = cgiOptionalString("prefix");
char *database = cgiOptionalString("db");

initGenbankTableNames(database);

int exact = cgiOptionalInt("exact", 0);
struct sqlConnection *conn;
char query[2048];
char **row;
struct sqlResult *sr;
int count = 0;
boolean hasKnownCanonical;
struct dyString *str = newDyString(10000);
char *table;

pushAbortHandler(htmlVaBadRequestAbort);
if(prefix == NULL || database == NULL)
    errAbort("%s", "Missing prefix and/or db CGI parameter");

conn = hAllocConn(database);
table = connGeneSuggestTable(conn);
if(table == NULL)
    errAbort("gene autosuggest is not supported for db '%s'", database);
popAbortHandler();

hasKnownCanonical = sameString(table, "knownCanonical");

puts("Content-Type:text/plain");
puts("\n");

dyStringPrintf(str, "[\n");

if(exact)
    {
    // NOTE that exact is no longer used by the UI as of v271, but there are still some robots using it so we still support it.
    if(hasKnownCanonical)
        sqlSafef(query, sizeof(query), "select x.geneSymbol, k.chrom, kg.txStart, kg.txEnd, x.kgID, x.description "
              "from knownCanonical k, knownGene kg, kgXref x where k.transcript = x.kgID and k.transcript = kg.name "
              "and x.geneSymbol = '%s' order by x.geneSymbol, k.chrom, kg.txEnd - kg.txStart desc", prefix);
    else
        sqlSafef(query, sizeof(query), "select r.name2, r.chrom, r.txStart, r.txEnd, r.name, d.name "
              "from %s r, %s g, %s d where r.name2 = '%s' and g.acc = r.name "
              "and g.description = d.id order by r.name2, r.chrom, r.txEnd - r.txStart desc", table, gbCdnaInfoTable, descriptionTable, prefix);
    }
else
    {
    // We use a LIKE query b/c it uses the geneSymbol index (substr queries do not use indices in mysql).
    // Also note that we take advantage of the fact that searches are case-insensitive in mysql.
    // Unfortunately, knownCanonical sometimes has multiple entries for a given gene (e.g. 2 TTn's in mm9 knownCanonical;
    // 3 POU5F1's in hg19); we return all of them (#5962).
    if(hasKnownCanonical)
        sqlSafef(query, sizeof(query), "select x.geneSymbol, k.chrom, kg.txStart, kg.txEnd, x.kgID, x.description "
              "from knownCanonical k, knownGene kg, kgXref x where k.transcript = x.kgID and k.transcript = kg.name "
              "and x.geneSymbol LIKE '%s%%' order by x.geneSymbol, k.chrom, kg.txStart", prefix);
    else
        sqlSafef(query, sizeof(query), "select r.name2, r.chrom, r.txStart, r.txEnd, r.name, d.name "
              "from %s r, %s g, %s d where r.name2 LIKE '%s%%' and g.acc = r.name "
              "and g.description = d.id order by r.name2, r.chrom, r.txStart", table, gbCdnaInfoTable, descriptionTable, prefix);
    }
sr = sqlGetResult(conn, query);
while ((row = sqlNextRow(sr)) != NULL)
    {
    // ignore funny chroms (e.g. _hap chroms. See redmine #4257.
    if(!strchr(row[1], '_'))
        {
        // We have some very long descriptions, e.g. 4277 chars for hg38 CLOCK, so truncate:
        const int maxDesc = 120;
        char *description = row[5];
        if (strlen(description) > maxDesc + 4)
            strcpy(description + maxDesc, "...");
        count++;
        dyStringPrintf(str, "%s{\"value\": \"%s (%s)\", "
                       "\"id\": \"%s:%d-%s\", "
                       "\"geneSymbol\": \"%s\", "
                       "\"internalId\": \"%s\"}",
                       count == 1 ? "" : ",\n", row[0], jsonStringEscape(description),
                       row[1], atoi(row[2])+1, row[3],
                       jsonStringEscape(row[0]),
                       jsonStringEscape(row[4]));
        }
    }

dyStringPrintf(str, "\n]\n");
puts(dyStringContents(str));
cgiExitTime("hgSuggest", enteredMainTime);
return 0;
}