@rcsb/rcsb-saguaro-app
Version:
RCSB 1D Saguaro Web App
50 lines (49 loc) • 1.52 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Logo = void 0;
const Assertions_1 = require("../Helpers/Assertions");
var assertElementListDefined = Assertions_1.Assertions.assertElementListDefined;
var assertDefined = Assertions_1.Assertions.assertDefined;
class Logo {
constructor(logo) {
this.symbols = Object();
this.n = 0;
assertElementListDefined(logo);
logo.forEach(s => {
assertDefined(s.symbol);
assertDefined(s.value);
this.symbols[s.symbol] = s.value;
this.n += s.value;
});
}
aaTypes() {
return Object.keys(this.symbols);
}
/*add(aa: T): void{
this.symbols[aa] ++;
this.n ++;
}*/
get(aa) {
const n = this.symbols[aa];
assertDefined(n);
return n;
}
total() {
return this.n;
}
forEach(callback) {
this.aaTypes().forEach((aa, n) => {
callback(aa, n);
});
}
mode() {
return this.aaTypes().sort((a, b) => (this.symbols[b] - this.symbols[a]))[0];
}
frequency() {
return this.aaTypes().sort((a, b) => (this.symbols[b] - this.symbols[a])).map(a => ({ symbol: a, value: this.get(a) / this.n }));
}
entropy() {
return -this.aaTypes().map(aa => (this.get(aa) != 0 ? (this.get(aa) / this.n) * (Math.log(this.get(aa) / this.n) / Math.log(this.aaTypes().length)) : 0)).reduce((p, v) => (p + v));
}
}
exports.Logo = Logo;