e811848a75708cf437ed474e23a5997a29007aa7 hiram Tue Feb 19 18:34:33 2019 -0800 bigBed and bigWig hub data output examples refs #18869 diff --git src/hg/hubApi/getData.c src/hg/hubApi/getData.c index 7a7b0b8..65bd421 100644 --- src/hg/hubApi/getData.c +++ src/hg/hubApi/getData.c @@ -58,51 +58,99 @@ struct sqlFieldType *ft, *list = NULL; struct asColumn *col; for (col = as->columnList; col != NULL; col = col->next) { struct dyString *type = asColumnToSqlType(col); ft = sqlFieldTypeNew(col->name, type->string); slAddHead(&list, ft); dyStringFree(&type); } slReverse(&list); return list; } static void bedDataOutput(struct jsonWrite *jw, struct bbiFile *bbi, char *chrom, unsigned start, unsigned end, unsigned maxItems) -/* output the data for one chrom in the given bbi file */ +/* output bed data for one chrom in the given bbi file */ { struct lm *bbLm = lmInit(0); struct bigBedInterval *iv, *ivList = NULL; ivList = bigBedIntervalQuery(bbi,chrom, start, end, 0, bbLm); char *row[bbi->fieldCount]; for (iv = ivList; iv; iv = iv->next) { char startBuf[16], endBuf[16]; bigBedIntervalToRow(iv, chrom, startBuf, endBuf, row, bbi->fieldCount); jsonWriteListStart(jw, NULL); int i; for (i = 0; i < bbi->fieldCount; ++i) { jsonWriteString(jw, NULL, row[i]); } jsonWriteListEnd(jw); } lmCleanup(&bbLm); } +static void wigDataOutput(struct jsonWrite *jw, struct bbiFile *bwf, + char *chrom, unsigned start, unsigned end, unsigned maxItems) +/* output wig data for one chrom in the given bwf file */ +{ +struct lm *lm = lmInit(0); +struct bbiInterval *iv, *ivList = bigWigIntervalQuery(bwf, chrom, start, end, lm); +if (NULL == ivList) + return; + +unsigned itemCount = 0; + +jsonWriteObjectStart(jw, NULL); +jsonWriteListStart(jw, chrom); +for (iv = ivList; iv && itemCount < maxItems; iv = iv->next) + { + jsonWriteListStart(jw, NULL); + int s = max(iv->start, start); + int e = min(iv->end, end); + double val = iv->val; + jsonWriteNumber(jw, NULL, (long long)s); + jsonWriteNumber(jw, NULL, (long long)e); + jsonWriteDouble(jw, NULL, val); + jsonWriteListEnd(jw); + ++itemCount; + } +jsonWriteListEnd(jw); +jsonWriteObjectEnd(jw); +} + +static void wigData(struct jsonWrite *jw, struct bbiFile *bwf, char *chrom, + unsigned start, unsigned end, unsigned maxItems) +/* output the data for a bigWig bbi file */ +{ +struct bbiChromInfo *chromList = NULL; +// struct bbiSummaryElement sum = bbiTotalSummary(bwf); +if (isEmpty(chrom)) + { + chromList = bbiChromList(bwf); + struct bbiChromInfo *bci; + for (bci = chromList; bci; bci = bci->next) + { + wigDataOutput(jw, bwf, bci->name, 0, bci->size, maxItems); + } + } + else + wigDataOutput(jw, bwf, chrom, start, end, maxItems); +} + static void getHubTrackData(char *hubUrl) /* return data from a hub track, optionally just one chrom data, * optionally just one section of that chrom data */ { char *genome = cgiOptionalString("genome"); char *track = cgiOptionalString("track"); char *chrom = cgiOptionalString("chrom"); char *start = cgiOptionalString("start"); char *end = cgiOptionalString("end"); if (isEmpty(genome)) apiErrAbort("missing genome=<name> for endpoint '/getdata/track' given hubUrl='%s'", hubUrl); if (isEmpty(track)) apiErrAbort("missing track=<name> for endpoint '/getdata/track' given hubUrl='%s'", hubUrl); @@ -186,30 +234,34 @@ jsonWriteListStart(jw, "trackData"); if (isEmpty(chrom)) { struct bbiChromInfo *bci; for (bci = chromList; bci; bci = bci->next) { bedDataOutput(jw, bbi, bci->name, 0, bci->size, maxItems); } } else bedDataOutput(jw, bbi, chrom, uStart, uEnd, maxItems); jsonWriteListEnd(jw); } else if (startsWith("bigWig", thisTrack->type)) { + unsigned maxItems = 1000; /* TBD will use this later for paging */ + jsonWriteListStart(jw, "trackData"); + wigData(jw, bbi, chrom, uStart, uEnd, maxItems); + jsonWriteListEnd(jw); } bbiFileClose(&bbi); jsonWriteObjectEnd(jw); fputs(jw->dy->string,stdout); } static void getTrackData() /* return data from a track, optionally just one chrom data, * optionally just one section of that chrom data */ { char *db = cgiOptionalString("db"); char *chrom = cgiOptionalString("chrom"); char *start = cgiOptionalString("start"); char *end = cgiOptionalString("end");