5847cd8496ad3bc5783f9f6eb03f97c7ff3699a1
braney
  Tue Jul 24 15:30:33 2018 -0700
fix problems with filtering on beds with less than 9 fields, and work
around brain damaged cart variables that different based on the storage
class of a bigBed field  ... argh!

diff --git src/hg/hgTracks/bedTrack.c src/hg/hgTracks/bedTrack.c
index 8f3c67f..e98e835 100644
--- src/hg/hgTracks/bedTrack.c
+++ src/hg/hgTracks/bedTrack.c
@@ -2,30 +2,31 @@
 
 /* Copyright (C) 2013 The Regents of the University of California 
  * See README in this or parent directory for licensing information. */
 
 #include "common.h"
 #include "jksql.h"
 #include "bed.h"
 #include "hdb.h"
 #include "bedCart.h"
 #include "bbiFile.h"
 #include "bigBed.h"
 #include "hgTracks.h"
 #include "cds.h"
 #include "bedTabix.h"
 #include "obscure.h"
+#include "bigBedFilter.h"
 
 #define SEQ_DELIM '~'
 
 struct bed *bedLoadPairedTagAlign(char **row)
 /* Load first six fields of bed.
  * Add ~seq1~seq2 to end of name
  * Then remove the sequence to extra field when we convert to linkedFeature.
  * Assumes seq1 and seq2 are in row[6] and row[7], as they would be with a
  * pairedTagAlign type (hg/lib/encode/pairedTagAlign.as). It would be good to be
  * able to check these columns exist but we dont have the sqlResult here. */
 {
 char buf[1024];
 struct bed *ret = bedLoad6(row);
 safef(buf, sizeof(buf), "%s%c%s%c%s", ret->name, SEQ_DELIM, row[6], SEQ_DELIM, row[7]);
 freez(&(ret->name));
@@ -128,52 +129,52 @@
             topTable = cloneString(tg->table);
         }
     }
 
 /* Get list of items */
 if (startsWith("bedTabix", tg->tdb->type ) || startsWith("longTabix", tg->tdb->type ))
     {
     struct hash *settings = tg->tdb->settingsHash;
     char *bigDataUrl = hashFindVal(settings, "bigDataUrl");
     struct bedTabixFile *btf = bedTabixFileMayOpen(bigDataUrl, NULL, 0, 0);
     list = bedTabixReadBeds(btf, chromName, winStart, winEnd, loader);
     bedTabixFileClose(&btf);
     }
 else if (tg->isBigBed)
     { // avoid opening an unneeded db connection for bigBed; required not to use mysql for parallel fetch tracks
-    char *scoreFilter = cartOrTdbString(cart, tg->tdb, "scoreFilter", NULL);
     struct lm *lm = lmInit(0);
     struct bigBedInterval *bb, *bbList = bigBedSelectRange(tg, chromName, winStart, winEnd, lm);
     char *bedRow[32];
     char startBuf[16], endBuf[16];
-    int minScore = 0;
-    if (scoreFilter)
-	minScore = atoi(scoreFilter);
+    struct bbiFile *bbi = fetchBbiForTrack(tg);
+    struct bigBedFilter *filters = bigBedBuildFilters(cart, bbi, tg->tdb);
 
      if (tg->itemName == bedName && !trackDbSettingClosestToHomeOn(tg->tdb, "linkIdInName"))
         tg->itemName = bigBedItemName;
 
     calculateLabelFields(tg);
     for (bb = bbList; bb != NULL; bb = bb->next)
         {
         bigBedIntervalToRow(bb, chromName, startBuf, endBuf, bedRow, ArraySize(bedRow));
+        if (!bigBedFilterInterval(bedRow, filters))
+            continue;
         bed = loader(bedRow);
         // FIXME BRANEY: either disable for all tracks with NUM_FIELDS > label field or better,
         // fix how label is stored so it doesn't trash custom bed field
         if (differentString(tg->tdb->type, "bigInteract"))
             bed->label = makeLabel(tg, bb);
-        if (scoreFilter == NULL || bed->score >= minScore)
+
         slAddHead(&list, bed);
         }
     lmCleanup(&lm);
     }
 else
     {
     char *table = tg->table;
     struct customTrack *ct = tg->customPt;
     struct sqlConnection *conn = NULL;
     if (ct == NULL)
         conn = hAllocConnTrack(database, tg->tdb);
     else
         {
         conn = hAllocConn(CUSTOM_TRASH);
         table = ct->dbTableName;