f619ad52904568bd108d65e34d2e10cb381f439e
hiram
  Mon Sep 21 14:04:09 2020 -0700
now recognizing assembly hub chromAlias file to allow bed type custom tracks with other names refs #24396

diff --git src/hg/lib/hdb.c src/hg/lib/hdb.c
index d606637..5632fbf 100644
--- src/hg/lib/hdb.c
+++ src/hg/lib/hdb.c
@@ -187,69 +187,93 @@
 
 static struct chromInfo *mustGetChromInfo(char *db, char *chrom)
 /* Get chromInfo for named chrom from primary database or
  * die trying. */
 {
 struct chromInfo *ci = hGetChromInfo(db, chrom);
 if (ci == NULL)
     errAbort("Couldn't find chromosome/scaffold %s in database", chrom);
 return ci;
 }
 
 char *hgOfficialChromName(char *db, char *name)
 /* Returns "canonical" name of chromosome or NULL
  * if not a chromosome. (Case-insensitive search w/sameWord()) */
 {
-/* aliasHash will be initialized if chromAlias table exists */
+/* aliasHash will be initialized if chromAlias table exists
+ *   or track chromAlias file exists
+ *  hash key is alias name, hash value is chromosome name
+ */
 static struct hash *aliasHash = NULL;
 
 if (strlen(name) > HDB_MAX_CHROM_STRING)
     return NULL;
 struct chromInfo *ci = NULL;
 char buf[HDB_MAX_CHROM_STRING];
 strncpy(buf, name, HDB_MAX_CHROM_STRING);
 buf[HDB_MAX_CHROM_STRING-1] = 0;
 ci = hGetChromInfo(db, buf);
 if (ci != NULL)
     return cloneString(ci->chrom);
 else
     {
+    if (!aliasHash && trackHubDatabase(db))
+	{
+        aliasHash = newHash(4); /* could remain empty if no hubHash which
+	 * will prevent getting into this initalization again and all hash
+	 * lookups will return NULL.
+	 * hub hash is different than what we want here, its key is
+         * the chrom name with a hash value of: struct chromAlias *
+         */
+        struct hash *hubHash = trackHubAllChromAlias(db);
+        if (hubHash)
+	    {
+	    /* rearrange hubHash into the hash we need here */
+	    struct hashCookie cookie = hashFirst(hubHash);
+	    struct hashEl *hel;
+	    while ((hel = hashNext(&cookie)) != NULL)
+		{
+		struct chromAlias *ca = (struct chromAlias *)hel->val;
+		hashAdd(aliasHash, ca->alias, cloneString(ca->chrom));
+		}
+	    }
+	}
     if (aliasHash || hTableExists(db, "chromAlias"))
        {
        if (! aliasHash)	/* first time, initialize aliasHash */
 	    {
             aliasHash = newHash(4);
 	    struct sqlConnection *conn = hAllocConn(db);
 	    char query[512];
 	    sqlSafef(query, sizeof(query), "select * from chromAlias");
 	    struct sqlResult *sr = sqlGetResult(conn, query);
 	    char **row;
 	    while ((row = sqlNextRow(sr)) != NULL)
 		{
-		struct chromAlias *new = chromAliasLoad(row);
-		hashAdd(aliasHash, new->alias, cloneString(new->chrom));
+		struct chromAlias *ca = chromAliasLoad(row);
+		hashAdd(aliasHash, ca->alias, cloneString(ca->chrom));
 		}
 	    sqlFreeResult(&sr);
 	    hFreeConn(&conn);
 	    }
 	char *chrom = (char *)hashFindVal(aliasHash, name);
        if (isNotEmpty(chrom))
          return cloneString(chrom);
        }
     }
 return NULL;
-}
+}	/*	char *hgOfficialChromName(char *db, char *name)	*/
 
 boolean hgIsOfficialChromName(char *db, char *name)
 /* Determine if name is exact (case-sensitive) match with
  * a chromosome in the given assembly */
 {
 char *chrom;
 return ((chrom = hgOfficialChromName(db, name)) != NULL &&
 	sameString(name, chrom));
 }
 
 static boolean minLen = 0;
 
 void setMinIndexLengthForTrashCleaner()
 /* set the minimum index size so trash cleaner will not die
  * on custom tracks on hubs that are not currently loading.