4f14572c392ef39b38df827df7adba1a36a75cd1
kent
  Wed Feb 3 08:21:48 2021 -0800
Removing barChartFitGene tag and associated code since have integrated the best part of it into the existing code.

diff --git src/hg/hgTracks/barChartTrack.c src/hg/hgTracks/barChartTrack.c
index 41166c4..65ee121 100644
--- src/hg/hgTracks/barChartTrack.c
+++ src/hg/hgTracks/barChartTrack.c
@@ -12,31 +12,30 @@
 #include "fieldedTable.h"
 #include "facetedTable.h"
 #include "barChartBed.h"
 #include "barChartCategory.h"
 #include "barChartUi.h"
 
 // If a category contributes more than this percentage, its color is displayed in squish mode
 // Could be a trackDb setting
 
 struct barChartTrack
 /* Track extras */
     {
     boolean noWhiteout;         /* Suppress whiteout of graph background (allow highlight, blue lines) */
     double maxMedian;           /* Maximum median across all categories */
     boolean doLogTransform;     /* Log10(x+1) */
-    boolean fitToGene;		/* Fit our width to gene */
     char *unit;                /* Units for category values (e.g. RPKM) */
     struct barChartCategory *categories; /* Category names, colors, etc. */
     int categCount;             /* Count of categories - derived from above */
     char **categNames;          /* Category names  - derived from above */
     char **categLabels;         /* Category labels  - derived from above */
     struct rgbColor *colors;    /* Colors  for all categories */
     struct hash *categoryFilter; /* NULL out excluded factors */
     int maxViewLimit;
 
     // dimensions for drawing
     char *maxGraphSize;         /* optionally limit graph size (override semantic zoom)
                                      small, medium, or large (default) */
      int winMaxGraph;             /* Draw large graphs if window size smaller than this */
      int winMedGraph;             /* Draw medium graphs if window size greater than this 
                                         and smaller than winMaxGraph */
@@ -341,32 +340,32 @@
 
 static int barChartItemHeight(struct track *tg, void *item)
 {
 int height = chartItemHeightOptionalMax(tg, item, FALSE);
 return height;
 }
 
 static int chartStandardWidth(struct track *tg, struct barChartItem *itemInfo)
 /* How wide is chart if we go by standard bar chart size? */
 {
 struct barChartTrack *extras = (struct barChartTrack *)tg->extraUiData;
 int count = filteredCategoryCount(extras);
 return (extras->barWidth * count) + (extras->padding * (count-1)) + 2;
 }
 
-int windowsTotalIntersection(struct window *list, char *chrom, int chromStart, int chromEnd)
-/* Return total size all bits of region that intersects windows list */
+static int windowsTotalIntersection(struct window *list, char *chrom, int chromStart, int chromEnd)
+/* Return total size all bits of region defined by chrom/start/end that intersects windows list */
 {
 if (list == NULL || list->next == NULL)
     return (double)insideWidth * (chromEnd - chromStart) / (winEnd - winStart);
 long long totalGenoSize = 0;
 int totalPixelSize = 0;
 struct window *w;
 int totalIntersect = 0;
 for (w = list; w != NULL; w = w->next)
     {
     totalPixelSize += w->insideWidth;;
     totalGenoSize += w->winEnd - w->winStart;
 
     if (sameString(w->chromName, chrom))
         {
 	int single = rangeIntersection(w->winStart, w->winEnd, chromStart, chromEnd);
@@ -374,68 +373,59 @@
 	    totalIntersect += single;
 	}
     }
 if (totalIntersect == 0)
     return 0;
 else
     {
     return totalPixelSize * (double)totalIntersect / totalGenoSize;
     }
 }
 
 static int chartWidth(struct track *tg, struct barChartItem *itemInfo)
 /* How wide is the chart? */
 {
 struct bed *bed = itemInfo->bed;
-struct barChartTrack *extras = (struct barChartTrack *)tg->extraUiData;
 int geneSize = windowsTotalIntersection(windows, bed->chrom, bed->chromStart, bed->chromEnd);
-if (extras->fitToGene)
-    {
-    return geneSize;
-    }
-else
-    {
 int standardSize =  chartStandardWidth(tg, itemInfo);
 return max(standardSize, geneSize);
 }
-}
 
 static void barChartLoadItems(struct track *tg)
 /* Load method for track items */
 {
 /* Initialize colors for visibilities that don't display actual barchart */
 if (tg->visibility == tvSquish || tg->limitedVis == tvSquish)
     tg->itemColor = barChartItemColor;
 tg->colorShades = shadesOfGray;
 
 /* Get track UI info */
 struct barChartTrack *extras;
 if (!tg->extraUiData)
     {
     AllocVar(extras);
     tg->extraUiData = extras;
     }
 extras = (struct barChartTrack *)tg->extraUiData;
 
 extras->colors = getCategoryColors(tg);
 
 struct trackDb *tdb = tg->tdb;
 extras->doLogTransform = barChartIsLogTransformed(cart, tdb->track, tdb);
 extras->maxMedian = barChartUiMaxMedianScore(tdb);
 extras->noWhiteout = cartUsualBooleanClosestToHome(cart, tdb, FALSE, 
                                                         BAR_CHART_NO_WHITEOUT, BAR_CHART_NO_WHITEOUT_DEFAULT);
-extras->fitToGene = trackDbSettingClosestToHomeOn(tdb, BAR_CHART_FIT_TO_GENE);
 extras->maxViewLimit = barChartCurViewMax(cart, tg->track, tg->tdb);
 extras->maxGraphSize = trackDbSettingClosestToHomeOrDefault(tdb, 
                                 BAR_CHART_MAX_GRAPH_SIZE, BAR_CHART_MAX_GRAPH_SIZE_DEFAULT);
 extras->unit = trackDbSettingClosestToHomeOrDefault(tdb, BAR_CHART_UNIT, "");
 
 /* Set barchart dimensions to draw.  For three window sizes */
 
 #define MAX_BAR_CHART_MODEL_HEIGHT     2
 #define MED_BAR_CHART_MODEL_HEIGHT     2
 #define SMALL_BAR_CHART_MODEL_HEIGHT   1
 #define MIN_BAR_CHART_MODEL_HEIGHT     1
 
 #define WIN_MAX_GRAPH_DEFAULT 50000
 #define MAX_GRAPH_HEIGHT 175
 #define MAX_BAR_WIDTH 5
@@ -603,77 +593,51 @@
 struct barChartItem *itemInfo = (struct barChartItem *)item;
 struct bed *bed = itemInfo->bed;
 if (vis == tvDense)
     {
     bedDrawSimpleAt(tg, bed, hvg, xOff, y, scale, font, MG_WHITE, vis);     // color ignored (using grayscale)
     return;
     }
 if (vis == tvSquish)
     {
     Color color = barChartItemColor(tg, bed, hvg);
     int height = extras->squishHeight;
     drawScaledBox(hvg, bed->chromStart, bed->chromEnd, scale, xOff, y, height, color);
     return;
     }
 
-int graphX = barChartX(bed);
-if (graphX < 0)
-    return;
-
 // draw line to show range of item on the genome (since chart is fixed width)
 int topGraphHeight = chartHeight(tg, itemInfo);
 topGraphHeight = max(topGraphHeight, tl.fontHeight);
 int yZero = topGraphHeight + y - 1;  // yZero is bottom of graph
 int yGene = yZero + extras->margin;
 int heightPer = tg->heightPer;
 tg->heightPer = extras->modelHeight;
 int height = extras->boxModelHeight;
 drawScaledBox(hvg, bed->chromStart, bed->chromEnd, scale, xOff, yGene+1, height, 
                 MG_GRAY);
 tg->heightPer = heightPer;
 }
 
 static int barChartNonPropPixelWidth(struct track *tg, void *item)
 /* Return end chromosome coordinate of item, including graph */
 {
 struct barChartItem *itemInfo = (struct barChartItem *)item;
 int graphWidth = chartWidth(tg, itemInfo);
 return graphWidth;
 }
 
-static void findBarPosX(int chromStart, int chromEnd, double scale, int barIx, int barCount, int *pStart, int *pEnd)
-/* Figure out start/end position of our bar's region */
-{
-int baseWidth = chromEnd - chromStart;
-double scaledSize = baseWidth *scale;
-double oneWidth = scaledSize/barCount;
-double barThinner = 1.0;
-if (oneWidth >= 5.0)
-    {
-    if (oneWidth > 7.0)
-       barThinner = 0.8;
-    else
-       barThinner = 0.75;
-    }
-
-int iStart = baseWidth*barIx/barCount;
-int iEnd = baseWidth*(barIx+1)/barCount;
-int iWidth = iEnd - iStart;
-int start = *pStart = iStart + chromStart;
-*pEnd = start + iWidth*barThinner;
-}
-
 static void barChartNonPropDrawAt(struct track *tg, void *item, struct hvGfx *hvg, int xOff, int y,
                 double scale, MgFont *font, Color color, enum trackVisibility vis)
 {
 if (vis != tvFull && vis != tvPack)
     return;
 struct barChartTrack *extras = (struct barChartTrack *)tg->extraUiData;
 struct barChartItem *itemInfo = (struct barChartItem *)item;
 struct bed *bed = itemInfo->bed;
 int topGraphHeight = chartHeight(tg, itemInfo);
 int graphWidth = chartWidth(tg, itemInfo);
 #ifdef OLD
 #endif /* OLD */
 topGraphHeight = max(topGraphHeight, tl.fontHeight);
 int yZero = topGraphHeight + y - 1;  // yZero is bottom of graph
 
@@ -804,52 +768,49 @@
 
 int labelWidth = mgFontStringWidth(tl.font, itemName);
 getItemX(start, end, &x1, &x2);
 if (x1-labelWidth <= insideX)
     labelWidth = 0;
 // map over label
 int itemHeight = itemInfo->height;
 mapBoxHc(hvg, itemStart, itemEnd, x1-labelWidth, y, labelWidth, itemHeight-3, 
                     tg->track, mapItemName, itemName);
 
 // add maps to category bars
 struct barChartCategory *categs = getCategories(tg);
 struct barChartCategory *categ = NULL;
 
 int graphX = barChartX(bed);
-if (graphX < 0)
-    return;
 
 int graphWidth = chartWidth(tg, itemInfo);
 int x0 = insideX + graphX;
 int barCount = filteredCategoryCount(extras);
 double invCount = 1.0/barCount;
 int i = 0, barsDrawn = 0;
 for (categ = categs; categ != NULL; categ = categ->next, i++)
     {
     if (!filterCategory(extras, categ->name))
 	continue;
     x1 = barsDrawn * graphWidth * invCount;
     barsDrawn += 1;
     x2 = barsDrawn * graphWidth * invCount;
-    int width = x2-x1;
+    int width = max(1, x2-x1);
     double expScore = bed->expScores[i];
     int height = valToClippedHeight(expScore, extras->maxMedian, extras->maxViewLimit,
                                         extras->maxHeight, extras->doLogTransform);
     mapBoxHc(hvg, itemStart, itemEnd, x0 + x1, yZero-height, width, height, 
                         tg->track, mapItemName, chartMapText(tg, categ, expScore));
-    x1 = x1 + extras->barWidth + extras->padding;
     }
 
 // map over background of chart
 getItemX(start, end, &x1, &x2);
 mapBoxHc(hvg, itemStart, itemEnd, x1, y, graphWidth, itemHeight-3,
                     tg->track, mapItemName, itemName);
 }
 
 /* This is lifted nearly wholesale from gtexGene track.  Could be shared */
 
 static int getBarChartHeight(void *item)
 {
 struct barChartItem *itemInfo = (struct barChartItem *)item;
 assert(itemInfo->height != 0);
 return itemInfo->height;
@@ -939,144 +900,40 @@
 struct bed *bed = chartItem->bed;
 return bed->name;
 }
 
 static char *barChartItemName(struct track *tg, void *item)
 /* Return item name for labeling */
 {
 struct barChartItem *chartItem = (struct barChartItem *)item;
 struct bed *bed = chartItem->bed;
 if (tg->isBigBed)
     return bigBedItemName(tg, bed);
 return bed->name;
 }
 
 
-static void barsToGeneDrawAt(struct track *tg, void *item, struct hvGfx *hvg, int xOff, int y, 
-                double scale, MgFont *font, Color color, enum trackVisibility vis)
-/* Draw bar chart over item in proportional to gene way*/
-{
-if (vis != tvFull && vis != tvPack)
-    {
-    barChartDrawAt(tg, item, hvg, xOff, y, scale, font, color, vis);
-    return;
-    }
-
-/* Fetch our item and the bed from it */
-struct barChartItem *itemInfo = (struct barChartItem *)item;
-struct bed *bed = itemInfo->bed;
-
-/* Figure out where to draw things in Y dimension */
-struct barChartTrack *extras = (struct barChartTrack *)tg->extraUiData;
-int topGraphHeight = chartHeight(tg, itemInfo);
-topGraphHeight = max(topGraphHeight, tl.fontHeight);
-int yZero = topGraphHeight + y - 1;  // yZero is bottom of graph
-int yGene = yZero + extras->margin;
-
-/* Figure out width between bars */
-int barCount = filteredCategoryCount(extras);
-int expCount = bed->expCount;
-
-
-int i;
-Color clipColor = MG_MAGENTA;
-int outBarIx = 0;
-struct barChartCategory *categ;
-for (i=0, categ=extras->categories; i<expCount && categ != NULL; ++i, categ=categ->next)
-    {
-    if (!filterCategory(extras, categ->name))
-        continue;
-    struct rgbColor rgb = extras->colors[i];
-    int color = hvGfxFindColorIx(hvg, rgb.r, rgb.g, rgb.b);
-    int cStart, cEnd;
-    findBarPosX(bed->chromStart, bed->chromEnd, scale, outBarIx, barCount, &cStart, &cEnd);
-    double expScore = bed->expScores[i];
-    int height = valToClippedHeight(expScore, extras->maxMedian, extras->maxViewLimit, 
-                                        extras->maxHeight, extras->doLogTransform);
-    drawScaledBox(hvg, cStart, cEnd, scale, xOff, y+topGraphHeight-height, height, color);
-    if (!extras->doLogTransform && expScore > extras->maxViewLimit)
-        drawScaledBox(hvg, cStart, cEnd, scale, xOff, yZero-height+1, 2, clipColor);
-    ++outBarIx;
-    }
-
-
-// Draw the line our graph sits on top of
-drawScaledBox(hvg, bed->chromStart, bed->chromEnd, scale, xOff, yGene+1, extras->boxModelHeight, 
-                MG_GRAY);
-}
-
-static void barsToGeneMapItem(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 item and label, and one for each category (bar in the graph) */
-{
-enum trackVisibility vis = trackVisAfterLimit(tg);
-if (vis != tvFull && vis != tvPack)
-    {
-    barChartMapItem(tg, hvg, item, itemName, mapItemName, start, end, x, y, width, height);
-    return;
-    }
-struct barChartTrack *extras = tg->extraUiData;
-struct barChartItem *itemInfo = item;
-struct bed *bed = itemInfo->bed;
-int barCount = filteredCategoryCount(extras);
-if (width > barCount)	// When get down to less than a pixel suppress the bar-by-bar map boxes*/
-    {
-    int i = 0;
-    int outBarIx = 0;
-    struct barChartCategory *categs = getCategories(tg);
-    struct barChartCategory *categ;
-    int topGraphHeight = chartHeight(tg, itemInfo);
-    double scale = scaleForWindow(insideWidth, winStart, winEnd);
-    for (categ = categs; categ != NULL; categ = categ->next, i++)
-	{
-	if (!filterCategory(extras, categ->name))
-	    continue;
-	int cStart, cEnd;
-	findBarPosX(bed->chromStart, bed->chromEnd, scale, outBarIx, barCount, &cStart, &cEnd);
-	int x1,x2;
-	if (scaledBoxToPixelCoords(cStart, cEnd, scale, 0, &x1, &x2))
-	    {
-	    double expScore = bed->expScores[i];
-	    int height = valToClippedHeight(expScore, extras->maxMedian, extras->maxViewLimit, 
-						extras->maxHeight, extras->doLogTransform);
-	    mapBoxHc(hvg, bed->chromStart, bed->chromEnd, x1+insideX, y+topGraphHeight-height, x2-x1, height,
-				tg->track, mapItemName, chartMapText(tg, categ, expScore));
-	    }
-	++outBarIx;
-	}
-    }
-mapBoxHc(hvg, bed->chromStart, bed->chromEnd, x, y, width, height, tg->track, itemName, NULL);
-}
-
 void barChartMethods(struct track *tg)
 /* Bar Chart track type: draw fixed width chart of colored bars over a BED item */
 {
 tg->bedSize = 8;
 bedMethods(tg);
 tg->canPack = TRUE;
 tg->loadItems = barChartLoadItems;
 tg->itemName = barChartItemName;
 tg->mapItemName = barChartMapItemName;
 tg->itemHeight = barChartItemHeight;
 tg->itemStart = barChartItemStart;
 tg->itemEnd = barChartItemEnd;
 tg->totalHeight = barChartTotalHeight;
-if (trackDbSettingClosestToHomeOn(tg->tdb, BAR_CHART_FIT_TO_GENE))
-    {
-    tg->drawItemAt = barsToGeneDrawAt;
-    tg->mapItem = barsToGeneMapItem;
-    }
-else
-    {
 tg->preDrawItems = barChartPreDrawItems;
 tg->drawItemAt = barChartDrawAt;
 tg->mapItem = barChartMapItem;
 tg->nonPropDrawItemAt = barChartNonPropDrawAt;
 tg->nonPropPixelWidth = barChartNonPropPixelWidth;
 }
-}
 
 void barChartCtMethods(struct track *tg)
 /* Bar Chart track methods for custom track */
 {
 barChartMethods(tg);
 }