src/hg/lib/hdb.c 1.428

1.428 2010/05/18 20:05:31 kent
Adding hGetTrackForTable()
Index: src/hg/lib/hdb.c
===================================================================
RCS file: /projects/compbio/cvsroot/kent/src/hg/lib/hdb.c,v
retrieving revision 1.427
retrieving revision 1.428
diff -b -B -U 4 -r1.427 -r1.428
--- src/hg/lib/hdb.c	11 May 2010 22:15:41 -0000	1.427
+++ src/hg/lib/hdb.c	18 May 2010 20:05:31 -0000	1.428
@@ -25,8 +25,9 @@
 #include "liftOver.h"
 #include "liftOverChain.h"
 #include "grp.h"
 #include "twoBit.h"
+#include "ra.h"
 #include "genbank.h"
 #include "chromInfo.h"
 #ifndef GBROWSE
 #include "axtInfo.h"
@@ -216,9 +217,9 @@
 for(; tdbList; tdbList = tdbList->next)
     {
     if (hTableExists(sqlGetDatabase(conn), tdbList->name))
         {
-        char query[2048];
+        char query[1024];
         safef(query, sizeof query,
             "select tableName from %s where type like '%s'", tdbList->name, type);
 
         struct sqlResult *sr = sqlGetResult(conn, query);
@@ -3748,8 +3749,55 @@
 hFreeConn(&conn);
 return ret;
 }
 
+static struct hash *makeDbTableToTrackHash(char *db)
+/* Create a hash based on trackDb tables in given database that 
+ * will give you a track name given a table name as a key. */
+{
+struct hash *hash = hashNew(0);
+struct slName *trackTable, *trackTableList = hTrackDbList();
+struct sqlConnection *conn = hAllocConn(db);
+for (trackTable = trackTableList; trackTable != NULL; trackTable = trackTable->next)
+    {
+    if (hTableExists(db, trackTable->name))
+        {
+	char query[512];
+	safef(query, sizeof(query), "select settings from %s", trackTable->name);
+	struct sqlResult *sr = sqlGetResult(conn, query);
+	char **row;
+	while ((row = sqlNextRow(sr)) != NULL)
+	    {
+	    struct hash *settings = raFromString(row[0]);
+	    char *track = hashMustFindVal(settings, "track");
+	    char *table = hashFindVal(settings, "table");
+	    if (table == NULL)
+	         table = track;
+	    hashAdd(hash, table, track);
+	    }
+	sqlFreeResult(&sr);
+	}
+    }
+slNameFreeList(&trackTableList);
+hFreeConn(&conn);
+return hash;
+}
+
+char *hGetTrackForTable(char *db, char *table)
+/* Given a table name, get first track associated with it. */
+{
+static struct hash *dbHash = NULL;
+if (dbHash == NULL)
+    dbHash = hashNew(0);
+struct hash *tableToTrackHash = hashFindVal(dbHash, db);
+if (tableToTrackHash == NULL)
+    {
+    tableToTrackHash = makeDbTableToTrackHash(db);
+    hashAdd(dbHash, db, tableToTrackHash);
+    }
+return hashFindVal(tableToTrackHash, table);
+}
+
 static struct dbDb *hGetIndexedDbsMaybeClade(char *theDb)
 /* Get list of active databases, in theDb's clade if theDb is not NULL.
  * Dispose of this with dbDbFreeList. */
 {