0842d0243de1cbe7e9d579e3792b92e3b6a552ab
kate
  Mon Aug 3 23:16:48 2015 -0700
Fix a few bugs - connection handling, bin field in genePred, more color handling - introduced while messing with schemas (to extend features and improve perf). refs #15645

diff --git src/hg/hgTracks/gtexTracks.c src/hg/hgTracks/gtexTracks.c
index c266343..a0c8552 100644
--- src/hg/hgTracks/gtexTracks.c
+++ src/hg/hgTracks/gtexTracks.c
@@ -133,48 +133,43 @@
     warn("scaled=%f\n", scaled);
 int y = scaled * (maxHeight);
 //uglyf("%.2f -> %.2f height %d", val, scaled, (int)scaled * (maxHeight-1));
 return (maxHeight-1) - y;
 }
 
 struct gtexGeneExtras 
     {
     double maxMedian;
     char *graphType;
     boolean isComparison;
     };
 
 /* Cache tissue metadata */
 
-struct gtexTissue *getGtexTissues()
+struct gtexTissue *getTissues()
 /* Get tissue metadata from database */
 {
 static struct gtexTissue *gtexTissues = NULL;
 if (gtexTissues == NULL)
-    {
-    char query[1024];
-    struct sqlConnection *conn = sqlConnect("hgFixed");
-    sqlSafef(query, sizeof(query), "select * from gtexTissue order by id");
-    gtexTissues = gtexTissueLoadByQuery(conn, query);
-    }
+    gtexTissues = gtexGetTissues();
 return gtexTissues;
 }
 
 struct rgbColor *getGtexTissueColors()
 /* Get RGB colors from tissue table */
 {
-struct gtexTissue *tissues = getGtexTissues();
+struct gtexTissue *tissues = getTissues();
 struct gtexTissue *tissue = NULL;
 int count = slCount(tissues);
 struct rgbColor *colors;
 AllocArray(colors, count);
 int i = 0;
 for (tissue = tissues; tissue != NULL; tissue = tissue->next)
     {
     // TODO: reconcile 
     colors[i] = (struct rgbColor){.r=COLOR_32_BLUE(tissue->color), .g=COLOR_32_GREEN(tissue->color), .b=COLOR_32_RED(tissue->color)};
     //colors[i] = mgColorIxToRgb(NULL, tissue->color);
     i++;
     }
 return colors;
 }
 
@@ -199,65 +194,71 @@
     return 8; 
 }
 
 static int gtexGeneMargin()
 {
     return 1;
 }
 
 static struct gtexGeneBed *loadComputedMedians(struct gtexGeneBed *geneBed, char *graphType)
 /* Compute medians based on graph type.  Returns a list of 2 for comparison graph types */
 /* TODO: add support for filter function */
 {
 /* FIXME: dummy load of two for display implementation */
 struct gtexGeneBed *medians = NULL, *medians2 = NULL;
 
+AllocVar(medians);
+medians->expCount = geneBed->expCount;
+int i;
+
+#ifdef NEW
 struct sqlConnection *conn = hAllocConn("hgFixed");
 if (conn == NULL)
     return NULL;
 char query[1024];
 
-AllocVar(medians);
-medians->expCount = geneBed->expCount;
 // FIXME: experiment with query on sex
+
 sqlSafef(query, sizeof(query), "select gtexTissue.id, gtexSampleData.score from gtexTissue, gtexSampleData, gtexSample, gtexDonor where gtexSampleData.tissue=gtexTissue.name and gtexSampleData.geneId='%s' and gtexSampleData.sample=gtexSample.name and gtexSample.donor=gtexDonor.name and gtexDonor.gender='F'", geneBed->geneId);
 struct slDouble **scores = NULL, *score = NULL;
 AllocArray(scores, geneBed->expCount);
 struct slPair *tissueScore = NULL, *tissueScores = sqlQuickPairList(conn, query);
-int i;
 for (tissueScore = tissueScores; tissueScore != NULL; tissueScore = tissueScore->next)
     {
     AllocVar(score);
     i = sqlUnsigned(tissueScore->name);
     if (scores[i] == NULL)
         scores[i] = score;
     else
         slAddHead(&scores[i], score);
     }
+hFreeConn(&conn);
+#endif
+
 AllocArray(medians->expScores, medians->expCount);
 for (i=0; i<geneBed->expCount; i++)
-    medians->expScores[i] = slDoubleMedian(scores[i]);
+    //medians->expScores[i] = slDoubleMedian(scores[i]);
+    medians->expScores[i] = geneBed->expScores[i];
 
 AllocVar(medians2);
 medians2->expCount = geneBed->expCount;
 AllocArray(medians2->expScores, medians2->expCount);
 for (i = 0; i < medians2->expCount; ++i)
     medians2->expScores[i] = geneBed->expScores[i];
 
 medians->next = medians2;
 
-hFreeConn(&conn);
 return medians;
 }
 
 static void gtexGeneDrawAt(struct track *tg, void *item, struct hvGfx *hvg, int xOff, int y, 
                 double scale, MgFont *font, Color color, enum trackVisibility vis)
 {
 struct gtexGeneBed *geneBed = item;
 initGeneColors(hvg);
 //warn("item: %s, xOff=%d\n", geneBed->name, xOff);
 // Color using transcriptClass
 Color statusColor;
 if (geneBed->transcriptClass == NULL)
     statusColor = statusColors.unknown;
 else if (sameString(geneBed->transcriptClass, "coding"))
     statusColor = statusColors.coding;
@@ -321,75 +322,72 @@
     colors = getGtexTissueColors();
     }
 else
     {
     // currently the only other choice
     // TODO: cache this
     colors = getRainbow(&saturatedRainbowAtPos, expCount);
     //colors = getRainbow(&lightRainbowAtPos, expCount);
     }
 for (i=0; i<expCount; i++)
     {
     struct rgbColor fillColor = colors[i];
     if (barWidth == 1 && sameString(colorScheme, GTEX_COLORS_GTEX))
         {
         // brighten colors a bit so they'll be more visible at this scale
-        struct hslColor hsl = mgRgbToHsl(fillColor);
-        hsl.s = min(1000, hsl.s + 300);
-        fillColor = mgHslToRgb(hsl);
+        fillColor = gtexTissueBrightenColor(fillColor);
         }
     int fillColorIx = hvGfxFindColorIx(hvg, fillColor.r, fillColor.g, fillColor.b);
     double expScore = geneBed->expScores[i];
     int height = valToHeight(expScore, maxMedian, gtexGraphHeight());
     // TODO: adjust yGene to get gene track distance as desired
     //if (i ==0) uglyf("DRAW: expScore=%.2f, maxMedian=%.2f, graphHeight=%d, y=%d<br>", expScore, maxMedian, gtexGraphHeight(), y);
     //if (i ==0) uglyf("DRAW: yZero=%d, yMedian=%d, height=%d<br>", yZero, yMedian, height);
     if (graphPadding == 0 || sameString(colorScheme, GTEX_COLORS_GTEX))
         hvGfxBox(hvg, x1, yZero-height, barWidth, height, fillColorIx);
     else
         hvGfxOutlinedBox(hvg, x1, yZero-height, barWidth, height, fillColorIx, lineColorIx);
     x1 = x1 + barWidth + graphPadding;
     }
 
 // mark gene extent
 int yGene = yZero + gtexGeneMargin() - 1;
 
 // load & draw gene model
 
 char query[1024];
 char **row;
-sqlSafef(query, sizeof query, "select * from gtexGeneModel where name='%s'", geneBed->geneId);
+// FIXME: move to load items (load all in range)
+char *modelTable = "gtexGeneModel";
+int hasBin = hOffsetPastBin(database, geneBed->chrom, modelTable);
+sqlSafef(query, sizeof query, "select * from %s where name='%s'", modelTable, geneBed->geneId);
 struct sqlConnection *conn = hAllocConn(database);
 if (conn == NULL)
     return;
-//uglyf("query: %s<br>", query);
-struct sqlResult *sr = sqlGetResult(conn, query);
 struct genePred *geneModel = NULL;
-if (sr != NULL)
-    {
+struct sqlResult *sr = sqlGetResult(conn, query);
 if ((row = sqlNextRow(sr)) != NULL)
-        geneModel = genePredLoad(row);
+    geneModel = genePredLoad(row + hasBin);
 sqlFreeResult(&sr);
-}
+hFreeConn(&conn);
 if (geneModel == NULL)
     return;
 struct linkedFeatures *lf = linkedFeaturesFromGenePred(tg, geneModel, FALSE);
 tg->heightPer = gtexGeneHeight()+1;
 lf->filterColor = statusColor;
 linkedFeaturesDrawAt(tg, lf, hvg, xOff, yGene, scale, font, color, tvSquish);
 tg->heightPer = heightPer;
-hFreeConn(&conn);
 
 if (!extras->isComparison || slCount(computedMedians) != 2)
     return;
 
 // draw comparison graph (upside down)
 
 x1 = startX;
 // yZero is at top of graph
 yZero = yGene + gtexGeneHeight();
 for (i=0; i<expCount; i++)
     {
     struct rgbColor fillColor = colors[i];
     if (barWidth == 1 && sameString(colorScheme, GTEX_COLORS_GTEX))
         {
         // brighten colors a bit so they'll be more visible at this scale
@@ -411,31 +409,31 @@
     }
 }
 
 static void gtexGeneMapItem(struct track *tg, struct hvGfx *hvg, void *item, char *itemName, 
                         char *mapItemName, int start, int end, int x, int y, int width, int height)
 /* Create a map box for each tissue (bar in the graph) or a single map for squish/dense modes */
 {
 //uglyf("map item: itemName=%s, mapItemName=%s, start=%d, end=%d, x=%d, y=%d, width=%d, height=%d, insideX=%d<br>",
         //itemName, mapItemName, start, end, x, y, width, height, insideX);
 
 if (tg->visibility == tvDense || tg->visibility == tvSquish)
     {
     genericMapItem(tg, hvg, item, itemName, itemName, start, end, x, y, width, height);
     }
 
-struct gtexTissue *tissues = getGtexTissues();
+struct gtexTissue *tissues = getTissues();
 struct gtexTissue *tissue = NULL;
 struct gtexGeneBed *gtex = item;
 int barWidth = gtexBarWidth();
 int padding = gtexGraphPadding();
 double maxMedian = ((struct gtexGeneExtras *)tg->extraUiData)->maxMedian;
 
 int graphX = gtexGraphX((struct gtexGeneBed *)item);
 if (graphX < 0)
     return;
 // x1 is at left of graph
 int x1 = insideX + graphX;
 
 int i = 0;
 int yZero = gtexGraphHeight() + y - 1;
 for (tissue = tissues; tissue != NULL; tissue = tissue->next, i++)
@@ -449,31 +447,31 @@
     mapBoxHc(hvg, start, end, x1, yMedian+1, barWidth, height, tg->track, mapItemName, tissue->description);
     //if (i==0) uglyf("MAP: expScore=%.2f, maxMedian=%.2f, graphHeight=%d, y=%d<br>", expScore, maxMedian, gtexGraphHeight(), y);
     //if (i==0) uglyf("MAP: x=%d, x1=%d, y=%d, yZero=%d<br>", x, x1, y, yZero); 
     //if (i==0) uglyf("MAP: yZero=%d, yMedian=%d, height=%d<br>", yZero, yMedian, height); 
     x1 = x1 + barWidth + padding;
     }
 }
 
 static struct gtexGeneBed *loadGtexGeneBed(char **row)
 {
 struct gtexGeneBed *geneBed = gtexGeneBedLoad(row);
 // TODO: rethink schemas
 // for now... replace expScores with medians from tissue data file
 
 #ifdef NEW
-struct gtexTissue *tissue = NULL, *tissues = getGtexTissues();
+struct gtexTissue *tissue = NULL, *tissues = getTissues();
 int i=0;
 char query[1024];
 struct sqlConnection *conn = hAllocConn("hgFixed");
 for (tissue = tissues; tissue != NULL; tissue = tissue->next, i++)
     {
     sqlSafef(query, sizeof(query), 
             "select * from gtexTissueData where geneId='%s' and tissue='%s'", 
                 geneBed->geneId, tissue->name);
     struct gtexTissueData *tissueData = gtexTissueDataLoadByQuery(conn, query);
     geneBed->expScores[i] = tissueData->median;
     }
 hFreeConn(&conn);
 #endif
 return geneBed;
 }
@@ -481,31 +479,30 @@
 static void gtexGeneLoadItems(struct track *tg)
 {
 bedLoadItem(tg, tg->table, (ItemLoader)loadGtexGeneBed);
 
 struct gtexGeneExtras *extras;
 AllocVar(extras);
 tg->extraUiData = extras;
 
 // TODO: move test to lib
 char *graphType = cartUsualStringClosestToHome(cart, tg->tdb, FALSE, GTEX_GRAPH, 
                                                 GTEX_GRAPH_DEFAULT);
 extras->graphType = cloneString(graphType);
 if (sameString(graphType, GTEX_GRAPH_AGE) || sameString(graphType, GTEX_GRAPH_SEX))
     extras->isComparison = TRUE;
 
-
 extras->maxMedian = gtexMaxMedianScore(NULL);
 }
 
 static int gtexGeneItemHeight(struct track *tg, void *item)
 {
 if ((item == NULL) || (tg->visibility == tvSquish) || (tg->visibility == tvDense))
     return 0;
 int extra = 0;
 if (((struct gtexGeneExtras *)tg->extraUiData)->isComparison)
     extra = gtexGraphHeight() + 2;
 //uglyf("GTEX itemHeight extra = %d<br>", extra);
 return gtexGraphHeight() + gtexGeneMargin() + gtexGeneHeight() + extra;
 }
 
 static int gtexTotalHeight(struct track *tg, enum trackVisibility vis)