060985eeea466cb4321e1e5e8c6401b62290f3ca
hiram
  Wed May 1 13:15:03 2019 -0700
correctly finding table for list chromosome function with database table refs #18869

diff --git src/hg/hubApi/list.c src/hg/hubApi/list.c
index 041c74f..4716815 100644
--- src/hg/hubApi/list.c
+++ src/hg/hubApi/list.c
@@ -182,59 +182,108 @@
 	ci = trackHubAllChromInfo(foundGenome->name);
 	}
     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
+ * 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);
+
+/* 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;
+/* 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);
+    }
+else
+    {
+    tableName = cloneString(table);
+    *splitTableName = table;
+    }
+
+if (sqlColumnExists(conn, tableName, "chrom"))	/* standard bed tables */
+    returnChrom = cloneString("chrom");
+else if (sqlColumnExists(conn, tableName, "tName"))	/* track type psl */
+    returnChrom = cloneString("tName");
+else if (sqlColumnExists(conn, tableName, "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);
 if (NULL == conn)
     apiErrAbort(err400, err400Msg, "can not find 'genome=%s' for endpoint '/list/chromosomes", db);
 
-/* in trackDb language: track == table */
 if (table)
+    chromName = validChromName(conn, db, table, &splitSqlTable, &tableInfo);
+
+/* in trackDb language: track == table */
+/* punting on split tables, just return chromInfo */
+if (table && chromName && ! (tableInfo && tableInfo->isSplit) )
     {
-    if (! sqlTableExists(conn, table))
+    if (! sqlTableExists(conn, splitSqlTable))
 	apiErrAbort(err400, err400Msg, "can not find specified 'track=%s' for endpoint: /list/chromosomes?genome=%s;track=%s", table, db, table);
-    if (sqlColumnExists(conn, table, "chrom"))
+
+    if (sqlColumnExists(conn, splitSqlTable, chromName))
 	{
-	char *dataTime = sqlTableUpdate(conn, table);
+	char *dataTime = sqlTableUpdate(conn, splitSqlTable);
 	time_t dataTimeStamp = sqlDateToUnixTime(dataTime);
 	replaceChar(dataTime, ' ', 'T');	/* ISO 8601 */
         struct jsonWrite *jw = apiStartOutput();
 	jsonWriteString(jw, "genome", db);
 	jsonWriteString(jw, "track", table);
 	jsonWriteString(jw, "dataTime", dataTime);
 	jsonWriteNumber(jw, "dataTimeStamp", (long long)dataTimeStamp);
 	freeMem(dataTime);
         struct slPair *list = NULL;
 	char query[2048];
-        sqlSafef(query, sizeof(query), "select distinct chrom from %s", table);
+        sqlSafef(query, sizeof(query), "select distinct %s from %s", chromName, splitSqlTable);
 	struct sqlResult *sr = sqlGetResult(conn, query);
 	char **row;
 	while ((row = sqlNextRow(sr)) != NULL)
     	{
             int size = hChromSize(db, row[0]);
 	    slAddHead(&list, slPairNew(row[0], intToPt(size)));
     	}
 	sqlFreeResult(&sr);
         slPairIntSort(&list);
         slReverse(&list);
         jsonWriteNumber(jw, "chromCount", (long long)slCount(list));
 	jsonWriteObjectStart(jw, "chromosomes");
         struct slPair *el = list;
         for ( ; el != NULL; el = el->next )
             jsonWriteNumber(jw, el->name, (long long)ptToInt(el->val));