UNPKG

molstar

Version:

A comprehensive macromolecular library.

158 lines (157 loc) 7.28 kB
"use strict"; /** * Copyright (c) 2024 mol* contributors, licensed under MIT, See LICENSE file for more info. * * @author David Sehnal <david.sehnal@gmail.com> * @author Alexander Rose <alexander.rose@weirdbyte.de> * @author Ludovic Autin <ludovic.autin@gmail.com> */ Object.defineProperty(exports, "__esModule", { value: true }); exports.LammpsTrajectoryFormat = void 0; exports.coordinatesFromLammpsTrajectory = coordinatesFromLammpsTrajectory; exports.trajectoryFromLammpsTrajectory = trajectoryFromLammpsTrajectory; const coordinates_1 = require("../../mol-model/structure/coordinates"); const schema_1 = require("../../mol-io/reader/lammps/schema"); const model_1 = require("../../mol-model/structure/model"); const mol_task_1 = require("../../mol-task"); const db_1 = require("../../mol-data/db"); const schema_2 = require("./basic/schema"); const component_1 = require("./common/component"); const entity_1 = require("./common/entity"); const parser_1 = require("./basic/parser"); const types_1 = require("../../mol-model/structure/model/types"); function coordinatesFromLammpsTrajectory(file, unitsStyle = 'real') { return mol_task_1.Task.create('Parse Lammps Trajectory', async (ctx) => { await ctx.update('Converting to coordinates'); const scale = schema_1.lammpsUnitStyles[unitsStyle].scale; const deltaTime = (0, coordinates_1.Time)(file.deltaTime, 'step'); const offsetTime = (0, coordinates_1.Time)(file.timeOffset, deltaTime.unit); const offset_pos = { x: 0.0, y: 0.0, z: 0.0 }; const offset_scale = { x: 1.0, y: 1.0, z: 1.0 }; const atomsMode = file.frames[0].atomMode; const isScaled = atomsMode.includes('s'); const frames = []; for (let i = 0, il = file.frames.length; i < il; ++i) { const box = file.bounds[i]; if (isScaled) { offset_scale.x = box.length[0]; offset_scale.y = box.length[1]; offset_scale.z = box.length[2]; offset_pos.x = box.lower[0]; offset_pos.y = box.lower[1]; offset_pos.z = box.lower[2]; } const count = file.frames[i].count; const cx = new Float32Array(count); const cy = new Float32Array(count); const cz = new Float32Array(count); let offset = 0; for (let j = 0; j < count; j++) { cx[offset] = (file.frames[i].x.value(j) * offset_scale.x + offset_pos.x) * scale; cy[offset] = (file.frames[i].y.value(j) * offset_scale.y + offset_pos.y) * scale; cz[offset] = (file.frames[i].z.value(j) * offset_scale.z + offset_pos.z) * scale; offset++; } frames.push({ elementCount: file.frames[i].count, x: cx, y: cy, z: cz, xyzOrdering: { isIdentity: true }, time: (0, coordinates_1.Time)(offsetTime.value + deltaTime.value * i, deltaTime.unit) }); } return coordinates_1.Coordinates.create(frames, deltaTime, offsetTime); }); } async function getModels(mol, ctx, unitsStyle = 'real') { const atoms = mol.frames[0]; const count = atoms.count; const atomsMode = atoms.atomMode; const box = mol.bounds[0]; const offset_pos = { x: 0.0, y: 0.0, z: 0.0 }; const offset_scale = { x: 1.0, y: 1.0, z: 1.0 }; const scale = schema_1.lammpsUnitStyles[unitsStyle].scale; // if caracter s in atomsMode, we need to scale the coordinates if (atomsMode.includes('s')) { offset_scale.x = box.length[0]; offset_scale.y = box.length[1]; offset_scale.z = box.length[2]; offset_pos.x = box.lower[0]; offset_pos.y = box.lower[1]; offset_pos.z = box.lower[2]; } const type_symbols = new Array(count); const id = new Int32Array(count); const cx = new Float32Array(count); const cy = new Float32Array(count); const cz = new Float32Array(count); const model_num = new Int32Array(count); let offset = 0; for (let j = 0; j < count; j++) { type_symbols[offset] = atoms.atomType.value(j).toString(); cx[offset] = (atoms.x.value(j) * offset_scale.x + offset_pos.x) * scale; cy[offset] = (atoms.y.value(j) * offset_scale.y + offset_pos.y) * scale; cz[offset] = (atoms.z.value(j) * offset_scale.z + offset_pos.z) * scale; id[offset] = atoms.atomId.value(j); model_num[offset] = 0; offset++; } const MOL = db_1.Column.ofConst('MOL', count, db_1.Column.Schema.str); const asym_id = db_1.Column.ofLambda({ value: (row) => atoms.moleculeId.value(row).toString(), rowCount: count, schema: db_1.Column.Schema.str, }); const seq_id = db_1.Column.ofConst(1, count, db_1.Column.Schema.int); const type_symbol = db_1.Column.ofStringArray(type_symbols); const atom_site = db_1.Table.ofPartialColumns(schema_2.BasicSchema.atom_site, { auth_asym_id: asym_id, auth_atom_id: type_symbol, auth_comp_id: MOL, auth_seq_id: seq_id, Cartn_x: db_1.Column.ofFloatArray(cx), Cartn_y: db_1.Column.ofFloatArray(cy), Cartn_z: db_1.Column.ofFloatArray(cz), id: db_1.Column.ofIntArray(id), label_asym_id: asym_id, label_atom_id: type_symbol, label_comp_id: MOL, label_seq_id: seq_id, label_entity_id: db_1.Column.ofConst('1', count, db_1.Column.Schema.str), occupancy: db_1.Column.ofConst(1, count, db_1.Column.Schema.float), type_symbol, pdbx_PDB_model_num: db_1.Column.ofIntArray(model_num), }, count); const entityBuilder = new entity_1.EntityBuilder(); entityBuilder.setNames([['MOL', 'Unknown Entity']]); entityBuilder.getEntityId('MOL', types_1.MoleculeType.Unknown, 'A'); const componentBuilder = new component_1.ComponentBuilder(seq_id, type_symbol); componentBuilder.setNames([['MOL', 'Unknown Molecule']]); componentBuilder.add('MOL', 0); const basic = (0, schema_2.createBasic)({ entity: entityBuilder.getEntityTable(), chem_comp: componentBuilder.getChemCompTable(), atom_site }); const _models = await (0, parser_1.createModels)(basic, LammpsTrajectoryFormat.create(mol), ctx); const first = _models.representative; const coordinates = await coordinatesFromLammpsTrajectory(mol, unitsStyle).runInContext(ctx); return model_1.Model.trajectoryFromModelAndCoordinates(first, coordinates); } var LammpsTrajectoryFormat; (function (LammpsTrajectoryFormat) { function is(x) { return (x === null || x === void 0 ? void 0 : x.kind) === 'lammpstrj'; } LammpsTrajectoryFormat.is = is; function create(mol) { return { kind: 'lammpstrj', name: 'lammpstrj', data: mol }; } LammpsTrajectoryFormat.create = create; })(LammpsTrajectoryFormat || (exports.LammpsTrajectoryFormat = LammpsTrajectoryFormat = {})); function trajectoryFromLammpsTrajectory(mol, unitsStyle) { if (unitsStyle === void 0) unitsStyle = 'real'; return mol_task_1.Task.create('Parse Lammps Traj Data', ctx => getModels(mol, ctx, unitsStyle)); }