21bf4f713e944af4e8810544d68c441510ea85a3 kate Wed Jul 26 16:31:01 2017 -0700 Add support for maxHeightPixels in trackDb and UI of barChart type tracks. refs #19872 diff --git src/hg/hgTracks/barChartTrack.c src/hg/hgTracks/barChartTrack.c index 97a3fb3..4de45a3 100644 --- src/hg/hgTracks/barChartTrack.c +++ src/hg/hgTracks/barChartTrack.c @@ -10,30 +10,31 @@ #include "spaceSaver.h" #include "hubConnect.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 #define SPECIFICITY_THRESHOLD 10 struct barChartTrack /* Track info */ { boolean noWhiteout; /* Suppress whiteout of graph background (allow highlight, blue lines) */ double maxMedian; /* Maximum median across all categories */ + int maxHeight; /* Maximum height in pixels for track */ boolean doLogTransform; /* Log10(x+1) */ boolean doAutoScale; /* Scale to maximum in window, alternative to log */ 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 */ }; struct barChartItem /* BED item plus computed values for display */ { struct barChartItem *next; /* Next in singly linked list */ @@ -139,30 +140,45 @@ #define WIN_MAX_GRAPH 50000 #define WIN_MED_GRAPH 500000 #define MAX_BAR_CHART_MODEL_HEIGHT 2 #define MED_BAR_CHART_MODEL_HEIGHT 2 #define MIN_BAR_CHART_MODEL_HEIGHT 1 if (winSize < WIN_MAX_GRAPH) return MAX_BAR_CHART_MODEL_HEIGHT; else if (winSize < WIN_MED_GRAPH) return MED_BAR_CHART_MODEL_HEIGHT; else return MIN_BAR_CHART_MODEL_HEIGHT; } +static int barChartMaxHeight(int maxHeight) +/* Set maximum graph height based on window size */ +{ +// scale based on subjective aesthetic (previous hardcoded were 175/100) +#define WIN_MED_GRAPH_SCALE .57 + +long winSize = virtWinBaseCount; +if (winSize < WIN_MAX_GRAPH) + return maxHeight; +else if (winSize < WIN_MED_GRAPH) + return maxHeight * WIN_MED_GRAPH_SCALE; +else + return tl.fontHeight * 4; +} + static int barChartItemHeight(struct track *tg, void *item); static void filterCategories(struct track *tg) /* Check cart for category selection. NULL out unselected categorys in category list */ { struct barChartTrack *extras = (struct barChartTrack *)tg->extraUiData; struct barChartCategory *categ = NULL; extras->categories = getCategories(tg); extras->categoryFilter = hashNew(0); if (cartListVarExistsAnyLevel(cart, tg->tdb, FALSE, BAR_CHART_CATEGORY_SELECT)) { struct slName *selectedValues = cartOptionalSlNameListClosestToHome(cart, tg->tdb, FALSE, BAR_CHART_CATEGORY_SELECT); if (selectedValues != NULL) { @@ -264,30 +280,34 @@ /* Get track UI info */ struct barChartTrack *extras; AllocVar(extras); tg->extraUiData = extras; struct trackDb *tdb = tg->tdb; extras->doLogTransform = cartUsualBooleanClosestToHome(cart, tdb, FALSE, BAR_CHART_LOG_TRANSFORM, BAR_CHART_LOG_TRANSFORM_DEFAULT); extras->doAutoScale = cartUsualBooleanClosestToHome(cart, tdb, FALSE, BAR_CHART_AUTOSCALE, BAR_CHART_AUTOSCALE_DEFAULT); extras->noWhiteout = cartUsualBooleanClosestToHome(cart, tdb, FALSE, BAR_CHART_NO_WHITEOUT, BAR_CHART_NO_WHITEOUT_DEFAULT); extras->unit = trackDbSettingClosestToHomeOrDefault(tdb, BAR_CHART_UNIT, ""); +int min, max, deflt; +wigFetchMinMaxPixelsWithCart(cart, tdb, tdb->track, &min, &max, &deflt); +extras->maxHeight = barChartMaxHeight(deflt); + /* Get bed (names and all-sample category median scores) in range */ loadSimpleBedWithLoader(tg, (bedItemLoader)barChartSimpleBedLoad); /* Create itemInfo items with BED and geneModels */ struct barChartItem *itemInfo = NULL, *infoList = NULL; struct bed *bed = (struct bed *)tg->items; /* Load category colors */ extras->colors = getCategoryColors(tg); filterCategories(tg); /* create list of barChart items */ while (bed != NULL) { @@ -308,36 +328,34 @@ } if (!extras->doAutoScale) // maximum median score in entire dataset extras->maxMedian = barChartUiMaxMedianScore(tdb); /* replace item list with wrapped beds */ slReverse(&infoList); tg->items = infoList; } /***********************************************/ /* Draw */ /* Bargraph layouts for three window sizes */ #define WIN_MAX_GRAPH 50000 -#define MAX_GRAPH_HEIGHT 175 #define MAX_BAR_WIDTH 5 #define MAX_GRAPH_PADDING 2 #define WIN_MED_GRAPH 500000 -#define MED_GRAPH_HEIGHT 100 #define MED_BAR_WIDTH 3 #define MED_GRAPH_PADDING 1 #define MIN_BAR_WIDTH 1 #define MIN_GRAPH_PADDING 0 #define MARGIN_WIDTH 1 static int barChartBarWidth(struct track *tg) { long winSize = virtWinBaseCount; int scale = (getCategoryCount(tg) < 15 ? 2 : 1); if (winSize < WIN_MAX_GRAPH) return MAX_BAR_WIDTH * scale; @@ -352,41 +370,30 @@ return barChartBoxModelHeight()+3; } static int barChartPadding() { 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 barChartMaxHeight() -{ -long winSize = virtWinBaseCount; -if (winSize < WIN_MAX_GRAPH) - return MAX_GRAPH_HEIGHT; -else if (winSize < WIN_MED_GRAPH) - return MED_GRAPH_HEIGHT; -else - return tl.fontHeight * 4; -} - static boolean barChartUseViewLimit(struct barChartTrack *extras) { return !extras->doLogTransform && !extras->doAutoScale; } static int barChartWidth(struct track *tg, struct barChartItem *itemInfo) /* Width of bar chart in pixels */ { int barWidth = barChartBarWidth(tg); int padding = barChartPadding(); int count = filteredCategoryCount(tg); return (barWidth * count) + (padding * (count-1)) + 2; } static int barChartX(struct bed *bed) @@ -406,54 +413,53 @@ static int valToHeight(double val, double maxVal, int maxHeight, boolean doLogTransform) /* Log-scale and convert a value from 0 to maxVal to 0 to maxHeight-1 */ { if (val == 0.0) return 0; double scaled = 0.0; if (doLogTransform) scaled = log10(val+1.0) / log10(maxVal+1.0); else scaled = val/maxVal; if (scaled < 0) warn("scaled=%f\n", scaled); return (scaled * (maxHeight-1)); } -static int valToClippedHeight(double val, double maxVal, int maxView, int maxHeight, - struct barChartTrack *extras) +static int valToClippedHeight(double val, double maxVal, int maxView, struct barChartTrack *extras) /* Convert a value from 0 to maxVal to 0 to maxHeight-1, with clipping, or log transform the value */ { double useVal = val; double useMax = maxVal; if (barChartUseViewLimit(extras)) { useMax = maxView; if (val > maxView) useVal = maxView; } -return valToHeight(useVal, useMax, barChartMaxHeight(), extras->doLogTransform); +return valToHeight(useVal, useMax, extras->maxHeight, extras->doLogTransform); } static int barChartHeight(struct track *tg, struct barChartItem *itemInfo) /* Determine height in pixels of graph. This will be the box for category with highest value */ { struct barChartTrack *extras = (struct barChartTrack *)tg->extraUiData; double maxExp = barChartMaxExpScore(tg, itemInfo); double viewMax = (double)cartUsualIntClosestToHome(cart, tg->tdb, FALSE, BAR_CHART_MAX_VIEW_LIMIT, BAR_CHART_MAX_VIEW_LIMIT_DEFAULT); double maxMedian = ((struct barChartTrack *)tg->extraUiData)->maxMedian; -return valToClippedHeight(maxExp, maxMedian, viewMax, barChartMaxHeight(), extras); +return valToClippedHeight(maxExp, maxMedian, viewMax, extras); } static void drawGraphBox(struct track *tg, struct barChartItem *itemInfo, struct hvGfx *hvg, int x, int y) /* Draw white background for graph */ { Color lighterGray = MAKECOLOR_32(0xF3, 0xF3, 0xF3); int width = barChartWidth(tg, itemInfo); int height = barChartHeight(tg, itemInfo); hvGfxOutlinedBox(hvg, x, y-height, width, height, MG_WHITE, lighterGray); } static void drawGraphBase(struct track *tg, struct barChartItem *itemInfo, struct hvGfx *hvg, int x, int y) /* Draw faint line under graph to delineate extent when bars are missing (category w/ 0 value) */ { Color lightGray = MAKECOLOR_32(0xD1, 0xD1, 0xD1); @@ -536,31 +542,31 @@ // draw bar graph double viewMax = (double)cartUsualIntClosestToHome(cart, tg->tdb, FALSE, BAR_CHART_MAX_VIEW_LIMIT, BAR_CHART_MAX_VIEW_LIMIT_DEFAULT); double maxMedian = ((struct barChartTrack *)tg->extraUiData)->maxMedian; int i; int expCount = bed->expCount; struct barChartCategory *categ; for (i=0, categ=extras->categories; i<expCount && categ != NULL; i++, categ=categ->next) { if (!filterCategory(tg, categ->name)) continue; struct rgbColor fillColor = extras->colors[i]; int fillColorIx = hvGfxFindColorIx(hvg, fillColor.r, fillColor.g, fillColor.b); double expScore = bed->expScores[i]; - int height = valToClippedHeight(expScore, maxMedian, viewMax, barChartMaxHeight(), extras); + int height = valToClippedHeight(expScore, maxMedian, viewMax, extras); if (graphPadding == 0 || sameString(colorScheme, BAR_CHART_COLORS_USER)) 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 (barChartUseViewLimit(extras) && expScore > viewMax) hvGfxBox(hvg, x1, yZero-height+1, barWidth, 2, clipColor); x1 = x1 + barWidth + graphPadding; } } static int barChartItemHeightOptionalMax(struct track *tg, void *item, boolean isMax) { // It seems that this can be called early or late enum trackVisibility vis = tg->visibility; @@ -570,31 +576,31 @@ int height; if (vis == tvSquish || vis == tvDense) { if (vis == tvSquish) { tg->lineHeight = barChartSquishItemHeight(); tg->heightPer = tg->lineHeight; } height = tgFixedItemHeight(tg, item); return height; } struct barChartTrack *extras = (struct barChartTrack *)tg->extraUiData; if (isMax) { int extra = 0; - height = barChartMaxHeight() + barChartMargin() + barChartModelHeight(extras) + extra; + height = extras->maxHeight + barChartMargin() + barChartModelHeight(extras) + extra; return height; } if (item == NULL) return 0; struct barChartItem *itemInfo = (struct barChartItem *)item; if (itemInfo->height != 0) { return itemInfo->height; } int topGraphHeight = barChartHeight(tg, itemInfo); topGraphHeight = max(topGraphHeight, tl.fontHeight); int bottomGraphHeight = 0; height = topGraphHeight + bottomGraphHeight + barChartMargin() + barChartModelHeight(extras); @@ -702,31 +708,31 @@ int graphX = barChartX(bed); if (graphX < 0) return; // x1 is at left of graph x1 = insideX + graphX; double viewMax = (double)cartUsualIntClosestToHome(cart, tg->tdb, FALSE, BAR_CHART_MAX_VIEW_LIMIT, BAR_CHART_MAX_VIEW_LIMIT_DEFAULT); int i = 0; for (categ = categs; categ != NULL; categ = categ->next, i++) { if (!filterCategory(tg, categ->name)) continue; double expScore = bed->expScores[i]; - int height = valToClippedHeight(expScore, maxMedian, viewMax, barChartMaxHeight(), extras); + int height = valToClippedHeight(expScore, maxMedian, viewMax, extras); mapBoxHc(hvg, itemStart, itemEnd, x1, yZero-height, barWidth, height, tg->track, mapItemName, barChartMapText(tg, categ, expScore)); x1 = x1 + barWidth + padding; } // map over background of chart int graphWidth = barChartWidth(tg, itemInfo); 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)