Index: src/hg/hgTables/wiggle.c
===================================================================
RCS file: /projects/compbio/cvsroot/kent/src/hg/hgTables/wiggle.c,v
retrieving revision 1.75
retrieving revision 1.76
diff -b -B -U 4 -r1.75 -r1.76
--- src/hg/hgTables/wiggle.c	20 Jul 2009 23:33:13 -0000	1.75
+++ src/hg/hgTables/wiggle.c	4 Jan 2010 19:12:23 -0000	1.76
@@ -383,9 +383,8 @@
 struct trackDb *tdb1 = hTrackDbForTrack(database, table);
 struct trackTable *tt1 = trackTableNew(tdb1, table, conn);
 struct dataVector *dataVector1 = dataVectorFetchOneRegion(tt1, region, conn);
 struct trackDb *cTdb = hCompositeTrackDbForSubtrack(database, tdb1);
-struct trackDb *sTdb = NULL;
 int numSubtracks = 1;
 char *op = cartString(cart, hgtaSubtrackMergeWigOp);
 boolean requireAll = cartBoolean(cart, hgtaSubtrackMergeRequireAll);
 boolean useMinScore = cartBoolean(cart, hgtaSubtrackMergeUseMinScore);
@@ -399,10 +398,13 @@
     {
     return NULL;
     }
 
-for (sTdb = cTdb->subtracks;  sTdb != NULL;  sTdb = sTdb->next)
+struct slRef *tdbRefList = trackDbListGetRefsToDescendantLeaves(cTdb->subtracks);
+struct slRef *tdbRef;
+for (tdbRef = tdbRefList; tdbRef != NULL; tdbRef = tdbRef->next)
     {
+    struct trackDb *sTdb = tdbRef->val;
     if (isSubtrackMerged(sTdb->tableName) &&
 	! sameString(tdb1->tableName, sTdb->tableName) &&
 	hSameTrackDbType(tdb1->type, sTdb->type))
 	{
@@ -431,8 +433,9 @@
 	    errAbort("mergedWigOutRegion: unknown WigOp %s", op);
 	dataVectorFree(&dataVector2);
 	}
     }
+slFreeList(&tdbRefList);
 if (sameString(op, "average"))
     dataVectorNormalize(dataVector1, numSubtracks);
 if (useMinScore)
     dataVectorFilterMin(dataVector1, minScore);
@@ -641,17 +644,20 @@
 if (track != NULL)
     {
     if (!sameString(track->tableName, table) && track->subtracks != NULL)
 	{
-	struct trackDb *tdb = NULL;
-	for (tdb = track->subtracks;  tdb != NULL;  tdb = tdb->next)
+	struct slRef *tdbRefList = trackDbListGetRefsToDescendantLeaves(track->subtracks);
+	struct slRef *tdbRef;
+	for (tdbRef = tdbRefList; tdbRef != NULL; tdbRef = tdbRef->next)
 	    {
+	    struct trackDb *tdb = tdbRef->val;
 	    if (sameString(tdb->tableName, table))
 		{
 		track = tdb;
 		break;
 		}
 	    }
+	slFreeList(&tdbRefList);
 	}
     shortLabel = track->shortLabel;
     longLabel = track->longLabel;
     }

