src/hg/hgTables/bigBed.c 1.8

1.8 2009/03/19 00:42:38 kent
Making a lot more of the bigBed stuff work by implementing bigBedGetFilteredBedsOnRegions.
Index: src/hg/hgTables/bigBed.c
===================================================================
RCS file: /projects/compbio/cvsroot/kent/src/hg/hgTables/bigBed.c,v
retrieving revision 1.7
retrieving revision 1.8
diff -b -B -U 4 -r1.7 -r1.8
--- src/hg/hgTables/bigBed.c	18 Mar 2009 01:34:52 -0000	1.7
+++ src/hg/hgTables/bigBed.c	19 Mar 2009 00:42:38 -0000	1.8
@@ -452,8 +452,59 @@
 slReverse(&asFilter->columnList);
 return asFilter;
 }
 
+static void addFilteredBedsOnRegion(struct bbiFile *bbi, struct region *region, 
+	char *table, struct asFilter *filter, struct lm *bedLm, struct bed **pBedList)
+/* Add relevant beds in reverse order to pBedList */
+{
+struct lm *bbLm = lmInit(0);
+struct bigBedInterval *ivList = NULL, *iv;
+ivList = bigBedIntervalQuery(bbi, region->chrom, region->start, region->end, 0, bbLm);
+char *row[bbi->fieldCount];
+char startBuf[16], endBuf[16];
+for (iv = ivList; iv != NULL; iv = iv->next)
+    {
+    bigBedIntervalToRow(iv, region->chrom, startBuf, endBuf, row, bbi->fieldCount);
+    if (asFilterOnRow(filter, row))
+        {
+	struct bed *bed = bedLoadN(row, bbi->definedFieldCount);
+	struct bed *lmBed = lmCloneBed(bed, bedLm);
+	slAddHead(pBedList, lmBed);
+	bedFree(&bed);
+	}
+    }
+
+lmCleanup(&bbLm);
+}
+
+struct bed *bigBedGetFilteredBedsOnRegions(struct sqlConnection *conn, 
+	char *db, char *table, struct region *regionList, struct lm *lm, 
+	int *retFieldCount)
+/* Get list of beds from bigBed, in all regions, that pass filtering. */
+{
+/* Connect to big bed and get metadata and filter. */
+char *fileName = bigBedFileName(table, conn);
+struct bbiFile *bbi = bigBedFileOpen(fileName);
+struct asObject *as = bigBedAsOrDefault(bbi);
+struct asFilter *filter = asFilterFromCart(cart, db, table, as);
+
+/* Get beds a region at a time. */
+struct bed *bedList = NULL;
+struct region *region;
+for (region = regionList; region != NULL; region = region->next)
+    addFilteredBedsOnRegion(bbi, region, table, filter, lm, &bedList);
+slReverse(&bedList);
+
+/* Clean up and return. */
+if (retFieldCount != NULL) 
+	*retFieldCount = bbi->definedFieldCount;
+bbiFileClose(&bbi);
+freeMem(fileName);
+return bedList;
+}
+
+
 void bigBedTabOut(char *db, char *table, struct sqlConnection *conn, char *fields, FILE *f)
 /* Print out selected fields from Big Bed.  If fields is NULL, then print out all fields. */
 {
 if (f == NULL)