Index: src/hg/hgTracks/hgTracks.c
===================================================================
RCS file: /projects/compbio/cvsroot/kent/src/hg/hgTracks/hgTracks.c,v
retrieving revision 1.1616
retrieving revision 1.1617
diff -b -B -U 4 -r1.1616 -r1.1617
--- src/hg/hgTracks/hgTracks.c	21 Dec 2009 22:43:33 -0000	1.1616
+++ src/hg/hgTracks/hgTracks.c	4 Jan 2010 19:12:24 -0000	1.1617
@@ -353,10 +352,9 @@
     y += fontHeight;
 if (tdbIsComposite(track->tdb))
     {
     struct track *subtrack;
-    for (subtrack = track->subtracks;  subtrack != NULL;
-     subtrack = subtrack->next)
+    for (subtrack = track->subtracks;  subtrack != NULL; subtrack = subtrack->next)
     {
     if (isSubtrackVisible(subtrack) && isWithCenterLabels(subtrack))
         y += fontHeight;
     }
@@ -1286,9 +1284,9 @@
                         trackPastTabWidth, fontHeight, insideHeight, labelColor);
                 toggleDone = TRUE;
                 }
             }
-        if(!toggleDone)
+        if (!toggleDone)
             mapBoxToggleVis(hvg, trackPastTabX, y+1,trackPastTabWidth, insideHeight,
                             (theImgBox ? track : parentTrack));
         y += fontHeight;
         }
@@ -1532,25 +1530,28 @@
 {
 enum trackVisibility vis = subtrack->limitedVis == tvHide ?
                            subtrack->visibility :
                            tvMin(subtrack->visibility,subtrack->limitedVis);
-
-if(tdbIsCompositeChild(subtrack->tdb))
+struct trackDb *tdb = subtrack->tdb;
+if(tdbIsCompositeChild(tdb))
     {
-    char *stView;
-    // If the composite track has "view" based drop downs, set visibility based upon those
-    if(subgroupFind(subtrack->tdb,"view",&stView))
+    char *viewName = NULL;
+    if (subgroupFind(tdb,"view",&viewName))
         {
-        int len = strlen(subtrack->tdb->parent->tableName) + strlen(stView) + 10;
-        char *ddName = needMem(len);
-        safef(ddName,len,"%s.%s.vis",subtrack->tdb->parent->tableName,stView);
+	struct trackDb *parent = trackDbCompositeParent(tdb);
+	assert(parent != NULL);
+        int len = strlen(parent->tableName) + strlen(viewName) + 10;
+
+	// Create the view dropdown var name.  This needs to have the view name surrounded by dots 
+	// in the middle for the javascript to work.
+	char ddName[len];
+        safef(ddName,len,"%s.%s.vis", parent->tableName,viewName);
         char * fromParent = cartOptionalString(cart, ddName);
         if(fromParent)
             vis = hTvFromString(fromParent);
         else
-            vis = visCompositeViewDefault(subtrack->tdb->parent,stView);
-        freeMem(ddName);
-        subgroupFree(&stView);
+            vis = visCompositeViewDefault(parent,viewName);
+        subgroupFree(&viewName);
         }
     }
 return vis;
 }
@@ -1825,11 +1826,13 @@
 int sliceOffsetX[stMaxSliceTypes];
 int sliceHeight        = 0;
 int sliceOffsetY       = 0;
 char *rulerTtl = NULL;
-if(theImgBox)  // theImgBox is a global for now to avoid huge rewrite of hgTracks.  It is started prior to this in doTrackForm()
+if(theImgBox)  
+// theImgBox is a global for now to avoid huge rewrite of hgTracks.  It is started 
+// prior to this in doTrackForm()
     {
-    rulerTtl = (dragZooming?"drag select or click to zoom":"click to zoom 3x");//"click or drag mouse in base position track to zoom in" : NULL);
+    rulerTtl = (dragZooming?"drag select or click to zoom":"click to zoom 3x");
     hPrintf("<input type='hidden' name='db' value='%s'>\n", database);
     hPrintf("<input type='hidden' name='c' value='%s'>\n", chromName);
     hPrintf("<input type='hidden' name='l' value='%d'>\n", winStart);
     hPrintf("<input type='hidden' name='r' value='%d'>\n", winEnd);
@@ -2114,9 +2117,13 @@
                 }
             if (track->hasUi)
                 {
                 if(tdbIsCompositeChild(track->tdb))
-                    mapBoxTrackUi(hvg, trackTabX, yStart, trackTabWidth, (yEnd - yStart - 1), track->tdb->parent->tableName, track->tdb->parent->shortLabel, track->mapName);
+		    {
+		    struct trackDb *parent = trackDbCompositeParent(track->tdb);
+                    mapBoxTrackUi(hvg, trackTabX, yStart, trackTabWidth, (yEnd - yStart - 1), 
+		    	parent->tableName, parent->shortLabel, track->mapName);
+		    }
                 else
                     mapBoxTrackUi(hvg, trackTabX, yStart, trackTabWidth, h, track->mapName, track->shortLabel, track->mapName);
                 }
             }
@@ -2626,10 +2633,12 @@
     cartSetString(cart, track->tdb->tableName, "full");
     }
     else if (track->tdb->subtracks != NULL)
     {
-    for (subtrack = track->tdb->subtracks; subtrack != NULL; subtrack = subtrack->next)
+	struct slRef *tdbRef, *tdbRefList = trackDbListGetRefsToDescendants(track->tdb->subtracks);
+	for (tdbRef = tdbRefList; tdbRef != NULL; tdbRef = tdbRef->next)
         {
+	    subtrack = tdbRef->val;
         if (sameString(subtrack->tableName, table))
             {
             char selName[SMALLBUF];
             track->visibility = tvFull;
@@ -2639,8 +2648,9 @@
             safef(selName, sizeof(selName), "%s_sel", table);
             cartSetBoolean(cart, selName, TRUE);
             }
         }
+	slFreeList(&tdbRefList);
     }
     }
 hashElFreeList(&hels);
 }
@@ -2662,25 +2672,27 @@
 
 void loadFromTrackDb(struct track **pTrackList)
 /* Load tracks from database, consulting handler list. */
 {
-struct trackDb *tdb, *tdbList = NULL;
+struct trackDb *tdb, *next, *tdbList = NULL;
 struct track *track;
 TrackHandler handler;
 char *trackNameFilter = cartOptionalString(cart, "hgt.trackNameFilter");
 if(trackNameFilter == NULL)
     tdbList = hTrackDb(database, chromName);
 else
     tdbList = hTrackDbForTrack(database, trackNameFilter);
 tdbSortPrioritiesFromCart(cart, &tdbList);
-for (tdb = tdbList; tdb != NULL; tdb = tdb->next)
+for (tdb = tdbList; tdb != NULL; tdb = next)
     {
+    next = tdb->next;
     if(trackNameFilter != NULL && strcmp(trackNameFilter, tdb->tableName))
         // suppress loading & display of all tracks except for the one passed in via trackNameFilter
         continue;
     track = trackFromTrackDb(tdb);
     track->hasUi = TRUE;
-    if (slCount(tdb->subtracks) != 0) {
+    if (slCount(tdb->subtracks) != 0) 
+        {
         tdbSortPrioritiesFromCart(cart, &(tdb->subtracks));
         makeCompositeTrack(track, tdb);
         }
     else

