ebb8b21be417b0d17cdc4ace34c8c82e558f2b5e
kate
  Thu Oct 22 21:49:39 2015 -0700
Fix map boxes on non-log display. refs #15645

diff --git src/hg/hgTracks/gtexTracks.c src/hg/hgTracks/gtexTracks.c
index d481cc7..5d8f1da 100644
--- src/hg/hgTracks/gtexTracks.c
+++ src/hg/hgTracks/gtexTracks.c
@@ -376,30 +376,45 @@
 static int valToHeight(double val, double maxVal, int maxHeight, boolean doLogTransform)
 /* Log-scale and convert a value from 0 to maxVal to 0 to maxHeight-1 */
 {
 if (val == 0.0)
     return 0;
 double scaled = 0.0;
 if (doLogTransform)
     scaled = log10(val+1.0) / log10(maxVal+1.0);
 else
     scaled = val/maxVal;
 if (scaled < 0)
     warn("scaled=%f\n", scaled);
 return (scaled * (maxHeight-1));
 }
 
+static int valToClippedHeight(double val, double maxVal, int maxView, int maxHeight, 
+                                        boolean doLogTransform)
+/* Convert a value from 0 to maxVal to 0 to maxHeight-1, with clipping, or log transform the value */
+{
+double useVal = val;
+double useMax = maxVal;
+if (!doLogTransform)
+    {
+    useMax = maxView;
+    if (val > maxView)
+        useVal = maxView;
+    }
+return valToHeight(useVal, useMax, gtexGraphHeight(), doLogTransform);
+}
+
 static void drawGraphBase(struct hvGfx *hvg, int x, int y, struct gtexGeneInfo *geneInfo)
 /* Draw faint line under graph to delineate extent when bars are missing (tissue w/ 0 expression) */
 {
 Color lightGray = MAKECOLOR_32(0xD1, 0xD1, 0xD1);
 int graphWidth = gtexGraphWidth(geneInfo);
 hvGfxBox(hvg, x, y, graphWidth, 1, lightGray);
 }
 
 static void gtexGeneDrawAt(struct track *tg, void *item, struct hvGfx *hvg, int xOff, int y, 
                 double scale, MgFont *font, Color color, enum trackVisibility vis)
 /* Draw tissue expression bar graph over gene model. 
    Optionally, draw a second graph under gene, to compare sample sets */
 {
 struct gtexGeneInfo *geneInfo = (struct gtexGeneInfo *)item;
 struct gtexGeneBed *geneBed = geneInfo->geneBed;
@@ -429,70 +444,63 @@
 int keepX = x1;                 // FIXME:  Too many X's!
 drawGraphBase(hvg, keepX, yZero+1, geneInfo);
 
 int startX = x1;
 int i;
 int expCount = geneBed->expCount;
 double maxMedian = ((struct gtexGeneExtras *)tg->extraUiData)->maxMedian;
 struct rgbColor lineColor = {.r=0};
 int lineColorIx = hvGfxFindColorIx(hvg, lineColor.r, lineColor.g, lineColor.b);
 int barWidth = gtexBarWidth();
 int graphPadding = gtexGraphPadding();
 char *colorScheme = cartUsualStringClosestToHome(cart, tg->tdb, FALSE, GTEX_COLORS, 
                         GTEX_COLORS_DEFAULT);
 Color labelColor = MG_GRAY;
 Color clipColor = MG_MAGENTA;
-
-double viewMax = (double)cartUsualIntClosestToHome(cart, tg->tdb, FALSE, GTEX_MAX_LIMIT, GTEX_MAX_LIMIT_DEFAULT);
+double viewMax = (double)cartUsualIntClosestToHome(cart, tg->tdb, FALSE, 
+                                GTEX_MAX_LIMIT, GTEX_MAX_LIMIT_DEFAULT);
 
 // add labels to comparison graphs
 // TODO: generalize
 if (geneInfo->medians2)
     {
     hvGfxText(hvg, x1, yZero - tl.fontHeight, labelColor, font, "F");
     hvGfxText(hvg, x1, yZero + gtexGeneHeight() + gtexGeneMargin(), labelColor, font, "M");
     startX = startX + tl.mWidth+2;
     x1 = startX;
     }
 
 // draw bar graph
 // TODO: share this code with other graph
 for (i=0; i<expCount; i++)
     {
     struct rgbColor fillColor = extras->colors[i];
     if (barWidth == 1 && sameString(colorScheme, GTEX_COLORS_GTEX))
         {
         // brighten colors a bit so they'll be more visible at this scale
         fillColor = gtexTissueBrightenColor(fillColor);
         }
     int fillColorIx = hvGfxFindColorIx(hvg, fillColor.r, fillColor.g, fillColor.b);
-
     double expScore = (geneInfo->medians1 ? geneInfo->medians1[i] : geneBed->expScores[i]);
-    double useScore = expScore;
-    double useMax = maxMedian;
-    if (!doLogTransform)
-        {
-        useMax = viewMax;
-        if (expScore > viewMax)
-            useScore = viewMax;
-        }
-    int height = valToHeight(useScore, useMax, gtexGraphHeight(), doLogTransform);
+    int height = valToClippedHeight(expScore, maxMedian, viewMax, 
+                                        gtexGraphHeight(), doLogTransform);
     if (graphPadding == 0 || sameString(colorScheme, GTEX_COLORS_GTEX))
         hvGfxBox(hvg, x1, yZero-height+1, barWidth, height, fillColorIx);
     else
         hvGfxOutlinedBox(hvg, x1, yZero-height+1, barWidth, height, fillColorIx, lineColorIx);
-    if (useScore != expScore)
+    // mark clipped bar with magenta tip
+    if (!doLogTransform && expScore > viewMax)
         hvGfxBox(hvg, x1, yZero-height+1, barWidth, 1, clipColor);
     x1 = x1 + barWidth + graphPadding;
     }
 #endif
 
 // draw gene model
 int yGene = yZero + gtexGeneMargin()-1;
 tg->heightPer = gtexGeneHeight()+1;
 struct linkedFeatures *lf = linkedFeaturesFromGenePred(tg, geneInfo->geneModel, FALSE);
 lf->filterColor = statusColor;
 linkedFeaturesDrawAt(tg, lf, hvg, xOff, yGene, scale, font, color, tvSquish);
 tg->heightPer = heightPer;
 
 if (!geneInfo->medians2)
     return;
@@ -503,44 +511,36 @@
 yZero = yGene + gtexGeneHeight() + 1; // yZero is at top of graph
 drawGraphBase(hvg, keepX, yZero-1, geneInfo);
 
 for (i=0; i<expCount; i++)
     {
     struct rgbColor fillColor = extras->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 = geneInfo->medians2[i];
-    double useScore = expScore;
-    double useMax = maxMedian;
-    if (!doLogTransform)
-        {
-        useMax = viewMax;
-        if (expScore > viewMax)
-            useScore = viewMax;
-        }
-    int height = valToHeight(useScore, useMax, gtexGraphHeight(), doLogTransform);
+    int height = valToClippedHeight(expScore, maxMedian, viewMax, gtexGraphHeight(), doLogTransform);
     if (graphPadding == 0 || sameString(colorScheme, GTEX_COLORS_GTEX))
         hvGfxBox(hvg, x1, yZero, barWidth, height, fillColorIx);
     else
         hvGfxOutlinedBox(hvg, x1, yZero, barWidth, height, fillColorIx, lineColorIx);
-    if (useScore != expScore)
+    if (!doLogTransform && expScore > viewMax)
         hvGfxBox(hvg, x1, yZero + height, barWidth, 1, clipColor);
     x1 = x1 + barWidth + graphPadding;
     }
 #endif
 }
 
 #ifdef MULTI_REGION
 static int gtexGeneNonPropPixelWidth(struct track *tg, void *item)
 /* Return end chromosome coordinate of item, including graph */
 {
 struct gtexGeneInfo *geneInfo = (struct gtexGeneInfo *)item;
 int graphWidth = gtexGraphWidth(geneInfo);
 return graphWidth;
 }
 #endif
@@ -677,42 +677,42 @@
 int graphX = gtexGraphX(geneBed);
 if (graphX < 0)
     return;
 // x1 is at left of graph
 int x1 = insideX + graphX;
 
 if (geneInfo->medians2)
     {
     // skip over labels in comparison graphs
     x1 = x1 + tl.mWidth+ 2;
     }
 int i = 0;
 int yZero = gtexGraphHeight() + y - 1;
 boolean doLogTransform = cartUsualBooleanClosestToHome(cart, tg->tdb, FALSE, GTEX_LOG_TRANSFORM, 
                                                 GTEX_LOG_TRANSFORM_DEFAULT);
+double viewMax = (double)cartUsualIntClosestToHome(cart, tg->tdb, FALSE, 
+                                GTEX_MAX_LIMIT, GTEX_MAX_LIMIT_DEFAULT);
 for (tissue = tissues; tissue != NULL; tissue = tissue->next, i++)
     {
-    double expScore = geneBed->expScores[i];
-    int height = valToHeight(expScore, maxMedian, gtexGraphHeight(), doLogTransform);
-    int yMedian = yZero - height;
-// FIXME: need proper scaling for clipped (non-log) display
-    mapBoxHc(hvg, start, end, x1, yMedian+1, barWidth, height, tg->track, mapItemName, tissue->description);
+    int height = valToClippedHeight(geneBed->expScores[i], maxMedian, viewMax, 
+                                        gtexGraphHeight(), doLogTransform);
+    mapBoxHc(hvg, start, end, x1, yZero-height, barWidth, height, tg->track, mapItemName, tissue->description);
     // add map box to comparison graph
     if (geneInfo->medians2)
         {
         double expScore = geneInfo->medians2[i];
-        int height = valToHeight(expScore, maxMedian, gtexGraphHeight(), doLogTransform);
+        int height = valToClippedHeight(expScore, maxMedian, viewMax, gtexGraphHeight(), doLogTransform);
         int y = yZero + gtexGeneHeight() + gtexGeneMargin();
         mapBoxHc(hvg, start, end, x1, y, barWidth, height, tg->track, mapItemName, tissue->description);
         }
     x1 = x1 + barWidth + padding;
     }
 }
 
 static char *gtexGeneItemName(struct track *tg, void *item)
 /* Return gene name */
 {
 struct gtexGeneInfo *geneInfo = (struct gtexGeneInfo *)item;
 struct gtexGeneBed *geneBed = geneInfo->geneBed;
 return geneBed->name;
 }