void writeBedTab(char *fileName, struct bedStub *bedList, int bedSize) /* Write out bed list to tab-separated file. */ { struct bedStub *bed; FILE *f = mustOpen(fileName, "w"); char *words[64]; int i, wordCount; for (bed = bedList; bed != NULL; bed = bed->next) { if (!noBin) if (fprintf(f, "%u\t", hFindBin(bed->chromStart, bed->chromEnd)) <= 0) writeFailed(fileName); if (strictTab) wordCount = chopTabs(bed->line, words); else wordCount = chopLine(bed->line, words); for (i=0; i<wordCount; ++i) { /* new definition for old "reserved" field, now itemRgb */ /* and when itemRgb, it is a comma separated string r,g,b */ if (itemRgb && (i == 8)) { char *comma; /* Allow comma separated list of rgb values here */ comma = strchr(words[8], ','); if (comma) { int itemRgb = 0; if (-1 == (itemRgb = bedParseRgb(words[8]))) errAbort("ERROR: expecting r,g,b specification, " "found: '%s'", words[8]); else if (fprintf(f, "%d", itemRgb) <= 0) writeFailed(fileName); verbose(2, "itemRgb: %s, rgb: %#x\n", words[8], itemRgb); } else if (fputs(words[i], f) == EOF) writeFailed(fileName); } else if (fputs(words[i], f) == EOF) writeFailed(fileName); if (i == wordCount-1) { if (fputc('\n', f) == EOF) writeFailed(fileName); } else if (fputc('\t', f) == EOF) writeFailed(fileName); } } fclose(f); }
void bedMergeOverlappingBlocks(char *inBed, char *outBed) /* bedMergeOverlappingBlocks - Fix faulty BED 12 files with illegal overlapping blocks. Also reports a summary of the changes.. */ { int badBeds = 0; FILE *log = NULL; FILE *newBedFile = mustOpen(outBed, "w"); char *logName = optionVal("report", NULL); struct lineFile *lf = lineFileOpen(inBed, TRUE); char *line, *row[12]; boolean isItemRgb = FALSE; if (logName) log = mustOpen(logName, "w"); while (lineFileNext(lf, &line, NULL)) { struct bed *bed; int numFields = chopByWhite(line, row, ArraySize(row)); /* strange it's reading empty lines... whatever */ if (numFields == 0) continue; if (numFields < 12) errAbort("file %s doesn't appear to be in blocked-bed format. At least 12 fields required, got %d", inBed, numFields); if (bedParseRgb(row[8])) isItemRgb = TRUE; bed = bedLoadN(row, numFields); badBeds += fixBed(bed, lf->lineIx, log); if (isItemRgb) bedTabOutNitemRgb(bed, numFields, newBedFile); else bedTabOutN(bed, numFields, newBedFile); } lineFileClose(&lf); if (log) { fprintf(log, "Fixed %d bad beds in all.\n", badBeds); carefulClose(&log); } carefulClose(&newBedFile); }
struct encodePeak *encodePeakLineFileLoad(char **row, enum encodePeakType pt, struct lineFile *lf) /* From a linefile line, load an encodePeak row. Errors outputted */ /* have line numbers, etc. Does more error checking as well. */ { struct encodePeak *peak; if (!pt) errAbort("Unknown peak type set for track"); AllocVar(peak); peak->chrom = cloneString(row[0]); peak->chromStart = lineFileNeedNum(lf, row, 1); peak->chromEnd = lineFileNeedNum(lf, row, 2); peak->peak = -1; if (peak->chromEnd < 1) lineFileAbort(lf, "chromEnd less than 1 (%d)", peak->chromEnd); if (peak->chromEnd < peak->chromStart) lineFileAbort(lf, "chromStart after chromEnd (%d > %d)", peak->chromStart, peak->chromEnd); peak->name = cloneString(row[3]); peak->score = lineFileNeedNum(lf, row, 4); safecpy(peak->strand, sizeof(peak->strand), row[5]); if (peak->strand[0] != '+' && peak->strand[0] != '-' && peak->strand[0] != '.') lineFileAbort(lf, "Expecting +, -, or . in strand"); if (pt != gappedPeak) /* deal with signalValue, pValue, qValue, and peak */ { peak->signalValue = (float)lineFileNeedDouble(lf, row, 6); peak->pValue = (float)lineFileNeedDouble(lf, row, 7); peak->qValue = (float)lineFileNeedDouble(lf, row, 8); if ((pt == narrowPeak) || (pt == encodePeak)) { peak->peak = lineFileNeedNum(lf, row, 9); if (peak->peak >= (int)peak->chromEnd) lineFileAbort(lf, "peak site past chromEnd (%d > %d)", peak->peak, peak->chromEnd); } } else /* must be gappedPeak */ /* deal with thickStart, thickEnd, itemRgb even though they're not used */ { int thickStart = lineFileNeedNum(lf, row, 6); int thickEnd = lineFileNeedNum(lf, row, 7); int itemRgb = 0; char *comma; /* Allow comma separated list of rgb values here */ comma = strchr(row[8], ','); if (comma) itemRgb = bedParseRgb(row[8]); else itemRgb = lineFileNeedNum(lf, row, 8); if ((thickStart != 0) || (thickEnd != 0) || (itemRgb != 0)) lineFileAbort(lf, "thickStart, thickEnd, and itemRgb columns not used in gappedPeak type, set all to 0"); } /* Deal with blocks */ if ((pt == gappedPeak) || (pt == encodePeak)) { int i, count; int lastEnd, lastStart; int blockCountIx, blockSizesIx, blockStartsIx; if (pt == gappedPeak) { blockCountIx = 9; blockSizesIx = 10; blockStartsIx = 11; } else { blockCountIx = 10; blockSizesIx = 11; blockStartsIx = 12; } peak->blockCount = lineFileNeedNum(lf, row, blockCountIx); sqlUnsignedDynamicArray(row[blockSizesIx], &peak->blockSizes, &count); if (count != peak->blockCount) lineFileAbort(lf, "expecting %d elements in array", peak->blockCount); sqlUnsignedDynamicArray(row[blockStartsIx], &peak->blockStarts, &count); if (count != peak->blockCount) lineFileAbort(lf, "expecting %d elements in array", peak->blockCount); // tell the user if they appear to be using absolute starts rather than // relative... easy to forget! Also check block order, coord ranges... lastStart = -1; lastEnd = 0; for (i=0; i < peak->blockCount; i++) { if (peak->blockStarts[i]+peak->chromStart >= peak->chromEnd) { if (peak->blockStarts[i] >= peak->chromStart) lineFileAbort(lf, "BED blockStarts offsets must be relative to chromStart, " "not absolute. Try subtracting chromStart from each offset " "in blockStarts."); else lineFileAbort(lf, "BED blockStarts[i]+chromStart must be less than chromEnd."); } lastStart = peak->blockStarts[i]; lastEnd = peak->chromStart + peak->blockStarts[i] + peak->blockSizes[i]; } if (peak->blockStarts[0] != 0) lineFileAbort(lf, "BED blocks must span chromStart to chromEnd. " "BED blockStarts[0] must be 0 (==%d) so that (chromStart + " "blockStarts[0]) equals chromStart.", peak->blockStarts[0]); i = peak->blockCount-1; if ((peak->chromStart + peak->blockStarts[i] + peak->blockSizes[i]) != peak->chromEnd) { lineFileAbort(lf, "BED blocks must span chromStart to chromEnd. (chromStart + " "blockStarts[last] + blockSizes[last]) must equal chromEnd."); } } if (pt == gappedPeak) /* deal with final three columns of a gappedPeak */ { peak->signalValue = (float)lineFileNeedDouble(lf, row, 12); peak->pValue = (float)lineFileNeedDouble(lf, row, 13); peak->qValue = (float)lineFileNeedDouble(lf, row, 14); } return peak; }
void writeBedTab(char *fileName, struct bedStub *bedList) /* Write out bed list to tab-separated file. */ { struct bedStub *bed; FILE *f = mustOpen(fileName, "w"); char *words[64]; int i, wordCount; for (bed = bedList; bed != NULL; bed = bed->next) { if (!noBin) { // allow for zero-length at start of seq [bin code can't handle 0-0] unsigned end = (bed->chromEnd > 0) ? bed->chromEnd : 1; if (fprintf(f, "%u\t", hFindBin(bed->chromStart, end)) <= 0) writeFailed(fileName); } if (strictTab) wordCount = chopTabs(bed->line, words); else wordCount = chopLine(bed->line, words); for (i=0; i<wordCount; ++i) { /* new definition for old "reserved" field, now itemRgb */ /* and when itemRgb, it is a comma separated string r,g,b */ if (itemRgb && (i == 8)) { char *comma; /* Allow comma separated list of rgb values here */ comma = strchr(words[8], ','); if (comma) { int itemRgb = 0; if (-1 == (itemRgb = bedParseRgb(words[8]))) errAbort("ERROR: expecting r,g,b specification, " "found: '%s'", words[8]); else if (fprintf(f, "%d", itemRgb) <= 0) writeFailed(fileName); verbose(2, "itemRgb: %s, rgb: %#x\n", words[8], itemRgb); } else if (fputs(words[i], f) == EOF) writeFailed(fileName); } else if ((dotIsNull > 0) && (dotIsNull == i) && sameString(words[i],".")) /* If the . was used to represent NULL, replace with -1 in the tables */ { if (fputs("-1", f) == EOF) writeFailed(fileName); } else if (fputs(words[i], f) == EOF) writeFailed(fileName); if (i == wordCount-1) { if (fputc('\n', f) == EOF) writeFailed(fileName); } else if (fputc('\t', f) == EOF) writeFailed(fileName); } } fclose(f); }