UNPKG

@rcsb/rcsb-saguaro-app

Version:
246 lines (245 loc) 12.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AnnotationTrackManagerFactory = void 0; const GqlTypes_1 = require("@rcsb/rcsb-api-tools/lib/RcsbGraphQL/Types/Borrego/GqlTypes"); const TrackUtils_1 = require("../../RcsbFvTrackFactory/TrackFactoryImpl/Helper/TrackUtils"); const Assertions_1 = require("../../../../RcsbUtils/Helpers/Assertions"); var assertElementListDefined = Assertions_1.Assertions.assertElementListDefined; var assertDefined = Assertions_1.Assertions.assertDefined; const TagDelimiter_1 = require("@rcsb/rcsb-api-tools/lib/RcsbUtils/TagDelimiter"); class AnnotationTrackManagerFactory { getTrackManager(trackId, annotationConfig, entityInstanceTranslator) { return new AnnotationTrackManager(trackId, annotationConfig, entityInstanceTranslator); } } exports.AnnotationTrackManagerFactory = AnnotationTrackManagerFactory; class AnnotationTrackManager { constructor(type, annotationConfig, entityInstanceTranslator) { this.valueRange = { max: Number.MIN_SAFE_INTEGER, min: Number.MAX_SAFE_INTEGER }; this.trackElementMap = new Map(); this.entityInstanceTranslator = entityInstanceTranslator; this.type = type; this.annotationConfig = annotationConfig; } getId() { return this.type; } getConfig() { return this.annotationConfig; } getRange() { return this.valueRange; } addFeature(ann, annotationProcessing) { assertElementListDefined(ann.feature.feature_positions); computeFeatureGaps(ann.feature.feature_positions).forEach(p => { if (p.beg_seq_id != null) { this.annotationRangeKeys(p).forEach(rangeKey => { this.addRange(ann.reference, ann.queryId, ann.source, ann.targetId, ann.feature, p, rangeKey, annotationProcessing); }); } }); } size() { return this.trackElementMap.size; } forEach(f) { this.trackElementMap.forEach((ann, loc) => { f(ann, loc); }); } addAll(trackElementsMap, color) { trackElementsMap.forEach((ann, loc) => { ann.color = color !== null && color !== void 0 ? color : ann.color; this.trackElementMap.set(loc, ann); }); } getTrackProvenance() { return new Set(Array.from(this.trackElementMap.values()).map(e => e.provenanceName).filter((x) => x != null)); } values() { return Array.from(this.trackElementMap.values()); } buildRcsbFvTrackDataElement(p, d, targetId, source, provenance) { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l; let title = (_b = (_a = this.annotationConfig) === null || _a === void 0 ? void 0 : _a.title) !== null && _b !== void 0 ? _b : this.type; let value = undefined; if (this.isNumericalDisplay(this.type)) { if ((_c = this.annotationConfig) === null || _c === void 0 ? void 0 : _c.transformToNumerical) { value = 1; } else { value = Array.isArray(p.values) && p.values[0] ? p.values[0] : 0; } } if (Array.isArray(p.values) && p.values.length > 0) { assertElementListDefined(p.values); if (Math.max(...p.values) > this.valueRange.max) this.valueRange.max = Math.max(...p.values); if (Math.min(...p.values) < this.valueRange.min) this.valueRange.min = Math.min(...p.values); } else if (typeof value === "number") { if (value > this.valueRange.max) this.valueRange.max = value; if (value < this.valueRange.min) this.valueRange.min = value; } const sourceId = source == GqlTypes_1.AnnotationReference.PdbInstance && this.entityInstanceTranslator != null ? TagDelimiter_1.TagDelimiter.parseInstance(targetId).entryId + TagDelimiter_1.TagDelimiter.instance + this.entityInstanceTranslator.translateAsymToAuth(TagDelimiter_1.TagDelimiter.parseInstance(targetId).instanceId) : targetId; assertDefined(p.beg_seq_id); return { begin: p.beg_seq_id, end: (_d = p.end_seq_id) !== null && _d !== void 0 ? _d : p.beg_seq_id, oriBegin: (_e = p.beg_ori_id) !== null && _e !== void 0 ? _e : undefined, oriEnd: (_g = ((_f = p.end_ori_id) !== null && _f !== void 0 ? _f : p.beg_ori_id)) !== null && _g !== void 0 ? _g : undefined, description: new Array(), featureId: (_h = d.feature_id) !== null && _h !== void 0 ? _h : undefined, type: this.type, title: title, name: (_j = d.name) !== null && _j !== void 0 ? _j : undefined, value: value, gaps: p.gaps, sourceId: sourceId, source: TrackUtils_1.TrackUtils.transformSourceFromTarget(targetId, source), provenanceName: provenance, provenanceColor: TrackUtils_1.TrackUtils.getProvenanceColorFromProvenance(provenance), openBegin: (_k = p.open_begin) !== null && _k !== void 0 ? _k : undefined, openEnd: (_l = p.open_end) !== null && _l !== void 0 ? _l : undefined }; } addRange(reference, queryId, source, targetId, d, p, rangeKey, annotationProcessing) { var _a, _b, _c, _d; const key = rangeKey.join(":"); if (!this.trackElementMap.has(key)) { const a = this.buildRcsbFvTrackDataElement(p, d, targetId, source, (_a = d.provenance_source) !== null && _a !== void 0 ? _a : undefined); if ((_b = this.annotationConfig) === null || _b === void 0 ? void 0 : _b.transformToNumerical) this.transformToNumerical(targetId, rangeKey, key, a, d, p, annotationProcessing === null || annotationProcessing === void 0 ? void 0 : annotationProcessing.getAnnotationValue); if (typeof (annotationProcessing === null || annotationProcessing === void 0 ? void 0 : annotationProcessing.addTrackElementCallback) === "function") annotationProcessing === null || annotationProcessing === void 0 ? void 0 : annotationProcessing.addTrackElementCallback({ type: this.type, targetId: targetId, positionKey: key, d: d, p: p }); const translateContext = { from: reference, to: source, queryId: queryId, targetId: targetId }; this.addAuthorResIds(a, translateContext); this.trackElementMap.set(key, a); if (Array.isArray(p.values)) { assertElementListDefined(p.values); this.expandValues(a, p.values, translateContext); } } else if (this.isNumericalDisplay(this.type) && ((_c = this.annotationConfig) === null || _c === void 0 ? void 0 : _c.transformToNumerical) && typeof ((_d = this.trackElementMap.get(key)) === null || _d === void 0 ? void 0 : _d.value) === "number") { const o = this.trackElementMap.get(key); assertDefined(o === null || o === void 0 ? void 0 : o.value); if (typeof o.value !== "number") return; o.value += typeof (annotationProcessing === null || annotationProcessing === void 0 ? void 0 : annotationProcessing.getAnnotationValue) === "function" ? annotationProcessing.getAnnotationValue({ type: this.type, targetId: targetId, positionKey: key, d: d, p: p }) : 1; if (o.value > this.valueRange.max) this.valueRange.max = o.value; if (o.value < this.valueRange.min) this.valueRange.min = o.value; } if (typeof d.description === "string") { const o = this.trackElementMap.get(key); if (o) { if (!o.description) o.description = []; o.description.push(d.description); } } } transformToNumerical(targetId, rangeKey, key, a, d, p, getAnnotationValue) { if (typeof getAnnotationValue === "function") a.value = getAnnotationValue({ type: this.type, targetId: targetId, positionKey: key, d: d, p: p }); a.begin = rangeKey[0]; a.end = rangeKey[0]; } expandValues(e, values, translateContext) { values.forEach((v, i) => { if (i > 0) { const key = (e.begin + i).toString(); const a = Object.assign(Object.assign({}, e), { begin: (e.begin + i), end: undefined, oriBegin: e.oriBegin ? (e.oriBegin + i) : undefined, oriEnd: undefined, value: v }); this.addAuthorResIds(a, translateContext); this.trackElementMap.set(key, a); } }); } addAuthorResIds(e, annotationContext) { let o = e; if (this.entityInstanceTranslator != null && annotationContext.from) { this.entityInstanceTranslator.addAuthorResIds(o, annotationContext); } return o; } isNumericalDisplay(type) { return (this.annotationConfig != null && (this.annotationConfig.display === "area" /* RcsbFvDisplayTypes.AREA */ || this.annotationConfig.display === "block-area" /* RcsbFvDisplayTypes.BLOCK_AREA */ || this.annotationConfig.display === "multi-area" /* RcsbFvDisplayTypes.MULTI_AREA */ || this.annotationConfig.display === "line" /* RcsbFvDisplayTypes.LINE */)); } annotationRangeKeys(p) { var _a, _b, _c; const rangeKeys = new Array(); if (((_a = this.annotationConfig) === null || _a === void 0 ? void 0 : _a.transformToNumerical) && p.beg_seq_id && p.end_seq_id != null) { for (let i = p.beg_seq_id; i <= p.end_seq_id; i++) { rangeKeys.push([i]); } } else if (((_b = this.annotationConfig) === null || _b === void 0 ? void 0 : _b.transformToNumerical) && p.beg_seq_id) { rangeKeys.push([p.beg_seq_id]); } else if (p.beg_seq_id) { const key = p.end_seq_id != null ? [p.beg_seq_id, p.end_seq_id] : [p.beg_seq_id, p.beg_seq_id]; if ((_c = this.annotationConfig) === null || _c === void 0 ? void 0 : _c.displayCooccurrence) key.push(Math.ceil(Math.random() * 1000000000000)); rangeKeys.push(key); } return rangeKeys; } } function computeFeatureGaps(featurePositions) { const rangeIdMap = new Map(); const out = new Array(); featurePositions.forEach(fp => { var _a; if (fp.range_id != null) { if (!rangeIdMap.has(fp.range_id)) rangeIdMap.set(fp.range_id, new Array()); (_a = rangeIdMap.get(fp.range_id)) === null || _a === void 0 ? void 0 : _a.push(fp); } else { out.push(fp); } }); rangeIdMap.forEach((fpList, rangeId) => { var _a, _b, _c; if (fpList.length == 1) { out.push(fpList[0]); } else { const sorted = fpList.sort((a, b) => { if (a.beg_seq_id && b.beg_seq_id) return a.beg_seq_id - b.beg_seq_id; return Number.MAX_SAFE_INTEGER; }); const gapedFeaturePosition = Object.assign(Object.assign({}, sorted[0]), { end_seq_id: sorted[sorted.length - 1].end_seq_id, end_ori_id: sorted[sorted.length - 1].end_ori_id, open_end: sorted[sorted.length - 1].open_end, gaps: new Array() }); for (let n = 0; n < sorted.length - 1; n++) { const prevEnd = sorted[n].end_seq_id; const nextBeg = sorted[n + 1].beg_seq_id; if (prevEnd && nextBeg) (_a = gapedFeaturePosition === null || gapedFeaturePosition === void 0 ? void 0 : gapedFeaturePosition.gaps) === null || _a === void 0 ? void 0 : _a.push({ begin: prevEnd, end: nextBeg, isConnected: (sorted[n].beg_ori_id == null || sorted[n].end_ori_id == null || sorted[n + 1].beg_ori_id == null || sorted[n + 1].end_ori_id == null || ((_b = sorted[n].end_ori_id) !== null && _b !== void 0 ? _b : Number.MIN_SAFE_INTEGER) + 1 == sorted[n + 1].beg_ori_id || sorted[n].end_ori_id == ((_c = sorted[n + 1].beg_ori_id) !== null && _c !== void 0 ? _c : Number.MIN_SAFE_INTEGER) + 1 || sorted[n].end_ori_id == sorted[n + 1].beg_ori_id) }); } out.push(gapedFeaturePosition); } }); return out; }