2e8710df040089d0492249b1b83b1da579b4ec40
kate
  Tue Aug 4 19:49:13 2015 -0700
More prep moved to loader.  Fix mapping problem in squish mode. refs #15645

diff --git src/hg/hgTracks/gtexTracks.c src/hg/hgTracks/gtexTracks.c
index 6743353..3c0c01d 100644
--- src/hg/hgTracks/gtexTracks.c
+++ src/hg/hgTracks/gtexTracks.c
@@ -204,30 +204,31 @@
 }
 
 static int gtexGeneMargin()
 {
     return 1;
 }
 
 /* Track info generated during load, used by draw */
 
 struct gtexGeneExtras 
 /* Track info */
     {
     double maxMedian;
     char *graphType;
     boolean isComparison;
+    struct rgbColor *colors;
     };
 
 struct gtexGeneInfo
 /* GTEx gene model, names, and expression medians */
     {
     struct gtexGeneInfo *next;  /* Next in singly linked list */
     struct gtexGeneBed *geneBed;/* Gene name, id, canonical transcript, exp count and medians 
                                         from BED table */
     struct genePred *geneModel; /* Gene structure from model table */
     float *medians1;            /* Computed medians */
     float *medians2;            /* Computed medians for comparison (inverse) graph */
     };
 
 static struct gtexGeneBed *loadComputedMedians(struct gtexGeneBed *geneBed, char *graphType)
 /* Compute medians based on graph type.  Returns a list of 2 for comparison graph types */
@@ -318,30 +319,46 @@
 extras->maxMedian = gtexMaxMedianScore(NULL);
 
 // Construct track items
 
 // Get geneModels in range
 //TODO: version the table name ?
 char *modelTable = "gtexGeneModel";
 struct hash *modelHash = loadGeneModels(modelTable);
 
 // Get geneBeds (names and all-sample tissue median scores) in range
 bedLoadItem(tg, tg->table, (ItemLoader)gtexGeneBedLoad);
 
 // Create geneInfo items with BED and geneModels attached
 struct gtexGeneInfo *geneInfo = NULL, *list = NULL;
 struct gtexGeneBed *geneBed = (struct gtexGeneBed *)tg->items;
+
+char *colorScheme = cartUsualStringClosestToHome(cart, tg->tdb, FALSE, GTEX_COLORS, 
+                        GTEX_COLORS_DEFAULT);
+if (sameString(colorScheme, GTEX_COLORS_GTEX))
+    {
+    // retrieve from table
+    extras->colors = getGtexTissueColors();
+    }
+else
+    {
+    // currently the only other choice
+    int expCount = geneBed->expCount;
+    extras->colors = getRainbow(&saturatedRainbowAtPos, expCount);
+    //colors = getRainbow(&lightRainbowAtPos, expCount);
+    }
+
 while (geneBed != NULL)
     {
     AllocVar(geneInfo);
     geneInfo->geneBed = geneBed;
     geneInfo->geneModel = hashFindVal(modelHash, geneBed->geneId);
     slAddHead(&list, geneInfo);
     geneBed = geneBed->next;
     geneInfo->geneBed->next = NULL;
     }
 slReverse(&list);
 tg->items = list;
 }
 
 static void gtexGeneDrawAt(struct track *tg, void *item, struct hvGfx *hvg, int xOff, int y, 
                 double scale, MgFont *font, Color color, enum trackVisibility vis)
@@ -382,49 +399,37 @@
 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();
+
+// Move to loader
 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
-    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];
+    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 = 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);
@@ -440,31 +445,31 @@
 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 (!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];
+    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 = geneBed->expScores[i];
     int height = valToHeight(expScore, maxMedian, gtexGraphHeight());
     // TODO: adjust yGene instead of yMedian+1 to get gene track distance as desired
     //if (i ==0) uglyf("DRAW2: expScore=%.2f, maxMedian=%.2f, graphHeight=%d, y=%d<br>", expScore, maxMedian, 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);
@@ -472,30 +477,31 @@
         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);
+    return;
     }
 
 struct gtexTissue *tissues = getTissues();
 struct gtexTissue *tissue = NULL;
 struct gtexGeneInfo *geneInfo = item;
 struct gtexGeneBed *geneBed = geneInfo->geneBed;
 int barWidth = gtexBarWidth();
 int padding = gtexGraphPadding();
 double maxMedian = ((struct gtexGeneExtras *)tg->extraUiData)->maxMedian;
 
 int graphX = gtexGraphX(geneBed);
 if (graphX < 0)
     return;
 // x1 is at left of graph
 int x1 = insideX + graphX;