5de752e3fa6fd4941978107956e2a87b4c5b46ee
kate
  Thu Jul 16 12:04:16 2015 -0700
Add support for GTEX tissue color scheme. refs #15645

diff --git src/hg/hgTracks/gtexTracks.c src/hg/hgTracks/gtexTracks.c
index 33e9b15..b30faee 100644
--- src/hg/hgTracks/gtexTracks.c
+++ src/hg/hgTracks/gtexTracks.c
@@ -1,25 +1,27 @@
 /* GTEX tracks  */
 
 /* Copyright (C) 2015 The Regents of the University of California 
  * See README in this or parent directory for licensing information. */
 
 #include "common.h"
 #include "hgTracks.h"
 #include "hvGfx.h"
 #include "rainbow.h"
 #include "gtexGeneBed.h"
+#include "gtexTissue.h"
+#include "gtexUi.h"
 
 #define WIN_MAX_GRAPH 20000
 #define MAX_GRAPH_HEIGHT 100
 #define MAX_BAR_WIDTH 5
 #define MAX_GRAPH_PADDING 2
 
 #define WIN_MED_GRAPH 500000
 #define MED_GRAPH_HEIGHT 60
 #define MED_BAR_WIDTH 3
 #define MED_GRAPH_PADDING 1
 
 #define MIN_GRAPH_HEIGHT 20
 #define MIN_BAR_WIDTH 1
 #define MIN_GRAPH_PADDING 0
 
@@ -95,84 +97,128 @@
 }
 
 static int valToY(double val, double maxVal, int height)
 /* Convert a value from 0 to maxVal to 0 to height-1 */
 {
 double scaled = val/maxVal;
 int y = scaled * (height-1);
 return height - 1 - y;
 }
 
 struct gtexGeneExtras 
     {
     double maxExp;
     };
 
+struct rgbColor *getGtexTissueColors()
+/* Get RGB colors from tissue table */
+{
+char query[1024];
+struct sqlConnection *conn = sqlConnect("hgFixed");
+sqlSafef(query, sizeof(query), "select * from gtexTissue order by id");
+struct gtexTissue *tissues = gtexTissueLoadByQuery(conn, query);
+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++;
+    }
+sqlDisconnect(&conn);
+return colors;
+}
+
 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;
 //warn("item: %s, xOff=%d\n", geneBed->name, xOff);
 if (vis != tvFull && vis != tvPack)
     {
     initGeneColors(hvg);
     // Color using transcriptClass
     if (geneBed->transcriptClass == NULL)
         color = statusColors.unknown;
     if (sameString(geneBed->transcriptClass, "coding"))
         color = statusColors.coding;
     else if (sameString(geneBed->transcriptClass, "nonCoding"))
         color = statusColors.nonCoding;
     else if (sameString(geneBed->transcriptClass, "problem"))
         color = statusColors.problem;
     else 
         color = statusColors.unknown;
 
     bedDrawSimpleAt(tg, item, hvg, xOff, y, scale, font, color, vis);
     return;
     }
 int i;
 int expCount = geneBed->expCount;
-double invExpCount = 1.0/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 start = max(geneBed->chromStart, winStart);
 //int geneEnd = min(geneBed->chromEnd, winEnd);
 //int width = expCount * (BAR_WIDTH + PADDING);
 //int end = min(expCount*(BAR_WIDTH + PADDING), winEnd);
 //if (start > end)
     //return;
 int x1 = round((start-winStart)*scale) + xOff;
 //int x2 = round((e-winStart)*scale) + xOff;
 
 int yBase = valToY(0, maxExp, heightPer) + y;
 //int yZero = yBase - 5;
 
 //int firstX = x1;
 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
+    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++)
     {
-    double colPos = invExpCount * i;
-    struct rgbColor fillColor = saturatedRainbowAtPos(colPos);
+    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, heightPer) + y;
-    if (graphPadding == 0)
+    if (graphPadding == 0 || sameString(colorScheme, GTEX_COLORS_GTEX))
         hvGfxBox(hvg, x1, yMedian, barWidth, yBase - yMedian, fillColorIx);
     else
         hvGfxOutlinedBox(hvg, x1, yMedian, barWidth, yBase - yMedian, fillColorIx, lineColorIx);
     //warn("x1=%d, yMedian=%d, height=%d\n", x1, yMedian, yBase-yMedian);
     x1 = x1 + barWidth + graphPadding;
     }
 // underline gene extent
 //hvGfxBox(hvg, firstX, yBase, round((geneEnd - start)*scale), 3, lineColorIx);
 
 // TODO: replace with cached table hash
 //char *tissueTable = "gtexTissue";
 //struct sqlConnection *conn = hAllocConn("hgFixed");
 //hFreeConn(&conn);
 }