4b83e0cd3c28ecda13879b2c4c4a5904b8aa95ba
jcasper
  Thu May 5 13:44:28 2016 -0700
Speeding up ORegAnno load and draw, refs #15957

diff --git src/hg/hgTracks/simpleTracks.c src/hg/hgTracks/simpleTracks.c
index fac00a6..d07eb52 100644
--- src/hg/hgTracks/simpleTracks.c
+++ src/hg/hgTracks/simpleTracks.c
@@ -1941,78 +1941,81 @@
     {
     AllocVar(bed);
     bed->chromStart = el->chromStart;
     bed->chromEnd = el->chromEnd;
     bed->chrom = cloneString(el->chrom);
     bed->name = cloneString(el->name);
     slAddHead(&list, bed);
     }
 /* reset globals */
 chromName = chromNameCopy;
 winStart = winStartCopy;
 winEnd = winEndCopy;
 return list;
 }
 
-boolean oregannoFilterType (struct oreganno *el)
+
+
+
+boolean oregannoFilterType (struct oreganno *el, struct hash *attrTable)
 /* filter of the type of region from the oregannoAttr table */
 {
 int cnt = 0;
-struct oregannoAttr *attr = NULL;
-char query[256];
-struct sqlConnection *conn = hAllocConn(database);
+struct oregannoAttr *attr = hashFindVal(attrTable, el->id);
+boolean tmpAttr = FALSE;
 
-sqlSafef(query, sizeof(query), "select * from oregannoAttr where id = '%s' and attribute = 'type'", el->id);
-attr = oregannoAttrLoadByQuery(conn, query);
-hFreeConn(&conn);
 if (attr == NULL)
     {
     AllocVar(attr);
     attr->attrVal = cloneString("NULL");
     attr->id = NULL; /* so free will work */
     attr->attribute = NULL;
+    tmpAttr = TRUE;
     }
+
 for (cnt = 0; cnt < oregannoTypeSize; cnt++)
     {
     if ((!cartVarExists(cart, oregannoTypeString[cnt])
     ||  (cartString(cart, oregannoTypeString[cnt]) != NULL
         && differentString(cartString(cart, oregannoTypeString[cnt]), "0")))
         && (cmpWordsWithEmbeddedNumbers(oregannoTypeDbValue[cnt], attr->attrVal))==0)
         {
+        if (tmpAttr == TRUE)
             oregannoAttrFree(&attr);
         return TRUE; /* include this type */
         }
     }
+if (tmpAttr == TRUE)
     oregannoAttrFree(&attr);
 return FALSE;
 }
 
 void loadOreganno (struct track *tg)
 /* loads the oreganno track */
 {
 struct oreganno *list = NULL;
 struct sqlConnection *conn = hAllocConn(database);
 struct sqlResult *sr;
 char **row;
 int rowOffset;
 
 sr = hRangeQuery(conn, tg->table, chromName, winStart, winEnd,
                  NULL, &rowOffset);
 while ((row = sqlNextRow(sr)) != NULL)
     {
     struct oreganno *el = oregannoLoad(row);
-    if (!oregannoFilterType(el))
+    if (!oregannoFilterType(el, tg->attrTable))
         oregannoFree(&el);
     else
         slAddHead(&list, el);
     }
 sqlFreeResult(&sr);
 slReverse(&list);
 tg->items = list;
 hFreeConn(&conn);
 }
 
 struct bed* loadBigBedAsBed (struct track *tg, char *chr, int start, int end)
 /* load bigBed for a range, as a bed list (for next item button). Just grab one item */
 {
 struct bbiFile *bbiFile = fetchBbiForTrack(tg);
 struct lm *lm = lmInit(0);
@@ -11998,62 +12001,57 @@
 if (choice != NULL && sameString(choice, "type"))
     return gvColorByType(tg, item, hvg);
 else if (choice != NULL && sameString(choice, "count"))
     return gvColorByCount(tg, item, hvg);
 else if (choice != NULL && sameString(choice, "disease"))
     return gvColorByDisease(tg, item, hvg);
 else
     return gvColorByType(tg, item, hvg);
 }
 
 Color oregannoColor(struct track *tg, void *item, struct hvGfx *hvg)
 /* color items by type for ORegAnno track */
 {
 struct oreganno *el = item;
 struct oregannoAttr *details = NULL;
-struct sqlConnection *conn = hAllocConn(database);
 char *id = NULL;
-char query[256];
 Color itemColor = MG_BLACK;
 if (el->id != NULL)
     id = el->id;
 else
     id = el->name;
 
-sqlSafef(query, sizeof(query), "select * from oregannoAttr where attribute = 'type' and id = '%s'", id);
-details = oregannoAttrLoadByQuery(conn, query);
+details = hashFindVal(tg->attrTable, id);
 /* ORegAnno colors 666600 (Dark Green), CCCC66 (Tan), CC0033 (Red),
                    CCFF99 (Background Green)                        */
 if (sameString(details->attrVal, "REGULATORY POLYMORPHISM"))
     itemColor = hvGfxFindColorIx(hvg, 204, 0, 51); /* red */
 else if (sameString(details->attrVal, "TRANSCRIPTION FACTOR BINDING SITE"))
     itemColor = hvGfxFindColorIx(hvg, 165, 165, 65);  /* tan, darkened some */
 else if (sameString(details->attrVal, "REGULATORY REGION"))
     itemColor = hvGfxFindColorIx(hvg, 102, 102, 0);  /* dark green */
 /* New ORegAnno colors (colorblind friendly) */
 else if (sameString(details->attrVal, "Regulatory Polymorphism"))
     itemColor = hvGfxFindColorIx(hvg, 0, 114, 178); /* Blue */
 else if (sameString(details->attrVal, "Transcription Factor Binding Site"))
     itemColor = hvGfxFindColorIx(hvg, 230, 159, 0);  /* Orange */
 else if (sameString(details->attrVal, "Regulatory Region"))
     itemColor = hvGfxFindColorIx(hvg, 86, 180, 233);  /* Sky Blue */
 else if (sameString(details->attrVal, "Regulatory Haplotype"))
     itemColor = hvGfxFindColorIx(hvg, 213, 94, 0);  /* Vermillion */
 else if (sameString(details->attrVal, "miRNA Binding Site"))
     itemColor = hvGfxFindColorIx(hvg, 0, 158, 115);  /* bluish Green */
-oregannoAttrFreeList(&details);
-hFreeConn(&conn);
 return itemColor;
 }
 
 Color omiciaColor(struct track *tg, void *item, struct hvGfx *hvg)
 /* color by confidence score */
 {
 struct bed *el = item;
 if (sameString(tg->table, "omiciaHand"))
     return hvGfxFindColorIx(hvg, 0, 0, 0);
 else if (el->score < 200)
     return MG_BLACK;
 else if (el->score < 600)
     return hvGfxFindColorIx(hvg, 230, 130, 0); /* orange */
 else
     return MG_GREEN;
@@ -12306,30 +12304,33 @@
 }
 
 void protVarMethods (struct track *tg)
 /* name vs id, next items */
 {
 tg->loadItems = loadProtVar;
 tg->itemName = protVarName;
 tg->mapItemName = protVarMapName;
 tg->nextItemButtonable = TRUE;
 tg->nextPrevItem = linkedFeaturesLabelNextPrevItem;
 }
 
 void oregannoMethods (struct track *tg)
 /* load so can allow filtering on type */
 {
+struct sqlConnection *conn = hAllocConn(database);
+tg->attrTable = oregannoLoadAttrHash(conn);
+hFreeConn(&conn);
 tg->loadItems = loadOreganno;
 tg->itemColor = oregannoColor;
 tg->itemNameColor = oregannoColor;
 tg->nextItemButtonable = TRUE;
 tg->nextPrevItem = linkedFeaturesLabelNextPrevItem;
 }
 
 
 static char *omimGene2DisorderList(char *name)
 /* Return list of disorders associated with a OMIM entry.  Do not free result! */
 {
 static struct dyString *dy = NULL;
 struct sqlConnection *conn;
 char query[256];