56e6b83f725ff84cb71c0ce66aca02a5e1f519c8 kent Sun Mar 20 13:23:34 2011 -0700 Adding utility to calculate average bigWig value for a bed file. diff --git src/lib/basicBed.c src/lib/basicBed.c index f8f24fa..9b0e16e 100644 --- src/lib/basicBed.c +++ src/lib/basicBed.c @@ -425,42 +425,71 @@ struct bed *bedLoadNAll(char *fileName, int numFields) /* Load all bed from a tab-separated file. * Dispose of this with bedFreeList(). */ { return bedLoadNAllChrom(fileName, numFields, NULL); } struct bed *bedLoadAll(char *fileName) /* Determines how many fields are in a bedFile and load all beds from * a tab-separated file. Dispose of this with bedFreeList(). */ { struct bed *list = NULL; struct lineFile *lf = lineFileOpen(fileName, TRUE); char *line, *row[bedKnownFields]; -while (lineFileNext(lf, &line, NULL)) +while (lineFileNextReal(lf, &line)) { int numFields = chopByWhite(line, row, ArraySize(row)); if (numFields < 4) errAbort("file %s doesn't appear to be in bed format. At least 4 fields required, got %d", fileName, numFields); slAddHead(&list, bedLoadN(row, numFields)); } lineFileClose(&lf); slReverse(&list); return list; } +void bedLoadAllReturnFieldCount(char *fileName, struct bed **retList, int *retFieldCount) +/* Load bed of unknown size and return number of fields as well as list of bed items. + * Ensures that all lines in bed file have same field count. */ +{ +struct bed *list = NULL; +struct lineFile *lf = lineFileOpen(fileName, TRUE); +char *line, *row[bedKnownFields]; +int fieldCount = 0; + +while (lineFileNextReal(lf, &line)) + { + int numFields = chopByWhite(line, row, ArraySize(row)); + if (numFields < 4) + errAbort("file %s doesn't appear to be in bed format. At least 4 fields required, got %d", + fileName, numFields); + if (fieldCount == 0) + fieldCount = numFields; + else + if (fieldCount != numFields) + errAbort("Inconsistent number of fields in file. %d on line %d of %s, %d previously.", + numFields, lf->lineIx, lf->fileName, fieldCount); + slAddHead(&list, bedLoadN(row, fieldCount)); + } +lineFileClose(&lf); +slReverse(&list); +*retList = list; +*retFieldCount = fieldCount; +} + static void bedOutputN_Opt(struct bed *el, int wordCount, FILE *f, char sep, char lastSep, boolean useItemRgb) /* Write a bed of wordCount fields, optionally interpreting field nine as R,G,B values. */ { int i; if (sep == ',') fputc('"',f); fprintf(f, "%s", el->chrom); if (sep == ',') fputc('"',f); fputc(sep,f); fprintf(f, "%u", el->chromStart); fputc(sep,f); fprintf(f, "%u", el->chromEnd); if (wordCount <= 3) {