f29b65452a1cca8a5bc62f007100b313e589d036
braney
  Sun Sep 6 15:11:36 2026 -0700
quickLift: lift a bigNet track, refs #20824

A net is not an ordinary track to lift.  It is an alignment of two assemblies,
so only its target side moves to the new reference; the query side names a third
assembly and is carried across untouched.  And the browser draws a net by
recursion, so what really has to survive the lift is the tree: a row's level only
means anything relative to the row above it.

chainNetLoadRangeQuickLift() maps each row's target range with
quickLiftIntervalsToBedClip, which is the same code every other quickLift track
uses, so a net row lands where a bed of the same span would.  The surviving rows
then go to the same helpToNet() the unlifted path uses, so there is one tree
builder and not two.  The row collection either path does is now cnlHelperNew and
cnlHelperAddBigNet.

validateOneTdb lets bigNet into a quickLift hub.  Its chain track is not offered,
so a lifted net's details page has no chain to follow and says so rather than
looking for a track this assembly does not have.  bigNetLoadOne lifts the same
way for that page, unclipped, so it reports the item's whole extent.

bigNetFromInterval passes -1 as the cached chromId.  bbiCachedChromLookup leaves
the buffer alone when the id matches the one before it, so a cache that outlives
the buffer hands back stale bytes.

Measured against the standalone liftOver tool, on hg19 chr22's mouse net lifted
to hg38: 36 of 36 source rows in the sampled window land on the same coordinates,
counting the one liftOver will not take whole, whose two ends it does place
exactly where the browser puts them.  Against hg38's own mouse net, computed
independently, 1.02% of the drawn pixels differ.

diff --git src/hg/lib/chainNetDbLoad.c src/hg/lib/chainNetDbLoad.c
index c349b6a89d9..f5d5dba18b4 100644
--- src/hg/lib/chainNetDbLoad.c
+++ src/hg/lib/chainNetDbLoad.c
@@ -5,30 +5,32 @@
 /* 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 "hash.h"
 #include "linefile.h"
 #include "jksql.h"
 #include "hdb.h"
 #include "chain.h"
 #include "chainDb.h"
 #include "chainLink.h"
 #include "chainNet.h"
 #include "netAlign.h"
 #include "bigNet.h"
 #include "bigBed.h"
+#include "bed.h"
+#include "quickLift.h"
 #include "chainNetDbLoad.h"
 #include "chromAlias.h"
 
 
 struct cnFill *cnFillFromNetAlign(struct netAlign *na, struct hash *nameHash)
 /* Convert netAlign to cnFill. Name hash is a place to store
  * the strings. */
 {
 struct cnFill *fill;
 AllocVar(fill);
 fill->tStart = na->tStart;
 fill->tSize = na->tEnd - na->tStart;
 fill->qName = hashStoreName(nameHash, na->qName);
 fill->qStrand = na->strand[0];
 fill->qStart = na->qStart;
@@ -205,74 +207,152 @@
 if (!sameString(bn->type, "gap"))
     fill->type = hashStoreName(nameHash, bn->type);
 fill->tN = bn->tN;
 fill->qN = bn->qN;
 fill->tR = bn->tR;
 fill->qR = bn->qR;
 fill->tNewR = bn->tNewR;
 fill->qNewR = bn->qNewR;
 fill->tOldR = bn->tOldR;
 fill->qOldR = bn->qOldR;
 fill->tTrf = bn->tTrf;
 fill->qTrf = bn->qTrf;
 return fill;
 }
 
+static struct cnlHelper *cnlHelperNew(char *tName)
+/* A place to collect net rows by level, before helpToNet rebuilds the tree from
+ * them.  tName is not allocated here; helpToNet clones it. */
+{
+struct cnlHelper *help;
+AllocVar(help);
+help->tName = tName;
+help->nameHash = hashNew(8);
+help->maxDepth = 40;
+AllocArray(help->levels, help->maxDepth);
+return help;
+}
+
+static void cnlHelperAddBigNet(struct cnlHelper *help, char *fileName, struct bigNet *bn,
+                               int tStart, int tEnd)
+/* Add one bigNet row to the helper at the target range given.  That is the row's own
+ * range for a plain bigNet, and where the row landed for a quickLifted one. */
+{
+struct cnFill *fill;
+
+if (bn->level < 1 || bn->level >= help->maxDepth)
+    errAbort("%s has level %d, net levels run from 1 to %d",
+            fileName, bn->level, help->maxDepth-1);
+fill = cnFillFromBigNet(bn, help->nameHash);
+fill->tStart = tStart;
+fill->tSize = tEnd - tStart;
+slAddHead(&help->levels[bn->level], fill);
+}
+
 struct chainNet *chainNetLoadRangeHub(char *fileName, char *chrom, int start, int end)
 /* Load the parts of a bigNet file that intersect range into a chainNet.
  * Note the net->size field is not filled in. */
 {
 struct lm *lm = lmInit(0);
 struct bbiFile *bbi = bigBedFileOpenAlias(fileName, chromAliasFindAliases);
 struct bigBedInterval *bb, *bbList = bigBedIntervalQuery(bbi, chrom, start, end, 0, lm);
 char *bedRow[BIGNET_NUM_COLS];
 char startBuf[16], endBuf[16];
 struct cnlHelper *help = NULL;
 struct chainNet *net;
 
 if (bbList == NULL)
     {
     bbiFileClose(&bbi);
     lmCleanup(&lm);
     return NULL;
     }
 
-AllocVar(help);
-help->tName = chrom;	/* helpToNet clones this. */
-help->nameHash = hashNew(8);
-help->maxDepth = 40;
-AllocArray(help->levels, help->maxDepth);
-
+help = cnlHelperNew(chrom);
 for (bb = bbList; bb != NULL; bb = bb->next)
     {
     struct bigNet bn;
     int fieldCount = bigBedIntervalToRow(bb, chrom, startBuf, endBuf, bedRow, ArraySize(bedRow));
     if (fieldCount != BIGNET_NUM_COLS)
         errAbort("%s has %d fields, bigNet needs %d", fileName, fieldCount, BIGNET_NUM_COLS);
     bigNetStaticLoad(bedRow, &bn);
-    if (bn.level < 1 || bn.level >= help->maxDepth)
-        errAbort("%s has level %d, net levels run from 1 to %d",
-                fileName, bn.level, help->maxDepth-1);
-    slAddHead(&help->levels[bn.level], cnFillFromBigNet(&bn, help->nameHash));
+    cnlHelperAddBigNet(help, fileName, &bn, bn.chromStart, bn.chromEnd);
     }
 
 net = helpToNet(&help);
 bbiFileClose(&bbi);
 lmCleanup(&lm);
 return net;
 }
 
+struct bigNet *bigNetFromInterval(struct bbiFile *bbi, struct bigBedInterval *bb,
+                                  char *fileName, struct bigNet *bn)
+/* Fill in bn from one interval of a bigNet file.  The chrom name is the one the file
+ * carries, which for a quickLifted net is in the source assembly. */
+{
+char *bedRow[BIGNET_NUM_COLS];
+char startBuf[16], endBuf[16];
+char chromName[256];
+int fieldCount;
+
+/* -1 rather than a remembered chromId: bbiCachedChromLookup leaves the buffer alone
+ * when the id matches, so a cache that outlives the buffer returns stale bytes. */
+bbiCachedChromLookup(bbi, bb->chromId, -1, chromName, sizeof(chromName));
+fieldCount = bigBedIntervalToRow(bb, chromName, startBuf, endBuf, bedRow, ArraySize(bedRow));
+if (fieldCount != BIGNET_NUM_COLS)
+    errAbort("%s has %d fields, bigNet needs %d", fileName, fieldCount, BIGNET_NUM_COLS);
+bigNetStaticLoad(bedRow, bn);
+return bn;
+}
+
+struct chainNet *chainNetLoadRangeQuickLift(char *quickLiftFile, char *fileName,
+                                            char *chrom, int start, int end)
+/* Load the part of a bigNet file that quickLifts into chrom:start-end, and build a
+ * chainNet in the destination assembly's coordinates.  Only the target side of the net
+ * moves; the query side describes a third assembly and is carried across untouched.
+ * Note the net->size field is not filled in. */
+{
+struct bbiFile *bbi = bigBedFileOpenAlias(fileName, chromAliasFindAliases);
+struct hash *chainHash = NULL;
+struct bigBedInterval *bb, *bbList = quickLiftGetIntervals(quickLiftFile, bbi, chrom,
+                                                           start, end, &chainHash);
+struct cnlHelper *help = NULL;
+struct chainNet *net;
+
+for (bb = bbList; bb != NULL; bb = bb->next)
+    {
+    /* Lift through the same code every other quickLift track uses, so a net row lands
+     * where a bed of the same span would.  Only the target range comes from the lifted
+     * bed; the rest of the row is read from the interval it came from. */
+    struct bed *bed = quickLiftIntervalsToBedClip(bbi, chainHash, bb);
+    struct bigNet bn;
+
+    if ((bed == NULL) || !sameString(bed->chrom, chrom))
+        continue;
+    bigNetFromInterval(bbi, bb, fileName, &bn);
+    if (help == NULL)
+        help = cnlHelperNew(chrom);
+    cnlHelperAddBigNet(help, fileName, &bn, bed->chromStart, bed->chromEnd);
+    }
+
+bbiFileClose(&bbi);
+if (help == NULL)
+    return NULL;
+net = helpToNet(&help);
+return net;
+}
+
 struct chainNet *chainNetLoadChrom(char *database, char *track,
 	char *chrom, char *extraWhere)
 /* Load net on whole chromosome. */
 {
 int rowOffset;
 struct sqlConnection *conn;
 struct sqlResult *sr;
 struct chainNet *net;
 
 conn = sqlConnect(database);
 sr = hChromQuery(conn, track, chrom, extraWhere, &rowOffset);
 net = chainNetLoadResult(sr, rowOffset);
 sqlFreeResult(&sr);
 net->size = hChromSize(database, chrom);
 sqlDisconnect(&conn);