41d01fd0f045a58696edb753981feef6865f62e5
kate
  Mon Apr 24 15:11:24 2017 -0700
1. Fix schema file syntax problem, strangely in comment. Caused crash in QA testing. 2. Allow bigBarChart w/o offset/len into matrix. refs #18736

diff --git src/hg/hgc/barChartClick.c src/hg/hgc/barChartClick.c
index 113727f..e965168 100644
--- src/hg/hgc/barChartClick.c
+++ src/hg/hgc/barChartClick.c
@@ -1,27 +1,29 @@
 /* Details pages for barChart 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 "hash.h"
 #include "hdb.h"
 #include "hvGfx.h"
 #include "trashDir.h"
-#include "hgc.h"
 #include "hCommon.h"
+#include "hui.h"
+#include "asParse.h"
+#include "hgc.h"
 
 #include "barChartBed.h"
 #include "barChartCategory.h"
 #include "barChartData.h"
 #include "barChartSample.h"
 #include "barChartUi.h"
 
 struct barChartItemData
 /* Measured value for a sample and the sample category at a locus.
  * Used for barChart track details (boxplot) */
     {
     struct barChartItemData *next;  /* Next in singly linked list. */
     char *sample;	/* Sample identifier */
     char *category;     /* Sample category (from barChartSample table  or barChartSampleUrl file) */
     double value;	/* Measured value (e.g. expression level) */
@@ -32,86 +34,96 @@
  * (though maybe better to prune matrix for performance) */
 {
 char *categs = trackDbSetting(tdb, BAR_CHART_CATEGORY_LABELS);
 char *words[BAR_CHART_MAX_CATEGORIES];
 int wordCt;
 wordCt = chopLine(cloneString(categs), words);
 int i;
 struct hash *categoryHash = hashNew(0);
 for (i=0; i<wordCt; i++)
     {
     hashStore(categoryHash, words[i]);
     }
 return categoryHash;
 }
 
-static struct barChartBed *getBarChartFromFile(char *item, char *chrom, int start, int end, 
-                                                char *file)
+static struct barChartBed *getBarChartFromFile(struct trackDb *tdb, char *file, 
+                                                char *item, char *chrom, int start, int end)
 /* Retrieve barChart BED item from big file */
 {
+struct sqlConnection *conn = hAllocConnTrack(database, tdb);
+struct asObject *as = NULL;
+boolean hasOffsets = TRUE;
+if (conn != NULL)
+    {
+    as = asForTdb(conn, tdb);
+    hasOffsets = (
+        asColumnFind(as, BARCHART_OFFSET_COLUMN) != NULL && 
+        asColumnFind(as, BARCHART_LEN_COLUMN) != NULL);
+    hFreeConn(&conn);
+    }
 struct bbiFile *bbi = bigBedFileOpen(file);
 struct lm *lm = lmInit(0);
 struct bigBedInterval *bb, *bbList =  bigBedIntervalQuery(bbi, chrom, start, end, 0, lm);
 for (bb = bbList; bb != NULL; bb = bb->next)
     {
     char startBuf[16], endBuf[16];
     char *row[32];
     bigBedIntervalToRow(bb, chrom, startBuf, endBuf, row, ArraySize(row));
-    struct barChartBed *barChart = barChartBedLoadOptionalOffsets(row, TRUE);
+    struct barChartBed *barChart = barChartBedLoadOptionalOffsets(row, hasOffsets);
     if (sameString(barChart->name, item))
         return barChart;
     }
 return NULL;
 }
 
-static struct barChartBed *getBarChartFromTable(char *item, char *chrom, int start, int end, 
-                                                char *table)
+static struct barChartBed *getBarChartFromTable(struct trackDb *tdb, char *table, 
+                                                char *item, char *chrom, int start, int end)
 /* Retrieve barChart BED item from track table */
 {
 struct barChartBed *barChart = NULL;
 struct sqlConnection *conn = hAllocConn(database);
 char **row;
 char query[512];
 struct sqlResult *sr;
 if (sqlTableExists(conn, table))
     {
     sqlSafef(query, sizeof query, 
                 "SELECT * FROM %s WHERE name='%s'"
                     "AND chrom='%s' AND chromStart=%d AND chromEnd=%d", 
                                 table, item, chrom, start, end);
     sr = sqlGetResult(conn, query);
     row = sqlNextRow(sr);
     if (row != NULL)
         {
         barChart = barChartBedLoadOptionalOffsets(row, FALSE);
         }
     sqlFreeResult(&sr);
     }
 hFreeConn(&conn);
 return barChart;
 }
 
-static struct barChartBed *getBarChart(char *item, char *chrom, int start, int end, 
-                                        struct trackDb *tdb)
+static struct barChartBed *getBarChart(struct trackDb *tdb, char *item, char *chrom, int start, int end)
 /* Retrieve barChart BED item from track */
 {
 struct barChartBed *barChart = NULL;
 char *file = trackDbSetting(tdb, "bigDataUrl");
 if (file != NULL)
-    barChart = getBarChartFromFile(item, chrom, start, end, file);
+    barChart = getBarChartFromFile(tdb, file, item, chrom, start, end);
 else
-    barChart = getBarChartFromTable(item, chrom, start, end, tdb->table);
+    barChart = getBarChartFromTable(tdb, tdb->table, item, chrom, start, end);
 return barChart;
 }
 
 static struct barChartItemData *getSampleValsFromFile(struct trackDb *tdb, 
                                                         struct hash *categoryHash,
                                                         struct barChartBed *bed)
 /* Get all data values in a file for this item (locus) */
 {
 // Get sample categories from sample file
 // Format: id, category, extras
 char *url = trackDbSetting(tdb, "barChartSampleUrl");
 struct lineFile *lf = udcWrapShortLineFile(url, NULL, 0);
 struct hash *sampleHash = hashNew(0);
 char *words[2];
 int sampleCt = 0;
@@ -299,31 +311,31 @@
 
 /* Exec R in quiet mode, without reading/saving environment or workspace */
 char cmd[256];
 safef(cmd, sizeof(cmd), "Rscript --vanilla --slave hgcData/barChartBoxplot.R %s %s %s %s %s",
                                 item, units, colorFile, df, pngTn.forHtml);
 int ret = system(cmd);
 if (ret == 0)
     printf("<img src = \"%s\" border=1><br>\n", pngTn.forHtml);
 }
 
 void doBarChartDetails(struct trackDb *tdb, char *item)
 /* Details of barChart item */
 {
 int start = cartInt(cart, "o");
 int end = cartInt(cart, "t");
-struct barChartBed *chartItem = getBarChart(item, seqName, start, end, tdb);
+struct barChartBed *chartItem = getBarChart(tdb, item, seqName, start, end);
 if (chartItem == NULL)
     errAbort("Can't find item %s in barChart table/file %s\n", item, tdb->table);
 
 genericHeader(tdb, item);
 int categId;
 float highLevel = barChartMaxValue(chartItem, &categId);
 char *units = trackDbSettingClosestToHomeOrDefault(tdb, BAR_CHART_UNIT, "");
 printf("<b>Maximum value: </b> %0.2f %s in %s<br>\n", 
                 highLevel, units, barChartUiGetCategoryLabelById(categId, database, tdb));
 printf("<b>Total all values: </b> %0.2f<br>\n", barChartTotalValue(chartItem));
 printf("<b>Score: </b> %d<br>\n", chartItem->score); 
 printf("<b>Genomic position: "
                 "</b>%s <a href='%s&db=%s&position=%s%%3A%d-%d'>%s:%d-%d</a><br>\n", 
                     database, hgTracksPathAndSettings(), database, 
                     chartItem->chrom, chartItem->chromStart+1, chartItem->chromEnd,