molstar
Version:
A comprehensive macromolecular library.
141 lines (140 loc) • 5.39 kB
JavaScript
/**
* Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* Taken/adapted from DensityServer (https://github.com/dsehnal/DensityServer)
*
* @author David Sehnal <david.sehnal@gmail.com>
*/
import { __awaiter, __generator } from "tslib";
import * as Api from './api';
import * as Coordinate from './algebra/coordinate';
import * as fs from 'fs';
import * as path from 'path';
export function run(jobs) {
return __awaiter(this, void 0, void 0, function () {
var progress, started, _i, jobs_1, job, e_1, elapsed;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
progress = 0;
started = getTime();
_i = 0, jobs_1 = jobs;
_a.label = 1;
case 1:
if (!(_i < jobs_1.length)) return [3 /*break*/, 7];
job = jobs_1[_i];
_a.label = 2;
case 2:
_a.trys.push([2, 4, , 5]);
return [4 /*yield*/, query(job)];
case 3:
_a.sent();
return [3 /*break*/, 5];
case 4:
e_1 = _a.sent();
console.error(e_1);
return [3 /*break*/, 5];
case 5:
progress++;
elapsed = (getTime() - started) / 1000;
console.log("[Progress] ".concat(progress, "/").concat(jobs.length, " in ").concat(elapsed.toFixed(2), "s"));
_a.label = 6;
case 6:
_i++;
return [3 /*break*/, 1];
case 7: return [2 /*return*/];
}
});
});
}
function getTime() {
var t = process.hrtime();
return t[0] * 1000 + t[1] / 1000000;
}
function query(job) {
var _a;
return __awaiter(this, void 0, void 0, function () {
var box, params, filename, res;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
if (job.query.kind.toLocaleLowerCase() === 'cell') {
box = { kind: 'Cell' };
}
else if (job.query.space === 'fractional') {
box = {
kind: 'Fractional',
a: Coordinate.fractional(job.query.bottomLeft[0], job.query.bottomLeft[1], job.query.bottomLeft[2]),
b: Coordinate.fractional(job.query.topRight[0], job.query.topRight[1], job.query.topRight[2]),
};
}
else {
box = {
kind: 'Cartesian',
a: Coordinate.cartesian(job.query.bottomLeft[0], job.query.bottomLeft[1], job.query.bottomLeft[2]),
b: Coordinate.cartesian(job.query.topRight[0], job.query.topRight[1], job.query.topRight[2]),
};
}
params = {
sourceFilename: job.source.filename,
sourceId: job.source.id,
asBinary: job.params.asBinary,
box: box,
detail: !job.params.detail ? 0 : job.params.detail,
forcedSamplingLevel: job.params.forcedSamplingLevel
};
if (!fs.existsSync(job.outputFolder)) {
makeDir(job.outputFolder);
}
filename = path.join(job.outputFolder, (_a = job.outputFilename) !== null && _a !== void 0 ? _a : Api.getOutputFilename(job.source.name, job.source.id, params));
res = function () { return wrapFile(filename); };
return [4 /*yield*/, Api.queryBox(params, res)];
case 1:
_b.sent();
return [2 /*return*/];
}
});
});
}
function makeDir(path, root) {
var dirs = path.split(/\/|\\/g), dir = dirs.shift();
root = (root || '') + dir + '/';
try {
fs.mkdirSync(root);
}
catch (e) {
if (!fs.statSync(root).isDirectory())
throw new Error(e);
}
return !dirs.length || makeDir(dirs.join('/'), root);
}
function wrapFile(fn) {
var w = {
open: function () {
if (this.opened)
return;
this.file = fs.openSync(fn, 'w');
this.opened = true;
},
writeBinary: function (data) {
this.open();
fs.writeSync(this.file, Buffer.from(data));
return true;
},
writeString: function (data) {
this.open();
fs.writeSync(this.file, data);
return true;
},
end: function () {
if (!this.opened || this.ended)
return;
fs.close(this.file, function () { });
this.ended = true;
},
file: 0,
ended: false,
opened: false
};
return w;
}