72327e4e2d294b9d2fcb6cbdadd4e628c006345b
braney
  Fri Sep 18 10:07:40 2026 -0700
bigChain: move the chain to bigChain conversion into the library

chainRecToBigChain() and chainBlockToBigLink() sat in chainToBigChain.c
next to its main(), so a CGI could not link against them.  Move both into
hg/lib/bigChain.c, add chainToBigChainOne() for one chain and
chainToBigChainList() for a list, and leave the utility as a thin main().
No behavior change; the chainToBigChain test output is unchanged.

refs #38382

diff --git src/hg/lib/bigChain.c src/hg/lib/bigChain.c
index 6665c6a4e91..8a2fb0cd1e0 100644
--- src/hg/lib/bigChain.c
+++ src/hg/lib/bigChain.c
@@ -1,24 +1,26 @@
 /* bigChain.c was originally generated by the autoSql program, which also 
  * generated bigChain.h and bigChain.sql.  This module links the database and
  * the RAM representation of objects. */
 
 #include "common.h"
 #include "linefile.h"
 #include "dystring.h"
 #include "jksql.h"
+#include "chain.h"
 #include "bigChain.h"
+#include "bigLink.h"
 
 
 
 char *bigChainCommaSepFieldNames = "chrom,chromStart,chromEnd,name,score,strand,tSize,qName,qSize,qStart,qEnd,chainScore";
 
 void bigChainStaticLoad(char **row, struct bigChain *ret)
 /* Load a row from bigChain table into ret.  The contents of ret will
  * be replaced at the next call to this function. */
 {
 
 ret->chrom = row[0];
 ret->chromStart = sqlUnsigned(row[1]);
 ret->chromEnd = sqlUnsigned(row[2]);
 ret->name = row[3];
 ret->score = sqlUnsigned(row[4]);
@@ -200,15 +202,73 @@
 if (!endsWith(chainBigBedName, ".bb"))
     errAbort("chain bigBed file (%s) must end in .bb", chainBigBedName);
 
 safef(linkBuffer, sizeof linkBuffer, "%s", chainBigBedName);
 
 // truncate string at ending ".bb"
 int insertOffset = strlen(linkBuffer) - sizeof ".bb" + 1;
 char *insert = &linkBuffer[insertOffset];
 *insert = 0;
 
 // add .link.bb
 strcpy(insert, ".link.bb");
 
 return cloneString(linkBuffer);
 }
+
+struct bigChain *chainRecToBigChain(struct chain *chain)
+/* make a bigChain from a chain */
+{
+struct bigChain *bc;
+AllocVar(bc);
+bc->chrom = cloneString(chain->tName);
+bc->chromStart = chain->tStart;
+bc->chromEnd = chain->tEnd;
+char buf[128];
+safef(buf, sizeof(buf), "%d", chain->id);
+bc->name = cloneString(buf);
+bc->score = 1000;
+bc->strand[0] = chain->qStrand;
+bc->tSize = chain->tSize;
+bc->qName = cloneString(chain->qName);
+bc->qSize = chain->qSize;
+bc->qStart = chain->qStart;
+bc->qEnd = chain->qEnd;
+bc->chainScore = chain->score;
+return bc;
+}
+
+struct bigLink *chainBlockToBigLink(struct chain *chain, struct cBlock *cblk)
+/* make a chain link from a chain block */
+{
+struct bigLink *bl;
+AllocVar(bl);
+bl->chrom = cloneString(chain->tName);
+bl->chromStart = cblk->tStart;
+bl->chromEnd = cblk->tEnd;
+char buf[128];
+safef(buf, sizeof(buf), "%d", chain->id);
+bl->name = cloneString(buf);
+bl->qStart = cblk->qStart;
+return bl;
+}
+
+void chainToBigChainOne(struct chain *chain, struct bigChain **bigChains, struct bigLink **bigLinks)
+/* convert one chain to a bigChain and a bigLink per block, adding them to the
+ * heads of the given lists */
+{
+for (struct cBlock *cblk = chain->blockList; cblk != NULL; cblk = cblk->next)
+    {
+    slAddHead(bigLinks, chainBlockToBigLink(chain, cblk));
+    }
+slAddHead(bigChains, chainRecToBigChain(chain));
+}
+
+void chainToBigChainList(struct chain *chains, struct bigChain **bigChains, struct bigLink **bigLinks)
+/* convert a list of chains to bigChains and bigLinks, sorted by target position.
+ * The new records are added to whatever is already on the given lists */
+{
+for (struct chain *chain = chains; chain != NULL; chain = chain->next)
+    chainToBigChainOne(chain, bigChains, bigLinks);
+slSort(bigChains, bigChainCmpTarget);
+slSort(bigLinks, bigLinkCmpTarget);
+}