molstar
Version:
A comprehensive macromolecular library.
120 lines • 5.92 kB
JavaScript
/**
* Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author David Sehnal <david.sehnal@gmail.com>
*/
import { __awaiter, __generator } from "tslib";
import * as fs from 'fs';
import * as path from 'path';
import { StructureQualityReportProvider, StructureQualityReport } from '../../../../extensions/pdbe/structure-quality-report/prop';
import { fetchRetry } from '../../utils/fetch-retry';
import { UUID } from '../../../../mol-util';
import { PDBePreferredAssembly } from '../../../../extensions/pdbe/preferred-assembly';
import { PDBeStructRefDomain } from '../../../../extensions/pdbe/struct-ref-domain';
import { ConsoleLogger } from '../../../../mol-util/console-logger';
import { getParam } from '../../../common/util';
export var PDBe_structureQualityReport = function (_a) {
var model = _a.model, params = _a.params, cache = _a.cache;
return __awaiter(void 0, void 0, void 0, function () {
var PDBe_apiSourceJson, data, _b, _c, _d;
return __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', StructureQualityReport.DefaultServerUrl), cache);
_c = (_b = StructureQualityReport).fromJson;
_d = [model];
return [4 /*yield*/, PDBe_apiSourceJson(model)];
case 1:
data = _c.apply(_b, _d.concat([_e.sent()]));
return [2 /*return*/, StructureQualityReportProvider.set(model, { serverUrl: StructureQualityReport.DefaultServerUrl }, data)];
}
});
});
};
export 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 PDBePreferredAssembly.attachFromCifOrApi(model, { PDBe_apiSourceJson: PDBe_apiSourceJson });
};
export 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 PDBeStructRefDomain.attachFromCifOrApi(model, { PDBe_apiSourceJson: PDBe_apiSourceJson });
};
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 __awaiter(_this, void 0, void 0, function () {
var key, fn;
return __generator(this, function (_a) {
key = "" + model.entryId[1] + model.entryId[2];
if (!json.has(key)) {
fn = path.join(pathPrefix, 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 = 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 = getParam(params, 'PDBe', 'File', name);
if (!ret)
throw new Error("PDBe file prefix '" + name + "' not set!");
return ret;
}
function useFileSource(params) {
return !!getParam(params, 'PDBe', 'UseFileSource');
}
function apiQueryProvider(urlPrefix, cache) {
var _this = this;
var cacheKey = UUID.create22();
return function (model) { return __awaiter(_this, void 0, void 0, function () {
var rawData, json, e_1;
return __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*/, fetchRetry(urlPrefix + "/" + 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
ConsoleLogger.warn('Props', "Could not retrieve prop @" + (urlPrefix + "/" + model.entryId.toLowerCase()));
return [2 /*return*/, {}];
case 4: return [2 /*return*/];
}
});
}); };
}
//# sourceMappingURL=pdbe.js.map