0c551e8432f5c9bbf86ac83e49a5c640ae7dbc55 angie Fri Jun 2 11:40:19 2023 -0700 Add support for a couple Influenza A RefSeq assemblies -- only for segment 4 (HA gene, the "H" in serotypes like "H3N2") for now since we'll need another menu (or smart matching) in order to support all segments. diff --git src/hg/hgPhyloPlace/hgPhyloPlace.c src/hg/hgPhyloPlace/hgPhyloPlace.c index f68e460..e77277f 100644 --- src/hg/hgPhyloPlace/hgPhyloPlace.c +++ src/hg/hgPhyloPlace/hgPhyloPlace.c @@ -1,737 +1,761 @@ /* hgPhyloPlace - Upload SARS-CoV-2 or MPXV sequence for placement in phylo tree. */ /* Copyright (C) 2020-2022 The Regents of the University of California */ #include "common.h" #include "botDelay.h" #include "cart.h" #include "cgiApoptosis.h" #include "cheapcgi.h" #include "hCommon.h" #include "hash.h" #include "hgConfig.h" #include "htmshell.h" #include "hui.h" #include "jsHelper.h" #include "knetUdc.h" #include "linefile.h" #include "md5.h" #include "net.h" #include "options.h" #include "phyloPlace.h" #include "portable.h" #include "trackLayout.h" #include "udc.h" #include "web.h" #include "wikiLink.h" /* Global Variables */ struct cart *cart = NULL; // CGI and other variables struct hash *oldVars = NULL; // Old contents of cart before it was updated by CGI boolean measureTiming = FALSE; // Print out how long things take char *leftLabelWidthForLongNames = "55";// Leave plenty of room for tree and long virus strain names /* for botDelay call, 10 second for warning, 20 second for immediate exit */ #define delayFraction 0.25 static boolean issueBotWarning = FALSE; static long enteredMainTime = 0; #define seqFileVar "sarsCoV2File" #define pastedIdVar "namesOrIds" #define remoteFileVar "remoteFile" #define serverCommandVar "hgpp_serverCommand" #define serverCommentVar "hgpp_serverComment" #define serverPlainVar "hgpp_serverPlain" #define serverSaltyVar "hgpp_serverSalty" static struct lineFile *lineFileFromFileInput(struct cart *cart, char *fileVar) /* Return a lineFile on data from an uploaded file with cart variable name fileVar. * If the file is binary, attempt to decompress it. Return NULL if no data are found * or if there is a problem decompressing binary data. If retFileName is not NULL */ { struct lineFile *lf = NULL; // Depending on whether the file is plain text or binary, different cart variables are present. char *filePlainContents = cartOptionalString(cart, fileVar); char cartVar[2048]; safef(cartVar, sizeof cartVar, "%s__binary", fileVar); char *fileBinaryCoords = cartOptionalString(cart, cartVar); // Also get the file name for error reporting. safef(cartVar, sizeof cartVar, "%s__filename", fileVar); char *fileName = cartOptionalString(cart, cartVar); if (fileName == NULL) fileName = ""; if (isNotEmpty(filePlainContents)) { lf = lineFileOnString(fileName, TRUE, cloneString(trimSpaces(filePlainContents))); } else if (isNotEmpty(fileBinaryCoords)) { fprintf(stderr, "%s=%s fileBinaryCoords=%s\n", cartVar, fileName, fileBinaryCoords); char *binInfo = cloneString(fileBinaryCoords); char *words[2]; char *mem; unsigned long size; chopByWhite(binInfo, words, ArraySize(words)); mem = (char *)sqlUnsignedLong(words[0]); size = sqlUnsignedLong(words[1]); lf = lineFileDecompressMem(TRUE, mem, size); } return lf; } static char *labelForDb(char *db) /* The assembly hub name is just the accession; make a special label for hMPXV. Otherwise just * return hGenome(db). */ { -char *label = NULL; -if (sameString(trackHubSkipHubName(db), "GCF_014621545.1")) - label = cloneString("hMPXV"); -else if (sameString(trackHubSkipHubName(db), "GCF_002815475.1")) - label = cloneString("RSV-A"); -else if (sameString(trackHubSkipHubName(db), "GCF_000855545.1")) - label = cloneString("RSV-B"); -else +char *label = phyloPlaceDbSetting(db, "name"); +if (isEmpty(label)) label = hGenome(db); return label; } +static int labelCmp(const void *va, const void *vb) +/* Compare two slPairs on their string values -- but treat SARS-CoV-2 as more important. */ +{ +const struct slPair *a = *((struct slPair **)va); +const struct slPair *b = *((struct slPair **)vb); +if (sameString((char *)(a->val), "SARS-CoV-2")) + return -1; +else if (sameString((char *)(b->val), "SARS-CoV-2")) + return 1; +return strcmp((char *)(a->val), (char *)(b->val)); +} + + +static struct slName *sortByLabel(struct slName *dbList) +/* Return a newly allocated version of dbList, sorted by labelForDb value -- but favor SARS-CoV-2. */ +{ +struct slPair *dbLabelList = NULL; +struct slName *sln; +for (sln = dbList; sln != NULL; sln = sln->next) + slPairAdd(&dbLabelList, sln->name, cloneString(labelForDb(sln->name))); +slSort(&dbLabelList, labelCmp); +struct slName *newList = NULL; +struct slPair *slp; +for (slp = dbLabelList; slp != NULL; slp = slp->next) + slAddHead(&newList, slNameNew(slp->name)); +slReverse(&newList); +slPairFreeValsAndList(&dbLabelList); +return newList; +} + static void selectDb(char **pDb, char **pLabel) /* Search for assembly config.ra files in hgPhyloPlaceData. If there is more than one * supported assembly, then make a menu / select input for supported assemblies; * reload the page on change. */ { -struct slName *supportedDbs = phyloPlaceDbList(cart); +struct slName *supportedDbs = sortByLabel(phyloPlaceDbList(cart)); if (supportedDbs == NULL) errAbort("Sorry, this server is not configured to perform phylogenetic placement."); if (!slNameInList(supportedDbs, *pDb)) { *pDb = cloneString(supportedDbs->name); } *pLabel = labelForDb(*pDb); int supportedDbCount = slCount(supportedDbs); if (supportedDbCount > 1) { char *labels[supportedDbCount]; char *values[supportedDbCount]; struct slName *sDb; int i; for (sDb = supportedDbs, i = 0; i < supportedDbCount; sDb = sDb->next, i++) { values[i] = sDb->name; labels[i] = labelForDb(values[i]); } char *selectVar = "db"; struct dyString *dy = jsOnChangeStart(); jsDropDownCarryOver(dy, selectVar); char *js = jsOnChangeEnd(&dy); puts("

Choose your pathogen: "); cgiMakeDropListFull(selectVar, labels, values, supportedDbCount, *pDb, "change", js); puts("

"); } else cgiMakeHiddenVar("db", *pDb); slNameFreeList(&supportedDbs); } static void newPageStartStuff() { // Copied these from hgGtexTrackSettings.c which says "// NOTE: This will likely go to web.c". puts(""); puts(""); //#*** TODO: move this out to a CSS (hardcoding for now because we're doing a standalone push //#*** independent of the release cycle). puts("\n" ); // Container for bootstrap grid layout puts( "
\n"); } static void newPageEndStuff() { puts( "
"); jsIncludeFile("utils.js", NULL); webIncludeFile("inc/gbFooter.html"); webEndJWest(); } #define CHECK_FILE_OR_PASTE_INPUT_JS(fileVarName, pasteVarName) \ "{ var $fileInput = $('input[name="fileVarName"]');" \ " var $pasteInput = $('textarea[name="pasteVarName"]');" \ " if ($fileInput && $fileInput[0] && $fileInput[0].files && !$fileInput[0].files.length &&" \ " $pasteInput && !$pasteInput.val()) {" \ " alert('Please either choose a file or paste in sequence names/IDs first, ' +" \ " 'and then click the upload button.');" \ " return false; " \ " } else if ($fileInput && $fileInput[0] && $fileInput[0].files && " \ " !!$fileInput[0].files.length &&" \ " $pasteInput && !!$pasteInput.val()) {" \ " alert('Sorry, unable to process both a file and pasted-in sequence names/IDs at the ' +" \ " 'same time. Please clear one or the other and then click the upload button.');" \ " return false; " \ " } else { loadingImage.run(); return true; } }" static void inputForm(char *db) /* Ask the user for FASTA or VCF. */ { printf("
\n\n", "hgPhyloPlace"); cartSaveSession(cart); puts("
"); puts("
"); puts("
" "Place your sequences in a global phylogenetic tree
"); // If db is not a supported db then switch to the default supported db, and if multiple dbs are // supported then make a menu so the user can select. char *label = NULL; selectDb(&db, &label); printf("

Select your FASTA, VCF or list of sequence names/IDs: "); printf("", seqFileVar, seqFileVar); printf("

or paste in sequence names/IDs:
\n"); cgiMakeTextArea(pastedIdVar, "", 10, 70); struct treeChoices *treeChoices = loadTreeChoices(db); if (treeChoices) { puts("

"); printf("Phylogenetic tree version: "); char *phyloPlaceTree = cartOptionalString(cart, "phyloPlaceTree"); cgiMakeDropListWithVals("phyloPlaceTree", treeChoices->descriptions, treeChoices->protobufFiles, treeChoices->count, phyloPlaceTree); } puts("

"); printf("Number of samples per subtree showing sample placement: "); int subtreeSize = cartUsualInt(cart, "subtreeSize", 50); cgiMakeIntVarWithLimits("subtreeSize", subtreeSize, "Number of samples in subtree showing neighborhood of placement", 5, 10, 5000); puts("

"); cgiMakeOnClickSubmitButton(CHECK_FILE_OR_PASTE_INPUT_JS(seqFileVar, pastedIdVar), "submit", "Upload"); char *exampleFile = phyloPlaceDbSettingPath(db, "exampleFile"); if (isNotEmpty(exampleFile)) { puts("  "); cgiMakeOnClickSubmitButton("{ loadingImage.run(); return true; }", "exampleButton", "Upload Example File"); if (sameString(db, "wuhCor1")) { puts("  "); puts("More example files"); } } puts("

"); // Add a loading image to reassure people that we're working on it when they upload a big file printf("
\n"); printf("
\n"); jsInline("$(document).ready(function() {\n" " loadingImage.init($('#loadingImg'), $('#loadingMsg'), " "'

Uploading and processing your sequences " "may take some time. Please leave this window open while we work on your sequences.

');" "});\n"); puts("
"); puts("
"); puts("
"); puts("
"); puts("

More information

"); printf("

Upload your %s sequence (FASTA or VCF file) to find the most similar\n" "complete, high-coverage samples from \n", label); if (sameString(db, "wuhCor1")) { puts("GISAID\n" "or from public sequence databases (INSDC: GenBank/ENA/DDBJ accessed using " "NCBI Virus,\n" "COG-UK and the\n" "China National Center for Bioinformation), " "and your sequence's placement in the phylogenetic tree generated by the\n" "sarscov2phylo\n" "pipeline.\n"); } else { //#*** TODO get NCBI link from db not hardcoded puts("public sequence databases (INSDC: GenBank/ENA/DDBJ accessed using " "NCBI Virus)\n" "and your sequence's placement in a global phylogenetic tree.\n" ); } puts("Placement is performed by\n" "" "Ultrafast Sample placement on Existing tRee (UShER) " "(" "Turakhia et al.). UShER also generates local subtrees to show samples " "in the context of the most closely related sequences. The subtrees can be visualized " "as Genome Browser custom tracks and/or using " "Nextstrain's interactive display " "which supports " "drag-and-drop of local metadata that remains on your computer.

\n"); if (sameString(db, "wuhCor1")) { puts("

\n" "GISAID data displayed in the Genome Browser are subject to GISAID's\n" "" "Terms and Conditions.\n" "SARS-CoV-2 genome sequences and metadata are available for download from\n" "GISAID EpiCoV™.\n" "

"); puts("

\n" "COVID-19 Pandemic Resources at UCSC

\n"); } puts("
"); puts("
"); puts("
"); puts("
"); puts("

Privacy and sharing

"); puts("

Please do not upload " "Protected Health Information (PHI).

\n" "If even virus sequence files must remain local on your computer, then you can try " "ShUShER " "which runs entirely in your web browser so that no files leave your computer." "

\n" "

We do not store your information " "(aside from the information necessary to display results)\n" "and will not share it with others unless you choose to share your Genome Browser view.

\n" "

In order to enable rapid progress in pandemic research and genomic contact tracing,\n" "please share your sequences by submitting them to an " "INSDC member institution\n" "(NCBI,\n" "EMBL-EBI\n" "or DDBJ)\n"); if (sameString(db, "wuhCor1")) puts("and GISAID\n"); puts(".

\n"); puts("
"); puts("
"); puts("
"); puts(""); puts("
"); puts("
"); } static void mainPage(char *db) { // Start web page with new-style header webStartGbNoBanner(cart, db, "UShER: Upload"); jsInit(); jsIncludeFile("jquery.js", NULL); jsIncludeFile("ajax.js", NULL); newPageStartStuff(); // Hidden form for reloading page when db select is changed static char *saveVars[] = { "db" }; jsCreateHiddenForm(cart, cgiScriptName(), saveVars, ArraySize(saveVars)); puts("
" "
\n" "
UShER: Ultrafast Sample placement on Existing tRee
\n" "
\n" "
\n" "
\n" "
\n"); if (hgPhyloPlaceEnabled()) { inputForm(db); } else { puts("
"); puts(" Sorry, this server is not configured to perform phylogenetic placement."); puts("
"); } puts("
\n"); newPageEndStuff(); } static void resultsPage(char *db, struct lineFile *lf) /* QC the user's uploaded sequence(s) or VCF; if input looks valid then run usher * and display results. */ { webStartGbNoBanner(cart, db, "UShER: Results"); jsIncludeFile("jquery.js", NULL); jsIncludeFile("ajax.js", NULL); newPageStartStuff(); if (issueBotWarning) { char *ip = getenv("REMOTE_ADDR"); botDelayMessage(ip, botDelayMillis); } // Allow 10 minutes for big sets of sequences lazarusLives(15 * 60); puts("
" "
\n" "
UShER: Ultrafast Sample placement on Existing tRee
\n" "
\n" "
\n" "
\n" "
\n"); // Form submits subtree custom tracks to hgTracks printf("
\n\n", hgTracksName(), cartUsualString(cart, "formMethod", "POST")); cartSaveSession(cart); puts("
"); fflush(stdout); if (lf != NULL) { // Use trackLayout to get hgTracks parameters relevant to displaying trees: struct trackLayout tl; trackLayoutInit(&tl, cart); // Do our best to place the user's samples, make custom tracks if successful: char *phyloPlaceTree = cartOptionalString(cart, "phyloPlaceTree"); int subtreeSize = cartUsualInt(cart, "subtreeSize", 50); boolean success = FALSE; char *ctFile = phyloPlaceSamples(lf, db, phyloPlaceTree, measureTiming, subtreeSize, tl.fontHeight, &success); if (ctFile) { cgiMakeHiddenVar(CT_CUSTOM_TEXT_VAR, ctFile); if (tl.leftLabelWidthChars < 0 || tl.leftLabelWidthChars == leftLabelWidthDefaultChars) cgiMakeHiddenVar(leftLabelWidthVar, leftLabelWidthForLongNames); cgiMakeButton("submit", "view in Genome Browser"); puts("
"); puts("
"); } else if (! success) { puts("

"); puts("
"); puts(""); // Let the user upload something else and try again: inputForm(db); } } else { warn("Unable to read your uploaded data - please choose a file and try again, or click the " ""try example" button."); // Let the user try again: puts(" "); puts(""); inputForm(db); } puts("\n"); newPageEndStuff(); } static boolean serverAuthOk(char *plain, char *salty) /* Construct a salted hash of plain and compare it to salty. */ { char *salt = cfgOption(CFG_LOGIN_COOKIE_SALT); if (! salt) salt = ""; char *plainMd5 = md5HexForString(plain); struct dyString *dySalted = dyStringCreate("%s-%s", salt, plainMd5); char *rightSalty = md5HexForString(dySalted->string); boolean ok = sameOk(salty, rightSalty); dyStringFree(&dySalted); return ok; } INLINE void maybeComment(char *comment) /* If comment is nonempty, append it to stderr. Then print a newline regardless of comment. */ { if (isNotEmpty(comment)) fprintf(stderr, ": %s", comment); fputc('\n', stderr); } #define CONTENT_TYPE "Content-Type: text/plain\n\n" static void sendServerCommand(char *db) /* If a recognized server command is requested (with minimal auth to prevent DoS), and usher server * is configured, then send the command to the usher server's manager fifo. */ { pushWarnHandler(htmlVaBadRequestAbort); pushAbortHandler(htmlVaBadRequestAbort); char *plain = cgiOptionalString(serverPlainVar); char *salty = cgiOptionalString(serverSaltyVar); if (isNotEmpty(plain) && isNotEmpty(salty) && serverAuthOk(plain, salty)) { if (serverIsConfigured(db)) { char *command = cgiString(serverCommandVar); char *comment = cgiOptionalString(serverCommentVar); struct tempName tnCheckServer; trashDirFile(&tnCheckServer, "ct", "usher_check_server", ".txt"); FILE *errFile = mustOpen(tnCheckServer.forCgi, "w"); boolean serverUp = serverIsRunning(db, errFile); carefulClose(&errFile); if (sameString(command, "start")) { // This one is really a command for the CGI not the server manager fifo (because the // server is not yet running and needs to be started at this point), but uses the // same CGI interface. struct treeChoices *treeChoices = loadTreeChoices(db); if (treeChoices != NULL) { if (serverUp) errAbort("Server is already running for db %s, see %s", db, tnCheckServer.forCgi); struct tempName tnServerStartup; trashDirFile(&tnServerStartup, "ct", "usher_server_startup", ".txt"); errFile = mustOpen(tnServerStartup.forCgi, "w"); fprintf(stderr, "Usher server start for %s", db); maybeComment(comment); boolean success = startServer(db, treeChoices, errFile); carefulClose(&errFile); if (success) { fprintf(stderr, "Spawned usher server background process, details in %s", tnServerStartup.forCgi); printf(CONTENT_TYPE"Started server for %s\n", db); } else errAbort("Unable to spawn usher server background process, details in %s", tnServerStartup.forCgi); } else errAbort("No treeChoices for db=%s", db); } else if (serverUp) { if (sameString(command, "reload")) { struct treeChoices *treeChoices = loadTreeChoices(db); fprintf(stderr, "Usher server reload for %s", db); maybeComment(comment); serverReloadProtobufs(db, treeChoices); printf(CONTENT_TYPE"Sent reload command for %s\n", db); } else if (sameString(command, "stop")) { fprintf(stderr, "Usher server stop for %s", db); maybeComment(comment); serverStop(db); printf(CONTENT_TYPE"Sent stop command for %s\n", db); } else { char commandCopy[16]; safecpy(commandCopy, sizeof commandCopy, command); char *words[3]; int wordCount = chopLine(commandCopy, words); int val; if (wordCount == 2 && (val = atol(words[1])) > 0) { if (sameString(words[0], "thread")) { fprintf(stderr, "Usher server thread count set to %d", val); maybeComment(comment); serverSetThreadCount(db, val); printf(CONTENT_TYPE"Sent thread %d command for %s\n", val, db); } else if (sameString(words[0], "timeout")) { fprintf(stderr, "Usher server timeout set to %d", val); maybeComment(comment); serverSetTimeout(db, val); printf(CONTENT_TYPE"Sent timeout %d command for %s\n", val, db); } else errAbort("Unrecognized command '%s'", command); } else errAbort("Unrecognized command '%s'", command); } } else errAbort("Server for %s is down (see %s), cannot send command '%s'", db, tnCheckServer.forCgi, command); } else errAbort("Usher server mode not configured for db=%s", db); } else errAbort("Bad request"); popWarnHandler(); popAbortHandler(); } static void doMiddle(struct cart *theCart) /* Set up globals and make web page */ { cart = theCart; char *db = NULL, *genome = NULL; // Get the current db from the cart getDbAndGenome(cart, &db, &genome, oldVars); int timeout = cartUsualInt(cart, "udcTimeout", 300); if (udcCacheTimeout() < timeout) udcSetCacheTimeout(timeout); knetUdcInstall(); measureTiming = cartUsualBoolean(cart, "measureTiming", measureTiming); char *submitLabel = cgiOptionalString("submit"); char *newExampleButton = cgiOptionalString("exampleButton"); if ((submitLabel && sameString(submitLabel, "try example")) || (newExampleButton && sameString(newExampleButton, "Upload Example File"))) { char *exampleFile = phyloPlaceDbSettingPath(db, "exampleFile"); struct lineFile *lf = lineFileOpen(exampleFile, TRUE); resultsPage(db, lf); } else if (cgiOptionalString(remoteFileVar)) { char *url = cgiString(remoteFileVar); struct lineFile *lf = netLineFileOpen(url); resultsPage(db, lf); } else if (isNotEmpty(trimSpaces(cgiOptionalString(pastedIdVar)))) { char *pastedIds = cgiString(pastedIdVar); struct lineFile *lf = lineFileOnString("pasted names/IDs", TRUE, pastedIds); resultsPage(db, lf); } else if (cgiOptionalString(seqFileVar) || cgiOptionalString(seqFileVar "__filename")) { struct lineFile *lf = lineFileFromFileInput(cart, seqFileVar); resultsPage(db, lf); } else if (isNotEmpty(cgiOptionalString(serverCommandVar))) { sendServerCommand(db); } else mainPage(db); } #define LD_LIBRARY_PATH "LD_LIBRARY_PATH" static void addLdLibraryPath() /* usher requires a tbb lib that is not in the yum package tbb-devel, so for now * I'm adding the .so files to hgPhyloPlaceData. Set environment variable LD_LIBRARY_PATH * to pick them up from there. */ { char *oldValue = getenv(LD_LIBRARY_PATH); struct dyString *dy = dyStringNew(0); if (startsWith("/", PHYLOPLACE_DATA_DIR)) dyStringAppend(dy, PHYLOPLACE_DATA_DIR); else { char cwd[4096]; getcwd(cwd, sizeof cwd); dyStringPrintf(dy, "%s/%s", cwd, PHYLOPLACE_DATA_DIR); } if (isNotEmpty(oldValue)) dyStringPrintf(dy, ":%s", oldValue); setenv(LD_LIBRARY_PATH, dyStringCannibalize(&dy), TRUE); } int main(int argc, char *argv[]) /* Process command line. */ { /* Null terminated list of CGI Variables we don't want to save to cart */ char *excludeVars[] = {"submit", "Submit", seqFileVar, seqFileVar "__binary", seqFileVar "__filename", pastedIdVar, remoteFileVar, serverCommandVar, serverCommentVar, serverPlainVar, serverSaltyVar, NULL}; enteredMainTime = clock1000(); issueBotWarning = earlyBotCheck(enteredMainTime, "hgPhyloPlace", delayFraction, 0, 0, "html"); cgiSpoof(&argc, argv); oldVars = hashNew(10); addLdLibraryPath(); cartEmptyShellNoContent(doMiddle, hUserCookie(), excludeVars, oldVars); cgiExitTime("hgPhyloPlace", enteredMainTime); return 0; }