88c3b3d8ed217d673a1879972d488f2303ea72f3
hiram
  Wed Jun 22 15:53:12 2011 -0700
properly recognize custom tracks, and allow close of bigWig files after load time
diff --git src/hg/hgTracks/bigWigTrack.c src/hg/hgTracks/bigWigTrack.c
index 32622c3..53bc1bd 100644
--- src/hg/hgTracks/bigWigTrack.c
+++ src/hg/hgTracks/bigWigTrack.c
@@ -61,37 +61,46 @@
     }
 errCatchEnd(errCatch);
 if (errCatch->gotError)
     {
     tg->networkErrMsg = cloneString(errCatch->message->string);
     }
 errCatchFree(&errCatch);
 tg->preDrawContainer = preDrawList;
 return preDrawList;
 }
 
 static void bigWigDrawItems(struct track *tg, int seqStart, int seqEnd,
 	struct hvGfx *hvg, int xOff, int yOff, int width,
 	MgFont *font, Color color, enum trackVisibility vis)
 {
-struct preDrawContainer *pre = bigWigLoadPreDraw(tg, seqStart, seqEnd, width);
+boolean canCloseHere = FALSE;
+struct preDrawContainer *pre = tg->customPt; // populated by bigWigLoadItems
+if (! pre)	// custom tracks could not do this at their load time
+    {
+    pre = bigWigLoadPreDraw(tg, seqStart, seqEnd, width);
+    canCloseHere = TRUE;
+    }
+
 if (tg->networkErrMsg == NULL)
     {
     /* Call actual graphing routine. */
     wigDrawPredraw(tg, seqStart, seqEnd, hvg, xOff, yOff, width, font, color, vis,
 		   pre, pre->preDrawZero, pre->preDrawSize, 
 		   &tg->graphUpperLimit, &tg->graphLowerLimit);
+    if (canCloseHere)
+	bigWigFileClose(&tg->bbiFile);
     }
 else
     bigDrawWarning(tg, seqStart, seqEnd, hvg, xOff, yOff, width, font, color, vis);
 }
 
 static void bigWigOpenCatch(struct track *tg, char *fileName)
 /* Try to open big wig file, store error in track struct */
 {
 /* protect against temporary network error */
 struct errCatch *errCatch = errCatchNew();
 if (errCatchStart(errCatch))
     {
     struct bbiFile *bbiFile = bigWigFileOpen(fileName);
     slAddHead(&tg->bbiFile, bbiFile);
     }
@@ -113,27 +122,29 @@
 char *extTableString = trackDbSetting(tg->tdb, "extTable");
 
 if (tg->bbiFile == NULL)
     {
     /* Figure out bigWig file name. */
     struct sqlConnection *conn = hAllocConnTrack(database, tg->tdb);
     char *fileName = bbiNameFromSettingOrTable(tg->tdb, conn, tg->table);
     bigWigOpenCatch(tg, fileName);
     // if there's an extra table, read this one in too
     if (extTableString != NULL)
 	{
 	fileName = bbiNameFromSettingOrTable(tg->tdb, conn, extTableString);
         bigWigOpenCatch(tg, fileName);
 	}
     hFreeConn(&conn);
+    tg->customPt = (void *)bigWigLoadPreDraw(tg, winStart, winEnd, insideWidth);
+    bigWigFileClose(&tg->bbiFile);
     }
 }
 
 void bigWigMethods(struct track *track, struct trackDb *tdb, 
 	int wordCount, char *words[])
 /* Set up bigWig methods. */
 {
 bedGraphMethods(track, tdb, wordCount, words);
 track->loadItems = bigWigLoadItems;
 track->drawItems = bigWigDrawItems;
 track->loadPreDraw = bigWigLoadPreDraw;
 }