src/utils/bigBedToBed/bigBedToBed.c 1.5

1.5 2009/03/16 05:07:54 kent
Adding maxItems parameter to bigBedIntervalQuery. Checking that asFile field count matches tab-file field count.
Index: src/utils/bigBedToBed/bigBedToBed.c
===================================================================
RCS file: /projects/compbio/cvsroot/kent/src/utils/bigBedToBed/bigBedToBed.c,v
retrieving revision 1.4
retrieving revision 1.5
diff -b -B -U 1000000 -r1.4 -r1.5
--- src/utils/bigBedToBed/bigBedToBed.c	5 Feb 2009 20:44:37 -0000	1.4
+++ src/utils/bigBedToBed/bigBedToBed.c	16 Mar 2009 05:07:54 -0000	1.5
@@ -1,75 +1,89 @@
 /* bigBedToBed - Convert from bigBed to ascii bed format.. */
 #include "common.h"
 #include "linefile.h"
 #include "hash.h"
 #include "options.h"
 #include "localmem.h"
 #include "bigBed.h"
 
 static char const rcsid[] = "$Id$";
 
 char *clChrom = NULL;
 int clStart = -1;
 int clEnd = -1;
+int maxItems = 0;
 
 void usage()
 /* Explain usage and exit. */
 {
 errAbort(
   "bigBedToBed - Convert from bigBed to ascii bed format.\n"
   "usage:\n"
   "   bigBedToBed input.bb output.bed\n"
   "options:\n"
   "   -chrom=chr1 - if set restrict output to given chromosome\n"
   "   -start=N - if set, restrict output to only that over start\n"
   "   -end=N - if set, restict output to only that under end\n"
+  "   -maxItems=N - if set, restrict output to first N items\n"
   );
 }
 
 static struct optionSpec options[] = {
    {"chrom", OPTION_STRING},
    {"start", OPTION_INT},
    {"end", OPTION_INT},
+   {"maxItems", OPTION_INT},
    {NULL, 0},
 };
 
 void bigBedToBed(char *inFile, char *outFile)
 /* bigBedToBed - Convert from bigBed to ascii bed format.. */
 {
 struct bbiFile *bbi = bigBedFileOpen(inFile);
 FILE *f = mustOpen(outFile, "w");
 struct bbiChromInfo *chrom, *chromList = bbiChromList(bbi);
+int itemCount = 0;
 for (chrom = chromList; chrom != NULL; chrom = chrom->next)
     {
     if (clChrom != NULL && !sameString(clChrom, chrom->name))
         continue;
     char *chromName = chrom->name;
-    struct lm *lm = lmInit(0);
     int start = 0, end = chrom->size;
     if (clStart > 0)
         start = clStart;
     if (clEnd > 0)
         end = clEnd;
+    int itemsLeft = 0;	// Zero actually means no limit.... 
+    if (maxItems != 0)
+        {
+	itemsLeft = maxItems - itemCount;
+	if (itemsLeft <= 0)
+	    break;
+	}
+    struct lm *lm = lmInit(0);
     struct bigBedInterval *interval, *intervalList = bigBedIntervalQuery(bbi, chromName, 
-    	start, end, lm);
+    	start, end, itemsLeft, lm);
     for (interval = intervalList; interval != NULL; interval = interval->next)
+	{
 	fprintf(f, "%s\t%u\t%u\t%s\n", chromName, interval->start, interval->end, interval->rest);
+	}
     lmCleanup(&lm);
     }
 bbiChromInfoFreeList(&chromList);
 carefulClose(&f);
 bbiFileClose(&bbi);
 }
 
 int main(int argc, char *argv[])
 /* Process command line. */
 {
 optionInit(&argc, argv, options);
 clChrom = optionVal("chrom", clChrom);
 clStart = optionInt("start", clStart);
 clEnd = optionInt("end", clEnd);
+maxItems = optionInt("maxItems", maxItems);
 if (argc != 3)
     usage();
 bigBedToBed(argv[1], argv[2]);
 return 0;
 }