6aaa041c6e3cea383a6ca3275144f6998613cec6
braney
  Thu Nov 18 17:34:47 2021 -0800
be a lot smarter about how we parse bigMaf files.  We were opening the
file on every bed item, which for bigMaf is about one per base refs #28534

diff --git src/hg/hgTables/maf.c src/hg/hgTables/maf.c
index 9fd8181..5eab26c 100644
--- src/hg/hgTables/maf.c
+++ src/hg/hgTables/maf.c
@@ -68,75 +68,83 @@
 
 if (isCustomTrack(table))
     {
     ctConn = hAllocConn(CUSTOM_TRASH);
     ctConn2 = hAllocConn(CUSTOM_TRASH);
     ct = ctLookupName(table);
     if (mafFile == NULL)
 	{
 	/* this shouldn't happen */
 	printf("cannot find custom track file %s\n", mafFile);
 	return;
 	}
     }
 
 mafWriteStart(stdout, NULL);
+
+// if this is a bigMaf file, open the source.
+struct bbiFile *bigMafBbi = NULL;
+if (isBigBed(database, table, curTrack, ctLookupName))
+    {
+    struct trackDb *subTdb = hashFindVal(fullTableToTdbHash, table);
+    char *fileName = trackDbSetting(subTdb, "bigDataUrl");
+    bigMafBbi = bigBedFileOpen(fileName);
+    }
+
 for (region = regionList; region != NULL; region = region->next)
     {
     struct bed *bedList = cookedBedList(conn, table, region, lm, NULL);
     struct bed *bed = NULL;
     for (bed = bedList;  bed != NULL;  bed = bed->next)
 	{
 	struct mafAli *mafList = NULL, *maf = NULL;
 	char dbChrom[64];
 	safef(dbChrom, sizeof(dbChrom), "%s.%s", hubConnectSkipHubPrefix(database), bed->chrom);
 	/* For MAF, we clip to viewing window (region) instead of showing
 	 * entire items that happen to overlap the window/region, which is
 	 * what we get from cookedBedList. */
 	if (bed->chromStart < region->start)
 	    bed->chromStart = region->start;
 	if (bed->chromEnd > region->end)
 	    bed->chromEnd = region->end;
 	if (bed->chromStart >= bed->chromEnd)
 	    continue;
 	if (ct == NULL)
 	    {
-            if (isBigBed(database, table, curTrack, ctLookupName))
-                {
-                struct trackDb *subTdb = hashFindVal(fullTableToTdbHash, table);
-                char *fileName = trackDbSetting(subTdb, "bigDataUrl");
-                struct bbiFile *bbi = bigBedFileOpen(fileName);
-                mafList = bigMafLoadInRegion(bbi, bed->chrom, bed->chromStart, bed->chromEnd);
-                }
+            if (bigMafBbi)
+                mafList = bigMafLoadInRegion(bigMafBbi, bed->chrom, bed->chromStart, bed->chromEnd);
 	    else if (mafFile != NULL)
 		mafList = mafLoadInRegion2(conn, conn, table,
 			bed->chrom, bed->chromStart, bed->chromEnd, mafFile);
 	    else
 		mafList = mafLoadInRegion(conn, table, bed->chrom,
 					  bed->chromStart, bed->chromEnd);
 	    }
 	else
 	    mafList = mafLoadInRegion2(ctConn, ctConn2, ct->dbTableName,
 		    bed->chrom, bed->chromStart, bed->chromEnd, mafFile);
 	for (maf = mafList; maf != NULL; maf = maf->next)
 	    {
 	    struct mafAli *subset = mafSubset(maf, dbChrom,
 					      bed->chromStart, bed->chromEnd);
 	    if (subset != NULL)
 		{
 		subset->score = mafScoreMultiz(subset);
 		mafWrite(stdout, subset);
 		mafAliFree(&subset);
 		}
 	    }
 	mafAliFreeList(&mafList);
 	}
     }
 mafWriteEnd(stdout);
 lmCleanup(&lm);
 
+if (bigMafBbi)
+    bigBedFileClose(&bigMafBbi);
+
 if (isCustomTrack(table))
     {
     hFreeConn(&ctConn);
     hFreeConn(&ctConn2);
     }
 }