8c8823d9a87b6046094577a808dda4ed4172b416
braney
  Thu Apr 23 11:09:48 2026 -0700
Make quickLifted GENCODE tracks usable: click details, position links, coloring. refs #36059

- Re-vet wgEncodeGencode* in trackHub.c:isVetted() so the quickLift hub
no longer emits `avoidHandler on` for GENCODE children. Without this,
hgc bypasses findNameBasedHandler and doGencodeGene is never reached,
so the details page falls through to the generic showGenePos output.
- In gencodeClick.c doGencodeGene/helpers, use quickLiftDb for the
MySQL connection (the attrs/tag/pubMed/refSeq/uniProt tables live in
the source assembly), use trackHubSkipHubName(tdb->track) for the
genePred table name and the Basic/Comp/PseudoGene/PolyA prefix
dispatch checks, and teach isGrcHuman/isGrcH37Native to consult the
source db — otherwise the page errAborted with
"BUG: gencodeClick on wrong database: hub_NNNNN_hs1".
- Lift the Position values (transcript + gene bounds, 2-way pseudo,
PolyA) through the quickLift chain before printing, so the details
page shows destination-assembly coords and the Position link lands
at a window that actually contains the gene.
- htcDnaNearGene (hgc.c): when quickLifted, lift seqName/winStart/winEnd
back to source coords before hgSeqItemsInRange. The prior code
pointed the query at the source-assembly table but still passed the
destination window, so the "Genomic Sequence from assembly" flow
returned "No results returned from query." No longer reachable from
the GENCODE details page (doGencodeGene writes its own Sequences
table) but still hit by other quickLifted genePred tracks.
- simpleTracks.c genePredItemClassColor: the hTableExists() guard was
checking `database` (the destination hub db like hub_NNNNN_hs1) even
though the MySQL conn was already routed to liftDb; the lookup was
skipped and every item fell back to tg->ixColor, rendering all
black. Check `db` instead so the gClass_* palette (coding / nonCoding
/ pseudo / problem) is applied.

Verified end-to-end on hgwdev-braney: Gerardo's SHH test case
(ENST00000297261.7) and Basic/Comp/PseudoGene/PolyA subtracks; no
regression on the non-quickLifted hg38 click.

diff --git src/hg/hgc/gencodeClick.c src/hg/hgc/gencodeClick.c
index bb7cdb480ac..b1bb04279ab 100644
--- src/hg/hgc/gencodeClick.c
+++ src/hg/hgc/gencodeClick.c
@@ -1,21 +1,23 @@
 /* gencodeClick - click handling for GENCODE tracks */
 
 /* Copyright (C) 2014 The Regents of the University of California 
  * See kent/LICENSE or http://genome.ucsc.edu/license/ for licensing information. */
 #include "common.h"
 #include "hgc.h"
+#include "trackHub.h"
+#include "quickLift.h"
 #include "gencodeClick.h"
 #include "gencodeTracksCommon.h"
 #include "ccdsClick.h"
 #include "genePred.h"
 #include "genePredReader.h"
 #include "ensFace.h"
 #include "htmshell.h"
 #include "jksql.h"
 #include "regexHelper.h"
 #include "encode/wgEncodeGencodeAttrs.h"
 #include "encode/wgEncodeGencodeGeneSource.h"
 #include "encode/wgEncodeGencodePdb.h"
 #include "encode/wgEncodeGencodePubMed.h"
 #include "encode/wgEncodeGencodeRefSeq.h"
 #include "encode/wgEncodeGencodeTag.h"
@@ -55,60 +57,77 @@
 static char *gencodeTagsUrl = "http://www.gencodegenes.org/pages/tags.html";
 
 static char *yalePseudoUrl = "http://tables.pseudogene.org/%s";
 static char *apprisHomeUrl = "https://appris.bioinfo.cnio.es/#/";
 static char *apprisGeneUrl = "https://appris.bioinfo.cnio.es/#/database/id/%s/%s?sc=ensembl";
 
 // species-specific
 static char *hgncByIdUrl = "https://www.genenames.org/data/gene-symbol-report/#!/hgnc_id/%s";
 static char *hgncBySymUrl = " https://www.genenames.org/data/gene-symbol-report/#!/symbol/%s";
 static char *geneCardsUrl = "http://www.genecards.org/cgi-bin/carddisp.pl?gene=%s";
 static char *mgiBySymUrl = "http://www.informatics.jax.org/quicksearch/summary?queryType=exactPhrase&query=%s";
 static char *mgiByIdUrl = "http://www.informatics.jax.org/accession/%s";
 
 static char* UNKNOWN = "unknown";
 
-static boolean isGrcHuman()
-/* is this a GRC human assembly? */
+static char *gencodeSourceDb(struct trackDb *tdb)
+/* return the assembly the gencode tables actually live in: the quickLiftDb
+ * when the track is quickLifted, otherwise the current database. */
 {
-//bool result = FALSE;
-//if (startsWith("hg", database))
-//    result = TRUE;
-//else if (!startsWith("mm", database))
-//    warn("BUG: gencodeClick on wrong database: %s", database);
-//return result;
+char *liftDb = trackDbSetting(tdb, "quickLiftDb");
+return (liftDb != NULL) ? liftDb : database;
+}
 
-if (startsWith("hg", database))
+static void liftAnnoPos(struct trackDb *tdb, char *chrom, int start, int end,
+                        char **retChrom, int *retStart, int *retEnd)
+/* If the tdb is quickLifted, translate chrom:start-end from the source
+ * assembly to the destination assembly the user is viewing.  Otherwise
+ * return the input unchanged.  Falls back to input on lift failure. */
+{
+*retChrom = chrom;
+*retStart = start;
+*retEnd = end;
+char *liftDb = trackDbSetting(tdb, "quickLiftDb");
+if (liftDb != NULL)
+    quickLiftLiftPos(liftDb, trackHubSkipHubName(database),
+                     chrom, start, end, retChrom, retStart, retEnd);
+}
+
+static boolean isGrcHuman(struct trackDb *tdb)
+/* is this a GRC human assembly? */
+{
+char *db = gencodeSourceDb(tdb);
+if (startsWith("hg", db))
     return TRUE;
-else if (startsWith("mm", database))
+else if (startsWith("mm", db))
     return FALSE;
 else
-    errAbort("BUG: gencodeClick on wrong database: %s", database);
+    errAbort("BUG: gencodeClick on wrong database: %s", db);
 return FALSE;
 }
 
 static bool haveGencodeTable(struct sqlConnection *conn, struct trackDb *tdb, char *tableBase)
 /* determine if a gencode table exists; it might be option or not in older releases */
 {
 return sqlTableExists(conn, gencodeGetTableName(tdb, tableBase));
 }
 
 static boolean isGrcH37Native(struct trackDb *tdb)
 /* Is this GENCODE GRCh37 native build, which requires a different Ensembl site. */
 {
 // check for non-lifted GENCODE on GRCh37/hg19
-if (sameString(database, "hg19"))
+if (sameString(gencodeSourceDb(tdb), "hg19"))
     return stringIn("lift37", gencodeGetVersion(tdb)) == NULL;
 else
     return FALSE;
 }
 
 static boolean isRealGeneSymbol(char* geneName, boolean haveGeneSymbolSource,
                                 struct wgEncodeGencodeGeneSymbol *geneSymbolSource)
 /* Attempt to determine if this is a gene symbol assigned by HGNC/MGI or a
  * generate one.  newer versions of GENCODE have wgEncodeGencodeGeneSymbol,
  * which has the definitions.  With older version of GENCODE guess a guess
  * is make by trying to match older clone-based names ones.
  */
 {
 if (haveGeneSymbolSource)
     {
@@ -139,60 +158,60 @@
 }
 
 static bool isProteinCodingTrans(struct wgEncodeGencodeAttrs *transAttrs)
 /* is a transcript protein coding? */
 {
 return sameString(transAttrs->transcriptClass, "coding");
 }
 
 static struct genePred *transAnnoLoad(struct sqlConnection *conn, struct trackDb *tdb, char *gencodeId)
 /* load the gencode annotations and sort the one corresponding to the one that was clicked on is
  * first.  Should only have one or two. */
 {
 // must check chrom due to PAR
 char where[256];
 sqlSafef(where, sizeof(where), "(chrom = \"%s\") and (name = \"%s\")", seqName, gencodeId);
-struct genePred *transAnno = genePredReaderLoadQuery(conn, tdb->track, where);
+struct genePred *transAnno = genePredReaderLoadQuery(conn, trackHubSkipHubName(tdb->track), where);
 slSort(&transAnno, transAnnoCmp);
 return transAnno;
 }
 
 static struct wgEncodeGencodeAttrs *transAttrsLoad(struct trackDb *tdb, struct sqlConnection *conn, char *gencodeId)
 /* load the gencode attributes */
 {
 char query[1024];
 sqlSafef(query, sizeof(query), "select * from %s where transcriptId = \"%s\"",
          gencodeGetTableName(tdb, "wgEncodeGencodeAttrs"), gencodeId);
 struct sqlResult *sr = sqlGetResult(conn, query);
 char **row = sqlNextRow(sr);
 if (row == NULL)
     errAbort("gencode transcript %s not found in %s", gencodeId,
              gencodeGetTableName(tdb, "wgEncodeGencodeAttrs"));
 // older version don't have proteinId column.
 struct wgEncodeGencodeAttrs *transAttrs = wgEncodeGencodeAttrsLoad(row, sqlCountColumns(sr));
 sqlFreeResult(&sr);
 return transAttrs;
 }
 
 static void getGeneBounds(struct trackDb *tdb, struct sqlConnection *conn, struct genePred *transAnno,
                           int *geneChromStart, int *geneChromEnd)
 /* find bounds for the gene */
 {
 // must check chrom due to PAR
 char where[256];
 sqlSafef(where, sizeof(where), "(chrom = \"%s\") and (name2 = \"%s\")", seqName, transAnno->name2);
-struct genePred *geneAnnos = genePredReaderLoadQuery(conn, tdb->track, where);
+struct genePred *geneAnnos = genePredReaderLoadQuery(conn, trackHubSkipHubName(tdb->track), where);
 struct genePred *geneAnno;
 *geneChromStart = transAnno->txStart;
 *geneChromEnd = transAnno->txEnd;
 for (geneAnno = geneAnnos; geneAnno != NULL; geneAnno = geneAnno->next)
     {
     *geneChromStart = min(*geneChromStart, geneAnno->txStart);
     *geneChromEnd = max(*geneChromEnd, geneAnno->txEnd);
     }
 genePredFreeList(&geneAnnos);
 }
 
 static void *metaDataLoad(struct trackDb *tdb, struct sqlConnection *conn, char *gencodeId, char *tableBase, char *keyCol, unsigned queryOpts, sqlLoadFunc loadFunc)
 /* load autoSql objects for gencode meta data. */
 {
 return sqlQueryObjs(conn, loadFunc, queryOpts, "select * from %s where %s = \"%s\"",
@@ -470,54 +489,60 @@
     printf("<tr><th>Protein id");
     if (strlen(transAttrs->proteinId) > 0)
         prTdEnsIdAnchor(transAttrs->proteinId,
                         (isGrcH37Native(tdb) ? ensemblH37ProteinIdUrl: ensemblProteinIdUrl));
     else
         printf("<td>&nbsp;");
     printf("<td>");
     printf("</tr>\n");
     }
 
 printf("<tr><th>HAVANA manual id");
 printf("<td>%s", transAttrs->havanaTranscriptId);
 printf("<td>%s", transAttrs->havanaGeneId);
 printf("</tr>\n");
 
+char *transChrom, *geneChrom;
+int transStart, transEnd, gStart, gEnd;
+liftAnnoPos(tdb, transAnno->chrom, transAnno->txStart, transAnno->txEnd,
+            &transChrom, &transStart, &transEnd);
+liftAnnoPos(tdb, transAnno->chrom, geneChromStart, geneChromEnd,
+            &geneChrom, &gStart, &gEnd);
 printf("<tr><th>Position");
 printf("<td>");
-writePosLink(transAnno->chrom, transAnno->txStart, transAnno->txEnd);
+writePosLink(transChrom, transStart, transEnd);
 printf("<td>");
-writePosLink(transAnno->chrom, geneChromStart, geneChromEnd);
+writePosLink(geneChrom, gStart, gEnd);
 printf("</tr>\n");
 
 printf("<tr><th>Strand<td>%s<td></tr>\n", transAnno->strand);
 
 printf("<tr><th><a href=\"%s\" target = _blank>Biotype</a><td>%s<td>%s</tr>\n", gencodeBiotypesUrl, transAttrs->transcriptType, transAttrs->geneType);
 
 printf("<tr><th>Annotation Level<td>%s (%d)<td></tr>\n", getLevelDesc(transAttrs->level), transAttrs->level);
 
 char *transSrcDesc = (transcriptSource != NULL) ? getMethodDesc(transcriptSource->source) : UNKNOWN;
 char *geneSrcDesc = (geneSource != NULL) ? getMethodDesc(geneSource->source) : UNKNOWN;
 printf("<tr><th>Annotation Method<td>%s<td>%s</tr>\n", transSrcDesc, geneSrcDesc);
 
 if (haveTsl)
     {
     char *tslDesc = getSupportLevelDesc(tsl);
     printf("<tr><th><a href=\"#tsl\">Transcription Support Level</a><td><a href=\"#%s\">%s</a><td></tr>\n", tslDesc, tslDesc);
     }
 
-if (isGrcHuman())
+if (isGrcHuman(tdb))
     writeHumanGeneLinkout(transAttrs, haveGeneSymbolSource, geneSymbolSource);
 else
     writeMouseGeneLinkout(transAttrs, haveGeneSymbolSource, geneSymbolSource);
 
 
 printf("<tr><th>CCDS<td>");
 if (!isEmpty(transAttrs->ccdsId))
     {
     printf("<a href=\"");
     printCcdsExtUrl(transAttrs->ccdsId);
     printf("\" target=_blank>%s</a>", transAttrs->ccdsId);
     }
 printf("<td></tr>\n");
 if (transAttrs->transcriptRank > 0)
     {
@@ -977,57 +1002,68 @@
 wgEncodeGencodeUniProtFreeList(&uniProts);
 wgEncodeGencodeGeneSymbolFreeList(&geneSymbolSource);
 wgEncodeGencodeTranscriptionSupportLevelFreeList(&tsl);
 }
 
 static void doGencodeGene2WayPseudo(struct trackDb *tdb, char *gencodeId, struct sqlConnection *conn, struct genePred *pseudoAnno)
 /* Process click on a GENCODE two-way pseudogene annotation track. */
 {
 char header[256];
 safef(header, sizeof(header), "GENCODE 2-way consensus pseudogene %s", gencodeId);
 cartWebStart(cart, database, "%s", header);
 printf("<H2>%s</H2>\n", header);
 printf("<b>Yale id:</b> ");
 prExtIdAnchor(gencodeId, yalePseudoUrl);
 printf("<br>");
-printPos(pseudoAnno->chrom, pseudoAnno->txStart, pseudoAnno->txEnd, pseudoAnno->strand, FALSE, NULL);
+char *chrom;
+int start, end;
+liftAnnoPos(tdb, pseudoAnno->chrom, pseudoAnno->txStart, pseudoAnno->txEnd, &chrom, &start, &end);
+printPos(chrom, start, end, pseudoAnno->strand, FALSE, NULL);
 }
 
 static void doGencodeGenePolyA(struct trackDb *tdb, char *gencodeId, struct sqlConnection *conn, struct genePred *polyAAnno)
 /* Process click on a GENCODE poly-A annotation track. */
 {
 char header[256];
 safef(header, sizeof(header), "GENCODE PolyA Annotation %s (%s)", polyAAnno->name2, gencodeId);
 cartWebStart(cart, database, "%s", header);
 printf("<H2>%s</H2>\n", header);
 printf("<b>Annotation id:</b> %s<br>", gencodeId);
 printf("<b>Annotation Type:</b> %s<br>",polyAAnno->name2);
-printPos(polyAAnno->chrom, polyAAnno->txStart, polyAAnno->txEnd, polyAAnno->strand, FALSE, NULL);
+char *chrom;
+int start, end;
+liftAnnoPos(tdb, polyAAnno->chrom, polyAAnno->txStart, polyAAnno->txEnd, &chrom, &start, &end);
+printPos(chrom, start, end, polyAAnno->strand, FALSE, NULL);
 }
 
 void doGencodeGene(struct trackDb *tdb, char *gencodeId)
 /* Process click on a GENCODE annotation. */
 {
-struct sqlConnection *conn = hAllocConn(database);
+// When the track is quickLifted, all GENCODE metadata tables live in the
+// source assembly (quickLiftDb), not in the currently-displayed destination.
+char *liftDb = trackDbSetting(tdb, "quickLiftDb");
+char *metaDb = (liftDb != NULL) ? liftDb : database;
+struct sqlConnection *conn = hAllocConn(metaDb);
 struct genePred *anno = transAnnoLoad(conn, tdb, gencodeId);
-if (startsWith("wgEncodeGencodeBasic", tdb->track)
-    || startsWith("wgEncodeGencodeComp", tdb->track)
-    || startsWith("wgEncodeGencodePseudoGene", tdb->track))
+char *track = trackHubSkipHubName(tdb->track);
+if (startsWith("wgEncodeGencodeBasic", track)
+    || startsWith("wgEncodeGencodeComp", track)
+    || startsWith("wgEncodeGencodePseudoGene", track))
     doGencodeGeneTrack(tdb, gencodeId, conn, anno);
-else if (startsWith("wgEncodeGencode2wayConsPseudo", tdb->track))
+else if (startsWith("wgEncodeGencode2wayConsPseudo", track))
     doGencodeGene2WayPseudo(tdb, gencodeId, conn, anno);
-else if (startsWith("wgEncodeGencodePolya", tdb->track))
+else if (startsWith("wgEncodeGencodePolya", track))
     doGencodeGenePolyA(tdb, gencodeId, conn, anno);
 else
     errAbort("doGencodeGene: track not handled: \"%s\"", tdb->track);
 
 htmlHorizontalLine();
 printTrackHtml(tdb);
 
 genePredFreeList(&anno);
 hFreeConn(&conn);
 }
 
 
 bool isNewGencodeGene(struct trackDb *tdb)
 /* is this a new-style gencode (>= V7) track, as indicated by
  * the presence of the wgEncodeGencodeVersion setting */