2d0e3b500f8e1d5f1ecf372797f942204fa729b8
hiram
  Thu May 2 10:08:25 2019 -0700
correctly manage list chromosomes function with various table types refs #18869

diff --git src/hg/hubApi/list.c src/hg/hubApi/list.c
index de79d06..e6e93ab 100644
--- src/hg/hubApi/list.c
+++ src/hg/hubApi/list.c
@@ -184,64 +184,75 @@
     slSort(ci, chromInfoCmp);
     jsonWriteNumber(jw, "chromCount", (long long)slCount(ci));
     jsonWriteObjectStart(jw, "chromosomes");
     struct chromInfo *el = ci;
     for ( ; el != NULL; el = el->next )
 	{
 	jsonWriteNumber(jw, el->chrom, (long long)el->size);
 	}
     jsonWriteObjectEnd(jw);	/* chromosomes */
     }
 apiFinishOutput(0, NULL, jw);
 }
 
 static char *validChromName(struct sqlConnection *conn, char *db, char *table,
    char **splitTableName, struct hTableInfo **tableInfo)
-/* determine what the 'chrom' name should be for this table
+/* determine what the 'chrom' name should be for this table (aka track)
  * this function could be used in getData() also TBD
  */
 {
 static char *returnChrom = NULL;
-/* to be determined if this table name is used or changes */
-char *tableName = cloneString(table);
+/* to be determined if this table name is used or is some other name */
+char *sqlTableName = cloneString(table);
+
+/* 'track' name in trackDb usually refers to a SQL 'table' */
+struct trackDb *tdb = obtainTdb(NULL, db);
+struct trackDb *thisTrack = findTrackDb(table,tdb);
+/* however, the trackDb might have a specific table defined instead */
+char *tableName = trackDbSetting(thisTrack, "table");
+if (isNotEmpty(tableName))
+    {
+    freeMem(sqlTableName);
+    sqlTableName = cloneString(tableName);
+    }
 
 /* this function knows how to deal with split chromosomes, the NULL
  * here for the chrom name means to use the first chrom name in chromInfo
  */
-struct hTableInfo *hti = hFindTableInfoWithConn(conn, NULL, table);
-*tableInfo = hti;
+struct hTableInfo *hti = hFindTableInfoWithConn(conn, NULL, sqlTableName);
+*tableInfo = hti;	/* returning to caller */
 /* check if table name needs to be modified */
 if (hti && hti->isSplit)
     {
     char *defaultChrom = hDefaultChrom(db);
     char fullTableName[256];
     safef(fullTableName, sizeof(fullTableName), "%s_%s", defaultChrom, hti->rootName);
-    freeMem(tableName);
-    tableName = cloneString(fullTableName);
-    *splitTableName = cloneString(fullTableName);
+    freeMem(sqlTableName);
+    sqlTableName = cloneString(fullTableName);
+    *splitTableName = cloneString(fullTableName);	/* return to caller */
     }
 else
     {
-    tableName = cloneString(table);
-    *splitTableName = table;
+    *splitTableName = sqlTableName;	/* return to caller */
     }
 
-if (sqlColumnExists(conn, tableName, "chrom"))	/* standard bed tables */
+/* may need to extend this in the future for other track types */
+if (sqlColumnExists(conn, sqlTableName, "chrom"))	/* standard bed tables */
     returnChrom = cloneString("chrom");
-else if (sqlColumnExists(conn, tableName, "tName"))	/* track type psl */
+else if (sqlColumnExists(conn, sqlTableName, "tName"))	/* track type psl */
     returnChrom = cloneString("tName");
-else if (sqlColumnExists(conn, tableName, "genoName"))	/* track type rmsk */
+else if (sqlColumnExists(conn, sqlTableName, "genoName"))	/* track type rmsk */
     returnChrom = cloneString("genoName");
 
 return returnChrom;
 }
 
 static void chromInfoJsonOutput(FILE *f, char *db)
 /* for given db, if there is a track, list the chromosomes in that track,
  * for no track, simply list the chromosomes in the sequence
  */
 {
 char *splitSqlTable = NULL;
 struct hTableInfo *tableInfo = NULL;
 char *chromName = NULL;
 char *table = cgiOptionalString("track");
 struct sqlConnection *conn = hAllocConnMaybe(db);
@@ -294,30 +305,32 @@
 	apiErrAbort(err400, err400Msg, "track '%s' is not a position track, request table without chrom specification, genome: '%s'", table, db);
     }
 else if (table && !chromName)	/* only allowing position tables at this time */
 	apiErrAbort(err400, err400Msg, "track '%s' is not a position track, request table without chrom specification, genome: '%s'", table, db);
 else
     {
     char *dataTime = sqlTableUpdate(conn, "chromInfo");
     time_t dataTimeStamp = sqlDateToUnixTime(dataTime);
     replaceChar(dataTime, ' ', 'T');	/* ISO 8601 */
     struct chromInfo *ciList = createChromInfoList(NULL, db);
     slSort(ciList, chromInfoCmp);
     struct chromInfo *el = ciList;
     struct jsonWrite *jw = apiStartOutput();
     jsonWriteString(jw, "genome", db);
     jsonWriteString(jw, "dataTime", dataTime);
+    if (tableInfo && tableInfo->isSplit)	/* the split table punt */
+	jsonWriteString(jw, "track", table);
     jsonWriteNumber(jw, "dataTimeStamp", (long long)dataTimeStamp);
     freeMem(dataTime);
     jsonWriteNumber(jw, "chromCount", (long long)slCount(ciList));
     jsonWriteObjectStart(jw, "chromosomes");
     for ( ; el != NULL; el = el->next )
 	{
         jsonWriteNumber(jw, el->chrom, (long long)el->size);
 	}
     jsonWriteObjectEnd(jw);	/* chromosomes */
     apiFinishOutput(0, NULL, jw);
     }
 hFreeConn(&conn);
 }
 
 static void recursiveTrackList(struct jsonWrite *jw, struct trackDb *tdb)