src/lib/bigBed.c 1.11

1.11 2009/03/15 00:17:52 kent
Adding asFile to bigBed.
Index: src/lib/bigBed.c
===================================================================
RCS file: /projects/compbio/cvsroot/kent/src/lib/bigBed.c,v
retrieving revision 1.10
retrieving revision 1.11
diff -b -B -U 4 -r1.10 -r1.11
--- src/lib/bigBed.c	10 Feb 2009 22:06:09 -0000	1.10
+++ src/lib/bigBed.c	15 Mar 2009 00:17:52 -0000	1.11
@@ -9,8 +9,9 @@
 #include "dystring.h"
 #include "rangeTree.h"
 #include "cirTree.h"
 #include "bPlusTree.h"
+#include "asParse.h"
 #include "sig.h"
 #include "udc.h"
 #include "bbiFile.h"
 #include "bigBed.h"
@@ -208,8 +209,9 @@
 	int blockSize,	  /* Number of items to bundle in r-tree.  1024 is good. */
 	int itemsPerSlot, /* Number of items in lowest level of tree.  64 is good. */
 	bits16 definedFieldCount,  /* Number of defined bed fields - 3-16 or so.  0 means all fields
 				    * are the defined bed ones. */
+	char *asFileName, /* If non-null points to a .as file that describes fields. */
 	char *outName)    /* BigBed output file name. */
 /* Convert tab-separated bed file to binary indexed, zoomed bigBed version. */
 {
 /* This code needs to agree with code in two other places currently - bigWigFileCreate,
@@ -225,8 +227,9 @@
 bits64 reserved64 = 0;
 bits64 dataOffset = 0, dataOffsetPos;
 bits64 indexOffset = 0, indexOffsetPos;
 bits64 chromTreeOffset = 0, chromTreeOffsetPos;
+bits64 asOffset = 0, asOffsetPos;
 bits32 sig = bigBedSig;
 struct bbiSummary *reduceSummaries[10];
 bits32 reductionAmounts[10];
 bits64 reductionDataOffsetPos[10];
@@ -317,9 +320,11 @@
 indexOffsetPos = ftell(f);
 writeOne(f, indexOffset);
 writeOne(f, fieldCount);
 writeOne(f, definedFieldCount);
-for (i=0; i<7; ++i)
+asOffsetPos = ftell(f);
+writeOne(f, asOffset);
+for (i=0; i<5; ++i)
     writeOne(f, reserved32);
 
 /* Write summary headers */
 for (i=0; i<summaryCount; ++i)
@@ -330,8 +335,21 @@
     writeOne(f, reserved64);	// Fill in with data offset later
     writeOne(f, reserved64);	// Fill in with index offset later
     }
 
+/* Optionally write out as file. */
+if (asFileName != NULL)
+    {
+    /* Parse it and do sanity check. */
+    struct asObject *as = asParseFile(asFileName);
+    if (as->next != NULL)
+        errAbort("Can only handle .as files containing a single object.");
+    asOffset = ftell(f);
+    FILE *asFile = mustOpen(asFileName, "r");
+    copyOpenFile(asFile, f);
+    fputc(0, f);
+    }
+
 /* Write chromosome bPlusTree */
 chromTreeOffset = ftell(f);
 int chromBlockSize = min(blockSize, chromCount);
 bptFileBulkIndexToOpenFile(chromInfoArray, sizeof(chromInfoArray[0]), chromCount, chromBlockSize,
@@ -382,8 +400,14 @@
 fseek(f, indexOffsetPos, SEEK_SET);
 writeOne(f, indexOffset);
 fseek(f, chromTreeOffsetPos, SEEK_SET);
 writeOne(f, chromTreeOffset);
+if (asOffset != 0)
+    {
+    uglyf("asOffsetPos = %lld, asOffset=%lld\n", asOffsetPos, asOffset);
+    fseek(f, asOffsetPos, SEEK_SET);
+    writeOne(f, asOffset);
+    }
 
 /* Also fill in offsets in zoom headers. */
 for (i=0; i<summaryCount; ++i)
     {
@@ -501,32 +525,40 @@
 rangeTreeFree(&rangeTree);
 return bwiList;
 }
 
-boolean bigBedSummaryArrayExtended(char *fileName, char *chrom, bits32 start, bits32 end,
+boolean bigBedSummaryArrayExtended(struct bbiFile *bbi, char *chrom, bits32 start, bits32 end,
 	int summarySize, struct bbiSummaryElement *summary)
 /* Get extended summary information for summarySize evenely spaced elements into
  * the summary array. */
 {
-struct bbiFile *bbi = bigBedFileOpen(fileName);
-boolean ret = bbiSummaryArrayExtended(bbi, chrom, start, end, bigBedCoverageIntervals,
+return bbiSummaryArrayExtended(bbi, chrom, start, end, bigBedCoverageIntervals,
 	summarySize, summary);
-bbiFileClose(&bbi);
-return ret;
 }
 
-boolean bigBedSummaryArray(char *fileName, char *chrom, bits32 start, bits32 end,
+boolean bigBedSummaryArray(struct bbiFile *bbi, char *chrom, bits32 start, bits32 end,
 	enum bbiSummaryType summaryType, int summarySize, double *summaryValues)
 /* Fill in summaryValues with  data from indicated chromosome range in bigBed file.
  * Be sure to initialize summaryValues to a default value, which will not be touched
  * for regions without data in file.  (Generally you want the default value to either
  * be 0.0 or nan("") depending on the application.)  Returns FALSE if no data
  * at that position. */
 {
-struct bbiFile *bbi = bigBedFileOpen(fileName);
-boolean ret = bbiSummaryArray(bbi, chrom, start, end, bigBedCoverageIntervals,
+return bbiSummaryArray(bbi, chrom, start, end, bigBedCoverageIntervals,
 	summaryType, summarySize, summaryValues);
-bbiFileClose(&bbi);
-return ret;
 }
 
 
+struct asObject *bigBedAs(struct bbiFile *bbi)
+/* Get autoSql object definition if any associated with file. */
+{
+if (bbi->asOffset == 0)
+    return NULL;
+struct udcFile *f = bbi->udc;
+bits64 curPos = udcTell(f);
+udcSeek(f, bbi->asOffset);
+char *asText = udcReadStringAndZero(f);
+udcSeek(f, curPos);
+struct asObject *as = asParseText(asText);
+freeMem(asText);
+return as;
+}