UNPKG

@rcsb/rcsb-saguaro-app

Version:
188 lines (187 loc) 9.33 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RcsbAnnotationConfig = void 0; const tslib_1 = require("tslib"); const acm = tslib_1.__importStar(require("./RcsbAnnotationConfig.ac.json")); const string_to_color_1 = tslib_1.__importDefault(require("string-to-color")); const Assertions_1 = require("../RcsbUtils/Helpers/Assertions"); var assertDefined = Assertions_1.Assertions.assertDefined; const uniqid_1 = tslib_1.__importDefault(require("uniqid")); const annotationConfigMap = acm; class RcsbAnnotationConfig { constructor(acm) { this.annotationMap = new Map(); this.annotationTypes = { instance: new Map(), entity: new Map(), external: new Map() }; this.mergedTypes = new Map(); this.annotationConfigMap = acm !== null && acm !== void 0 ? acm : annotationConfigMap; this.annotationConfigMap.config.forEach(m => { //TODO move provenance list from configuration to TrackManager m.provenanceList = new Set(); this.annotationMap.set(m.type, m); }); this.initTypes(); } getConfig(type) { return this.annotationMap.get(type); } allTypes() { const concat = this.uniprotOrder().concat(this.instanceOrder()).concat(this.entityOrder()); return new Set(concat); } uniprotOrder() { return Array.from(this.annotationTypes.external.entries()).map(e => e[1].sort((a, b) => a.localeCompare(b))).flat(); } instanceOrder() { return Array.from(this.annotationTypes.instance.entries()).map(e => e[1].sort((a, b) => a.localeCompare(b))).flat(); } entityOrder() { return Array.from(this.annotationTypes.entity.entries()).map(e => e[1].sort((a, b) => a.localeCompare(b))).flat(); } //TODO refactor how title and type are defined. Can this be split in two different methods ? getAnnotationType(requestConfig, ann, feature) { return tslib_1.__awaiter(this, void 0, void 0, function* () { var _a, _b, _c; const trackTitle = typeof requestConfig.trackTitle === "function" ? (yield requestConfig.trackTitle(ann, feature)) : undefined; const titleSuffix = typeof requestConfig.titleSuffix === "function" ? (yield requestConfig.titleSuffix(ann, feature)) : undefined; const typeSuffix = typeof requestConfig.typeSuffix === "function" ? (yield requestConfig.typeSuffix(ann, feature)) : undefined; const type = feature.type; assertDefined(type); const a = feature; const addToTypeSuffix = new ArraySuffix(); const ac = this.annotationMap.get(type); if (titleSuffix) addToTypeSuffix.push(titleSuffix); if (typeSuffix) addToTypeSuffix.push(typeSuffix); if (Array.isArray(ac === null || ac === void 0 ? void 0 : ac.addToType)) { (_a = ac === null || ac === void 0 ? void 0 : ac.addToType) === null || _a === void 0 ? void 0 : _a.forEach(field => { if (a[field] != null) addToTypeSuffix.push(a[field]); }); } let newType; if (ac && ac.key != null && a[ac.key]) { addToTypeSuffix.unshift(a[ac.key]); newType = type + addToTypeSuffix.join("."); if (!this.annotationMap.has(newType)) { const title = a[ac.key] != null ? a[ac.key] : ""; this.annotationMap.set(newType, Object.assign(Object.assign({}, ac), { type: newType, color: typeof ac.color === "string" ? RcsbAnnotationConfig.randomRgba(title) : Object.assign(Object.assign({}, ac.color), { colors: RcsbAnnotationConfig.randomRgba(title) }), prefix: trackTitle !== null && trackTitle !== void 0 ? trackTitle : ac.title, title: title, provenanceList: new Set() })); this.addNewType(newType, type); } } else if (titleSuffix != null) { newType = type + addToTypeSuffix.join("."); if (!this.annotationMap.has(newType)) { this.annotationMap.set(newType, Object.assign(Object.assign({ display: (_b = ac === null || ac === void 0 ? void 0 : ac.display) !== null && _b !== void 0 ? _b : "block" /* RcsbFvDisplayTypes.BLOCK */ }, ac), { type: newType, prefix: trackTitle !== null && trackTitle !== void 0 ? trackTitle : ac === null || ac === void 0 ? void 0 : ac.title, title: titleSuffix, provenanceList: new Set() })); this.addNewType(newType, type, titleSuffix); } } else if (trackTitle != null) { newType = type + addToTypeSuffix.join("."); if (!this.annotationMap.has(newType)) { this.annotationMap.set(newType, Object.assign(Object.assign({ display: "block" /* RcsbFvDisplayTypes.BLOCK */, type: (0, uniqid_1.default)("type-") }, ac), { title: trackTitle, provenanceList: new Set() })); this.addNewType(newType, type); } } else { newType = type + addToTypeSuffix.join("."); if (!this.annotationMap.has(newType) || this.mergedTypes.has(type)) { this.addNewType(newType, type); } } this.addProvenance(newType, (_c = feature.provenance_source) !== null && _c !== void 0 ? _c : "N/A"); return newType; }); } isMergedType(type) { return this.mergedTypes.has(type); } getMergeConfig(type) { return this.mergedTypes.get(type); } addNewType(newType, type, titleSuffix) { const mt = this.mergedTypes.get(type); if (mt) { const mergedType = titleSuffix ? mt.type + "." + titleSuffix : mt.type; if (newType != type) this.mergedTypes.set(newType, { merged_types: mt.merged_types, display: mt.display, type: mergedType, title: mt.title, }); this.annotationMap.set(mergedType, { type: mergedType, display: mt.display, title: titleSuffix !== null && titleSuffix !== void 0 ? titleSuffix : mt.title, prefix: titleSuffix ? mt.title : undefined, provenanceList: new Set() }); this.checkAndIncludeNewType(mergedType, mt.type); } else { this.checkAndIncludeNewType(newType, type); } } checkAndIncludeNewType(newType, type) { var _a, _b, _c, _d, _e, _f; if (type === newType) return; if (this.annotationTypes.instance.has(type) && !((_a = this.annotationTypes.instance.get(type)) === null || _a === void 0 ? void 0 : _a.includes(newType))) (_b = this.annotationTypes.instance.get(type)) === null || _b === void 0 ? void 0 : _b.push(newType); if (this.annotationTypes.entity.has(type) && !((_c = this.annotationTypes.entity.get(type)) === null || _c === void 0 ? void 0 : _c.includes(newType))) (_d = this.annotationTypes.entity.get(type)) === null || _d === void 0 ? void 0 : _d.push(newType); if (this.annotationTypes.external.has(type) && !((_e = this.annotationTypes.external.get(type)) === null || _e === void 0 ? void 0 : _e.includes(newType))) (_f = this.annotationTypes.external.get(type)) === null || _f === void 0 ? void 0 : _f.push(newType); } static randomRgba(str) { return (0, string_to_color_1.default)(str); } addProvenance(type, provenanceName) { var _a; if (this.annotationMap.has(type)) (_a = this.annotationMap.get(type)) === null || _a === void 0 ? void 0 : _a.provenanceList.add(provenanceName); } addMultipleProvenance(type, provenanceList) { provenanceList.forEach(p => { this.addProvenance(type, p); }); } initTypes() { var _a, _b, _c, _d; (_a = this.annotationConfigMap.external_data_order) === null || _a === void 0 ? void 0 : _a.forEach(type => { this.annotationTypes.external.set(type, [type]); }); (_b = this.annotationConfigMap.instance_order) === null || _b === void 0 ? void 0 : _b.forEach(type => { this.annotationTypes.instance.set(type, [type]); }); (_c = this.annotationConfigMap.entity_order) === null || _c === void 0 ? void 0 : _c.forEach(type => { this.annotationTypes.entity.set(type, [type]); }); (_d = this.annotationConfigMap.merge) === null || _d === void 0 ? void 0 : _d.forEach(m => { m.merged_types.forEach(type => { this.mergedTypes.set(type, m); }); }); } } exports.RcsbAnnotationConfig = RcsbAnnotationConfig; class ArraySuffix extends Array { join(separator) { if (this.length == 0) return ""; else return ":" + super.join(separator); } push(...items) { items.forEach(i => { if (i) super.push(i); }); return this.length; } }