UNPKG

molstar

Version:

A comprehensive macromolecular library.

126 lines (125 loc) 6.48 kB
"use strict"; /** * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info. * * @author David Sehnal <david.sehnal@gmail.com> */ Object.defineProperty(exports, "__esModule", { value: true }); exports.PDBe_structRefDomain = exports.PDBe_preferredAssembly = exports.PDBe_structureQualityReport = void 0; var tslib_1 = require("tslib"); var fs = tslib_1.__importStar(require("fs")); var path = tslib_1.__importStar(require("path")); var prop_1 = require("../../../../extensions/pdbe/structure-quality-report/prop"); var fetch_retry_1 = require("../../utils/fetch-retry"); var mol_util_1 = require("../../../../mol-util"); var preferred_assembly_1 = require("../../../../extensions/pdbe/preferred-assembly"); var struct_ref_domain_1 = require("../../../../extensions/pdbe/struct-ref-domain"); var console_logger_1 = require("../../../../mol-util/console-logger"); var util_1 = require("../../../common/util"); var PDBe_structureQualityReport = function (_a) { var model = _a.model, params = _a.params, cache = _a.cache; return tslib_1.__awaiter(void 0, void 0, void 0, function () { var PDBe_apiSourceJson, data, _b, _c, _d; return tslib_1.__generator(this, function (_e) { switch (_e.label) { case 0: PDBe_apiSourceJson = useFileSource(params) ? residuewise_outlier_summary.getDataFromAggregateFile(getFilePrefix(params, 'residuewise_outlier_summary')) : apiQueryProvider(getApiUrl(params, 'residuewise_outlier_summary', prop_1.StructureQualityReport.DefaultServerUrl), cache); _c = (_b = prop_1.StructureQualityReport).fromJson; _d = [model]; return [4 /*yield*/, PDBe_apiSourceJson(model)]; case 1: data = _c.apply(_b, _d.concat([_e.sent()])); return [2 /*return*/, prop_1.StructureQualityReportProvider.set(model, { serverUrl: prop_1.StructureQualityReport.DefaultServerUrl }, data)]; } }); }); }; exports.PDBe_structureQualityReport = PDBe_structureQualityReport; var PDBe_preferredAssembly = function (_a) { var model = _a.model, params = _a.params, cache = _a.cache; var PDBe_apiSourceJson = apiQueryProvider(getApiUrl(params, 'preferred_assembly', 'https://www.ebi.ac.uk/pdbe/api/pdb/entry/summary'), cache); return preferred_assembly_1.PDBePreferredAssembly.attachFromCifOrApi(model, { PDBe_apiSourceJson: PDBe_apiSourceJson }); }; exports.PDBe_preferredAssembly = PDBe_preferredAssembly; var PDBe_structRefDomain = function (_a) { var model = _a.model, params = _a.params, cache = _a.cache; var PDBe_apiSourceJson = apiQueryProvider(getApiUrl(params, 'struct_ref_domain', 'https://www.ebi.ac.uk/pdbe/api/mappings/sequence_domains'), cache); return struct_ref_domain_1.PDBeStructRefDomain.attachFromCifOrApi(model, { PDBe_apiSourceJson: PDBe_apiSourceJson }); }; exports.PDBe_structRefDomain = PDBe_structRefDomain; var residuewise_outlier_summary; (function (residuewise_outlier_summary) { var json = new Map(); function getDataFromAggregateFile(pathPrefix) { var _this = this; // This is for "testing" purposes and should probably only read // a single file with the appropriate prop in the "production" version. return function (model) { return tslib_1.__awaiter(_this, void 0, void 0, function () { var key, fn; return tslib_1.__generator(this, function (_a) { key = "".concat(model.entryId[1]).concat(model.entryId[2]); if (!json.has(key)) { fn = path.join(pathPrefix, "".concat(key, ".json")); if (!fs.existsSync(fn)) json.set(key, {}); // TODO: use async readFile? else json.set(key, JSON.parse(fs.readFileSync(fn, 'utf8'))); } return [2 /*return*/, json.get(key)[model.entryId.toLowerCase()] || {}]; }); }); }; } residuewise_outlier_summary.getDataFromAggregateFile = getDataFromAggregateFile; })(residuewise_outlier_summary || (residuewise_outlier_summary = {})); function getApiUrl(params, name, fallback) { var url = (0, util_1.getParam)(params, 'PDBe', 'API', name); if (!url) return fallback; if (url[url.length - 1] === '/') return url.substring(0, url.length - 1); return url; } function getFilePrefix(params, name) { var ret = (0, util_1.getParam)(params, 'PDBe', 'File', name); if (!ret) throw new Error("PDBe file prefix '".concat(name, "' not set!")); return ret; } function useFileSource(params) { return !!(0, util_1.getParam)(params, 'PDBe', 'UseFileSource'); } function apiQueryProvider(urlPrefix, cache) { var _this = this; var cacheKey = mol_util_1.UUID.create22(); return function (model) { return tslib_1.__awaiter(_this, void 0, void 0, function () { var rawData, json, e_1; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 3, , 4]); if (cache[cacheKey]) return [2 /*return*/, cache[cacheKey]]; return [4 /*yield*/, (0, fetch_retry_1.fetchRetry)("".concat(urlPrefix, "/").concat(model.entryId.toLowerCase()), 1500, 5)]; case 1: rawData = _a.sent(); // TODO: is this ok? if (rawData.status !== 200) return [2 /*return*/, {}]; return [4 /*yield*/, rawData.json()]; case 2: json = (_a.sent())[model.entryId.toLowerCase()] || {}; cache[cacheKey] = json; return [2 /*return*/, json]; case 3: e_1 = _a.sent(); // TODO: handle better console_logger_1.ConsoleLogger.warn('Props', "Could not retrieve prop @".concat("".concat(urlPrefix, "/").concat(model.entryId.toLowerCase()))); return [2 /*return*/, {}]; case 4: return [2 /*return*/]; } }); }); }; }