aba8125cb532df17beb7c7c9bc8467a43d09e3d6
braney
  Wed Feb 10 13:39:27 2016 -0800
changes to allow for GenBank metadata to be held in a common table.  #16809

diff --git src/hg/hgTracks/simpleTracks.c src/hg/hgTracks/simpleTracks.c
index 2690bd8..87603cc 100644
--- src/hg/hgTracks/simpleTracks.c
+++ src/hg/hgTracks/simpleTracks.c
@@ -128,30 +128,31 @@
 #include "retroTracks.h"
 #include "pcrResult.h"
 #include "variome.h"
 #include "pubsTracks.h"
 #endif /* GBROWSE */
 
 #ifdef LOWELAB
 #include "loweLabTracks.h"
 #include "rnaPLFoldTrack.h"
 #endif /* LOWELAB */
 #ifdef LOWELAB_WIKI
 #include "wiki.h"
 #endif /* LOWELAB_WIKI */
 
 #include "trackVersion.h"
+#include "genbank.h"
 
 #define CHROM_COLORS 26
 
 /* Declare our color gradients and the the number of colors in them */
 Color shadesOfGreen[EXPR_DATA_SHADES];
 Color shadesOfRed[EXPR_DATA_SHADES];
 Color shadesOfBlue[EXPR_DATA_SHADES];
 Color shadesOfYellow[EXPR_DATA_SHADES];
 Color shadesOfGreenOnWhite[EXPR_DATA_SHADES];
 Color shadesOfRedOnWhite[EXPR_DATA_SHADES];
 Color shadesOfBlueOnWhite[EXPR_DATA_SHADES];
 Color shadesOfYellowOnWhite[EXPR_DATA_SHADES];
 Color shadesOfRedOnYellow[EXPR_DATA_SHADES];
 Color shadesOfBlueOnYellow[EXPR_DATA_SHADES];
 Color orangeColor = 0;
@@ -5893,33 +5894,33 @@
                 dyStringAppend(name, geneSymbol);
                 if (useAll) dyStringAppendC(name, '/');
                 }
             }
         if (useKgId)
             {
             dyStringAppend(name, lf->name);
             if (useAll) dyStringAppendC(name, '/');
 	    }
         if (useProtDisplayId)
             {
 	    sqlSafefFrag(cond_str, sizeof(cond_str), "kgID='%s'", lf->name);
             protDisplayId = sqlGetField("hg17", "kgXref", "spDisplayID", cond_str);
             dyStringAppend(name, protDisplayId);
 	    }
-        if (useMimId && hTableExists(database, "refLink"))
+        if (useMimId && sqlTableExists(conn, refLinkTable))
             {
-            sqlSafef(cond_str, sizeof(cond_str), "select cast(refLink.omimId as char) from kgXref,refLink where kgID = '%s' and kgXref.refseq = refLink.mrnaAcc and refLink.omimId != 0", lf->name);
+            sqlSafef(cond_str, sizeof(cond_str), "select cast(r.omimId as char) from kgXref,%s r where kgID = '%s' and kgXref.refseq = r.mrnaAcc and r.omimId != 0",refLinkTable, lf->name);
             mimId = sqlQuickString(conn, cond_str);
             if (mimId)
                 dyStringAppend(name, mimId);
             }
         lf->extra = dyStringCannibalize(&name);
 	}
     }
 hFreeConn(&conn);
 }
 
 void loadHg17Kg(struct track *tg)
 /* Load up known genes. */
 {
 enum trackVisibility vis = tg->visibility;
 tg->items = lfFromGenePredInRange(tg, "hg17Kg", chromName, winStart, winEnd);
@@ -6067,35 +6068,35 @@
         if (useProtDisplayId)
             {
             if (labelStarted) dyStringAppendC(name, '/');
             else labelStarted = TRUE;
             if (lf->extra != NULL)
                 {
                 dyStringAppend(name, (char *)lf->extra);
                 }
             else
                 {
 	        sqlSafefFrag(cond_str, sizeof(cond_str), "kgID='%s'", lf->name);
                 protDisplayId = sqlGetField(database, "kgXref", "spDisplayID", cond_str);
                 dyStringAppend(name, protDisplayId);
                 }
 	    }
-        if (useMimId && hTableExists(database, "refLink"))
+        if (useMimId && sqlTableExists(conn, refLinkTable))
             {
             if (labelStarted) dyStringAppendC(name, '/');
             else labelStarted = TRUE;
-            sqlSafef(cond_str, sizeof(cond_str), "select cast(refLink.omimId as char) from kgXref,refLink where kgID = '%s' and kgXref.refseq = refLink.mrnaAcc and refLink.omimId != 0", lf->name);
+            sqlSafef(cond_str, sizeof(cond_str), "select cast(r.omimId as char) from kgXref,%s r where kgID = '%s' and kgXref.refseq = r.mrnaAcc and r.omimId != 0",refLinkTable, lf->name);
             mimId = sqlQuickString(conn, cond_str);
             if (mimId)
                 dyStringAppend(name, mimId);
             }
         /* should this be a hash instead? */
         kgE->name = dyStringCannibalize(&name);
         kgE->hgg_prot = lf->extra;
         lf->extra = kgE;
 	}
     }
 hFreeConn(&conn);
 }
 
 struct linkedFeatures *stripShortLinkedFeatures(struct linkedFeatures *list)
 /* Remove linked features with no tall component from list. */
@@ -6202,33 +6203,33 @@
 lighter.g = (6*normal->g + 4*255) / 10;
 lighter.b = (6*normal->b + 4*255) / 10;
 
 lightest.r = (1*normal->r + 2*255) / 3;
 lightest.g = (1*normal->g + 2*255) / 3;
 lightest.b = (1*normal->b + 2*255) / 3;
 
 /* set default to the lightest color */
 col = hvGfxFindColorIx(hvg, lightest.r, lightest.g, lightest.b);
 
 /* set color first according to RefSeq status (if there is a corresponding RefSeq) */
 sqlSafefFrag(cond_str, sizeof cond_str, "name='%s' ", lf->name);
 refAcc = sqlGetField(database, "refGene", "name", cond_str);
 if (refAcc != NULL)
     {
-    if (hTableExists(database, "refSeqStatus"))
+    if (sqlTableExists(conn, refSeqStatusTable))
         {
-        sqlSafef(query, sizeof query, "select status from refSeqStatus where mrnaAcc = '%s'", refAcc);
+        sqlSafef(query, sizeof query, "select status from %s where mrnaAcc = '%s'", refSeqStatusTable, refAcc);
         sr = sqlGetResult(conn, query);
         if ((row = sqlNextRow(sr)) != NULL)
             {
 	    if (startsWith("Reviewed", row[0]) || startsWith("Validated", row[0]))
                 {
                 /* Use the usual color */
                 col = tg->ixColor;
                 }
 	    else
                 {
                 col = hvGfxFindColorIx(hvg, lighter.r, lighter.g, lighter.b);
                 }
 	    }
 	sqlFreeResult(&sr);
 	}
@@ -6651,61 +6652,61 @@
 
 char *getOrganism(struct sqlConnection *conn, char *acc)
 /* lookup the organism for an mrna, or NULL if not found */
 {
 // cache results, as this can be called a lot of times trying to pack tracks and test
 // for row overflow
 static struct hash *cache = NULL;
 if (cache == NULL)
     cache = hashNew(0);
 // N.B. NULL is a valid value in the cache
 struct hashEl *cacheEl = hashLookup(cache, acc);
 if (cacheEl == NULL)
     {
     char query[256];
     sqlSafef(query, sizeof query, 
-	"select organism.name from gbCdnaInfo,organism where gbCdnaInfo.acc = '%s' and gbCdnaInfo.organism = organism.id", acc);
+	"select o.name from %s g,%s o where g.acc = '%s' and g.organism = o.id", gbCdnaInfoTable, organismTable, acc);
     char *org = sqlQuickString(conn, query);
     if ((org != NULL) && (org[0] == '\0'))
         org = NULL;
     cacheEl = hashAdd(cache, acc, org);
     }
 return cacheEl->val;
 }
 
 char *getOrganismShort(struct sqlConnection *conn, char *acc)
 /* lookup the organism for an mrna, or NULL if not found.  This will
  * only return the genus, and only the first seven letters of that.
  * WARNING: static return */
 {
 return orgShortName(getOrganism(conn, acc));
 }
 
 char *getGeneName(struct sqlConnection *conn, char *acc)
 /* get geneName from refLink or NULL if not found.
  * WARNING: static return */
 {
 static char nameBuf[256];
 char query[256], *name = NULL;
-if (hTableExists(database,  "refLink"))
+if (sqlTableExists(conn,  refLinkTable))
     {
     /* remove the version number if any */
     static char accBuf[1024];
     safecpy(accBuf, sizeof accBuf, acc);
     chopSuffix(accBuf);
 
-    sqlSafef(query, sizeof query, "select name from refLink where mrnaAcc = '%s'", accBuf);
+    sqlSafef(query, sizeof query, "select name from %s where mrnaAcc = '%s'", refLinkTable, accBuf);
     name = sqlQuickQuery(conn, query, nameBuf, sizeof(nameBuf));
     if ((name != NULL) && (name[0] == '\0'))
         name = NULL;
     }
 return name;
 }
 
 char *getRgdGene2Symbol(struct sqlConnection *conn, char *acc)
 /* get gene symbol from rgdGene2ToSymbol or NULL if not found.
  * WARNING: static return */
 {
 static char symbolBuf[256];
 char query[256], *symbol = NULL;
 if (hTableExists(database,  "rgdGene2ToSymbol"))
     {
@@ -7163,32 +7164,32 @@
  */
 {
 int col = tg->ixColor;
 struct rgbColor *normal = &(tg->color);
 struct rgbColor lighter, lightest;
 struct sqlConnection *conn = hAllocConn(database);
 struct sqlResult *sr;
 char **row;
 char query[256];
 
 if (startsWith("ncbiRefSeq", tg->table))
     {
     sqlSafef(query, sizeof query, "select status from ncbiRefSeqLink where id = '%s'", name);
     }
 else
-    sqlSafef(query, sizeof query, "select status from refSeqStatus where mrnaAcc = '%s'",
-        name);
+    sqlSafef(query, sizeof query, "select status from %s where mrnaAcc = '%s'",
+        refSeqStatusTable, name);
 sr = sqlGetResult(conn, query);
 if ((row = sqlNextRow(sr)) != NULL)
     {
     if (startsWith("Reviewed", row[0]) || startsWith("Validated", row[0]))
         {
         /* Use the usual color */
         }
     else if (startsWith("Provisional", row[0]))
         {
         lighter.r = (6*normal->r + 4*255) / 10;
         lighter.g = (6*normal->g + 4*255) / 10;
         lighter.b = (6*normal->b + 4*255) / 10;
         col = hvGfxFindRgb(hvg, &lighter);
         }
     else
@@ -7206,34 +7207,36 @@
 
 Color refGeneColor(struct track *tg, void *item, struct hvGfx *hvg)
 /* Return color to draw refseq gene in. */
 {
 struct linkedFeatures *lf = item;
 
 /* allow itemAttr to override coloring */
 if (lf->itemAttr != NULL)
     return hvGfxFindColorIx(hvg, lf->itemAttr->colorR, lf->itemAttr->colorG, lf->itemAttr->colorB);
 
 /* If refSeqStatus is available, use it to determine the color.
  * Reviewed, Validated -> normal, Provisional -> lighter,
  * Predicted, Inferred(other) -> lightest
  * If no refSeqStatus, color it normally.
  */
-if (hTableExists(database,  "refSeqStatus") || hTableExists(database,  "ncbiRefSeqLink"))
-    return refGeneColorByStatus(tg, lf->name, hvg);
-else
-    return(tg->ixColor);
+struct sqlConnection *conn = hAllocConn(database);
+Color color = tg->ixColor;
+if (sqlTableExists(conn,  refSeqStatusTable) || hTableExists(database,  "ncbiRefSeqLink"))
+    color = refGeneColorByStatus(tg, lf->name, hvg);
+hFreeConn(&conn);
+return color;
 }
 
 void ncbiRefSeqMethods(struct track *tg)
 /* Make NCBI Genes track */
 {
 tg->loadItems = loadNcbiRefSeq;
 tg->itemName = refGeneName;
 tg->mapItemName = ncbiRefGeneMapName;
 tg->itemColor = refGeneColor;
 }
 
 void refGeneMethods(struct track *tg)
 /* Make track of known genes from refSeq. */
 {
 tg->loadItems = loadRefGene;
@@ -7331,31 +7334,31 @@
 void ensGeneNonCodingMethods(struct track *tg)
 /* Make track of Ensembl predictions. */
 {
 tg->items = lfFromGenePredInRange(tg, tg->table, chromName, winStart, winEnd);
 tg->itemColor = ensGeneNonCodingColor;
 tg->loadItems = loadEnsGeneNonCoding;
 }
 
 int cDnaReadDirectionForMrna(struct sqlConnection *conn, char *acc)
 /* Return the direction field from the mrna table for accession
    acc. Return -1 if not in table.*/
 {
 int direction = -1;
 char query[512];
 char buf[SMALLBUF], *s = NULL;
-sqlSafef(query, sizeof query, "select direction from gbCdnaInfo where acc='%s'", acc);
+sqlSafef(query, sizeof query, "select direction from %s where acc='%s'", gbCdnaInfoTable, acc);
 if ((s = sqlQuickQuery(conn, query, buf, sizeof(buf))) != NULL)
     {
     direction = atoi(s);
     }
 return direction;
 }
 
 void orientEsts(struct track *tg)
 /* Orient ESTs from the estOrientInfo table.  */
 {
 struct linkedFeatures *lf = NULL, *lfList = tg->items;
 struct sqlConnection *conn = hAllocConn(database);
 struct sqlResult *sr = NULL;
 char **row = NULL;
 int rowOffset = 0;