be4311c07e14feb728abc6425ee606ffaa611a58 markd Fri Jan 22 06:46:58 2021 -0800 merge with master diff --git src/hg/hgPhyloPlace/treeToAuspiceJson.c src/hg/hgPhyloPlace/treeToAuspiceJson.c index 8f6030b..78c9355 100644 --- src/hg/hgPhyloPlace/treeToAuspiceJson.c +++ src/hg/hgPhyloPlace/treeToAuspiceJson.c @@ -5,94 +5,97 @@ #include "common.h" #include "hash.h" #include "hui.h" #include "jsonWrite.h" #include "linefile.h" #include "parsimonyProto.h" #include "phyloPlace.h" #include "phyloTree.h" #include "variantProjector.h" // Globals extern char *chrom; extern int chromSize; -static void writeAuspiceMeta(FILE *outF, struct slName *subtreeUserSampleIds) +static void writeAuspiceMeta(FILE *outF, struct slName *subtreeUserSampleIds, char *source) /* Write metadata to configure Auspice display. */ { fprintf(outF, "\"meta\": { " "\"title\": \"Subtree with %s", subtreeUserSampleIds->name); struct slName *sln; for (sln = subtreeUserSampleIds->next; sln != NULL; sln = sln->next) fprintf(outF, ", %s", sln->name); fputs("\", " "\"panels\": [ \"tree\"] , " "\"colorings\": [ " " { \"key\": \"pangolin_lineage\", " " \"title\": \"Pangolin lineage\", \"type\": \"categorical\" }," " { \"key\": \"Nextstrain_clade\"," " \"scale\": [ [ \"19B\", \"#EC676D\" ], [ \"19A\", \"#F79E43\" ]," " [ \"20A\", \"#B6D77A\" ], [ \"20C\", \"#8FD4ED\" ]," " [ \"20B\", \"#A692C3\" ] ]," " \"title\": \"Nextstrain Clade\", \"type\": \"categorical\" }," " { \"key\": \"GISAID_clade\"," " \"scale\": [ [ \"S\", \"#EC676D\" ], [ \"L\", \"#F79E43\" ], [ \"O\", \"#F9D136\" ]," " [ \"V\", \"#FAEA95\" ], [ \"G\", \"#B6D77A\" ], [ \"GH\", \"#8FD4ED\" ]," " [ \"GR\", \"#A692C3\" ] ]," " \"title\": \"GISAID Clade\", \"type\": \"categorical\" }," - " { \"key\": \"userOrOld\", " - " \"scale\": [ [ \"uploaded sample\", \"#CC0000\"] , [ \"GISAID sample\", \"#000000\"] ]," + , outF); +fprintf(outF, " { \"key\": \"userOrOld\", " + " \"scale\": [ [ \"uploaded sample\", \"#CC0000\"] , [ \"%s\", \"#000000\"] ]," " \"title\": \"Sample type\", \"type\": \"categorical\" }" - " ] , " + , source); +fputs(" ] , " //#*** Filters didn't seem to work... maybe something about the new fetch feature, or do I need to spcify in some other way? //#*** "\"filters\": [ \"GISAID_clade\", \"region\", \"country\", \"division\", \"author\" ], " "\"display_defaults\": { " " \"branch_label\": \"nuc mutations\" " "}, " , outF); fprintf(outF, "\"description\": \"Dataset generated by [UShER web interface]" "(%shgPhyloPlace) using the " "[usher](https://github.com/yatisht/usher/) program. " //#*** TODO: describe input from which tree was generated: user sample, version of tree, etc. , hLocalHostCgiBinUrl()); fputs("If you have metadata you wish to display, you can now drag on a CSV file and it will be " "added into this view, [see here]("NEXTSTRAIN_DRAG_DROP_DOC") " "for more info.\"} ," , outF); } static void jsonWriteObjectValue(struct jsonWrite *jw, char *name, char *value) /* Write an object with one member, "value", set to value, as most Auspice node attributes are * formatted. */ { jsonWriteObjectStart(jw, name); jsonWriteString(jw, "value", value); jsonWriteObjectEnd(jw); } static void jsonWriteLeafNodeAttributes(struct jsonWrite *jw, char *name, struct sampleMetadata *met, boolean isUserSample, + char *source, char **retUserOrOld, char **retNClade, char **retGClade, char **retLineage) /* Write elements of node_attrs for a sample which may be preexisting and in our metadata hash, * or may be a new sample from the user. Set rets for color categories so parent branches can * determine their color categories. */ { -*retUserOrOld = isUserSample ? "uploaded sample" : "GISAID sample"; +*retUserOrOld = isUserSample ? "uploaded sample" : source; jsonWriteObjectValue(jw, "userOrOld", *retUserOrOld); if (met && met->date) jsonWriteObjectValue(jw, "date", met->date); if (met && met->author) { jsonWriteObjectValue(jw, "author", met->author); // Note: Nextstrain adds paper_url and title when available; they also add author and use // a uniquified value (e.g. "author": "Wenjie Tan et al" / "value": "Wenjie Tan et al A") } *retNClade = isUserSample ? "uploaded sample" : (met && met->nClade) ? met->nClade : NULL; if (isNotEmpty(*retNClade)) jsonWriteObjectValue(jw, "Nextstrain_clade", *retNClade); *retGClade = isUserSample ? "uploaded sample" : (met && met->gClade) ? met->gClade : NULL; if (isNotEmpty(*retGClade)) jsonWriteObjectValue(jw, "GISAID_clade", *retGClade); @@ -270,30 +273,31 @@ jsonWriteObjectEnd(jw); // mutations jsonWriteObjectEnd(jw); // branch_attrs } } struct auspiceJsonInfo /* Collection of a bunch of things used when writing out auspice JSON for a subtree, so the * recursive function doesn't need a dozen args. */ { struct jsonWrite *jw; struct slName *subtreeUserSampleIds; // Subtree node names for user samples (not from big tree) struct geneInfo *geneInfoList; // Transcript seq & alignment for predicting AA change struct seqWindow *gSeqWin; // Reference genome seq for predicting AA change struct hash *sampleMetadata; // Sample metadata for decorating tree int nodeNum; // For generating sequential node ID (in absence of name) + char *source; // Source of non-user sequences in tree (GISAID or public) }; static int cmpstringp(const void *p1, const void *p2) /* strcmp on pointers to strings, as in 'man qsort' but tolerate NULLs */ { char *s1 = *(char * const *)p1; char *s2 = *(char * const *)p2; if (s1 && s2) return strcmp(s1, s2); else if (s1 && !s2) return 1; else if (s2 && !s1) return -1; return 0; } @@ -360,31 +364,31 @@ jsonWriteObjectEnd(aji->jw); } jsonWriteListEnd(aji->jw); if (retUserOrOld) *retUserOrOld = majorityMaybe(kidUserOrOld, node->numEdges); if (retNClade) *retNClade = majorityMaybe(kidNClade, node->numEdges); if (retGClade) *retGClade = majorityMaybe(kidGClade, node->numEdges); if (retLineage) *retLineage = majorityMaybe(kidLineage, node->numEdges); } jsonWriteObjectStart(aji->jw, "node_attrs"); jsonWriteDouble(aji->jw, "div", depth); if (node->numEdges == 0) - jsonWriteLeafNodeAttributes(aji->jw, name, met, isUserSample, + jsonWriteLeafNodeAttributes(aji->jw, name, met, isUserSample, aji->source, retUserOrOld, retNClade, retGClade, retLineage); else if (retUserOrOld && retGClade && retLineage) jsonWriteBranchNodeAttributes(aji->jw, *retUserOrOld, *retNClade, *retGClade, *retLineage); jsonWriteObjectEnd(aji->jw); } struct phyloTree *phyloTreeNewNode(char *name) /* Alloc & return a new node with no children. */ { struct phyloTree *node; AllocVar(node); AllocVar(node->ident); node->ident->name = cloneString(name); return node; } @@ -418,47 +422,48 @@ } struct geneInfo *gi; AllocVar(gi); gi->psl = genePredToPsl((struct genePred *)gp, chromSize, txLen); gi->txSeq = newDnaSeq(seq, txLen, gp->name2); slAddHead(&geneInfoList, gi); } lmCleanup(&lm); bigBedFileClose(&bbi); } slReverse(&geneInfoList); return geneInfoList; } void treeToAuspiceJson(struct subtreeInfo *sti, char *db, struct dnaSeq *ref, - char *bigGenePredFile, struct hash *sampleMetadata, char *jsonFile) + char *bigGenePredFile, struct hash *sampleMetadata, char *jsonFile, + char *source) /* Write JSON for tree in Nextstrain's Augur/Auspice V2 JSON format * (https://github.com/nextstrain/augur/blob/master/augur/data/schema-export-v2.json). */ { struct phyloTree *tree = sti->subtree; FILE *outF = mustOpen(jsonFile, "w"); fputs("{ \"version\": \"v2\", ", outF); -writeAuspiceMeta(outF, sti->subtreeUserSampleIds); +writeAuspiceMeta(outF, sti->subtreeUserSampleIds, source); // The meta part is mostly constant & easier to just write out, but jsonWrite is better for the // nested tree structure. struct jsonWrite *jw = jsonWriteNew(); jsonWriteObjectStart(jw, "tree"); int nodeNum = 10000; // Auspice.us starting node number for newick -> json int depth = 0; // Add an extra root node because otherwise Auspice won't draw branch from big tree root to subtree struct phyloTree *root = phyloTreeNewNode("wrapper"); phyloAddEdge(root, tree); tree = root; struct geneInfo *geneInfoList = getGeneInfoList(bigGenePredFile, ref); struct seqWindow *gSeqWin = chromSeqWindowNew(db, chrom, 0, chromSize); struct auspiceJsonInfo aji = { jw, sti->subtreeUserSampleIds, geneInfoList, gSeqWin, - sampleMetadata, nodeNum }; + sampleMetadata, nodeNum, source }; rTreeToAuspiceJson(tree, depth, &aji, NULL, NULL, NULL, NULL); chromSeqWindowFree(&gSeqWin); jsonWriteObjectEnd(jw); fputs(jw->dy->string, outF); jsonWriteFree(&jw); fputs("}", outF); carefulClose(&outF); }