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,214 +1,274 @@
 /* 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]);
 safecpy(ret->strand, sizeof(ret->strand), row[5]);
 ret->tSize = sqlUnsigned(row[6]);
 ret->qName = row[7];
 ret->qSize = sqlUnsigned(row[8]);
 ret->qStart = sqlUnsigned(row[9]);
 ret->qEnd = sqlUnsigned(row[10]);
 ret->chainScore = sqlDouble(row[11]);
 }
 
 struct bigChain *bigChainLoad(char **row)
 /* Load a bigChain from row fetched with select * from bigChain
  * from database.  Dispose of this with bigChainFree(). */
 {
 struct bigChain *ret;
 
 AllocVar(ret);
 ret->chrom = cloneString(row[0]);
 ret->chromStart = sqlUnsigned(row[1]);
 ret->chromEnd = sqlUnsigned(row[2]);
 ret->name = cloneString(row[3]);
 ret->score = sqlUnsigned(row[4]);
 safecpy(ret->strand, sizeof(ret->strand), row[5]);
 ret->tSize = sqlUnsigned(row[6]);
 ret->qName = cloneString(row[7]);
 ret->qSize = sqlUnsigned(row[8]);
 ret->qStart = sqlUnsigned(row[9]);
 ret->qEnd = sqlUnsigned(row[10]);
 ret->chainScore = sqlDouble(row[11]);
 return ret;
 }
 
 struct bigChain *bigChainLoadAll(char *fileName) 
 /* Load all bigChain from a whitespace-separated file.
  * Dispose of this with bigChainFreeList(). */
 {
 struct bigChain *list = NULL, *el;
 struct lineFile *lf = lineFileOpen(fileName, TRUE);
 char *row[12];
 
 while (lineFileRow(lf, row))
     {
     el = bigChainLoad(row);
     slAddHead(&list, el);
     }
 lineFileClose(&lf);
 slReverse(&list);
 return list;
 }
 
 struct bigChain *bigChainLoadAllByChar(char *fileName, char chopper) 
 /* Load all bigChain from a chopper separated file.
  * Dispose of this with bigChainFreeList(). */
 {
 struct bigChain *list = NULL, *el;
 struct lineFile *lf = lineFileOpen(fileName, TRUE);
 char *row[12];
 
 while (lineFileNextCharRow(lf, chopper, row, ArraySize(row)))
     {
     el = bigChainLoad(row);
     slAddHead(&list, el);
     }
 lineFileClose(&lf);
 slReverse(&list);
 return list;
 }
 
 struct bigChain *bigChainCommaIn(char **pS, struct bigChain *ret)
 /* Create a bigChain out of a comma separated string. 
  * This will fill in ret if non-null, otherwise will
  * return a new bigChain */
 {
 char *s = *pS;
 
 if (ret == NULL)
     AllocVar(ret);
 ret->chrom = sqlStringComma(&s);
 ret->chromStart = sqlUnsignedComma(&s);
 ret->chromEnd = sqlUnsignedComma(&s);
 ret->name = sqlStringComma(&s);
 ret->score = sqlUnsignedComma(&s);
 sqlFixedStringComma(&s, ret->strand, sizeof(ret->strand));
 ret->tSize = sqlUnsignedComma(&s);
 ret->qName = sqlStringComma(&s);
 ret->qSize = sqlUnsignedComma(&s);
 ret->qStart = sqlUnsignedComma(&s);
 ret->qEnd = sqlUnsignedComma(&s);
 ret->chainScore = sqlDoubleComma(&s);
 *pS = s;
 return ret;
 }
 
 void bigChainFree(struct bigChain **pEl)
 /* Free a single dynamically allocated bigChain such as created
  * with bigChainLoad(). */
 {
 struct bigChain *el;
 
 if ((el = *pEl) == NULL) return;
 freeMem(el->chrom);
 freeMem(el->name);
 freeMem(el->qName);
 freez(pEl);
 }
 
 void bigChainFreeList(struct bigChain **pList)
 /* Free a list of dynamically allocated bigChain's */
 {
 struct bigChain *el, *next;
 
 for (el = *pList; el != NULL; el = next)
     {
     next = el->next;
     bigChainFree(&el);
     }
 *pList = NULL;
 }
 
 void bigChainOutput(struct bigChain *el, FILE *f, char sep, char lastSep) 
 /* Print out bigChain.  Separate fields with sep. Follow last field with lastSep. */
 {
 if (sep == ',') fputc('"',f);
 fprintf(f, "%s", el->chrom);
 if (sep == ',') fputc('"',f);
 fputc(sep,f);
 fprintf(f, "%u", el->chromStart);
 fputc(sep,f);
 fprintf(f, "%u", el->chromEnd);
 fputc(sep,f);
 if (sep == ',') fputc('"',f);
 fprintf(f, "%s", el->name);
 if (sep == ',') fputc('"',f);
 fputc(sep,f);
 fprintf(f, "%u", el->score);
 fputc(sep,f);
 if (sep == ',') fputc('"',f);
 fprintf(f, "%s", el->strand);
 if (sep == ',') fputc('"',f);
 fputc(sep,f);
 fprintf(f, "%u", el->tSize);
 fputc(sep,f);
 if (sep == ',') fputc('"',f);
 fprintf(f, "%s", el->qName);
 if (sep == ',') fputc('"',f);
 fputc(sep,f);
 fprintf(f, "%u", el->qSize);
 fputc(sep,f);
 fprintf(f, "%u", el->qStart);
 fputc(sep,f);
 fprintf(f, "%u", el->qEnd);
 fputc(sep,f);
 fprintf(f, "%g", el->chainScore);
 fputc(lastSep,f);
 }
 
 /* -------------------------------- End autoSql Generated Code -------------------------------- */
 
 
 int bigChainCmpTarget(const void *va, const void *vb)
 /* Compare to sort based on target position. */
 {
 const struct bigChain *a = *((struct bigChain **)va);
 const struct bigChain *b = *((struct bigChain **)vb);
 int dif = strcmp(a->chrom, b->chrom);
 if (dif == 0)
     dif = a->chromStart - b->chromStart;
 return dif;
 }
 
 char *bigChainGetLinkFile(char *chainBigBedName)
 /* Construct the file name of the chain link file from the name of a chain file. 
  * That is, change file.bb to file.link.bb */
 {
 char linkBuffer[4096];
 
 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);
+}