src/utils/bigWigToWig/bigWigToWig.c 1.1

1.1 2009/11/13 00:26:28 kent
Adding new bigWigToWig utility. You won't get _exactly_ the same wig that you gave to wigToBigWig back. It'll omit printing the decimal point in the floating point values when possible, and also it will break up large sections into medium-sized ones. Still at least it keeps fixedStep and varStep rather than converting everything to bedGraph.
Index: src/utils/bigWigToWig/bigWigToWig.c
===================================================================
RCS file: src/utils/bigWigToWig/bigWigToWig.c
diff -N src/utils/bigWigToWig/bigWigToWig.c
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ src/utils/bigWigToWig/bigWigToWig.c	13 Nov 2009 00:26:28 -0000	1.1
@@ -0,0 +1,79 @@
+/* bigWigToWig - Convert bigWig to wig.  This will keep more of the same structure of the 
+ * original wig than bigWigToBedGraph does, but still will break up large stepped sections into 
+ * smaller ones. */
+#include "common.h"
+#include "linefile.h"
+#include "hash.h"
+#include "options.h"
+#include "udc.h"
+#include "bigWig.h"
+
+static char const rcsid[] = "$Id$";
+
+char *clChrom = NULL;
+int clStart = -1;
+int clEnd = -1;
+
+void usage()
+/* Explain usage and exit. */
+{
+errAbort(
+  "bigWigToWig - Convert bigWig to wig.  This will keep more of the same structure of the\n"
+  "original wig than bigWigToBedGraph does, but still will break up large stepped sections\n"
+  "into smaller ones.\n"
+  "usage:\n"
+  "   bigWigToWig in.bigWig out.wig\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"
+  "   -udcDir=/dir/to/cache - place to put cache for remote bigBed/bigWigs\n"
+  );
+}
+
+static struct optionSpec options[] = {
+   {"chrom", OPTION_STRING},
+   {"start", OPTION_INT},
+   {"end", OPTION_INT},
+   {"udcDir", OPTION_STRING},
+   {NULL, 0},
+};
+
+void bigWigToWig(char *inFile, char *outFile)
+/* bigWigToWig - Convert bigWig to wig.  This will keep more of the same structure of the 
+ * original wig than bigWigToBedGraph does, but still will break up large stepped sections into 
+ * smaller ones. */
+{
+struct bbiFile *bwf = bigWigFileOpen(inFile);
+FILE *f = mustOpen(outFile, "w");
+struct bbiChromInfo *chrom, *chromList = bbiChromList(bwf);
+for (chrom = chromList; chrom != NULL; chrom = chrom->next)
+    {
+    if (clChrom != NULL && !sameString(clChrom, chrom->name))
+        continue;
+    char *chromName = chrom->name;
+    int start = 0, end = chrom->size;
+    if (clStart > 0)
+        start = clStart;
+    if (clEnd > 0)
+        end = clEnd;
+    bigWigIntervalDump(bwf, chromName, start, end, 0, f);
+    }
+bbiChromInfoFreeList(&chromList);
+carefulClose(&f);
+bbiFileClose(&bwf);
+}
+
+int main(int argc, char *argv[])
+/* Process command line. */
+{
+optionInit(&argc, argv, options);
+if (argc != 3)
+    usage();
+clChrom = optionVal("chrom", clChrom);
+clStart = optionInt("start", clStart);
+clEnd = optionInt("end", clEnd);
+udcSetDefaultDir(optionVal("udcDir", udcDefaultDir()));
+bigWigToWig(argv[1], argv[2]);
+return 0;
+}