molstar
Version:
A comprehensive macromolecular library.
113 lines (112 loc) • 4.89 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 argparse from 'argparse';
import * as util from 'util';
import { Volume } from '../../mol-model/volume';
import { downloadCif } from './helpers';
import { CIF } from '../../mol-io/reader/cif';
import { Table } from '../../mol-data/db';
import { StringBuilder } from '../../mol-util';
import { Task } from '../../mol-task';
import { createVolumeIsosurfaceMesh } from '../../mol-repr/volume/isosurface';
import { Theme } from '../../mol-theme/theme';
import { volumeFromDensityServerData, DscifFormat } from '../../mol-model-formats/volume/density-server';
require('util.promisify').shim();
var writeFileAsync = util.promisify(fs.writeFile);
function getVolume(url) {
return __awaiter(this, void 0, void 0, function () {
var cif, data;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, downloadCif(url, true)];
case 1:
cif = _a.sent();
data = CIF.schema.densityServer(cif.blocks[1]);
return [4 /*yield*/, volumeFromDensityServerData(data).run()];
case 2: return [2 /*return*/, _a.sent()];
}
});
});
}
function print(volume) {
if (!DscifFormat.is(volume.sourceData))
return;
var volume_data_3d_info = volume.sourceData.data.volume_data_3d_info;
var row = Table.getRow(volume_data_3d_info, 0);
console.log(row);
console.log(volume.grid.transform);
console.log(volume.grid.stats);
}
function doMesh(volume, filename) {
return __awaiter(this, void 0, void 0, function () {
var mesh, vertexCount, triangleCount, vs, ts, obj, i, i;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, Task.create('', function (runtime) { return createVolumeIsosurfaceMesh({ runtime: runtime }, volume, Theme.createEmpty(), { isoValue: Volume.IsoValue.absolute(1.5) }); }).run()];
case 1:
mesh = _a.sent();
console.log({ vc: mesh.vertexCount, tc: mesh.triangleCount });
vertexCount = mesh.vertexCount, triangleCount = mesh.triangleCount;
vs = mesh.vertexBuffer.ref.value;
ts = mesh.indexBuffer.ref.value;
obj = StringBuilder.create();
for (i = 0; i < vertexCount; i++) {
StringBuilder.write(obj, 'v ');
StringBuilder.writeFloat(obj, vs[3 * i + 0], 100);
StringBuilder.whitespace1(obj);
StringBuilder.writeFloat(obj, vs[3 * i + 1], 100);
StringBuilder.whitespace1(obj);
StringBuilder.writeFloat(obj, vs[3 * i + 2], 100);
StringBuilder.newline(obj);
}
for (i = 0; i < triangleCount; i++) {
StringBuilder.write(obj, 'f ');
StringBuilder.writeIntegerAndSpace(obj, ts[3 * i + 0] + 1);
StringBuilder.writeIntegerAndSpace(obj, ts[3 * i + 1] + 1);
StringBuilder.writeInteger(obj, ts[3 * i + 2] + 1);
StringBuilder.newline(obj);
}
return [4 /*yield*/, writeFileAsync(filename, StringBuilder.getString(obj))];
case 2:
_a.sent();
return [2 /*return*/];
}
});
});
}
function run(url, meshFilename) {
return __awaiter(this, void 0, void 0, function () {
var volume;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, getVolume(url)];
case 1:
volume = _a.sent();
print(volume);
return [4 /*yield*/, doMesh(volume, meshFilename)];
case 2:
_a.sent();
return [2 /*return*/];
}
});
});
}
var parser = new argparse.ArgumentParser({
add_help: true,
description: 'Info about VolumeData from mol-model module'
});
parser.add_argument('--emdb', '-e', {
help: 'EMDB id, for example 8116',
});
parser.add_argument('--mesh', {
help: 'Mesh filename',
required: true
});
var args = parser.parse_args();
run("https://ds.litemol.org/em/emd-".concat(args.emdb, "/cell?detail=4"), args.mesh);