3a63c684fc647b345d52e96147b3344453b3d28a
kate
  Fri Jul 31 10:12:14 2015 -0700
1. Fix tissue ordering problem (hgGtex). 2. Add stats table (rename gtexTissueData to gtexTissueMedian and use gtexTissueData for stats.  3. Extend gtexInfo table.refs #15645

diff --git src/hg/hgTracks/gtexTracks.c src/hg/hgTracks/gtexTracks.c
index 24816da..c266343 100644
--- src/hg/hgTracks/gtexTracks.c
+++ src/hg/hgTracks/gtexTracks.c
@@ -1,27 +1,28 @@
 /* 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 "gtexInfo.h"
 #include "gtexGeneBed.h"
 #include "gtexTissue.h"
+#include "gtexTissueData.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
@@ -96,33 +97,33 @@
 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 valToHeight(double val, double maxVal, int maxHeight)
-/* Convert a value from 0 to maxVal to 0 to maxHeight-1 */
+/* Log-scale and Convert a value from 0 to maxVal to 0 to maxHeight-1 */
+// TODO: support linear or log scale
 {
-// FIXME:  This sort of works.  Seems to be dropping some tho. e.g. check MRAP lung
 if (val == 0.0)
     return 0;
 // smallest counts are 1x10e-3, translate to counter negativity
 double scaled = (log10(val) + 3.001)/(log10(maxVal) + 3.001);
 if (scaled < 0)
     warn("scaled=%f\n", scaled);
 //uglyf("%.2f -> %.2f height %d", val, scaled, (int)scaled * (maxHeight-1));
 return (scaled * (maxHeight-1));
 }
 
 // TODO: whack this
 static int valToY(double val, double maxVal, int maxHeight)
 /* Convert a value from 0 to maxVal to 0 to height-1 */
 {
 if (val == 0.0)
@@ -197,45 +198,66 @@
 {
     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;
+
+struct sqlConnection *conn = hAllocConn("hgFixed");
+if (conn == NULL)
+    return NULL;
+char query[1024];
+
 AllocVar(medians);
-/* TODO: move to lib */
 medians->expCount = geneBed->expCount;
-AllocArray(medians->expScores, medians->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 (i = 0; i < medians->expCount; ++i)
-    medians->expScores[i] = geneBed->expScores[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);
+    }
+AllocArray(medians->expScores, medians->expCount);
+for (i=0; i<geneBed->expCount; i++)
+    medians->expScores[i] = slDoubleMedian(scores[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;
@@ -422,48 +444,68 @@
     //TODO: use valToHeight
     int yMedian = valToY(expScore, maxMedian, gtexGraphHeight()) + y;
     int height = yZero - yMedian;
     // 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, 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)
 {
-return gtexGeneBedLoad(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();
+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;
 }
 
 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)