6eb046056d5151185fae6fdc18499be244ffd090
chmalee
  Thu Jun 10 14:53:14 2021 -0700
Ensure the labels and colors of the GTEx boxplots match hgTracks, refs #26845

diff --git src/hg/hgc/hgcData/gtexBoxplot.R src/hg/hgc/hgcData/gtexBoxplot.R
index be84847..22f8a92 100644
--- src/hg/hgc/hgcData/gtexBoxplot.R
+++ src/hg/hgc/hgcData/gtexBoxplot.R
@@ -14,49 +14,52 @@
 
 # TODO: replace with V6 colors
 # Currently, V6 and V8 tracks both use V4 colors.  V8 has one additional tissue (Kidney - Medulla)
 
 if (version == "V8") {
     colorVersion <- "V8"
     units <- "TPM"
 } else {
     colorVersion <- "V4"
     units <- "RPKM"
 }
 
 colorVersion <- if (version == "V8") "V8" else "V4"
 colorFile <- paste0("hgcData/gtexColors", colorVersion, ".R")
 source(colorFile)
-# sets colorsHex, darkerColorsHex
+# sets colorsHex, darkerColorsHex, tissueOrder
 
 df <- read.table(dataFile, sep="\t", header=TRUE)
 labels <- names(table(df$tissue))
 count <- length(labels)
 tissueColors <- data.frame(labels, colorsHex)
 
 if (isScoreOrder) {
     # order boxes, Y labels and colors by median descending
     tissueMedian <- with(df, reorder(tissue, -rpkm, median))
     orderedLevels <- levels(tissueMedian)
     tissueMedianFrame <- data.frame(orderedLevels, 1:count)
     dfColors <- merge(tissueMedianFrame, tissueColors, by.x="orderedLevels", by.y="labels")
     dfOrderedColors <- dfColors[with(dfColors, order(X1.count)),]
     colorsHex <- as.vector(dfOrderedColors$colorsHex)
     labels <- as.vector(dfOrderedColors$orderedLevels)
     tissueFactor <- tissueMedian
 } else {
-    tissueFactor <- df$tissue
+    # the gtexColorVX.R file has the tissue colors in a particular order, so match
+    # that up with the labels and data frame if we aren't sorting on median
+    labels <- labels[order(match(labels, tissueOrder))]
+    tissueFactor <- ordered(df$tissue, levels=labels)
 }
 
 # draw graph
 png(file=outFile, width=1070, height=600)
 # res=72 is default
 gray <- "#A6A6A6"
 darkgray <- "#737373"
 
 # plot with customized margins, symbol and line styles and colors, to mimic GTEx figure in
 # UCSC GTEx grant proposal
 par(mar=c(12,4,3,1) + 0.1, mgp=c(2,1,0), font.main=1)
 #yLabel <- if (isLog) "Log10 (RPKM+1)" else "RPKM"
 yLabel <- if (isLog) paste0("Log10 (",units,"+1)") else units 
 max <- max(df$rpkm)
 yLimit <- if (isLog) c(-.05, max+.1) else c(-(max*.02), max+ (max*.03))