UNPKG

drugflow-molstar

Version:
777 lines 50.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.renderSuperposition = exports.superposeAf = exports.loadAfStructure = exports.initSuperposition = void 0; var tslib_1 = require("tslib"); var linear_algebra_1 = require("Molstar/mol-math/linear-algebra"); var builder_1 = require("Molstar/mol-script/language/builder"); var mol_state_1 = require("Molstar/mol-state"); var transforms_1 = require("Molstar/mol-plugin-state/transforms"); var assets_1 = require("Molstar/mol-util/assets"); var helpers_1 = require("./helpers"); var lists_1 = require("Molstar/mol-util/color/lists"); var color_1 = require("Molstar/mol-util/color/color"); var script_1 = require("Molstar/mol-script/script"); var mol_task_1 = require("Molstar/mol-task"); var superposition_sifts_mapping_1 = require("./superposition-sifts-mapping"); var alphafold_transparency_1 = require("./alphafold-transparency"); var structure_1 = require("Molstar/mol-model/structure"); function getRandomColor(plugin, segmentIndex) { var clList = lists_1.ColorLists; var spState = plugin.customState.superpositionState; var palleteIndex = spState.colorState[segmentIndex].palleteIndex; var colorIndex = spState.colorState[segmentIndex].colorIndex; if (clList[spState.colorPalette[palleteIndex]].list[colorIndex + 1]) { colorIndex += 1; } else { colorIndex = 0; palleteIndex = spState.colorPalette[palleteIndex + 1] ? palleteIndex + 1 : 0; } var palleteName = spState.colorPalette[palleteIndex]; plugin.customState.superpositionState.colorState[segmentIndex].palleteIndex = palleteIndex; plugin.customState.superpositionState.colorState[segmentIndex].colorIndex = colorIndex; return clList[palleteName].list[colorIndex]; } function initSuperposition(plugin) { return tslib_1.__awaiter(this, void 0, void 0, function () { var customState, superpositionParams, segmentData, afStrUrls, segmentIndex, clusterIndexs, entryList, clusters; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, plugin.clear()]; case 1: _a.sent(); customState = plugin.customState; superpositionParams = customState.initParams.superpositionParams; plugin.customState.superpositionState = { models: {}, entries: {}, refMaps: {}, segmentData: void 0, matrixData: {}, activeSegment: 0, loadedStructs: [], visibleRefs: [], invalidStruct: [], noMatrixStruct: [], hets: {}, colorPalette: ['dark-2', 'red-yellow-green', 'paired', 'set-1', 'accent', 'set-2', 'rainbow'], colorState: [], alphafold: { apiData: { cif: '', pae: '', length: 0 }, length: 0, ref: '', traceOnly: true, visibility: [], transforms: [], rmsds: [], coordinateSystems: [] } }; // Get segment and cluster information for the given uniprot accession return [4 /*yield*/, getSegmentData(plugin)]; case 2: // Get segment and cluster information for the given uniprot accession _a.sent(); segmentData = plugin.customState.superpositionState.segmentData; if (!segmentData) return [2 /*return*/]; // Load Matrix Data return [4 /*yield*/, getMatrixData(plugin)]; case 3: // Load Matrix Data _a.sent(); if (!plugin.customState.superpositionState.segmentData) return [2 /*return*/]; return [4 /*yield*/, getAfUrl(plugin, customState.initParams.moleculeId)]; case 4: afStrUrls = _a.sent(); if (afStrUrls) customState.superpositionState.alphafold.apiData = afStrUrls; segmentData.forEach(function () { plugin.customState.superpositionState.loadedStructs.push([]); plugin.customState.superpositionState.visibleRefs.push([]); plugin.customState.superpositionState.colorState.push({ palleteIndex: 0, colorIndex: -1 }); }); segmentIndex = (superpositionParams && superpositionParams.segment) ? superpositionParams.segment - 1 : 0; plugin.customState.superpositionState.activeSegment = segmentIndex + 1; clusterIndexs = (superpositionParams && superpositionParams.cluster) ? superpositionParams.cluster : void 0; // Emit segment API data load event plugin.customState.events.superpositionInit.next(true); entryList = []; clusters = segmentData[segmentIndex].clusters; clusters.forEach(function (cluster, clusterIndex) { // Validate for cluster index if provided in superPositionParams if (clusterIndexs && clusterIndexs.indexOf(clusterIndex) === -1) return; // Add respresentative structure to the list if (superpositionParams && superpositionParams.superposeAll) { entryList = entryList.concat(cluster); } else { entryList.push(cluster[0]); } }); return [4 /*yield*/, renderSuperposition(plugin, segmentIndex, entryList)]; case 5: _a.sent(); return [2 /*return*/]; } }); }); } exports.initSuperposition = initSuperposition; function createCarbVisLabel(carbLigNamesAndCount) { var compList = []; for (var carbCompId in carbLigNamesAndCount) { compList.push("".concat(carbCompId, " (").concat(carbLigNamesAndCount[carbCompId], ")")); } return compList.join(', '); } function getAfUrl(plugin, accession) { return tslib_1.__awaiter(this, void 0, void 0, function () { var apiResponse, apiData; var _this = this; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, plugin.runTask(mol_task_1.Task.create('Get AlphaFold URL', function (ctx) { return tslib_1.__awaiter(_this, void 0, void 0, function () { var e_1; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); return [4 /*yield*/, plugin.fetch({ url: "https://alphafold.ebi.ac.uk/api/prediction/".concat(accession), type: 'json' }).runInContext(ctx)]; case 1: apiResponse = _a.sent(); if (apiResponse && (apiResponse === null || apiResponse === void 0 ? void 0 : apiResponse[0].bcifUrl)) { apiData = { cif: apiResponse === null || apiResponse === void 0 ? void 0 : apiResponse[0].cifUrl, pae: apiResponse === null || apiResponse === void 0 ? void 0 : apiResponse[0].paeImageUrl, length: apiResponse === null || apiResponse === void 0 ? void 0 : apiResponse[0].uniprotEnd }; } return [3 /*break*/, 3]; case 2: e_1 = _a.sent(); return [3 /*break*/, 3]; case 3: return [2 /*return*/]; } }); }); }))]; case 1: _a.sent(); return [2 /*return*/, apiData]; } }); }); } function loadAfStructure(plugin) { return tslib_1.__awaiter(this, void 0, void 0, function () { var customState, structure, strInstance, spState, chainSel; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: customState = plugin.customState; return [4 /*yield*/, loadStructure(plugin, customState.superpositionState.alphafold.apiData.cif, 'mmcif', false)]; case 1: structure = (_a.sent()).structure; strInstance = structure; if (!strInstance) return [2 /*return*/, false]; spState = plugin.customState.superpositionState; spState.alphafold.ref = strInstance === null || strInstance === void 0 ? void 0 : strInstance.ref; spState.models["AF-".concat(customState.initParams.moleculeId)] = strInstance === null || strInstance === void 0 ? void 0 : strInstance.ref; return [4 /*yield*/, plugin.builders.structure.tryCreateComponentStatic(strInstance, 'polymer', { label: "AlphaFold Structure", tags: ["alphafold-chain", "superposition-sel"] })]; case 2: chainSel = _a.sent(); if (!chainSel) return [3 /*break*/, 4]; return [4 /*yield*/, plugin.builders.structure.representation.addRepresentation(chainSel, { type: 'putty', color: 'plddt-confidence', size: 'uniform', sizeParams: { value: 1.5 } }, { tag: "af-superposition-visual" })]; case 3: _a.sent(); return [2 /*return*/, strInstance === null || strInstance === void 0 ? void 0 : strInstance.ref]; case 4: return [2 /*return*/, false]; } }); }); } exports.loadAfStructure = loadAfStructure; function superposeAf(plugin, traceOnly, segmentIndex) { return tslib_1.__awaiter(this, void 0, void 0, function () { var customState, spState, afStrRef, _a, afStr, segmentNum, mappingResult_1, coordinateSystems_1, failedPairsResult_1, zeroOverlapPairsResult_1, minRmsd_1, minIndex_1, rmsdList_1, segmentClusters; return tslib_1.__generator(this, function (_b) { switch (_b.label) { case 0: customState = plugin.customState; if (!customState.superpositionState || !customState.superpositionState.segmentData) return [2 /*return*/]; spState = customState.superpositionState; _a = spState.alphafold.ref; if (_a) return [3 /*break*/, 2]; return [4 /*yield*/, loadAfStructure(plugin)]; case 1: _a = (_b.sent()); _b.label = 2; case 2: afStrRef = _a; if (!afStrRef) return [2 /*return*/]; afStr = plugin.managers.structure.hierarchy.current.refs.get(afStrRef); segmentNum = segmentIndex ? segmentIndex : spState.activeSegment - 1; if (!spState.alphafold.transforms[segmentNum]) { mappingResult_1 = []; coordinateSystems_1 = []; failedPairsResult_1 = []; zeroOverlapPairsResult_1 = []; minRmsd_1 = 0; minIndex_1 = 0; rmsdList_1 = []; segmentClusters = spState.segmentData[segmentNum].clusters; segmentClusters.forEach(function (cluster) { var _a, _b, _c; var modelRef = spState.models["".concat(cluster[0].pdb_id, "_").concat(cluster[0].struct_asym_id)]; if (modelRef) { var structHierarchy = plugin.managers.structure.hierarchy.current.refs.get(modelRef); if (structHierarchy) { var input = [structHierarchy.components[0], afStr]; var structures = input.map(function (s) { var _a; return (_a = s.cell.obj) === null || _a === void 0 ? void 0 : _a.data; }); var _d = (0, superposition_sifts_mapping_1.alignAndSuperposeWithSIFTSMapping)(structures, { traceOnly: traceOnly, includeResidueTest: function (loc) { return structure_1.StructureProperties.atom.B_iso_or_equiv(loc) > 70; }, applyTestIndex: [1] }), entries = _d.entries, failedPairs = _d.failedPairs, zeroOverlapPairs = _d.zeroOverlapPairs; if (entries.length === 0 || (entries && entries[0] && entries[0].transform.rmsd.toFixed(1) === '0.0')) { var alignWithoutPlddt = (0, superposition_sifts_mapping_1.alignAndSuperposeWithSIFTSMapping)(structures, { traceOnly: traceOnly }); entries = alignWithoutPlddt.entries; } if (entries && entries[0]) { mappingResult_1.push(entries[0]); coordinateSystems_1.push((_c = (_b = (_a = input[0]) === null || _a === void 0 ? void 0 : _a.transform) === null || _b === void 0 ? void 0 : _b.cell.obj) === null || _c === void 0 ? void 0 : _c.data.coordinateSystem); var totalMappings = mappingResult_1.length; if (totalMappings === 1 || entries[0].transform.rmsd < minRmsd_1) { minRmsd_1 = entries[0].transform.rmsd; minIndex_1 = totalMappings === 1 ? 0 : mappingResult_1.length - 1; } rmsdList_1.push("".concat(cluster[0].pdb_id, " chain ").concat(cluster[0].struct_asym_id, ":").concat(entries[0].transform.rmsd.toFixed(2))); } else { if (failedPairs.length > 0) failedPairsResult_1.push(failedPairs); if (zeroOverlapPairs.length > 0) zeroOverlapPairsResult_1.push(zeroOverlapPairs); // rmsdList.push(`${cluster[0].pdb_id} ${cluster[0].struct_asym_id}:-`) } } } }); // console.log(failedPairsResult); // console.log(zeroOverlapPairsResult); if (mappingResult_1.length > 0) { spState.alphafold.visibility[segmentNum] = true; spState.alphafold.transforms[segmentNum] = mappingResult_1[minIndex_1].transform.bTransform; spState.alphafold.coordinateSystems[segmentNum] = coordinateSystems_1[minIndex_1]; spState.alphafold.rmsds[segmentNum] = rmsdList_1.sort(function (a, b) { return parseFloat(a.split(':')[1]) - parseFloat(b.split(':')[1]); }); } } return [4 /*yield*/, afTransform(plugin, afStr.cell, spState.alphafold.transforms[segmentNum], spState.alphafold.coordinateSystems[segmentNum])]; case 3: _b.sent(); (0, alphafold_transparency_1.applyAFTransparency)(plugin, afStr, 0.8, 70); return [2 /*return*/, true]; } }); }); } exports.superposeAf = superposeAf; function renderSuperposition(plugin, segmentIndex, entryList) { return tslib_1.__awaiter(this, void 0, void 0, function () { var customState, superpositionParams, busyFlagOn; var _this = this; return tslib_1.__generator(this, function (_a) { customState = plugin.customState; superpositionParams = customState.initParams.superpositionParams; busyFlagOn = false; if (entryList.length > 1) { busyFlagOn = true; customState.events.isBusy.next(true); } // Load Coordinates and render respresentations return [2 /*return*/, plugin.dataTransaction(function () { return tslib_1.__awaiter(_this, void 0, void 0, function () { var spState, entryList_1, entryList_1_1, s, strUrl, strInstance, modelRef, clearOnFail, polymerInstance, modelInstance, isBinary, _a, model, structure, matrix, chainSel, uniformColor1, uniformColor2, invalidStruct, state, hetInfo, hets, hets_1, hets_1_1, het, ligand, labelTagParams, hetColor, _b, r, g, b, ligandExp, e_2_1, carbEntityCount, allCarbPolymers, polymerChainWithSurroundings, i, _i, _c, carbEntityChainId, carbEntityChain, carbEntityChainInVicinity, data, carbChainSel, carbLigands, carbLigNamesAndCount, carbLigList, _d, _e, carbLigs, ligResDetails, carbVisLabel, branchedEntity, labelTagParams, ligandExp, loadedStructIndex, e_3_1; var e_3, _f, e_2, _g; return tslib_1.__generator(this, function (_h) { switch (_h.label) { case 0: spState = plugin.customState.superpositionState; _h.label = 1; case 1: _h.trys.push([1, 41, 42, 47]); entryList_1 = tslib_1.__asyncValues(entryList); _h.label = 2; case 2: return [4 /*yield*/, entryList_1.next()]; case 3: if (!(entryList_1_1 = _h.sent(), !entryList_1_1.done)) return [3 /*break*/, 40]; s = entryList_1_1.value; // validate matrix availability if (!spState.matrixData["".concat(s.pdb_id, "_").concat(s.auth_asym_id)]) { spState.noMatrixStruct.push("".concat(s.pdb_id, "_").concat(s.struct_asym_id)); spState.invalidStruct.push("".concat(s.pdb_id, "_").concat(s.struct_asym_id)); return [3 /*break*/, 39]; } spState.loadedStructs[segmentIndex].push("".concat(s.pdb_id, "_").concat(s.struct_asym_id)); strUrl = "".concat(customState.initParams.pdbeUrl, "model-server/v1/").concat(s.pdb_id, "/atoms?auth_asym_id=").concat(s.auth_asym_id, "&encoding=").concat(customState.initParams.encoding); if (superpositionParams && superpositionParams.ligandView) strUrl = "https://www.ebi.ac.uk/pdbe/entry-files/download/".concat(s.pdb_id, ".bcif"); strInstance = void 0; modelRef = void 0; clearOnFail = true; if (!(superpositionParams && superpositionParams.ligandView && spState.entries[s.pdb_id])) return [3 /*break*/, 5]; polymerInstance = plugin.state.data.select(spState.entries[s.pdb_id])[0]; modelRef = polymerInstance.transform.parent; modelInstance = plugin.state.data.select(modelRef)[0]; return [4 /*yield*/, plugin.builders.structure.createStructure(modelInstance, { name: 'model', params: {} })]; case 4: strInstance = _h.sent(); clearOnFail = false; return [3 /*break*/, 7]; case 5: isBinary = customState.initParams.encoding === 'bcif' ? true : false; return [4 /*yield*/, loadStructure(plugin, strUrl, 'mmcif', isBinary)]; case 6: _a = _h.sent(), model = _a.model, structure = _a.structure; strInstance = structure; modelRef = model.ref; _h.label = 7; case 7: if (!strInstance) return [3 /*break*/, 39]; // Store Refs in state if (!spState.models["".concat(s.pdb_id, "_").concat(s.struct_asym_id)]) spState.models["".concat(s.pdb_id, "_").concat(s.struct_asym_id)] = strInstance === null || strInstance === void 0 ? void 0 : strInstance.ref; if (superpositionParams && superpositionParams.ligandView && !spState.entries[s.pdb_id]) spState.entries[s.pdb_id] = strInstance === null || strInstance === void 0 ? void 0 : strInstance.ref; matrix = linear_algebra_1.Mat4.ofRows(plugin.customState.superpositionState.matrixData["".concat(s.pdb_id, "_").concat(s.auth_asym_id)].matrix); return [4 /*yield*/, transform(plugin, strInstance, matrix)]; case 8: _h.sent(); chainSel = void 0; if (!((superpositionParams && superpositionParams.ligandView) && s.is_representative)) return [3 /*break*/, 12]; uniformColor1 = getRandomColor(plugin, segmentIndex); return [4 /*yield*/, plugin.builders.structure.tryCreateComponentFromExpression(strInstance, chainSelection(s.struct_asym_id), "Chain-".concat(segmentIndex), { label: "Chain", tags: ["superposition-sel"] })]; case 9: chainSel = _h.sent(); if (!chainSel) return [3 /*break*/, 11]; return [4 /*yield*/, plugin.builders.structure.representation.addRepresentation(chainSel, { type: 'putty', color: 'uniform', colorParams: { value: uniformColor1 }, size: 'uniform' }, { tag: "superposition-visual" })]; case 10: _h.sent(); spState.refMaps[chainSel.ref] = "".concat(s.pdb_id, "_").concat(s.struct_asym_id); _h.label = 11; case 11: return [3 /*break*/, 16]; case 12: if (!((superpositionParams && superpositionParams.ligandView) && !s.is_representative)) return [3 /*break*/, 13]; return [3 /*break*/, 16]; case 13: uniformColor2 = getRandomColor(plugin, segmentIndex); return [4 /*yield*/, plugin.builders.structure.tryCreateComponentStatic(strInstance, 'polymer', { label: "Chain", tags: ["Chain-".concat(segmentIndex), "superposition-sel"] })]; case 14: chainSel = _h.sent(); if (!chainSel) return [3 /*break*/, 16]; return [4 /*yield*/, plugin.builders.structure.representation.addRepresentation(chainSel, { type: 'putty', color: 'uniform', colorParams: { value: uniformColor2 }, size: 'uniform' }, { tag: "superposition-visual" })]; case 15: _h.sent(); spState.refMaps[chainSel.ref] = "".concat(s.pdb_id, "_").concat(s.struct_asym_id); _h.label = 16; case 16: invalidStruct = chainSel ? false : true; if (!(superpositionParams && superpositionParams.ligandView)) return [3 /*break*/, 39]; state = plugin.state.data; return [4 /*yield*/, getLigandNamesFromModelData(plugin, state, modelRef)]; case 17: hetInfo = _h.sent(); hets = hetInfo ? hetInfo.hetNames : []; if (!(hets && hets.length > 0)) return [3 /*break*/, 31]; _h.label = 18; case 18: _h.trys.push([18, 25, 26, 31]); hets_1 = (e_2 = void 0, tslib_1.__asyncValues(hets)); _h.label = 19; case 19: return [4 /*yield*/, hets_1.next()]; case 20: if (!(hets_1_1 = _h.sent(), !hets_1_1.done)) return [3 /*break*/, 24]; het = hets_1_1.value; ligand = builder_1.MolScriptBuilder.struct.generator.atomGroups({ 'chain-test': builder_1.MolScriptBuilder.core.rel.eq([builder_1.MolScriptBuilder.struct.atomProperty.macromolecular.auth_asym_id(), s.auth_asym_id]), 'residue-test': builder_1.MolScriptBuilder.core.rel.eq([builder_1.MolScriptBuilder.struct.atomProperty.macromolecular.label_comp_id(), het]), 'group-by': builder_1.MolScriptBuilder.core.str.concat([builder_1.MolScriptBuilder.struct.atomProperty.core.operatorName(), builder_1.MolScriptBuilder.struct.atomProperty.macromolecular.residueKey()]) }); labelTagParams = { label: "".concat(het), tags: ["superposition-ligand-sel"] }; hetColor = color_1.Color.fromRgb(253, 3, 253); if (superpositionParams && superpositionParams.ligandColor) { _b = superpositionParams.ligandColor, r = _b.r, g = _b.g, b = _b.b; hetColor = color_1.Color.fromRgb(r, g, b); } return [4 /*yield*/, plugin.builders.structure.tryCreateComponentFromExpression(strInstance, ligand, "".concat(het, "-").concat(segmentIndex), labelTagParams)]; case 21: ligandExp = _h.sent(); if (!ligandExp) return [3 /*break*/, 23]; return [4 /*yield*/, plugin.builders.structure.representation.addRepresentation(ligandExp, { type: 'ball-and-stick', color: 'uniform', colorParams: { value: hetColor } }, { tag: "superposition-ligand-visual" })]; case 22: _h.sent(); spState.refMaps[ligandExp.ref] = "".concat(s.pdb_id, "_").concat(s.struct_asym_id); invalidStruct = false; _h.label = 23; case 23: return [3 /*break*/, 19]; case 24: return [3 /*break*/, 31]; case 25: e_2_1 = _h.sent(); e_2 = { error: e_2_1 }; return [3 /*break*/, 31]; case 26: _h.trys.push([26, , 29, 30]); if (!(hets_1_1 && !hets_1_1.done && (_g = hets_1.return))) return [3 /*break*/, 28]; return [4 /*yield*/, _g.call(hets_1)]; case 27: _h.sent(); _h.label = 28; case 28: return [3 /*break*/, 30]; case 29: if (e_2) throw e_2.error; return [7 /*endfinally*/]; case 30: return [7 /*endfinally*/]; case 31: carbEntityCount = hetInfo ? hetInfo.carbEntityCount : 0; if (!(carbEntityCount > 0)) return [3 /*break*/, 38]; return [4 /*yield*/, getCarbPolymerDetailsFromApi(plugin, s.pdb_id)]; case 32: allCarbPolymers = _h.sent(); polymerChainWithSurroundings = builder_1.MolScriptBuilder.struct.modifier.includeSurroundings({ 0: builder_1.MolScriptBuilder.struct.generator.atomGroups({ 'entity-test': builder_1.MolScriptBuilder.core.rel.eq([builder_1.MolScriptBuilder.ammp('entityType'), 'polymer']), 'chain-test': builder_1.MolScriptBuilder.core.rel.eq([builder_1.MolScriptBuilder.struct.atomProperty.macromolecular.auth_asym_id(), s.auth_asym_id]), 'group-by': builder_1.MolScriptBuilder.core.str.concat([builder_1.MolScriptBuilder.struct.atomProperty.core.operatorName(), builder_1.MolScriptBuilder.struct.atomProperty.macromolecular.residueKey()]) }), radius: 5, 'as-whole-residues': true }); i = 0; _i = 0, _c = allCarbPolymers.branchedChains; _h.label = 33; case 33: if (!(_i < _c.length)) return [3 /*break*/, 38]; carbEntityChainId = _c[_i]; carbEntityChain = builder_1.MolScriptBuilder.struct.generator.atomGroups({ 'entity-test': builder_1.MolScriptBuilder.core.rel.eq([builder_1.MolScriptBuilder.ammp('entityType'), 'branched']), 'chain-test': builder_1.MolScriptBuilder.core.rel.eq([builder_1.MolScriptBuilder.struct.atomProperty.macromolecular.auth_asym_id(), carbEntityChainId]), 'group-by': builder_1.MolScriptBuilder.core.str.concat([builder_1.MolScriptBuilder.struct.atomProperty.core.operatorName(), builder_1.MolScriptBuilder.struct.atomProperty.macromolecular.residueKey()]) }); carbEntityChainInVicinity = builder_1.MolScriptBuilder.struct.filter.intersectedBy({ 0: polymerChainWithSurroundings, by: carbEntityChain }); data = (plugin.state.data.select(strInstance.ref)[0].obj).data; carbChainSel = script_1.Script.getStructureSelection(carbEntityChainInVicinity, data); if (!(carbChainSel && carbChainSel.kind === 'sequence')) return [3 /*break*/, 36]; carbLigands = []; carbLigNamesAndCount = {}; carbLigList = []; for (_d = 0, _e = allCarbPolymers.branchedLigands[i]; _d < _e.length; _d++) { carbLigs = _e[_d]; ligResDetails = carbLigs.split('-'); carbLigands.push(builder_1.MolScriptBuilder.core.rel.eq([builder_1.MolScriptBuilder.struct.atomProperty.macromolecular.auth_seq_id(), +ligResDetails[1]])); if (carbLigNamesAndCount[ligResDetails[0]]) { carbLigNamesAndCount[ligResDetails[0]]++; } else { carbLigNamesAndCount[ligResDetails[0]] = 1; } carbLigList.push(ligResDetails[0]); } carbVisLabel = createCarbVisLabel(carbLigNamesAndCount); branchedEntity = builder_1.MolScriptBuilder.struct.generator.atomGroups({ 'entity-test': builder_1.MolScriptBuilder.core.rel.eq([builder_1.MolScriptBuilder.ammp('entityType'), 'branched']), 'group-by': builder_1.MolScriptBuilder.core.str.concat([builder_1.MolScriptBuilder.struct.atomProperty.core.operatorName(), builder_1.MolScriptBuilder.struct.atomProperty.macromolecular.residueKey()]), 'chain-test': builder_1.MolScriptBuilder.core.rel.eq([builder_1.MolScriptBuilder.struct.atomProperty.macromolecular.auth_asym_id(), carbEntityChainId]), 'residue-test': builder_1.MolScriptBuilder.core.logic.or(carbLigands) }); labelTagParams = { label: "".concat(carbVisLabel), tags: ["superposition-carb-sel"] }; return [4 /*yield*/, plugin.builders.structure.tryCreateComponentFromExpression(strInstance, branchedEntity, "".concat(carbLigList.join('-'), "-").concat(segmentIndex), labelTagParams)]; case 34: ligandExp = _h.sent(); if (!ligandExp) return [3 /*break*/, 36]; return [4 /*yield*/, plugin.builders.structure.representation.addRepresentation(ligandExp, { type: 'carbohydrate' }, { tag: "superposition-carb-visual" })]; case 35: _h.sent(); spState.refMaps[ligandExp.ref] = "".concat(s.pdb_id, "_").concat(s.struct_asym_id); invalidStruct = false; _h.label = 36; case 36: i++; _h.label = 37; case 37: _i++; return [3 /*break*/, 33]; case 38: if (invalidStruct) { spState.invalidStruct.push("".concat(s.pdb_id, "_").concat(s.struct_asym_id)); loadedStructIndex = spState.loadedStructs[segmentIndex].indexOf("".concat(s.pdb_id, "_").concat(s.struct_asym_id)); if (loadedStructIndex > -1) spState.loadedStructs[segmentIndex].splice(loadedStructIndex, 1); // remove downloaded data if (clearOnFail) { // const m = plugin.state.data.select(modelRef)[0]; // const t = plugin.state.data.select(m.transform.parent)[0]; // const d = plugin.state.data.select(t.transform.parent)[0]; // PluginCommands.State.RemoveObject(plugin, { state: d.parent!, ref: d.transform.parent, removeParentGhosts: true }); } } else { // if(interactingHets.length > 0) spState.hets[`${s.pdb_id}_${s.struct_asym_id}`] = interactingHets; } _h.label = 39; case 39: return [3 /*break*/, 2]; case 40: return [3 /*break*/, 47]; case 41: e_3_1 = _h.sent(); e_3 = { error: e_3_1 }; return [3 /*break*/, 47]; case 42: _h.trys.push([42, , 45, 46]); if (!(entryList_1_1 && !entryList_1_1.done && (_f = entryList_1.return))) return [3 /*break*/, 44]; return [4 /*yield*/, _f.call(entryList_1)]; case 43: _h.sent(); _h.label = 44; case 44: return [3 /*break*/, 46]; case 45: if (e_3) throw e_3.error; return [7 /*endfinally*/]; case 46: return [7 /*endfinally*/]; case 47: if (busyFlagOn) { busyFlagOn = false; customState.events.isBusy.next(false); } return [2 /*return*/]; } }); }); })]; }); }); } exports.renderSuperposition = renderSuperposition; function getLigandNamesFromModelData(plugin, state, modelRef) { var _a; return tslib_1.__awaiter(this, void 0, void 0, function () { var cell, model, structures, _i, _b, s, structure, info; return tslib_1.__generator(this, function (_c) { switch (_c.label) { case 0: cell = state.select(modelRef)[0]; if (!cell || !cell.obj) return [2 /*return*/, void 0]; model = cell.obj.data; if (!model) return [2 /*return*/]; structures = []; for (_i = 0, _b = plugin.managers.structure.hierarchy.selection.structures; _i < _b.length; _i++) { s = _b[_i]; structure = (_a = s.cell.obj) === null || _a === void 0 ? void 0 : _a.data; if (structure) structures.push(structure); } return [4 /*yield*/, helpers_1.ModelInfo.get(model, structures)]; case 1: info = _c.sent(); return [2 /*return*/, info]; } }); }); } function loadStructure(plugin, url, format, isBinary) { return tslib_1.__awaiter(this, void 0, void 0, function () { var data, trajectory, model, modelProperties, structure, e_4; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 7, , 8]); return [4 /*yield*/, plugin.builders.data.download({ url: assets_1.Asset.Url(url), isBinary: isBinary })]; case 1: data = _a.sent(); return [4 /*yield*/, plugin.builders.structure.parseTrajectory(data, format)]; case 2: trajectory = _a.sent(); return [4 /*yield*/, plugin.builders.structure.createModel(trajectory)]; case 3: model = _a.sent(); return [4 /*yield*/, plugin.builders.structure.insertModelProperties(model)]; case 4: modelProperties = _a.sent(); return [4 /*yield*/, plugin.builders.structure.createStructure(modelProperties || model, { name: 'model', params: {} })]; case 5: structure = _a.sent(); return [4 /*yield*/, plugin.builders.structure.insertStructureProperties(structure)]; case 6: _a.sent(); return [2 /*return*/, { data: data, trajectory: trajectory, model: model, structure: structure }]; case 7: e_4 = _a.sent(); return [2 /*return*/, { structure: void 0 }]; case 8: return [2 /*return*/]; } }); }); } function chainSelection(struct_asym_id) { return builder_1.MolScriptBuilder.struct.generator.atomGroups({ 'chain-test': builder_1.MolScriptBuilder.core.rel.eq([builder_1.MolScriptBuilder.struct.atomProperty.macromolecular.label_asym_id(), struct_asym_id]) }); } function transform(plugin, s, matrix) { var b = plugin.state.data.build().to(s) .insert(transforms_1.StateTransforms.Model.TransformStructureConformation, { transform: { name: 'matrix', params: { data: matrix, transpose: false } } }); return plugin.runTask(plugin.state.data.updateTree(b)); } function afTransform(plugin, s, matrix, coordinateSystem) { return tslib_1.__awaiter(this, void 0, void 0, function () { var r, o, transform, params, b; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: r = mol_state_1.StateObjectRef.resolveAndCheck(plugin.state.data, s); if (!r) return [2 /*return*/]; o = plugin.state.data.selectQ(function (q) { return q.byRef(r.transform.ref).subtree().withTransformer(transforms_1.StateTransforms.Model.TransformStructureConformation); })[0]; transform = coordinateSystem && !linear_algebra_1.Mat4.isIdentity(coordinateSystem.matrix) ? linear_algebra_1.Mat4.mul((0, linear_algebra_1.Mat4)(), coordinateSystem.matrix, matrix) : matrix; params = { transform: { name: 'matrix', params: { data: transform, transpose: false } } }; b = o ? plugin.state.data.build().to(o).update(params) : plugin.state.data.build().to(s) .insert(transforms_1.StateTransforms.Model.TransformStructureConformation, params, { tags: 'SuperpositionTransform' }); return [4 /*yield*/, plugin.runTask(plugin.state.data.updateTree(b))]; case 1: _a.sent(); return [2 /*return*/]; } }); }); } function getMatrixData(plugin) { return tslib_1.__awaiter(this, void 0, void 0, function () { var customState, matrixAccession, clusterRecUrlStr, assetManager, clusterRecUrl, clusterRecData, e_5; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: customState = plugin.customState; matrixAccession = customState.initParams.superpositionParams.matrixAccession ? customState.initParams.superpositionParams.matrixAccession : customState.initParams.moleculeId; clusterRecUrlStr = "".concat(customState.initParams.pdbeUrl, "static/superpose/matrices/").concat(matrixAccession); assetManager = plugin.managers.asset; clusterRecUrl = assets_1.Asset.getUrlAsset(assetManager, clusterRecUrlStr); _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); return [4 /*yield*/, plugin.runTask(assetManager.resolve(clusterRecUrl, 'json', false))]; case 2: clusterRecData = _a.sent(); if (clusterRecData && clusterRecData.data) { plugin.customState.superpositionState.matrixData = clusterRecData.data; } return [3 /*break*/, 4]; case 3: e_5 = _a.sent(); customState['superpositionError'] = "Matrix data not available for ".concat(matrixAccession); plugin.customState.events.superpositionInit.next(true); // Emit segment API data load event return [3 /*break*/, 4]; case 4: return [2 /*return*/]; } }); }); } function getSegmentData(plugin) { return tslib_1.__awaiter(this, void 0, void 0, function () { var customState, segmentsUrl, assetManager, url, result, e_6; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: customState = plugin.customState; segmentsUrl = "".concat(customState.initParams.pdbeUrl, "graph-api/uniprot/superposition/").concat(customState.initParams.moleculeId); assetManager = plugin.managers.asset; url = assets_1.Asset.getUrlAsset(assetManager, segmentsUrl); _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); return [4 /*yield*/, plugin.runTask(assetManager.resolve(url, 'json', false))]; case 2: result = _a.sent(); if (result && result.data) { customState.superpositionState.segmentData = result.data[customState.initParams.moleculeId]; } return [3 /*break*/, 4]; case 3: e_6 = _a.sent(); customState['superpositionError'] = "Superposition data not available for ".concat(customState.initParams.moleculeId); plugin.customState.events.superpositionInit.next(true); // Emit segment API data load event return [3 /*break*/, 4]; case 4: return [2 /*return*/]; } }); }); } function getChainLigands(carbEntity) { var ligandChain = []; var ligandLabels = []; var ligands = []; var labelValueArr = []; var ligNameStr = ''; for (var _i = 0, _a = carbEntity.chem_comp_list; _i < _a.length; _i++) { var chemComp = _a[_i]; labelValueArr.push("".concat(chemComp.chem_comp_id, " (").concat(chemComp.count, ")")); } ligNameStr = labelValueArr.join(', '); for (var _b = 0, _c = carbEntity.chains; _b < _c.length; _b++) { var chain = _c[_b]; ligandChain.push(chain.chain_id); ligandLabels.push(ligNameStr); var chainLigands = []; for (var _d = 0, _e = chain.residues; _d < _e.length; _d++) { var residue = _e[_d]; chainLigands.push(residue.chem_comp_id + '-' + residue.residue_number); } ligands.push(chainLigands); } return { ligands: ligands, ligandChain: ligandChain, ligandLabels: ligandLabels }; } function getCarbPolymerDetailsFromApi(plugin, pdb_id) { return tslib_1.__awaiter(this, void 0, void 0, function () { var customState, apiUrl, assetManager, url, branchedLigands, branchedChains, branchedlabels, result, carbEntities, _i, carbEntities_1, carbEntity, carbLigData, e_7; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: customState = plugin.customState; apiUrl = "".concat(customState.initParams.pdbeUrl, "api/pdb/entry/carbohydrate_polymer/").concat(pdb_id); assetManager = plugin.managers.asset; url = assets_1.Asset.getUrlAsset(assetManager, apiUrl); branchedLigands = []; branchedChains = []; branchedlabels = []; _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); return [4 /*yield*/, plugin.runTask(assetManager.resolve(url, 'json', false))]; case 2: result = _a.sent(); if (result && result.data) { carbEntities = result.data[pdb_id]; for (_i = 0, carbEntities_1 = carbEntities; _i < carbEntities_1.length; _i++) { carbEntity = carbEntities_1[_i]; carbLigData = getChainLigands(carbEntity); branchedLigands = branchedLig