UNPKG

@rcsb/rcsb-saguaro-app

Version:
296 lines (295 loc) 14.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PairwiseAlignmentBuilder = void 0; const RcsbAnnotationConstants_1 = require("../../RcsbAnnotationConfig/RcsbAnnotationConstants"); const GqlTypes_1 = require("@rcsb/rcsb-api-tools/lib/RcsbGraphQL/Types/Borrego/GqlTypes"); const FeatureTools_1 = require("../../RcsbCollectTools/FeatureTools/FeatureTools"); const TrackUtils_1 = require("../../RcsbFvWeb/RcsbFvFactories/RcsbFvTrackFactory/TrackFactoryImpl/Helper/TrackUtils"); const gradient = (color1, color2, weight) => { const w1 = weight; const w2 = 1 - w1; return [Math.round(color1[0] * w1 + color2[0] * w2), Math.round(color1[1] * w1 + color2[1] * w2), Math.round(color1[2] * w1 + color2[2] * w2)]; }; //TODO This class is not using factories to build PFV row configurations class PairwiseAlignmentBuilder { constructor(paI) { this.querySequence = paI.querySequence; this.targetSequence = paI.targetSequence; this.queryAlignment = paI.queryAlignment; this.targetAlignment = paI.targetAlignment; this.queryBegin = paI.queryBegin; this.targetBegin = paI.targetBegin; this.targetId = paI.targetId; this.queryId = paI.queryId; this.sequenceId = paI.sequenceId; this.isQueryExternal = paI.isQueryExternal; this.isTargetExternal = paI.isTargetExternal; this.pairwiseView = paI.pairwiseView; if (this.queryAlignment.length != this.targetAlignment.length) throw "Sequence alignments length mismatch"; if (this.pairwiseView) this.alignmentLength = this.queryAlignment.length; else this.alignmentLength = this.querySequence.length; } getLength() { return this.alignmentLength; } buildReferenceAlignment() { const qA = this.queryAlignment.split(''); const tA = this.targetAlignment.split(''); const alignedBlocks = []; const mismatchData = []; const targetSequence = []; let currentQueryIndex = this.queryBegin; let currentTargetIndex = this.targetBegin; let blockQueryStart = this.queryBegin; let blockTargetStart = this.targetBegin; qA.forEach((q, i) => { const t = tA[i]; if ((t === "-" || q === "-") && blockQueryStart > 0) { alignedBlocks.push({ begin: blockQueryStart, end: (currentQueryIndex - 1), oriBegin: blockTargetStart, oriEnd: (currentTargetIndex - 1), source: this.isTargetExternal ? RcsbAnnotationConstants_1.RcsbAnnotationConstants.provenanceName.userInput : TrackUtils_1.TrackUtils.transformSourceFromTarget(this.targetId, GqlTypes_1.SequenceReference.PdbEntity), sourceId: this.targetId, provenanceName: RcsbAnnotationConstants_1.RcsbAnnotationConstants.provenanceName.mmseqs, provenanceColor: RcsbAnnotationConstants_1.RcsbAnnotationConstants.provenanceColorCode.rcsbPdb, type: "ALIGNED_BLOCK", title: "ALIGNED REGION" }); blockQueryStart = 0; } if (q != "-" && t != "-") { targetSequence.push({ begin: currentQueryIndex, oriBegin: currentTargetIndex, source: this.isTargetExternal ? RcsbAnnotationConstants_1.RcsbAnnotationConstants.provenanceName.userInput : TrackUtils_1.TrackUtils.transformSourceFromTarget(this.targetId, GqlTypes_1.SequenceReference.PdbEntity), sourceId: this.targetId, provenanceName: TrackUtils_1.TrackUtils.getProvenanceConfigFormTarget(this.targetId, this.targetId).name, provenanceColor: TrackUtils_1.TrackUtils.getProvenanceConfigFormTarget(this.targetId, this.targetId).color, label: t, type: "RESIDUE", title: "RESIDUE" }); if (q != t) { mismatchData.push({ begin: currentQueryIndex, oriBegin: currentTargetIndex, source: this.isTargetExternal ? RcsbAnnotationConstants_1.RcsbAnnotationConstants.provenanceName.userInput : TrackUtils_1.TrackUtils.transformSourceFromTarget(this.targetId, GqlTypes_1.SequenceReference.PdbEntity), sourceId: this.targetId, provenanceName: RcsbAnnotationConstants_1.RcsbAnnotationConstants.provenanceName.mmseqs, provenanceColor: RcsbAnnotationConstants_1.RcsbAnnotationConstants.provenanceColorCode.rcsbPdb, type: "MISMATCH", title: "MISMATCH" }); } } if (t != "-" && q != "-" && blockQueryStart == 0) { blockQueryStart = currentQueryIndex; blockTargetStart = currentTargetIndex; } if (q != "-") { currentQueryIndex++; } if (t != "-") { currentTargetIndex++; } }); if (blockQueryStart > 0) { alignedBlocks.push({ begin: blockQueryStart, end: (currentQueryIndex - 1), oriBegin: blockTargetStart, oriEnd: (currentTargetIndex - 1), source: this.isTargetExternal ? RcsbAnnotationConstants_1.RcsbAnnotationConstants.provenanceName.userInput : TrackUtils_1.TrackUtils.transformSourceFromTarget(this.targetId, GqlTypes_1.SequenceReference.PdbEntity), sourceId: this.targetId, provenanceName: RcsbAnnotationConstants_1.RcsbAnnotationConstants.provenanceName.mmseqs, provenanceColor: RcsbAnnotationConstants_1.RcsbAnnotationConstants.provenanceColorCode.rcsbPdb, type: "ALIGNED_BLOCK", title: "ALIGNED REGION" }); } this.addBlockTerminalTags(alignedBlocks); const queryTrack = { trackId: "query_sequence", displayType: "sequence" /* RcsbFvDisplayTypes.SEQUENCE */, trackColor: "#F9F9F9", displayColor: "#000000", titleFlagColor: TrackUtils_1.TrackUtils.getProvenanceConfigFormTarget(this.queryId, GqlTypes_1.SequenceReference.PdbEntity).color, rowTitle: this.queryId, nonEmptyDisplay: true, trackData: this.querySequence.split('').map((s, n) => ({ source: this.isQueryExternal ? RcsbAnnotationConstants_1.RcsbAnnotationConstants.provenanceName.userInput : TrackUtils_1.TrackUtils.transformSourceFromTarget(this.queryId, GqlTypes_1.SequenceReference.PdbEntity), sourceId: this.queryId, provenanceName: TrackUtils_1.TrackUtils.getProvenanceConfigFormTarget(this.queryId, this.queryId).name, provenanceColor: TrackUtils_1.TrackUtils.getProvenanceConfigFormTarget(this.queryId, this.queryId).color, label: s, begin: n + 1 })) }; const targetSequenceDisplay = { displayType: "sequence" /* RcsbFvDisplayTypes.SEQUENCE */, displayColor: "#333333", displayData: targetSequence }; const mismatchDisplay = { displayType: "pin" /* RcsbFvDisplayTypes.PIN */, displayColor: "#FF9999", displayData: mismatchData, minRatio: 5 }; const blockColor = "rgb(" + gradient([153, 153, 255], [50, 50, 50], this.sequenceId).join(",") + ")"; const alignmentDisplay = { displayType: "block" /* RcsbFvDisplayTypes.BLOCK */, displayColor: blockColor, displayData: alignedBlocks }; const alignmentTrack = { trackId: "targetSequenceTrack", rowTitle: this.targetId, displayType: "composite" /* RcsbFvDisplayTypes.COMPOSITE */, trackColor: "#F9F9F9", titleFlagColor: TrackUtils_1.TrackUtils.getProvenanceConfigFormTarget(this.targetId, GqlTypes_1.SequenceReference.PdbEntity).color, displayConfig: [alignmentDisplay, mismatchDisplay, targetSequenceDisplay] }; return [queryTrack, alignmentTrack]; } buildPairwiseAlignment() { const qA = this.queryAlignment.split(''); const tA = this.targetAlignment.split(''); const alignedBlocks = []; const mismatchData = []; const querySeq = []; const targetSeq = []; let currentQueryIndex = this.queryBegin; let currentTargetIndex = this.targetBegin; let start = 1; qA.forEach((q, i) => { const t = tA[i]; if (q === "-" || t === "-") { if (start > 0 && i > start) { alignedBlocks.push({ begin: start, end: i, provenanceName: RcsbAnnotationConstants_1.RcsbAnnotationConstants.provenanceName.mmseqs, provenanceColor: RcsbAnnotationConstants_1.RcsbAnnotationConstants.provenanceColorCode.rcsbPdb, type: "ALIGNED_BLOCK", title: "ALIGNED REGION" }); start = 0; } } else if (q != "-" && t != "-" && start == 0) { start = i + 1; } if (q != "-" && t != "-" && q != t && start > 0) { mismatchData.push({ begin: i + 1, provenanceName: RcsbAnnotationConstants_1.RcsbAnnotationConstants.provenanceName.mmseqs, provenanceColor: RcsbAnnotationConstants_1.RcsbAnnotationConstants.provenanceColorCode.rcsbPdb, type: "MISMATCH", title: "MISMATCH" }); } querySeq.push({ begin: i + 1, label: q, source: RcsbAnnotationConstants_1.RcsbAnnotationConstants.provenanceName.userInput, sourceId: this.queryId, provenanceName: this.queryId, provenanceColor: RcsbAnnotationConstants_1.RcsbAnnotationConstants.provenanceColorCode.external, oriBegin: q != "-" ? currentQueryIndex : undefined }); if (q != "-") { currentQueryIndex++; } targetSeq.push({ begin: i + 1, label: t, source: TrackUtils_1.TrackUtils.transformSourceFromTarget(this.targetId, GqlTypes_1.SequenceReference.PdbEntity), sourceId: this.targetId, provenanceName: TrackUtils_1.TrackUtils.getProvenanceConfigFormTarget(this.targetId, GqlTypes_1.SequenceReference.PdbEntity).name, provenanceColor: TrackUtils_1.TrackUtils.getProvenanceConfigFormTarget(this.targetId, GqlTypes_1.SequenceReference.PdbEntity).color, oriBegin: t != "-" ? currentTargetIndex : undefined }); if (t != "-") { currentTargetIndex++; } }); if (start > 0) { alignedBlocks.push({ begin: start, end: this.alignmentLength, provenanceName: RcsbAnnotationConstants_1.RcsbAnnotationConstants.provenanceName.mmseqs, provenanceColor: RcsbAnnotationConstants_1.RcsbAnnotationConstants.provenanceColorCode.rcsbPdb, type: "ALIGNED_BLOCK", title: "ALIGNED REGION" }); } const queryTrack = { trackId: "query_sequence", displayType: "sequence" /* RcsbFvDisplayTypes.SEQUENCE */, trackColor: "#F9F9F9", displayColor: "#000000", rowTitle: this.queryId, titleFlagColor: TrackUtils_1.TrackUtils.getProvenanceConfigFormTarget(this.queryId, GqlTypes_1.SequenceReference.PdbEntity).color, nonEmptyDisplay: true, trackData: querySeq }; const targetTrack = { trackId: "target_sequence", displayType: "sequence" /* RcsbFvDisplayTypes.SEQUENCE */, trackColor: "#F9F9F9", displayColor: "#000000", rowTitle: this.targetId, titleFlagColor: TrackUtils_1.TrackUtils.getProvenanceConfigFormTarget(this.targetId, GqlTypes_1.SequenceReference.PdbEntity).color, nonEmptyDisplay: true, trackData: targetSeq }; const mismatchDisplay = { displayType: "pin" /* RcsbFvDisplayTypes.PIN */, displayColor: "#FF9999", displayData: mismatchData, minRatio: 5 }; const blockColor = "rgb(" + gradient([153, 153, 255], [50, 50, 50], this.sequenceId).join(",") + ")"; const alignmentDisplay = { displayType: "block" /* RcsbFvDisplayTypes.BLOCK */, displayColor: blockColor, displayData: alignedBlocks }; const alignmentTrack = { trackId: "targetSequenceTrack", displayType: "composite" /* RcsbFvDisplayTypes.COMPOSITE */, trackColor: "#F9F9F9", rowTitle: " ", titleFlagColor: RcsbAnnotationConstants_1.RcsbAnnotationConstants.provenanceColorCode.rcsbPdb, displayConfig: [alignmentDisplay, mismatchDisplay] }; return [queryTrack, alignmentTrack, targetTrack]; } addBlockTerminalTags(alignedBlocks) { var _a; if ((_a = alignedBlocks[0].oriBegin) !== null && _a !== void 0 ? _a : 0 > 1) { alignedBlocks[0].openBegin = true; } for (let n = 0; n < (alignedBlocks.length - 1); n++) { const oriEnd = alignedBlocks[n].oriEnd; if (oriEnd && oriEnd + 1 != alignedBlocks[n + 1].oriBegin) { alignedBlocks[n].openEnd = true; alignedBlocks[n + 1].openBegin = true; } } const oriEnd = alignedBlocks[alignedBlocks.length - 1].oriEnd; if (oriEnd && oriEnd < this.targetSequence.length) { alignedBlocks[alignedBlocks.length - 1].openEnd = true; } FeatureTools_1.FeatureTools.mergeBlocks(alignedBlocks); } } exports.PairwiseAlignmentBuilder = PairwiseAlignmentBuilder;