d1d559e9bee7fcaec3a50ae43688ca436656ad7b
kate
  Wed Nov 18 08:25:58 2015 -0800
Finish variable height item packing. Adds an optional rowSizes field to spaceSaver. refs #15645

diff --git src/hg/hgTracks/gtexTracks.c src/hg/hgTracks/gtexTracks.c
index e8f65dc..03439e5 100644
--- src/hg/hgTracks/gtexTracks.c
+++ src/hg/hgTracks/gtexTracks.c
@@ -1,53 +1,56 @@
 /* GTEx (Genotype Tissue Expression) 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"
+// TODO: move spaceSaver code to simpleTracks
+#include "spaceSaver.h"
 
 // NOTE: Sections to change for multi-region (vertical slice) display 
 //       are marked with #ifdef MULTI_REGION.  WARNING: These sections
 //       are a bit out-of-date (refer to #ifndef MULTI code when integrating)
 
 struct gtexGeneExtras 
 /* Track info */
     {
     double maxMedian;           /* Maximum median rpkm for all tissues */
     boolean isComparison;       /* Comparison of two sample sets (e.g. male/female).
                                        Displayed as two graphs, one oriented downward */
     char *graphType;            /* Additional info about graph (e.g. type of comparison graph */
     struct rgbColor *colors;    /* Color palette for tissues */
     };
 
 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 */
     double *medians1;            /* Computed medians */
     double *medians2;            /* Computed medians for comparison (inverse) graph */
+    int height;                  /* Item height in pixels */
     };
 
 /***********************************************/
 /* Color gene models using GENCODE conventions */
 
 // TODO: reuse GENCODE code for some/all of this ??
 // MAKE_COLOR_32 ?
 struct rgbColor codingColor = {12, 12, 120}; // #0C0C78
 struct rgbColor noncodingColor = {0, 100, 0}; // #006400
 struct rgbColor problemColor = {254, 0, 0}; // #FE0000
 struct rgbColor unknownColor = {1, 1, 1};
 
 static struct statusColors
 /* Color values for gene models */
     {
@@ -214,30 +217,32 @@
         if (scores)
             medians1[i] = slDoubleMedian(scores);
         scores = hashFindVal(scoreHash2, getTissueName(i));
         if (scores)
             medians2[i] = slDoubleMedian(scores);
         }
     geneInfo->medians1 = medians1;
     geneInfo->medians2 = medians2;
     }
 else
     {
     // TODO: compute median for single graph based on filtering of sample set
     }
 }
 
+static int gtexGeneItemHeight(struct track *tg, void *item);
+
 static void gtexGeneLoadItems(struct track *tg)
 /* Load method for track items */
 {
 /* Get track UI info */
 struct gtexGeneExtras *extras;
 AllocVar(extras);
 tg->extraUiData = extras;
 char *samples = cartUsualStringClosestToHome(cart, tg->tdb, FALSE, 
                                                 GTEX_SAMPLES, GTEX_SAMPLES_DEFAULT);
 extras->graphType = cloneString(samples);
 if (sameString(samples, GTEX_SAMPLES_COMPARE_SEX))
     extras->isComparison = TRUE;
 extras->maxMedian = gtexMaxMedianScore(NULL);
 
 /* Get geneModels in range */
@@ -263,30 +268,31 @@
     {
     int expCount = geneBed->expCount;
     extras->colors = getRainbow(&saturatedRainbowAtPos, 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;
     if (extras->isComparison && (tg->visibility == tvFull || tg->visibility == tvPack))
         // compute medians based on configuration (comparisons, and later, filters)
         loadComputedMedians(geneInfo, extras);
+    geneInfo->height = gtexGeneItemHeight(tg, geneInfo);
     }
 slReverse(&list);
 tg->items = list;
 }
 
 /***********************************************/
 /* Draw */
 
 /* Bargraph layouts for three window sizes */
 #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
@@ -449,57 +455,54 @@
                                                 GTEX_LOG_TRANSFORM_DEFAULT);
 // Color in dense mode using transcriptClass
 Color statusColor = getTranscriptStatusColor(hvg, geneBed);
 if (vis != tvFull && vis != tvPack)
     {
     bedDrawSimpleAt(tg, geneBed, hvg, xOff, y, scale, font, statusColor, vis);
     return;
     }
 
 int heightPer = tg->heightPer;
 int graphX = gtexGraphX(geneBed);
 if (graphX < 0)
     return;
 
 int topGraphHeight = gtexGeneGraphHeight(tg, geneInfo, doLogTransform, TRUE);
-if (geneInfo->medians2)
 topGraphHeight = max(topGraphHeight, tl.fontHeight);
-if (vis == tvPack)
-    topGraphHeight = gtexMaxGraphHeight();
 int yZero = topGraphHeight + y - 1;  // yZero is bottom of graph
 
 #ifndef MULTI_REGION
 int x1 = xOff + graphX;         // x1 is at left of graph
 int keepX = x1;                 // FIXME:  Too many X's!
 drawGraphBase(hvg, keepX, yZero+1, geneInfo);
 
 int startX = x1;
 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;
 
 // add labels to comparison graphs
 // TODO: generalize
 if (geneInfo->medians2)
     {
-    hvGfxText(hvg, x1, yZero - tl.fontHeight, labelColor, font, "F");
-    hvGfxText(hvg, x1, yZero + gtexGeneModelHeight() + gtexGeneMargin(), labelColor, font, "M");
+    hvGfxText(hvg, x1, yZero - tl.fontHeight + 1, labelColor, font, "F");
+    hvGfxText(hvg, x1, yZero + gtexGeneModelHeight() + gtexGeneMargin() + 1, labelColor, font, "M");
     startX = startX + tl.mWidth+2;
     x1 = startX;
     }
 
 // draw bar graph
 // TODO: share this code with other graph
 double viewMax = (double)cartUsualIntClosestToHome(cart, tg->tdb, FALSE, 
                                 GTEX_MAX_LIMIT, GTEX_MAX_LIMIT_DEFAULT);
 double maxMedian = ((struct gtexGeneExtras *)tg->extraUiData)->maxMedian;
 int i;
 int expCount = geneBed->expCount;
 struct gtexGeneExtras *extras = (struct gtexGeneExtras *)tg->extraUiData;
 for (i=0; i<expCount; i++)
     {
     struct rgbColor fillColor = extras->colors[i];
@@ -684,38 +687,40 @@
 
 static int gtexGeneItemHeightOptionalMax(struct track *tg, void *item, boolean isMax)
 {
 if (tg->visibility == tvSquish || tg->visibility == tvDense)
     return 0;
 if (isMax)
     {
     int extra = 0;
     if (((struct gtexGeneExtras *)tg->extraUiData)->isComparison)
         extra = gtexMaxGraphHeight() + 2;
     return gtexMaxGraphHeight() + gtexGeneMargin() + gtexGeneModelHeight() + extra;
     }
 if (item == NULL)
     return 0;
 struct gtexGeneInfo *geneInfo = (struct gtexGeneInfo *)item;
+if (geneInfo->height != 0)
+    return geneInfo->height;
 boolean doLogTransform = cartUsualBooleanClosestToHome(cart, tg->tdb, FALSE, GTEX_LOG_TRANSFORM, 
                                                 GTEX_LOG_TRANSFORM_DEFAULT);
 int topGraphHeight = gtexGeneGraphHeight(tg, geneInfo, doLogTransform, TRUE);
+topGraphHeight = max(topGraphHeight, tl.fontHeight);
 int bottomGraphHeight = 0;
 boolean isComparison = ((struct gtexGeneExtras *)tg->extraUiData)->isComparison;
 if (isComparison)
     {
-    topGraphHeight = max(topGraphHeight, tl.fontHeight);
     bottomGraphHeight = max(gtexGeneGraphHeight(tg, geneInfo, doLogTransform, FALSE),
                                 tl.fontHeight) + gtexGeneMargin();
     }
 int height = topGraphHeight + bottomGraphHeight + gtexGeneMargin() + gtexGeneModelHeight();
 return height;
 }
 
 static int gtexGeneMaxHeight(struct track *tg)
 /* Maximum height in pixels of a gene graph */
 {
 return gtexGeneItemHeightOptionalMax(tg, NULL, TRUE);
 }
 
 static int gtexGeneItemHeight(struct track *tg, void *item)
 {
@@ -774,34 +779,31 @@
 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;
 
 boolean doLogTransform = cartUsualBooleanClosestToHome(cart, tg->tdb, FALSE, GTEX_LOG_TRANSFORM, 
                                                 GTEX_LOG_TRANSFORM_DEFAULT);
 int topGraphHeight = gtexGeneGraphHeight(tg, geneInfo, doLogTransform, TRUE);
-if (geneInfo->medians2)
 topGraphHeight = max(topGraphHeight, tl.fontHeight);        // label
-if (tg->visibility == tvPack)
-    topGraphHeight = gtexGeneMaxHeight(tg);
 int yZero = topGraphHeight + y - 1;  // yZero is bottom of (top) graph
 
 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 =  (geneInfo->medians1 ? geneInfo->medians1[i] : geneBed->expScores[i]);
     int height = valToClippedHeight(expScore, maxMedian, viewMax, 
                                         gtexMaxGraphHeight(), 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];
@@ -811,49 +813,64 @@
         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;
 }
 
+static int gtexGeneHeight(void *item)
+{
+struct gtexGeneInfo *geneInfo = (struct gtexGeneInfo *)item;
+assert(geneInfo->height != 0);
+return geneInfo->height;
+}
+
 static int gtexGeneTotalHeight(struct track *tg, enum trackVisibility vis)
 /* Figure out total height of track. Set in track and also return it */
 {
 int height = 0;
 struct gtexGeneInfo *item;
 if (tg->visibility == tvSquish || tg->visibility == tvDense)
     {
     height = tgFixedTotalHeightOptionalOverflow(tg, vis, tl.fontHeight+1, tl.fontHeight, FALSE);
     }
 else if (tg->visibility == tvFull)
     {
     for (item = tg->items; item != NULL; item = item->next)
         height += gtexGeneItemHeight(tg, item);
     }
 else if (tg->visibility == tvPack)
     {
-    // layout as fixed height
-    int maxHeight = gtexGeneMaxHeight(tg);
-    height = tgFixedTotalHeightOptionalOverflow(tg, vis, maxHeight, maxHeight, FALSE); // TODO: allow oflow ?
+    if (!tg->ss)
+        {
+        // layout -- initially as fixed height
+        int height = gtexGeneMaxHeight(tg);
+        tgFixedTotalHeightOptionalOverflow(tg, vis, height, height, FALSE); // TODO: allow oflow ?
+        }
+    // set variable height rows
+    if (!tg->ss->rowSizes)
+        height = spaceSaverSetRowHeights(tg->ss, gtexGeneHeight);
+    else
+        height = spaceSaverGetRowHeightsTotal(tg->ss);
     }
 tg->height = height;
 return height;
 }
 
 static int gtexGeneItemStart(struct track *tg, void *item)
 /* Return end chromosome coordinate of item, including graph */
 {
 struct gtexGeneInfo *geneInfo = (struct gtexGeneInfo *)item;
 struct gtexGeneBed *geneBed = geneInfo->geneBed;
 return geneBed->chromStart;
 }
 
 static int gtexGeneItemEnd(struct track *tg, void *item)
 /* Return end chromosome coordinate of item, including graph */