7df99795b147931dfea8220ed5ab305d11fde6b4
braney
  Thu Mar 10 15:41:46 2022 -0800
move some chromAlias stuff around so bedToBigBed can use a chromAlias
bigBed as a chromAlias file.

diff --git src/hg/lib/chromAlias.c src/hg/lib/chromAlias.c
index a0d1027..f318542 100644
--- src/hg/lib/chromAlias.c
+++ src/hg/lib/chromAlias.c
@@ -1,29 +1,30 @@
 /* chromAlias.c was originally generated by the autoSql program, which also 
  * generated chromAlias.h and chromAlias.sql.  This module links the database and
  * the RAM representation of objects. */
 
 #include <pthread.h>
 #include "common.h"
 #include "linefile.h"
 #include "dystring.h"
 #include "jksql.h"
 #include "chromAlias.h"
 #include "hdb.h"
 #include "trackHub.h"
 #include "fieldedTable.h"
 #include "bigBed.h"
+#include "bbiAlias.h"
 #include "bPlusTree.h"
 
 
 char *chromAliasCommaSepFieldNames = "alias,chrom,source";
 
 void chromAliasStaticLoad(char **row, struct chromAlias *ret)
 /* Load a row from chromAlias table into ret.  The contents of ret will
  * be replaced at the next call to this function. */
 {
 
 ret->alias = row[0];
 ret->chrom = row[1];
 ret->source = row[2];
 }
 
@@ -156,43 +157,37 @@
 fputc('"',f);
 fputc(',',f);
 fputc('"',f);
 fprintf(f,"source");
 fputc('"',f);
 fputc(':',f);
 fputc('"',f);
 fprintf(f, "%s", el->source);
 fputc('"',f);
 fputc('}',f);
 }
 
 /* -------------------------------- End autoSql Generated Code -------------------------------- */
 
 /* our "global" data */
-struct bptIndex
-{
-struct bptIndex *next;
-int fieldIx;
-struct bptFile *bpt;
-};
-
 static struct
 {
 boolean inited;
 boolean bptInited;
 struct bptIndex *bptList;
 struct bbiFile *bbi;
+struct lm *lm;
 struct hash *chromToAliasHash;
 struct hash *aliasToChromHash;
 } chromAliasGlobals;
 
 static void readOldAlias(struct lineFile *lf)
 /* Don't assume the table is fully populated, and dummy up a value for source. */
 {
 char *words[1024];	/* process lines, no more than 1,024 words on a line */
 char *line;
 int size;
 while (lineFileNext(lf, &line, &size))
     {
     int wordCount = chopByWhite(line, words, ArraySize(words));
     if (wordCount > 1)
         {
@@ -229,30 +224,32 @@
         }
     }
 }
 
 static char * gbdbBbExists(char *database)
 /* use a gbdb bigBed as our alias file. */
 {
 // not supported at the moment
 return NULL;
 }
 
 static void chromAliasSetupBb(char *database, char *bbFile)
 /* Look for a chromAlias bigBed file and open it. */
 {
 chromAliasGlobals.bbi = bigBedFileOpen(bbFile);
+chromAliasGlobals.bptList = bbiAliasOpenExtra(chromAliasGlobals.bbi);
+chromAliasGlobals.lm = lmInit(0);
 }
 
 static void chromAliasSetupHub(char *database)
 /* Look for a chromAlias text table and load the hashes with its contents. */
 {
 char *aliasBbFile = trackHubAliasBbFile(database);
 if (aliasBbFile != NULL)
     {
     chromAliasSetupBb(database, aliasBbFile);
     return;
     }
 char *aliasFile = trackHubAliasFile(database);
 if (aliasFile == NULL)
     return;
 
@@ -331,125 +328,57 @@
     chromAliasSetupBb(database, gbdbFile);
 else
     chromAliasSetupSql(database);
 releaseLock();
 }
 
 char *findNativeHashes(char *alias)
 /* Find a native sequence given an alias using the hash tables. */
 {
 char *chrom = (char *)hashFindVal(chromAliasGlobals.aliasToChromHash, alias);
 if (isNotEmpty(chrom))
     return cloneString(chrom);
 return NULL;
 }
 
-static struct bptIndex *getBpts(struct bbiFile *bbi)
-/* Open any extra indices that this bigBed has. */
-{
-if (chromAliasGlobals.bptInited)
-    return chromAliasGlobals.bptList; 
-
-if (!chromAliasGlobals.bptInited)
-    {
-    struct bptIndex *bptList = NULL;
-    struct slName *indexList = bigBedListExtraIndexes(bbi);
-    for(; indexList; indexList = indexList->next)
-        {
-        struct bptIndex *bptIndex;
-        AllocVar(bptIndex);
-        bptIndex->bpt = bigBedOpenExtraIndex(bbi, indexList->name, &bptIndex->fieldIx);
-        slAddHead(&bptList, bptIndex);
-        }
-    chromAliasGlobals.bptList = bptList;
-    chromAliasGlobals.bptInited = TRUE;
-    }
-
-return chromAliasGlobals.bptList;
-}
-
-char *findNativeBb(struct bbiFile *bbi, char *alias)
-/* Find the native seqName for a given alias given a bigBed. */
-{
-struct bptIndex *bptIndex = getBpts(bbi);
-
-for(; bptIndex; bptIndex = bptIndex->next)
-    {
-    struct lm *lm = lmInit(0);
-    struct bigBedInterval *bb= bigBedNameQuery(bbi, bptIndex->bpt, bptIndex->fieldIx, alias, lm);
-
-    if (bb != NULL)
-        {
-        char chromName[1024];
-        bptStringKeyAtPos(bbi->chromBpt, bb->chromId,  chromName, sizeof(chromName));
-
-        return cloneString(chromName);
-        }
-    }
-
-return NULL;
-}
-
 char *chromAliasFindNative(char *alias)
 /* Find the native seqName for a given alias. */
 {
 static struct hash *cachedNative;
 char *chrom;
 
 if (cachedNative == NULL)
     cachedNative = newHash(6);
 
 if ((chrom = hashFindVal(cachedNative, alias)) != NULL)
     return chrom;
 
 getLock();
 if ((chrom = hashFindVal(cachedNative, alias)) == NULL)
     {
     if (chromAliasGlobals.bbi)
-        chrom = findNativeBb(chromAliasGlobals.bbi, alias);
+        chrom = bbiAliasFindNative(chromAliasGlobals.bbi, chromAliasGlobals.bptList, chromAliasGlobals.lm,  alias);
     else if (chromAliasGlobals.aliasToChromHash)
         chrom = findNativeHashes(alias);
 
     hashAdd(cachedNative, alias, chrom);
     }
 releaseLock();
 
 return cloneString(chrom);
 }
 
-struct slName *findAliasesBb(struct bbiFile *bbi, char *seqName)
-/* Find the aliases for a given seqName using the alias bigBed. */
-{
-struct lm *lm = lmInit(0);
-struct bigBedInterval *bb, *bbList =  bigBedIntervalQuery(bbi, seqName, 0, 1, 0, lm);
-char *bedRow[bbi->fieldCount];
-char startBuf[16], endBuf[16];
-struct slName *list = NULL;
-for (bb = bbList; bb != NULL; bb = bb->next)
-    {
-    bigBedIntervalToRow(bb, seqName, startBuf, endBuf, bedRow, ArraySize(bedRow));
-    int ii;
-    for(ii=3; ii < chromAliasGlobals.bbi->fieldCount; ii++)
-	{
-	struct slName *name = newSlName(bedRow[ii]);
-	slAddHead(&list, name);
-	}
-    }
-
-return list;
-}
-
 struct slName *findAliasesHashes(char *seqName)
 /* Find the aliases for a given seqName using the hashes. */
 {
 struct slName *slList = NULL;
 struct hashEl *thisEl = hashLookup(chromAliasGlobals.chromToAliasHash, seqName);
 
 for (;thisEl != NULL; thisEl = hashLookupNext(thisEl))
     {
     struct slName *name = newSlName((char *)thisEl->val);
     slAddHead(&slList, name);
     }
 
 return slList;
 }
 
@@ -457,25 +386,25 @@
 /* Find the aliases for a given seqName. */
 {
 static struct hash *cachedAliases;
 struct slName *aliases;
 
 if (cachedAliases == NULL)
     cachedAliases = newHash(6);
 
 if ((aliases = hashFindVal(cachedAliases, seqName)) != NULL)
     return aliases;
 
 getLock();
 if ((aliases = hashFindVal(cachedAliases, seqName)) == NULL)
     {
     if (chromAliasGlobals.bbi)
-        aliases = findAliasesBb(chromAliasGlobals.bbi, seqName);
+        aliases = bbiAliasFindAliases(chromAliasGlobals.bbi,chromAliasGlobals.lm, seqName);
     else if (chromAliasGlobals.chromToAliasHash)
         aliases = findAliasesHashes(seqName);
 
     hashAdd(cachedAliases, seqName, aliases);
     }
 releaseLock();
 
 return aliases;
 }