c28867b853a769949049ae459cee687bcad25aca
kate
  Fri Jul 24 13:07:01 2015 -0700
Add support for comparison graphs (2 graphs, one top and one below inverted, based on splitting samples by sex or age).  First cut of display only. refs #15645

diff --git src/hg/hgTracks/gtexTracks.c src/hg/hgTracks/gtexTracks.c
index ba91a5a..c4d9df3 100644
--- src/hg/hgTracks/gtexTracks.c
+++ src/hg/hgTracks/gtexTracks.c
@@ -93,46 +93,56 @@
 else if (winSize < WIN_MED_GRAPH)
     return MED_GRAPH_HEIGHT;
 else
     return MIN_GRAPH_HEIGHT;
 }
 
 static int gtexGraphWidth(struct gtexGeneBed *gtex)
 /* Width of GTEx graph in pixels */
 {
 int barWidth = gtexBarWidth();
 int padding = gtexGraphPadding();
 int count = gtex->expCount;
 return (barWidth * count) + (padding * (count-1));
 }
 
-static int valToY(double val, double maxVal, int height)
+
+static int valToHeight(double val, double maxVal, int maxHeight)
+/* Convert a value from 0 to maxVal to 0 to maxHeight-1 */
+{
+double scaled = val/maxVal;
+return (scaled * (maxHeight-1));
+}
+
+static int valToY(double val, double maxVal, int maxHeight)
 /* Convert a value from 0 to maxVal to 0 to height-1 */
 {
 double scaled = val/maxVal;
-int y = scaled * (height);
-return (height-1) - y;
+int y = scaled * (maxHeight);
+return (maxHeight-1) - y;
+
 //int y = scaled * (height-1);
 //return (height - 1) - y;
 }
 
 struct gtexGeneExtras 
     {
-    double maxExp;
+    double maxExp; // TODO: remove this local normalization factor
+    char *graphType;
+    boolean isComparison;
     };
 
-
 /* Cache tissue metadata */
 
 struct gtexTissue *getGtexTissues()
 /* 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);
     }
 return gtexTissues;
 }
@@ -170,71 +180,108 @@
 //if (x1 + width > x2)
     //return -1;
 return x1;
 }
 
 static int gtexGeneHeight()
 {
     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);
+/* TODO: move to lib */
+medians->expCount = geneBed->expCount;
+AllocArray(medians->expScores, medians->expCount);
+int i;
+for (i = 0; i < medians->expCount; ++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;
+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;
 else if (sameString(geneBed->transcriptClass, "nonCoding"))
     statusColor = statusColors.nonCoding;
 else if (sameString(geneBed->transcriptClass, "problem"))
     statusColor = statusColors.problem;
 else 
     statusColor = statusColors.unknown;
 
 if (vis != tvFull && vis != tvPack)
     {
     bedDrawSimpleAt(tg, item, hvg, xOff, y, scale, font, statusColor, vis);
     return;
     }
+struct gtexGeneBed *computedMedians = NULL;     // 1 or 2 (if comparison) 
+                                                // with medians computed for sample subsets
+
+struct gtexGeneExtras *extras = (struct gtexGeneExtras *)tg->extraUiData;
+if ((extras->isComparison) &&
+        (tg->visibility == tvFull || tg->visibility == tvPack) && 
+        gtexGraphHeight() != MIN_GRAPH_HEIGHT)
+    {
+    // compute medians based on configuration (comparisons, and later, filters)
+    computedMedians = loadComputedMedians(geneBed, extras->graphType);
+    }
 int i;
 int expCount = geneBed->expCount;
 double maxExp = ((struct gtexGeneExtras *)tg->extraUiData)->maxExp;
 struct rgbColor lineColor = {.r=0};
 int lineColorIx = hvGfxFindColorIx(hvg, lineColor.r, lineColor.g, lineColor.b);
 int heightPer = tg->heightPer;
 
 int graphX = gtexGraphX(geneBed);
 if (graphX < 0)
     return;
 // x1 is at left of graph
 int x1 = xOff + graphX;
+int startX = x1;
 
 // yZero is at bottom of graph
 int yZero = gtexGraphHeight() + y - 1;
 
 // draw faint line under graph to delineate extent when bars are missing (tissue w/ 0 expression)
+// TODO: skip missing bars -- then we can lose the gray line (at least for non-comparison mode)
 Color lightGray = MAKECOLOR_32(0xD1, 0xD1, 0xD1);
 int graphWidth = gtexGraphWidth(geneBed);
 hvGfxBox(hvg, x1, yZero+1, graphWidth, 1, lightGray);
 
 //uglyf("DRAW: xOff=%d, x1=%d, y=%d, yZero=%d<br>", xOff, x1, y, yZero);
 
 int barWidth = gtexBarWidth();
 int graphPadding = gtexGraphPadding();
 char *colorScheme = cartUsualStringClosestToHome(cart, tg->tdb, FALSE, GTEX_COLORS, 
                         GTEX_COLORS_DEFAULT);
 struct rgbColor *colors;
 if (sameString(colorScheme, GTEX_COLORS_GTEX))
     {
     // retrieve from table
     // TODO: cache this
@@ -248,31 +295,32 @@
     //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);
         }
     int fillColorIx = hvGfxFindColorIx(hvg, fillColor.r, fillColor.g, fillColor.b);
     double expScore = geneBed->expScores[i];
     int yMedian = valToY(expScore, maxExp, gtexGraphHeight()) + y;
-    //int height = yZero - yMedian + 1;
+    //int foo = valToY(expScore, maxExp, gtexGraphHeight());
+    //int yMedian = yZero - height;
     int height = yZero - yMedian;
     // TODO: adjust yGene instead of yMedian+1 to get gene track distance as desired
     //if (i ==0) uglyf("DRAW: expScore=%.2f, maxExp=%.2f, graphHeight=%d, y=%d<br>", expScore, maxExp, 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, yMedian+1, barWidth, height, fillColorIx);
     else
         hvGfxOutlinedBox(hvg, x1, yMedian+1, barWidth, height, fillColorIx, lineColorIx);
     x1 = x1 + barWidth + graphPadding;
     }
 
 // mark gene extent
 int yGene = yZero + gtexGeneMargin() - 1;
 
 // load & draw gene model
@@ -288,30 +336,61 @@
 struct genePred *geneModel = NULL;
 if (sr != NULL)
     {
     if ((row = sqlNextRow(sr)) != NULL)
         geneModel = genePredLoad(row);
     sqlFreeResult(&sr);
 }
 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
+        struct hslColor hsl = mgRgbToHsl(fillColor);
+        hsl.s = min(1000, hsl.s + 300);
+        fillColor = mgHslToRgb(hsl);
+        }
+    int fillColorIx = hvGfxFindColorIx(hvg, fillColor.r, fillColor.g, fillColor.b);
+    double expScore = geneBed->expScores[i];
+    int height = valToHeight(expScore, maxExp, gtexGraphHeight());
+    // TODO: adjust yGene instead of yMedian+1 to get gene track distance as desired
+    //if (i ==0) uglyf("DRAW2: expScore=%.2f, maxExp=%.2f, graphHeight=%d, y=%d<br>", expScore, maxExp, gtexGraphHeight(), y);
+    //if (i ==0) uglyf("DRAW2: yZero=%d, height=%d<br>", yZero, height);
+    if (graphPadding == 0 || sameString(colorScheme, GTEX_COLORS_GTEX))
+        hvGfxBox(hvg, x1, yZero, barWidth, height, fillColorIx);
+    else
+        hvGfxOutlinedBox(hvg, x1, yZero, barWidth, height, fillColorIx, lineColorIx);
+    x1 = x1 + barWidth + graphPadding;
+    }
 }
 
 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();
@@ -337,71 +416,88 @@
     // TODO: call genericMapItem
     //genericMapItem(tg, hvg, item, itemName, tissue->description, start, end, x1, y, barWidth, height);
     mapBoxHc(hvg, start, end, x1, yMedian+1, barWidth, height, tg->track, mapItemName, tissue->description);
     //if (i==0) uglyf("MAP: expScore=%.2f, maxExp=%.2f, graphHeight=%d, y=%d<br>", expScore, maxExp, 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)
 {
 return gtexGeneBedLoad(row);
 }
 
-static void gtexGeneLoadItems(struct track *track)
+static void gtexGeneLoadItems(struct track *tg)
 {
-bedLoadItem(track, track->table, (ItemLoader)loadGtexGeneBed);
+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;
+
 double maxExp = 0.0;
 int i;
 struct gtexGeneBed *geneBed;
-for (geneBed = track->items; geneBed != NULL; geneBed = geneBed->next)
+// TODO: Remove this fake (window-based) normalization
+for (geneBed = tg->items; geneBed != NULL; geneBed = geneBed->next)
     for (i=0; i<geneBed->expCount; i++)
         maxExp = (geneBed->expScores[i] > maxExp ? geneBed->expScores[i] : maxExp);
-struct gtexGeneExtras *extras;
-AllocVar(extras);
 extras->maxExp = maxExp;
-track->extraUiData = extras;
-
 }
 
-static int gtexGeneItemHeight(struct track *track, void *item)
+static int gtexGeneItemHeight(struct track *tg, void *item)
 {
-if ((item == NULL) || (track->visibility == tvSquish) || (track->visibility == tvDense))
+if ((item == NULL) || (tg->visibility == tvSquish) || (tg->visibility == tvDense))
     return 0;
-return gtexGraphHeight() + + gtexGeneMargin() + gtexGeneHeight();
+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 *track, enum trackVisibility vis)
+static int gtexTotalHeight(struct track *tg, enum trackVisibility vis)
 /* Figure out total height of track */
 {
 int height;
-if (track->visibility == tvSquish || track->visibility == tvDense)
+int extra = 0;
+if (((struct gtexGeneExtras *)tg->extraUiData)->isComparison)
+    extra = gtexGraphHeight() + 2;
+if (tg->visibility == tvSquish || tg->visibility == tvDense)
     height = 10;
 else 
-    height = gtexGraphHeight() + gtexGeneMargin() + gtexGeneHeight();
-return tgFixedTotalHeightOptionalOverflow(track, vis, height, height, FALSE);
+    height = gtexGraphHeight() + gtexGeneMargin() + gtexGeneHeight() + extra;
+//uglyf("GTEX totalHeight = %d<br>", height);
+return tgFixedTotalHeightOptionalOverflow(tg, vis, height, height, FALSE);
 }
 
-static int gtexGeneItemEnd(struct track *track, void *item)
+static int gtexGeneItemEnd(struct track *tg, void *item)
 /* Return end chromosome coordinate of item, including graph */
 {
 struct gtexGeneBed *geneBed = (struct gtexGeneBed *)item;
 double scale = scaleForWindow(insideWidth, winStart, winEnd);
 int graphWidth = gtexGraphWidth(geneBed);
 return max(geneBed->chromEnd, max(winStart, geneBed->chromStart) + graphWidth/scale);
 }
 
 
-void gtexGeneMethods(struct track *track)
+void gtexGeneMethods(struct track *tg)
 {
-track->drawItemAt = gtexGeneDrawAt;
-track->loadItems = gtexGeneLoadItems;
-track->mapItem = gtexGeneMapItem;
-track->itemHeight = gtexGeneItemHeight;
-track->itemEnd = gtexGeneItemEnd;
-track->totalHeight = gtexTotalHeight;
+tg->drawItemAt = gtexGeneDrawAt;
+tg->loadItems = gtexGeneLoadItems;
+tg->mapItem = gtexGeneMapItem;
+tg->itemHeight = gtexGeneItemHeight;
+tg->itemEnd = gtexGeneItemEnd;
+tg->totalHeight = gtexTotalHeight;
 }