@jbrowse/core
Version:
JBrowse 2 core libraries used by plugins
79 lines (78 loc) • 2.38 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.calcStdFromSums = calcStdFromSums;
exports.rectifyStats = rectifyStats;
exports.scoresToStats = scoresToStats;
exports.blankStats = blankStats;
const rxjs_1 = require("rxjs");
const operators_1 = require("rxjs/operators");
function calcStdFromSums(sum, sumSquares, n, population = false) {
if (n === 0) {
return 0;
}
let variance;
if (population) {
variance = sumSquares / n - (sum * sum) / (n * n);
}
else {
variance = sumSquares - (sum * sum) / n;
if (n > 1) {
variance /= n - 1;
}
}
return variance < 0 ? 0 : Math.sqrt(variance);
}
function rectifyStats(s) {
return {
...s,
scoreMean: (s.scoreSum || 0) / (s.featureCount || s.basesCovered || 1),
scoreStdDev: calcStdFromSums(s.scoreSum, s.scoreSumSquares, s.featureCount || s.basesCovered),
featureDensity: (s.featureCount || 1) / s.basesCovered,
};
}
async function scoresToStats(region, feats) {
const { start, end } = region;
const seed = {
scoreMin: Number.MAX_VALUE,
scoreMax: Number.MIN_VALUE,
scoreSum: 0,
scoreSumSquares: 0,
featureCount: 0,
};
let found = false;
const { scoreMin, scoreMax, scoreSum, scoreSumSquares, featureCount } = await (0, rxjs_1.firstValueFrom)(feats.pipe((0, operators_1.reduce)((acc, f) => {
const s = f.get('score');
const summary = f.get('summary');
const { scoreMax, scoreMin } = acc;
acc.scoreMax = Math.max(scoreMax, summary ? f.get('maxScore') : s);
acc.scoreMin = Math.min(scoreMin, summary ? f.get('minScore') : s);
acc.scoreSum += s;
acc.scoreSumSquares += s * s;
acc.featureCount += 1;
found = true;
return acc;
}, seed)));
return found
? rectifyStats({
scoreMax,
scoreMin,
scoreSum,
scoreSumSquares,
featureCount,
basesCovered: end - start + 1,
})
: blankStats();
}
function blankStats() {
return {
scoreMin: 0,
scoreMax: 0,
scoreMean: 0,
scoreStdDev: 0,
scoreSum: 0,
scoreSumSquares: 0,
featureCount: 0,
featureDensity: 0,
basesCovered: 0,
};
}
;