41a9770252865a7968aaa170ed14a713b1e43e1c
jcasper
  Mon Aug 12 07:51:02 2019 -0700
Fixing two bugs in displaying 4DN hic files, refs #22316

diff --git src/hg/hgTracks/hicTrack.c src/hg/hgTracks/hicTrack.c
index 3de1c1a..9e73689 100644
--- src/hg/hgTracks/hicTrack.c
+++ src/hg/hgTracks/hicTrack.c
@@ -44,31 +44,31 @@
     maxHeight *= 0.5;
 return maxHeight;
 }
 
 struct hicMeta *grabHeader(struct track *tg)
 /* Fetch a hicMeta structure that describes the Hi-C data associated with
  * the track. */
 {
 char *filename = trackDbSettingOrDefault(tg->tdb, "bigDataUrl", NULL);
 struct hicMeta *metaResult = NULL;
 if (filename == NULL)
     {
     warn("Missing bigDataUrl setting for track %s", tg->track);
     return NULL;
     }
-char *errMsg = hicLoadHeader(filename, &metaResult);
+char *errMsg = hicLoadHeader(filename, &metaResult, database);
 if (errMsg != NULL)
     {
     tg->networkErrMsg = errMsg;
     return NULL;
     }
 return metaResult;
 }
 
 static void loadAndFilterItems(struct track *tg)
 /* Load all Hi-C items in the current region and identify the window height
  * and median value for this region. */
 {
 if (tg->customPt == NULL)
     tg->customPt = grabHeader(tg);
 if (tg->customPt == NULL)
@@ -85,60 +85,65 @@
 // Pad the start because we want to display partial interactions if the end of an interacting block is
 // in view but not the start.  Straw won't report interactions if the start of the block isn't in the
 // supplied position range.
 int strawStart = winStart - binSize + 1;
 if (strawStart < 0) strawStart = 0;
 
 struct interact *hicItems = NULL;
 tg->networkErrMsg = hicLoadData(hicFileInfo, binSize, normalization, chromName, strawStart, winEnd-1, chromName,
         strawStart, winEnd-1, &hicItems);
 
 // Using the interact structure because it has convenient fields, but this is not interact data and
 // shouldn't be passed to those functions.
 int numRecords = slCount(hicItems), filtNumRecords = 0;
 tg->maxRange = 0.0; // the max height of an interaction in this window
 double *countsCopy = NULL;
+if (numRecords > 0)
     AllocArray(countsCopy, numRecords);
 
 struct interact *thisHic = hicItems;
 while (thisHic != NULL)
     {
     char *drawMode = hicUiFetchDrawMode(cart, tg->track);
     if (sameString(drawMode, HIC_DRAW_MODE_ARC))
         {
         // we omit self-interactions in arc mode (they'd just be weird vertical lines)
         if (sameString(thisHic->sourceChrom, thisHic->targetChrom) &&
                 (thisHic->sourceStart == thisHic->targetStart))
             continue;
         }
     countsCopy[filtNumRecords++] = thisHic->value;
 
     // Calculate the track draw height required to see this item
     int leftx = max(thisHic->chromStart, winStart);
     int rightx = min(thisHic->chromEnd, winEnd);
     double thisHeight = scaleForWindow(insideWidth, winStart, winEnd)*(rightx - leftx)/2.0; // triangle or arc
     if (sameString(drawMode,HIC_DRAW_MODE_SQUARE))
         thisHeight = scaleForWindow(insideWidth, winStart, winEnd)*(winEnd-winStart); // square - always draw the full square
 
     if (thisHeight > tg->maxRange)
         tg->maxRange = thisHeight;
     thisHic = thisHic->next;
     }
 
 // Heuristic for auto-scaling the color gradient based on the scores in view - draw the max color value
 // at or above 2*median score.
+if (filtNumRecords > 0)
     tg->graphUpperLimit = 2.0*doubleMedian(filtNumRecords, countsCopy);
+else
+    tg->graphUpperLimit = 0.0;
+if (countsCopy != NULL)
     free(countsCopy);
 tg->items = hicItems;
 }
 
 void hicLoadItems(struct track *tg)
 /* Load Hi-C items in (mostly) interact format */
 {
 char *filename = trackDbSettingOrDefault(tg->tdb, "bigDataUrl", NULL);
 if (filename == NULL)
     return;
 tg->customPt = grabHeader(tg);
 if (tg->customPt == NULL)
     return;
 loadAndFilterItems(tg);
 }