8c908f948b09826c6cb4452ee5b282aca41be85e
galt
  Tue Dec 8 21:52:59 2015 -0800
Multi-region (exonMostly). This work allows people to look at virtual chromosomes from a list of regions and then navigate and perform all of the usual functions on it.

diff --git src/hg/hgTracks/gtexTracks.c src/hg/hgTracks/gtexTracks.c
index b99fe3d..667c78f 100644
--- src/hg/hgTracks/gtexTracks.c
+++ src/hg/hgTracks/gtexTracks.c
@@ -307,40 +307,43 @@
 bedLoadItem(tg, tg->table, (ItemLoader)gtexGeneBedLoad);
 
 /* Create geneInfo items with BED and geneModels */
 struct gtexGeneInfo *geneInfo = NULL, *list = NULL;
 struct gtexGeneBed *geneBed = (struct gtexGeneBed *)tg->items;
 
 /* Load tissue colors: GTEx or rainbow */
 char *colorScheme = cartUsualStringClosestToHome(cart, tg->tdb, FALSE, GTEX_COLORS, 
                         GTEX_COLORS_DEFAULT);
 if (sameString(colorScheme, GTEX_COLORS_GTEX))
     {
     extras->colors = getGtexTissueColors();
     }
 else
     {
+    if (geneBed)
+	{
 	int expCount = geneBed->expCount;
 	extras->colors = getRainbow(&saturatedRainbowAtPos, expCount);
 	}
+    }
 filterTissues(tg);
 
 while (geneBed != NULL)
     {
     AllocVar(geneInfo);
     geneInfo->geneBed = geneBed;
-    geneInfo->geneModel = hashFindVal(modelHash, geneBed->geneId);
+    geneInfo->geneModel = hashFindVal(modelHash, geneBed->geneId); // sometimes this is missing, hash returns NULL. do we check?
     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 */
 
@@ -351,73 +354,74 @@
 #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
 
 #define MARGIN_WIDTH 1
 
 static int gtexBarWidth()
 {
-int winSize = winEnd - winStart;
+long winSize = virtWinBaseCount;
 if (winSize < WIN_MAX_GRAPH)
     return MAX_BAR_WIDTH;
 else if (winSize < WIN_MED_GRAPH)
     return MED_BAR_WIDTH;
 else
     return MIN_BAR_WIDTH;
 }
 
 static int gtexGraphPadding()
 {
-int winSize = winEnd - winStart;
+long winSize = virtWinBaseCount;
+
 if (winSize < WIN_MAX_GRAPH)
     return MAX_GRAPH_PADDING;
 else if (winSize < WIN_MED_GRAPH)
     return MED_GRAPH_PADDING;
 else
     return MIN_GRAPH_PADDING;
 }
 
 static int gtexMaxGraphHeight()
 {
-int winSize = winEnd - winStart;
+long winSize = virtWinBaseCount;
 if (winSize < WIN_MAX_GRAPH)
     return MAX_GRAPH_HEIGHT;
 else if (winSize < WIN_MED_GRAPH)
     return MED_GRAPH_HEIGHT;
 else
     return MIN_GRAPH_HEIGHT;
 }
 
 static int gtexGraphWidth(struct track *tg, struct gtexGeneInfo *geneInfo)
 /* Width of GTEx graph in pixels */
 {
 int barWidth = gtexBarWidth();
 int padding = gtexGraphPadding();
 int count = filteredTissueCount(tg);
 int labelWidth = geneInfo->medians2 ? tl.mWidth : 0;
 return (barWidth * count) + (padding * (count-1)) + labelWidth + 2;
 }
 
 static int gtexGraphX(struct gtexGeneBed *gtex)
-/* Locate graph on X, relative to viewport. Return -1 if it won't fit */
+/* Locate graph on X, relative to viewport. */
 {
 int start = max(gtex->chromStart, winStart);
 double scale = scaleForWindow(insideWidth, winStart, winEnd);
 int x1 = round((start - winStart) * scale);
 return x1;
 }
 
 static int gtexGeneModelHeight()
 {
     return 8; 
 }
 
 static int gtexGeneMargin()
 {
     return 1;
@@ -493,34 +497,64 @@
 struct gtexGeneInfo *geneInfo = (struct gtexGeneInfo *)item;
 struct gtexGeneBed *geneBed = geneInfo->geneBed;
 // 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;
 
+// draw gene model
+int topGraphHeight = gtexGeneGraphHeight(tg, geneInfo, TRUE);
+topGraphHeight = max(topGraphHeight, tl.fontHeight);
+int yZero = topGraphHeight + y - 1;  // yZero is bottom of graph
+int yGene = yZero + gtexGeneMargin() - 1;
+tg->heightPer = gtexGeneModelHeight() + 1;
+if (geneInfo->geneModel) // some BEDs do not have a corresponding geneModel record
+    {
+    struct linkedFeatures *lf = linkedFeaturesFromGenePred(tg, geneInfo->geneModel, FALSE);
+    lf->filterColor = statusColor;
+    linkedFeaturesDrawAt(tg, lf, hvg, xOff, yGene, scale, font, color, tvSquish);
+    }
+tg->heightPer = heightPer;
+}
+
+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(tg, geneInfo);
+return graphWidth;
+}
+
+static void gtexGeneNonPropDrawAt(struct track *tg, void *item, struct hvGfx *hvg, int xOff, int y,
+                double scale, MgFont *font, Color color, enum trackVisibility vis)
+{
+struct gtexGeneInfo *geneInfo = (struct gtexGeneInfo *)item;
+struct gtexGeneBed *geneBed = geneInfo->geneBed;
 int topGraphHeight = gtexGeneGraphHeight(tg, geneInfo, TRUE);
 topGraphHeight = max(topGraphHeight, tl.fontHeight);
 int yZero = topGraphHeight + y - 1;  // yZero is bottom of graph
+int yGene = yZero + gtexGeneMargin()-1;
 
+int graphX = gtexGraphX(geneBed);
 int x1 = xOff + graphX;         // x1 is at left of graph
 int keepX = x1;
 drawGraphBase(tg, geneInfo, hvg, keepX, yZero+1);
 
 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
@@ -552,38 +586,30 @@
         }
     int fillColorIx = hvGfxFindColorIx(hvg, fillColor.r, fillColor.g, fillColor.b);
     double expScore = (geneInfo->medians1 ? geneInfo->medians1[i] : geneBed->expScores[i]);
     int height = valToClippedHeight(expScore, maxMedian, viewMax, 
                                         gtexMaxGraphHeight(), extras->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);
     // mark clipped bar with magenta tip
     if (!extras->doLogTransform && expScore > viewMax)
         hvGfxBox(hvg, x1, yZero-height+1, barWidth, 1, clipColor);
     x1 = x1 + barWidth + graphPadding;
     }
 
-// draw gene model
-int yGene = yZero + gtexGeneMargin() - 1;
-tg->heightPer = gtexGeneModelHeight() + 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;
 
 // draw comparison bar graph (upside down)
 x1 = startX;
 yZero = yGene + gtexGeneModelHeight() + 1; // yZero is at top of graph
 drawGraphBase(tg, geneInfo, hvg, keepX, yZero-1);
 
 for (i=0, tis=extras->tissues; i<expCount; i++, tis=tis->next)
     {
     if (!filterTissue(tg, tis->name))
         continue;
     struct rgbColor fillColor = extras->colors[i];
     if (barWidth == 1 && sameString(colorScheme, GTEX_COLORS_GTEX))
         {
@@ -608,119 +634,90 @@
 
 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;
 int topGraphHeight = gtexGeneGraphHeight(tg, geneInfo, TRUE);
 topGraphHeight = max(topGraphHeight, tl.fontHeight);
 int bottomGraphHeight = 0;
 boolean isComparison = ((struct gtexGeneExtras *)tg->extraUiData)->isComparison;
 if (isComparison)
     {
     bottomGraphHeight = max(gtexGeneGraphHeight(tg, geneInfo, 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)
 {
 return gtexGeneItemHeightOptionalMax(tg, item, FALSE);
 }
 
-static void gtexGeneDrawItemsFull(struct track *tg, int seqStart, int seqEnd,
-                                      struct hvGfx *hvg, int xOff, int yOff, int width,
-                                      MgFont *font, Color color, enum trackVisibility vis)
-/* Draw GTEx gene graphs in full mode.  Special handling as they are variable height */
-{
-double scale = scaleForWindow(width, seqStart, seqEnd);
-struct slList *item;
-int y = yOff + 1;
-for (item = tg->items; item != NULL; item = item->next)
-    {
-    tg->drawItemAt(tg, item, hvg, xOff, y, scale, font, color, vis);
-    genericDrawNextItem(tg, item, hvg, xOff, y, scale, color, vis);
-    int height = gtexGeneItemHeight(tg, item);
-    y += height;
-    }
-}
-
-void gtexGeneDrawItems(struct track *tg, int seqStart, int seqEnd, 
-                        struct hvGfx *hvg, int xOff, int yOff, int width, 
-                        MgFont *font, Color color, enum trackVisibility vis)
-/* Draw GTEx gene graphs, which are of variable height so require custom layout in full
- * and pack modes */
-{
-if (vis == tvDense || vis == tvSquish)
-    genericDrawItems(tg, seqStart, seqEnd, hvg, xOff, yOff, width, font, color, vis);
-else if (vis == tvFull)
-    gtexGeneDrawItemsFull(tg, seqStart, seqEnd, hvg, xOff, yOff, width, font, color, vis);
-else if (vis == tvPack)
-    genericDrawItems(tg, seqStart, seqEnd, hvg, xOff, yOff, width, font, color, vis);
-}
-
 static char *tissueExpressionText(struct gtexTissue *tissue, double expScore, 
                                         boolean doLogTransform, char *qualifier)
 /* Construct mouseover text for tissue graph */
 {
 static char buf[128];
 safef(buf, sizeof(buf), "%s (%.1f %s%s%sRPKM)", tissue->description, 
                                 doLogTransform ? log10(expScore+1.0) : expScore,
                                 qualifier != NULL ? qualifier : "",
                                 qualifier != NULL ? " " : "",
                                 doLogTransform ? "log " : "");
 return buf;
 }
 
 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 on gene model and label, and one for each tissue (bar in the graph) in
  * pack or ful modes.  Just single map for squish/dense modes */
 {
 if (tg->visibility == tvDense || tg->visibility == tvSquish)
     {
     genericMapItem(tg, hvg, item, itemName, itemName, start, end, x, y, width, height);
     return;
     }
-
 // add map boxes to gene model and label
 struct gtexGeneInfo *geneInfo = item;
 struct gtexGeneExtras *extras = (struct gtexGeneExtras *)tg->extraUiData;
 struct gtexGeneBed *geneBed = geneInfo->geneBed;
 int topGraphHeight = gtexGeneGraphHeight(tg, geneInfo, TRUE);
 topGraphHeight = max(topGraphHeight, tl.fontHeight);        // label
 int yZero = topGraphHeight + y - 1;  // yZero is bottom of graph
 int yGene = yZero + gtexGeneMargin() - 1;
+if (!geneInfo->geneModel)
+    return;
 int geneStart = max(geneInfo->geneModel->txStart, winStart);
 int geneEnd = min(geneInfo->geneModel->txEnd, winEnd);
 double scale = scaleForWindow(insideWidth, winStart, winEnd);
 int x1 = round((double)((int)geneStart-winStart)*scale) + x;
 int x2 = round((double)((int)geneEnd-winStart)*scale) + x;
 int w = x2-x1;
 char query[256];
 sqlSafef(query, sizeof(query),
         "select kgXref.description from kgXref, knownToEnsembl where knownToEnsembl.value='%s' and knownToEnsembl.name=kgXref.kgID", geneBed->transcriptId);
 struct sqlConnection *conn = hAllocConn(database);
 char *desc = sqlQuickString(conn, query);
 hFreeConn(&conn);
 mapBoxHc(hvg, start, end, x, yGene, w, gtexGeneModelHeight()-1, tg->track, mapItemName, desc);
 
 // add maps to tissue bars in expresion graph
@@ -781,80 +778,94 @@
 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)
+else if ((tg->visibility == tvPack) || (tg->visibility == tvFull))
     {
     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->rowCount != 0)
         {
         if (!tg->ss->rowSizes)
-            height = spaceSaverSetRowHeights(tg->ss, gtexGeneHeight);
-        else
+	    {
+	    // collect the rowSizes data across all windows
+	    assert(currentWindow==windows); // first window
+	    assert(tg->ss->vis == vis); // viz matches, we have the right one
+	    struct spaceSaver *ssHold; 
+	    AllocVar(ssHold);
+	    struct track *tgSave = tg;
+	    for(tg=tgSave; tg; tg=tg->nextWindow)
+		{
+		assert(tgSave->ss->vis == tg->ss->vis); // viz matches, we have the right one
+		spaceSaverSetRowHeights(tg->ss, ssHold, gtexGeneHeight);
+		}
+	    // share the rowSizes data across all windows
+	    for(tg=tgSave; tg; tg=tg->nextWindow)
+		{
+		tg->ss->rowSizes = ssHold->rowSizes;
+		}
+	    tg = tgSave;
+	    }
 	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 */
 {
 struct gtexGeneInfo *geneInfo = (struct gtexGeneInfo *)item;
 struct gtexGeneBed *geneBed = geneInfo->geneBed;
 double scale = scaleForWindow(insideWidth, winStart, winEnd);
 int graphWidth = gtexGraphWidth(tg, geneInfo);
 return max(geneBed->chromEnd, max(winStart, geneBed->chromStart) + graphWidth/scale);
 }
 
 void gtexGeneMethods(struct track *tg)
 {
-tg->drawItems = gtexGeneDrawItems;
 tg->drawItemAt = gtexGeneDrawAt;
 tg->loadItems = gtexGeneLoadItems;
 tg->mapItem = gtexGeneMapItem;
 tg->itemName = gtexGeneItemName;
 tg->mapItemName = gtexGeneItemName;
 tg->itemHeight = gtexGeneItemHeight;
 tg->itemStart = gtexGeneItemStart;
 tg->itemEnd = gtexGeneItemEnd;
 tg->totalHeight = gtexGeneTotalHeight;
+tg->nonPropDrawItemAt = gtexGeneNonPropDrawAt;
+tg->nonPropPixelWidth = gtexGeneNonPropPixelWidth;
 }