353a34ac7e7638457db3f55e57452069113d860b braney Sun Sep 6 13:34:28 2026 -0700 Add a bigNet track type, a net of alignments in a bigBed, refs #20824 Track hubs have had no way to show a real net. The usual stand-in is a net rendered as a maf, which loses the level structure that makes a net useful for establishing orthologous sequence. bigNet holds the netAlign columns in a bigBed, so a hub can carry the net itself. The format is bed6+20: the target in chrom/chromStart/chromEnd, the query sequence in name, the query strand in strand, then level and the rest of the netAlign fields. The trackDb line is type bigNet <targetDb> <chainTrack> mirroring type netAlign. chainTrack is the plain trackDb name of the bigChain track in the same hub; hgc adds the hub prefix itself. chainNetLoadRangeHub() builds a chainNet from a bigBed range query and hands it to the same helpToNet() the SQL path uses, so the nesting is rebuilt the same way. netDraw picks its loader off tg->isBigBed and the drawing code below that is untouched. genericNetClick does the same for the details page and follows the named chain track for the alignment. Also bounds the level walk in helpToNet() by help->maxDepth. It could read one past the end of the levels array. netToBigNet converts a net file to bedToBigBed input. It writes the tab line itself rather than calling bigNetTabOut, because autoSql prints a double with %g and that drops digits off a chain score. diff --git src/hg/lib/chainNetDbLoad.c src/hg/lib/chainNetDbLoad.c index 5dd8f80f02b..c349b6a89d9 100644 --- src/hg/lib/chainNetDbLoad.c +++ src/hg/lib/chainNetDbLoad.c @@ -3,30 +3,32 @@ * representation of chain into chain. */ /* 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 "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; @@ -64,31 +66,31 @@ }; struct chainNet *helpToNet(struct cnlHelper **pHelp) /* Make a chainNet from cnlHelper. This will destroy * *pHelp in the process. */ { struct chainNet *net; struct cnlHelper *help = *pHelp; struct cnFill *parentList, *childList, *parent, *child, *nextChild; struct cnFill **levels = help->levels; int depth, maxDepth = 0; /* Note that level 0 is always empty. */ /* Sort everybody by target start. */ -for (depth=1; ; ++depth) +for (depth=1; depth < help->maxDepth; ++depth) { if (levels[depth] == NULL && depth != 0) break; maxDepth = depth; slSort(&levels[depth], cnFillCmpTarget); } /* Assign children to parents. */ for (depth=maxDepth; depth >= 2; --depth) { childList = levels[depth]; parentList = levels[depth-1]; child = childList; for (parent = parentList; parent != NULL; parent = parent->next) { @@ -170,30 +172,107 @@ int rowOffset; struct sqlConnection *conn; struct sqlResult *sr; struct chainNet *net; conn = sqlConnect(database); sr = hRangeQuery(conn, track, chrom, start, end, extraWhere, &rowOffset); net = chainNetLoadResult(sr, rowOffset); sqlFreeResult(&sr); if (net != NULL) net->size = hChromSize(database, chrom); sqlDisconnect(&conn); return net; } +static struct cnFill *cnFillFromBigNet(struct bigNet *bn, struct hash *nameHash) +/* Convert a bigNet row to cnFill. Name hash is a place to store + * the strings. */ +{ +struct cnFill *fill; +AllocVar(fill); +fill->tStart = bn->chromStart; +fill->tSize = bn->chromEnd - bn->chromStart; +fill->qName = hashStoreName(nameHash, bn->name); +fill->qStrand = bn->strand[0]; +fill->qStart = bn->qStart; +fill->qSize = bn->qEnd - bn->qStart; +fill->chainId = bn->chainId; +fill->score = bn->chainScore; +fill->ali = bn->ali; +fill->qOver = bn->qOver; +fill->qFar = bn->qFar; +fill->qDup = bn->qDup; +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; +} + +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); + +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)); + } + +net = helpToNet(&help); +bbiFileClose(&bbi); +lmCleanup(&lm); +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);