UNPKG

@jbrowse/core

Version:

JBrowse 2 core libraries used by plugins

69 lines (68 loc) 2.56 kB
import { firstValueFrom, merge } from 'rxjs'; import { toArray } from 'rxjs/operators'; import { BaseAdapter } from "./BaseAdapter.js"; import { aggregateQuantitativeStats, calculateFeatureDensityStats, } from "./stats.js"; import { ObservableCreate } from "../../util/rxjs.js"; import { blankStats, scoresToStats } from "../../util/stats.js"; export class BaseFeatureDataAdapter extends BaseAdapter { async getHeader(_opts) { return null; } async getMetadata(_opts) { return null; } getFeaturesInRegion(region, opts = {}) { return ObservableCreate(async (observer) => { const hasData = await this.hasDataForRefName(region.refName, opts); if (!hasData) { observer.complete(); } else { this.getFeatures(region, opts).subscribe(observer); } }); } getFeaturesInMultipleRegions(regions, opts = {}) { return merge(...regions.map(region => this.getFeaturesInRegion(region, opts))); } async hasDataForRefName(refName, opts = {}) { const refNames = await this.getRefNames(opts); return refNames.includes(refName); } async getRegionQuantitativeStats(region, opts) { const feats = this.getFeatures(region, { ...opts, statsEstimationMode: true, }); return scoresToStats(region, feats); } async getMultiRegionQuantitativeStats(regions = [], opts) { if (!regions.length) { return blankStats(); } const stats = await Promise.all(regions.map(region => this.getRegionQuantitativeStats(region, opts))); return aggregateQuantitativeStats(stats); } getRegionFeatureDensityStats(region, opts) { return calculateFeatureDensityStats(region, (region2, opts2) => this.getFeatures(region2, opts2), opts); } async getMultiRegionFeatureDensityStats(regions, opts) { if (!regions.length) { throw new Error('No regions supplied'); } return this.getRegionFeatureDensityStats(regions[0], opts); } async getSources(regions) { const features = await firstValueFrom(this.getFeaturesInMultipleRegions(regions).pipe(toArray())); const sources = new Set(); for (const f of features) { sources.add(f.get('source')); } return [...sources].map(source => ({ name: source, })); } async getExportData(_regions, _formatType, _opts) { return undefined; } }