model.ts 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  1. /**
  2. * Copyright (c) 2018-2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  6. */
  7. import { parse3DG } from '../../mol-io/reader/3dg/parser';
  8. import { parseDcd } from '../../mol-io/reader/dcd/parser';
  9. import { parseGRO } from '../../mol-io/reader/gro/parser';
  10. import { parsePDB } from '../../mol-io/reader/pdb/parser';
  11. import { Mat4, Vec3 } from '../../mol-math/linear-algebra';
  12. import { shapeFromPly } from '../../mol-model-formats/shape/ply';
  13. import { trajectoryFrom3DG } from '../../mol-model-formats/structure/3dg';
  14. import { coordinatesFromDcd } from '../../mol-model-formats/structure/dcd';
  15. import { trajectoryFromGRO } from '../../mol-model-formats/structure/gro';
  16. import { trajectoryFromMmCIF } from '../../mol-model-formats/structure/mmcif';
  17. import { trajectoryFromPDB } from '../../mol-model-formats/structure/pdb';
  18. import { topologyFromPsf } from '../../mol-model-formats/structure/psf';
  19. import { Coordinates, Model, Queries, QueryContext, Structure, StructureElement, StructureQuery, StructureSelection as Sel, Topology, ArrayTrajectory, Trajectory } from '../../mol-model/structure';
  20. import { PluginContext } from '../../mol-plugin/context';
  21. import { MolScriptBuilder } from '../../mol-script/language/builder';
  22. import Expression from '../../mol-script/language/expression';
  23. import { Script } from '../../mol-script/script';
  24. import { StateObject, StateTransformer } from '../../mol-state';
  25. import { RuntimeContext, Task } from '../../mol-task';
  26. import { deepEqual } from '../../mol-util';
  27. import { ParamDefinition as PD } from '../../mol-util/param-definition';
  28. import { RootStructureDefinition } from '../helpers/root-structure';
  29. import { createStructureComponent, StructureComponentParams, updateStructureComponent } from '../helpers/structure-component';
  30. import { StructureQueryHelper } from '../helpers/structure-query';
  31. import { StructureSelectionQueries } from '../helpers/structure-selection-query';
  32. import { PluginStateObject as SO, PluginStateTransform } from '../objects';
  33. import { parseMol } from '../../mol-io/reader/mol/parser';
  34. import { trajectoryFromMol } from '../../mol-model-formats/structure/mol';
  35. import { trajectoryFromCifCore } from '../../mol-model-formats/structure/cif-core';
  36. import { trajectoryFromCube } from '../../mol-model-formats/structure/cube';
  37. import { parseMol2 } from '../../mol-io/reader/mol2/parser';
  38. import { trajectoryFromMol2 } from '../../mol-model-formats/structure/mol2';
  39. import { parseXtc } from '../../mol-io/reader/xtc/parser';
  40. import { coordinatesFromXtc } from '../../mol-model-formats/structure/xtc';
  41. export { CoordinatesFromDcd };
  42. export { CoordinatesFromXtc };
  43. export { TopologyFromPsf };
  44. export { TrajectoryFromModelAndCoordinates };
  45. export { TrajectoryFromBlob };
  46. export { TrajectoryFromMmCif };
  47. export { TrajectoryFromPDB };
  48. export { TrajectoryFromGRO };
  49. export { TrajectoryFromMOL };
  50. export { TrajectoryFromMOL2 };
  51. export { TrajectoryFromCube };
  52. export { TrajectoryFromCifCore };
  53. export { TrajectoryFrom3DG };
  54. export { ModelFromTrajectory };
  55. export { StructureFromTrajectory };
  56. export { StructureFromModel };
  57. export { TransformStructureConformation };
  58. export { StructureSelectionFromExpression };
  59. export { MultiStructureSelectionFromExpression };
  60. export { StructureSelectionFromScript };
  61. export { StructureSelectionFromBundle };
  62. export { StructureComplexElement };
  63. export { StructureComponent };
  64. export { CustomModelProperties };
  65. export { CustomStructureProperties };
  66. export { ShapeFromPly };
  67. type CoordinatesFromDcd = typeof CoordinatesFromDcd
  68. const CoordinatesFromDcd = PluginStateTransform.BuiltIn({
  69. name: 'coordinates-from-dcd',
  70. display: { name: 'Parse DCD', description: 'Parse DCD binary data.' },
  71. from: [SO.Data.Binary],
  72. to: SO.Molecule.Coordinates
  73. })({
  74. apply({ a }) {
  75. return Task.create('Parse DCD', async ctx => {
  76. const parsed = await parseDcd(a.data).runInContext(ctx);
  77. if (parsed.isError) throw new Error(parsed.message);
  78. const coordinates = await coordinatesFromDcd(parsed.result).runInContext(ctx);
  79. return new SO.Molecule.Coordinates(coordinates, { label: a.label, description: 'Coordinates' });
  80. });
  81. }
  82. });
  83. type CoordinatesFromXtc = typeof CoordinatesFromDcd
  84. const CoordinatesFromXtc = PluginStateTransform.BuiltIn({
  85. name: 'coordinates-from-xtc',
  86. display: { name: 'Parse XTC', description: 'Parse XTC binary data.' },
  87. from: [SO.Data.Binary],
  88. to: SO.Molecule.Coordinates
  89. })({
  90. apply({ a }) {
  91. return Task.create('Parse XTC', async ctx => {
  92. const parsed = await parseXtc(a.data).runInContext(ctx);
  93. if (parsed.isError) throw new Error(parsed.message);
  94. const coordinates = await coordinatesFromXtc(parsed.result).runInContext(ctx);
  95. return new SO.Molecule.Coordinates(coordinates, { label: a.label, description: 'Coordinates' });
  96. });
  97. }
  98. });
  99. type TopologyFromPsf = typeof TopologyFromPsf
  100. const TopologyFromPsf = PluginStateTransform.BuiltIn({
  101. name: 'topology-from-psf',
  102. display: { name: 'PSF Topology', description: 'Parse PSF string data.' },
  103. from: [SO.Format.Psf],
  104. to: SO.Molecule.Topology
  105. })({
  106. apply({ a }) {
  107. return Task.create('Create Topology', async ctx => {
  108. const topology = await topologyFromPsf(a.data).runInContext(ctx);
  109. return new SO.Molecule.Topology(topology, { label: topology.label || a.label, description: 'Topology' });
  110. });
  111. }
  112. });
  113. async function getTrajectory(ctx: RuntimeContext, obj: StateObject, coordinates: Coordinates) {
  114. if (obj.type === SO.Molecule.Topology.type) {
  115. const topology = obj.data as Topology;
  116. return await Model.trajectoryFromTopologyAndCoordinates(topology, coordinates).runInContext(ctx);
  117. } else if (obj.type === SO.Molecule.Model.type) {
  118. const model = obj.data as Model;
  119. return Model.trajectoryFromModelAndCoordinates(model, coordinates);
  120. }
  121. throw new Error('no model/topology found');
  122. }
  123. type TrajectoryFromModelAndCoordinates = typeof TrajectoryFromModelAndCoordinates
  124. const TrajectoryFromModelAndCoordinates = PluginStateTransform.BuiltIn({
  125. name: 'trajectory-from-model-and-coordinates',
  126. display: { name: 'Trajectory from Topology & Coordinates', description: 'Create a trajectory from existing model/topology and coordinates.' },
  127. from: SO.Root,
  128. to: SO.Molecule.Trajectory,
  129. params: {
  130. modelRef: PD.Text('', { isHidden: true }),
  131. coordinatesRef: PD.Text('', { isHidden: true }),
  132. }
  133. })({
  134. apply({ params, dependencies }) {
  135. return Task.create('Create trajectory from model/topology and coordinates', async ctx => {
  136. const coordinates = dependencies![params.coordinatesRef].data as Coordinates;
  137. const trajectory = await getTrajectory(ctx, dependencies![params.modelRef], coordinates);
  138. const props = { label: 'Trajectory', description: `${trajectory.frameCount} model${trajectory.frameCount === 1 ? '' : 's'}` };
  139. return new SO.Molecule.Trajectory(trajectory, props);
  140. });
  141. }
  142. });
  143. type TrajectoryFromBlob = typeof TrajectoryFromBlob
  144. const TrajectoryFromBlob = PluginStateTransform.BuiltIn({
  145. name: 'trajectory-from-blob',
  146. display: { name: 'Parse Blob', description: 'Parse format blob into a single trajectory.' },
  147. from: SO.Format.Blob,
  148. to: SO.Molecule.Trajectory
  149. })({
  150. apply({ a }) {
  151. return Task.create('Parse Format Blob', async ctx => {
  152. const models: Model[] = [];
  153. for (const e of a.data) {
  154. if (e.kind !== 'cif') continue;
  155. const block = e.data.blocks[0];
  156. const xs = await trajectoryFromMmCIF(block).runInContext(ctx);
  157. if (xs.frameCount === 0) throw new Error('No models found.');
  158. for (let i = 0; i < xs.frameCount; i++) {
  159. const x = await Task.resolveInContext(xs.getFrameAtIndex(i), ctx);
  160. models.push(x);
  161. }
  162. }
  163. const props = { label: 'Trajectory', description: `${models.length} model${models.length === 1 ? '' : 's'}` };
  164. return new SO.Molecule.Trajectory(new ArrayTrajectory(models), props);
  165. });
  166. }
  167. });
  168. function trajectoryProps(trajectory: Trajectory) {
  169. const first = trajectory.representative;
  170. return { label: `${first.entry}`, description: `${trajectory.frameCount} model${trajectory.frameCount === 1 ? '' : 's'}` };
  171. }
  172. type TrajectoryFromMmCif = typeof TrajectoryFromMmCif
  173. const TrajectoryFromMmCif = PluginStateTransform.BuiltIn({
  174. name: 'trajectory-from-mmcif',
  175. display: { name: 'Trajectory from mmCIF', description: 'Identify and create all separate models in the specified CIF data block' },
  176. from: SO.Format.Cif,
  177. to: SO.Molecule.Trajectory,
  178. params(a) {
  179. if (!a) {
  180. return {
  181. blockHeader: PD.Optional(PD.Text(void 0, { description: 'Header of the block to parse. If none is specifed, the 1st data block in the file is used.' }))
  182. };
  183. }
  184. const { blocks } = a.data;
  185. return {
  186. blockHeader: PD.Optional(PD.Select(blocks[0] && blocks[0].header, blocks.map(b => [b.header, b.header] as [string, string]), { description: 'Header of the block to parse' }))
  187. };
  188. }
  189. })({
  190. isApplicable: a => a.data.blocks.length > 0,
  191. apply({ a, params }) {
  192. return Task.create('Parse mmCIF', async ctx => {
  193. const header = params.blockHeader || a.data.blocks[0].header;
  194. const block = a.data.blocks.find(b => b.header === header);
  195. if (!block) throw new Error(`Data block '${[header]}' not found.`);
  196. const models = await trajectoryFromMmCIF(block).runInContext(ctx);
  197. if (models.frameCount === 0) throw new Error('No models found.');
  198. const props = trajectoryProps(models);
  199. return new SO.Molecule.Trajectory(models, props);
  200. });
  201. }
  202. });
  203. type TrajectoryFromPDB = typeof TrajectoryFromPDB
  204. const TrajectoryFromPDB = PluginStateTransform.BuiltIn({
  205. name: 'trajectory-from-pdb',
  206. display: { name: 'Parse PDB', description: 'Parse PDB string and create trajectory.' },
  207. from: [SO.Data.String],
  208. to: SO.Molecule.Trajectory,
  209. params: {
  210. isPdbqt: PD.Boolean(false)
  211. }
  212. })({
  213. apply({ a, params }) {
  214. return Task.create('Parse PDB', async ctx => {
  215. const parsed = await parsePDB(a.data, a.label, params.isPdbqt).runInContext(ctx);
  216. if (parsed.isError) throw new Error(parsed.message);
  217. const models = await trajectoryFromPDB(parsed.result).runInContext(ctx);
  218. const props = trajectoryProps(models);
  219. return new SO.Molecule.Trajectory(models, props);
  220. });
  221. }
  222. });
  223. type TrajectoryFromGRO = typeof TrajectoryFromGRO
  224. const TrajectoryFromGRO = PluginStateTransform.BuiltIn({
  225. name: 'trajectory-from-gro',
  226. display: { name: 'Parse GRO', description: 'Parse GRO string and create trajectory.' },
  227. from: [SO.Data.String],
  228. to: SO.Molecule.Trajectory
  229. })({
  230. apply({ a }) {
  231. return Task.create('Parse GRO', async ctx => {
  232. const parsed = await parseGRO(a.data).runInContext(ctx);
  233. if (parsed.isError) throw new Error(parsed.message);
  234. const models = await trajectoryFromGRO(parsed.result).runInContext(ctx);
  235. const props = trajectoryProps(models);
  236. return new SO.Molecule.Trajectory(models, props);
  237. });
  238. }
  239. });
  240. type TrajectoryFromMOL = typeof TrajectoryFromMOL
  241. const TrajectoryFromMOL = PluginStateTransform.BuiltIn({
  242. name: 'trajectory-from-mol',
  243. display: { name: 'Parse MOL', description: 'Parse MOL string and create trajectory.' },
  244. from: [SO.Data.String],
  245. to: SO.Molecule.Trajectory
  246. })({
  247. apply({ a }) {
  248. return Task.create('Parse MOL', async ctx => {
  249. const parsed = await parseMol(a.data).runInContext(ctx);
  250. if (parsed.isError) throw new Error(parsed.message);
  251. const models = await trajectoryFromMol(parsed.result).runInContext(ctx);
  252. const props = trajectoryProps(models);
  253. return new SO.Molecule.Trajectory(models, props);
  254. });
  255. }
  256. });
  257. type TrajectoryFromMOL2 = typeof TrajectoryFromMOL
  258. const TrajectoryFromMOL2 = PluginStateTransform.BuiltIn({
  259. name: 'trajectory-from-mol2',
  260. display: { name: 'Parse MOL2', description: 'Parse MOL2 string and create trajectory.' },
  261. from: [SO.Data.String],
  262. to: SO.Molecule.Trajectory
  263. })({
  264. apply({ a }) {
  265. return Task.create('Parse MOL2', async ctx => {
  266. const parsed = await parseMol2(a.data, a.label).runInContext(ctx);
  267. if (parsed.isError) throw new Error(parsed.message);
  268. const models = await trajectoryFromMol2(parsed.result).runInContext(ctx);
  269. const props = trajectoryProps(models);
  270. return new SO.Molecule.Trajectory(models, props);
  271. });
  272. }
  273. });
  274. type TrajectoryFromCube = typeof TrajectoryFromCube
  275. const TrajectoryFromCube = PluginStateTransform.BuiltIn({
  276. name: 'trajectory-from-cube',
  277. display: { name: 'Parse Cube', description: 'Parse Cube file to create a trajectory.' },
  278. from: SO.Format.Cube,
  279. to: SO.Molecule.Trajectory
  280. })({
  281. apply({ a }) {
  282. return Task.create('Parse MOL', async ctx => {
  283. const models = await trajectoryFromCube(a.data).runInContext(ctx);
  284. const props = trajectoryProps(models);
  285. return new SO.Molecule.Trajectory(models, props);
  286. });
  287. }
  288. });
  289. type TrajectoryFromCifCore = typeof TrajectoryFromCifCore
  290. const TrajectoryFromCifCore = PluginStateTransform.BuiltIn({
  291. name: 'trajectory-from-cif-core',
  292. display: { name: 'Parse CIF Core', description: 'Identify and create all separate models in the specified CIF data block' },
  293. from: SO.Format.Cif,
  294. to: SO.Molecule.Trajectory,
  295. params(a) {
  296. if (!a) {
  297. return {
  298. blockHeader: PD.Optional(PD.Text(void 0, { description: 'Header of the block to parse. If none is specifed, the 1st data block in the file is used.' }))
  299. };
  300. }
  301. const { blocks } = a.data;
  302. return {
  303. blockHeader: PD.Optional(PD.Select(blocks[0] && blocks[0].header, blocks.map(b => [b.header, b.header] as [string, string]), { description: 'Header of the block to parse' }))
  304. };
  305. }
  306. })({
  307. apply({ a, params }) {
  308. return Task.create('Parse CIF Core', async ctx => {
  309. const header = params.blockHeader || a.data.blocks[0].header;
  310. const block = a.data.blocks.find(b => b.header === header);
  311. if (!block) throw new Error(`Data block '${[header]}' not found.`);
  312. const models = await trajectoryFromCifCore(block).runInContext(ctx);
  313. if (models.frameCount === 0) throw new Error('No models found.');
  314. const props = trajectoryProps(models);
  315. return new SO.Molecule.Trajectory(models, props);
  316. });
  317. }
  318. });
  319. type TrajectoryFrom3DG = typeof TrajectoryFrom3DG
  320. const TrajectoryFrom3DG = PluginStateTransform.BuiltIn({
  321. name: 'trajectory-from-3dg',
  322. display: { name: 'Parse 3DG', description: 'Parse 3DG string and create trajectory.' },
  323. from: [SO.Data.String],
  324. to: SO.Molecule.Trajectory
  325. })({
  326. apply({ a }) {
  327. return Task.create('Parse 3DG', async ctx => {
  328. const parsed = await parse3DG(a.data).runInContext(ctx);
  329. if (parsed.isError) throw new Error(parsed.message);
  330. const models = await trajectoryFrom3DG(parsed.result).runInContext(ctx);
  331. const props = trajectoryProps(models);
  332. return new SO.Molecule.Trajectory(models, props);
  333. });
  334. }
  335. });
  336. const plus1 = (v: number) => v + 1, minus1 = (v: number) => v - 1;
  337. type ModelFromTrajectory = typeof ModelFromTrajectory
  338. const ModelFromTrajectory = PluginStateTransform.BuiltIn({
  339. name: 'model-from-trajectory',
  340. display: { name: 'Molecular Model', description: 'Create a molecular model from specified index in a trajectory.' },
  341. from: SO.Molecule.Trajectory,
  342. to: SO.Molecule.Model,
  343. params: a => {
  344. if (!a) {
  345. return { modelIndex: PD.Numeric(0, {}, { description: 'Zero-based index of the model' }) };
  346. }
  347. return { modelIndex: PD.Converted(plus1, minus1, PD.Numeric(1, { min: 1, max: a.data.frameCount, step: 1 }, { description: 'Model Index' })) };
  348. }
  349. })({
  350. isApplicable: a => a.data.frameCount > 0,
  351. apply({ a, params }) {
  352. return Task.create('Model from Trajectory', async ctx => {
  353. let modelIndex = params.modelIndex % a.data.frameCount;
  354. if (modelIndex < 0) modelIndex += a.data.frameCount;
  355. const model = await Task.resolveInContext(a.data.getFrameAtIndex(modelIndex), ctx);
  356. const label = `Model ${modelIndex + 1}`;
  357. let description = a.data.frameCount === 1 ? undefined : `of ${a.data.frameCount}`;
  358. return new SO.Molecule.Model(model, { label, description });
  359. });
  360. },
  361. dispose({ b }) {
  362. b?.data.customProperties.dispose();
  363. }
  364. });
  365. type StructureFromTrajectory = typeof StructureFromTrajectory
  366. const StructureFromTrajectory = PluginStateTransform.BuiltIn({
  367. name: 'structure-from-trajectory',
  368. display: { name: 'Structure from Trajectory', description: 'Create a molecular structure from a trajectory.' },
  369. from: SO.Molecule.Trajectory,
  370. to: SO.Molecule.Structure
  371. })({
  372. apply({ a }) {
  373. return Task.create('Build Structure', async ctx => {
  374. const s = await Structure.ofTrajectory(a.data, ctx);
  375. const props = { label: 'Ensemble', description: Structure.elementDescription(s) };
  376. return new SO.Molecule.Structure(s, props);
  377. });
  378. },
  379. dispose({ b }) {
  380. b?.data.customPropertyDescriptors.dispose();
  381. }
  382. });
  383. type StructureFromModel = typeof StructureFromModel
  384. const StructureFromModel = PluginStateTransform.BuiltIn({
  385. name: 'structure-from-model',
  386. display: { name: 'Structure', description: 'Create a molecular structure (model, assembly, or symmetry) from the specified model.' },
  387. from: SO.Molecule.Model,
  388. to: SO.Molecule.Structure,
  389. params(a) { return RootStructureDefinition.getParams(a && a.data); }
  390. })({
  391. canAutoUpdate({ oldParams, newParams }) {
  392. return RootStructureDefinition.canAutoUpdate(oldParams.type, newParams.type);
  393. },
  394. apply({ a, params }, plugin: PluginContext) {
  395. return Task.create('Build Structure', async ctx => {
  396. return RootStructureDefinition.create(plugin, ctx, a.data, params && params.type);
  397. });
  398. },
  399. update: ({ a, b, oldParams, newParams }) => {
  400. if (!deepEqual(oldParams, newParams)) return StateTransformer.UpdateResult.Recreate;
  401. if (b.data.model === a.data) return StateTransformer.UpdateResult.Unchanged;
  402. if (Model.getRoot(b.data.model) !== Model.getRoot(a.data)
  403. && (a.data.atomicHierarchy !== b.data.model.atomicHierarchy
  404. || a.data.coarseHierarchy !== b.data.model.coarseHierarchy)) {
  405. return StateTransformer.UpdateResult.Recreate;
  406. }
  407. b.data = Structure.remapModel(b.data, a.data);
  408. return StateTransformer.UpdateResult.Updated;
  409. },
  410. dispose({ b }) {
  411. b?.data.customPropertyDescriptors.dispose();
  412. }
  413. });
  414. const _translation = Vec3(), _m = Mat4(), _n = Mat4();
  415. type TransformStructureConformation = typeof TransformStructureConformation
  416. const TransformStructureConformation = PluginStateTransform.BuiltIn({
  417. name: 'transform-structure-conformation',
  418. display: { name: 'Transform Conformation' },
  419. isDecorator: true,
  420. from: SO.Molecule.Structure,
  421. to: SO.Molecule.Structure,
  422. params: {
  423. transform: PD.MappedStatic('components', {
  424. components: PD.Group({
  425. axis: PD.Vec3(Vec3.create(1, 0, 0)),
  426. angle: PD.Numeric(0, { min: -180, max: 180, step: 0.1 }),
  427. translation: PD.Vec3(Vec3.create(0, 0, 0)),
  428. }, { isFlat: true }),
  429. matrix: PD.Group({
  430. data: PD.Mat4(Mat4.identity()),
  431. transpose: PD.Boolean(false)
  432. }, { isFlat: true })
  433. }, { label: 'Kind' })
  434. }
  435. })({
  436. canAutoUpdate({ newParams }) {
  437. return newParams.transform.name !== 'matrix';
  438. },
  439. apply({ a, params }) {
  440. // TODO: optimze
  441. // TODO: think of ways how to fast-track changes to this for animations
  442. const transform = Mat4();
  443. if (params.transform.name === 'components') {
  444. const { axis, angle, translation } = params.transform.params;
  445. const center = a.data.boundary.sphere.center;
  446. Mat4.fromTranslation(_m, Vec3.negate(_translation, center));
  447. Mat4.fromTranslation(_n, Vec3.add(_translation, center, translation));
  448. const rot = Mat4.fromRotation(Mat4(), Math.PI / 180 * angle, Vec3.normalize(Vec3(), axis));
  449. Mat4.mul3(transform, _n, rot, _m);
  450. } else if (params.transform.name === 'matrix') {
  451. Mat4.copy(transform, params.transform.params.data);
  452. if (params.transform.params.transpose) Mat4.transpose(transform, transform);
  453. }
  454. const s = Structure.transform(a.data, transform);
  455. return new SO.Molecule.Structure(s, { label: a.label, description: `${a.description} [Transformed]` });
  456. },
  457. dispose({ b }) {
  458. b?.data.customPropertyDescriptors.dispose();
  459. }
  460. // interpolate(src, tar, t) {
  461. // // TODO: optimize
  462. // const u = Mat4.fromRotation(Mat4(), Math.PI / 180 * src.angle, Vec3.normalize(Vec3(), src.axis));
  463. // Mat4.setTranslation(u, src.translation);
  464. // const v = Mat4.fromRotation(Mat4(), Math.PI / 180 * tar.angle, Vec3.normalize(Vec3(), tar.axis));
  465. // Mat4.setTranslation(v, tar.translation);
  466. // const m = SymmetryOperator.slerp(Mat4(), u, v, t);
  467. // const rot = Mat4.getRotation(Quat.zero(), m);
  468. // const axis = Vec3();
  469. // const angle = Quat.getAxisAngle(axis, rot);
  470. // const translation = Mat4.getTranslation(Vec3(), m);
  471. // return { axis, angle, translation };
  472. // }
  473. });
  474. type StructureSelectionFromExpression = typeof StructureSelectionFromExpression
  475. const StructureSelectionFromExpression = PluginStateTransform.BuiltIn({
  476. name: 'structure-selection-from-expression',
  477. display: { name: 'Selection', description: 'Create a molecular structure from the specified expression.' },
  478. from: SO.Molecule.Structure,
  479. to: SO.Molecule.Structure,
  480. params: {
  481. expression: PD.Value<Expression>(MolScriptBuilder.struct.generator.all, { isHidden: true }),
  482. label: PD.Optional(PD.Text('', { isHidden: true }))
  483. }
  484. })({
  485. apply({ a, params, cache }) {
  486. const { selection, entry } = StructureQueryHelper.createAndRun(a.data, params.expression);
  487. (cache as any).entry = entry;
  488. if (Sel.isEmpty(selection)) return StateObject.Null;
  489. const s = Sel.unionStructure(selection);
  490. const props = { label: `${params.label || 'Selection'}`, description: Structure.elementDescription(s) };
  491. return new SO.Molecule.Structure(s, props);
  492. },
  493. update: ({ a, b, oldParams, newParams, cache }) => {
  494. if (oldParams.expression !== newParams.expression) return StateTransformer.UpdateResult.Recreate;
  495. const entry = (cache as { entry: StructureQueryHelper.CacheEntry }).entry;
  496. if (entry.currentStructure === a.data) {
  497. return StateTransformer.UpdateResult.Unchanged;
  498. }
  499. const selection = StructureQueryHelper.updateStructure(entry, a.data);
  500. if (Sel.isEmpty(selection)) return StateTransformer.UpdateResult.Null;
  501. StructureQueryHelper.updateStructureObject(b, selection, newParams.label);
  502. return StateTransformer.UpdateResult.Updated;
  503. },
  504. dispose({ b }) {
  505. b?.data.customPropertyDescriptors.dispose();
  506. }
  507. });
  508. type MultiStructureSelectionFromExpression = typeof MultiStructureSelectionFromExpression
  509. const MultiStructureSelectionFromExpression = PluginStateTransform.BuiltIn({
  510. name: 'structure-multi-selection-from-expression',
  511. display: { name: 'Multi-structure Measurement Selection', description: 'Create selection object from multiple structures.' },
  512. from: SO.Root,
  513. to: SO.Molecule.Structure.Selections,
  514. params: {
  515. selections: PD.ObjectList({
  516. key: PD.Text(void 0, { description: 'A unique key.' }),
  517. ref: PD.Text(),
  518. groupId: PD.Optional(PD.Text()),
  519. expression: PD.Value<Expression>(MolScriptBuilder.struct.generator.empty)
  520. }, e => e.ref, { isHidden: true }),
  521. isTransitive: PD.Optional(PD.Boolean(false, { isHidden: true, description: 'Remap the selections from the original structure if structurally equivalent.' })),
  522. label: PD.Optional(PD.Text('', { isHidden: true }))
  523. }
  524. })({
  525. apply({ params, cache, dependencies }) {
  526. const entries = new Map<string, StructureQueryHelper.CacheEntry>();
  527. const selections: SO.Molecule.Structure.SelectionEntry[] = [];
  528. let totalSize = 0;
  529. for (const sel of params.selections) {
  530. const { selection, entry } = StructureQueryHelper.createAndRun(dependencies![sel.ref].data as Structure, sel.expression);
  531. entries.set(sel.key, entry);
  532. const loci = Sel.toLociWithSourceUnits(selection);
  533. selections.push({ key: sel.key, loci, groupId: sel.groupId });
  534. totalSize += StructureElement.Loci.size(loci);
  535. }
  536. (cache as object as any).entries = entries;
  537. const props = { label: `${params.label || 'Multi-selection'}`, description: `${params.selections.length} source(s), ${totalSize} element(s) total` };
  538. return new SO.Molecule.Structure.Selections(selections, props);
  539. },
  540. update: ({ b, oldParams, newParams, cache, dependencies }) => {
  541. if (!!oldParams.isTransitive !== !!newParams.isTransitive) return StateTransformer.UpdateResult.Recreate;
  542. const cacheEntries = (cache as any).entries as Map<string, StructureQueryHelper.CacheEntry>;
  543. const entries = new Map<string, StructureQueryHelper.CacheEntry>();
  544. const current = new Map<string, SO.Molecule.Structure.SelectionEntry>();
  545. for (const e of b.data) current.set(e.key, e);
  546. let changed = false;
  547. let totalSize = 0;
  548. const selections: SO.Molecule.Structure.SelectionEntry[] = [];
  549. for (const sel of newParams.selections) {
  550. const structure = dependencies![sel.ref].data as Structure;
  551. let recreate = false;
  552. if (cacheEntries.has(sel.key)) {
  553. const entry = cacheEntries.get(sel.key)!;
  554. if (StructureQueryHelper.isUnchanged(entry, sel.expression, structure) && current.has(sel.key)) {
  555. const loci = current.get(sel.key)!;
  556. if (loci.groupId !== sel.groupId) {
  557. loci.groupId = sel.groupId;
  558. changed = true;
  559. }
  560. entries.set(sel.key, entry);
  561. selections.push(loci);
  562. totalSize += StructureElement.Loci.size(loci.loci);
  563. continue;
  564. } if (entry.expression !== sel.expression) {
  565. recreate = true;
  566. } else {
  567. // TODO: properly support "transitive" queries. For that Structure.areUnitAndIndicesEqual needs to be fixed;
  568. let update = false;
  569. if (!!newParams.isTransitive) {
  570. if (Structure.areUnitAndIndicesEqual(entry.originalStructure, structure)) {
  571. const selection = StructureQueryHelper.run(entry, entry.originalStructure);
  572. entry.currentStructure = structure;
  573. entries.set(sel.key, entry);
  574. const loci = StructureElement.Loci.remap(Sel.toLociWithSourceUnits(selection), structure);
  575. selections.push({ key: sel.key, loci, groupId: sel.groupId });
  576. totalSize += StructureElement.Loci.size(loci);
  577. changed = true;
  578. } else {
  579. update = true;
  580. }
  581. } else {
  582. update = true;
  583. }
  584. if (update) {
  585. changed = true;
  586. const selection = StructureQueryHelper.updateStructure(entry, structure);
  587. entries.set(sel.key, entry);
  588. const loci = Sel.toLociWithSourceUnits(selection);
  589. selections.push({ key: sel.key, loci, groupId: sel.groupId });
  590. totalSize += StructureElement.Loci.size(loci);
  591. }
  592. }
  593. } else {
  594. recreate = true;
  595. }
  596. if (recreate) {
  597. changed = true;
  598. // create new selection
  599. const { selection, entry } = StructureQueryHelper.createAndRun(structure, sel.expression);
  600. entries.set(sel.key, entry);
  601. const loci = Sel.toLociWithSourceUnits(selection);
  602. selections.push({ key: sel.key, loci });
  603. totalSize += StructureElement.Loci.size(loci);
  604. }
  605. }
  606. if (!changed) return StateTransformer.UpdateResult.Unchanged;
  607. (cache as object as any).entries = entries;
  608. b.data = selections;
  609. b.label = `${newParams.label || 'Multi-selection'}`;
  610. b.description = `${selections.length} source(s), ${totalSize} element(s) total`;
  611. return StateTransformer.UpdateResult.Updated;
  612. }
  613. });
  614. type StructureSelectionFromScript = typeof StructureSelectionFromScript
  615. const StructureSelectionFromScript = PluginStateTransform.BuiltIn({
  616. name: 'structure-selection-from-script',
  617. display: { name: 'Selection', description: 'Create a molecular structure from the specified script.' },
  618. from: SO.Molecule.Structure,
  619. to: SO.Molecule.Structure,
  620. params: {
  621. script: PD.Script({ language: 'mol-script', expression: '(sel.atom.atom-groups :residue-test (= atom.resname ALA))' }),
  622. label: PD.Optional(PD.Text(''))
  623. }
  624. })({
  625. apply({ a, params, cache }) {
  626. const { selection, entry } = StructureQueryHelper.createAndRun(a.data, params.script);
  627. (cache as any).entry = entry;
  628. const s = Sel.unionStructure(selection);
  629. const props = { label: `${params.label || 'Selection'}`, description: Structure.elementDescription(s) };
  630. return new SO.Molecule.Structure(s, props);
  631. },
  632. update: ({ a, b, oldParams, newParams, cache }) => {
  633. if (!Script.areEqual(oldParams.script, newParams.script)) {
  634. return StateTransformer.UpdateResult.Recreate;
  635. }
  636. const entry = (cache as { entry: StructureQueryHelper.CacheEntry }).entry;
  637. if (entry.currentStructure === a.data) {
  638. return StateTransformer.UpdateResult.Unchanged;
  639. }
  640. const selection = StructureQueryHelper.updateStructure(entry, a.data);
  641. StructureQueryHelper.updateStructureObject(b, selection, newParams.label);
  642. return StateTransformer.UpdateResult.Updated;
  643. },
  644. dispose({ b }) {
  645. b?.data.customPropertyDescriptors.dispose();
  646. }
  647. });
  648. type StructureSelectionFromBundle = typeof StructureSelectionFromBundle
  649. const StructureSelectionFromBundle = PluginStateTransform.BuiltIn({
  650. name: 'structure-selection-from-bundle',
  651. display: { name: 'Selection', description: 'Create a molecular structure from the specified structure-element bundle.' },
  652. from: SO.Molecule.Structure,
  653. to: SO.Molecule.Structure,
  654. params: {
  655. bundle: PD.Value<StructureElement.Bundle>(StructureElement.Bundle.Empty, { isHidden: true }),
  656. label: PD.Optional(PD.Text('', { isHidden: true }))
  657. }
  658. })({
  659. apply({ a, params, cache }) {
  660. if (params.bundle.hash !== a.data.hashCode) {
  661. return StateObject.Null;
  662. }
  663. (cache as { source: Structure }).source = a.data;
  664. const s = StructureElement.Bundle.toStructure(params.bundle, a.data);
  665. if (s.elementCount === 0) return StateObject.Null;
  666. const props = { label: `${params.label || 'Selection'}`, description: Structure.elementDescription(s) };
  667. return new SO.Molecule.Structure(s, props);
  668. },
  669. update: ({ a, b, oldParams, newParams, cache }) => {
  670. if (!StructureElement.Bundle.areEqual(oldParams.bundle, newParams.bundle)) {
  671. return StateTransformer.UpdateResult.Recreate;
  672. }
  673. if (newParams.bundle.hash !== a.data.hashCode) {
  674. return StateTransformer.UpdateResult.Null;
  675. }
  676. if ((cache as { source: Structure }).source === a.data) {
  677. return StateTransformer.UpdateResult.Unchanged;
  678. }
  679. (cache as { source: Structure }).source = a.data;
  680. const s = StructureElement.Bundle.toStructure(newParams.bundle, a.data);
  681. if (s.elementCount === 0) return StateTransformer.UpdateResult.Null;
  682. b.label = `${newParams.label || 'Selection'}`;
  683. b.description = Structure.elementDescription(s);
  684. b.data = s;
  685. return StateTransformer.UpdateResult.Updated;
  686. },
  687. dispose({ b }) {
  688. b?.data.customPropertyDescriptors.dispose();
  689. }
  690. });
  691. export const StructureComplexElementTypes = {
  692. 'polymer': 'polymer',
  693. 'protein': 'protein',
  694. 'nucleic': 'nucleic',
  695. 'water': 'water',
  696. 'branched': 'branched', // = carbs
  697. 'ligand': 'ligand',
  698. 'non-standard': 'non-standard',
  699. 'coarse': 'coarse',
  700. // Legacy
  701. 'atomic-sequence': 'atomic-sequence',
  702. 'atomic-het': 'atomic-het',
  703. 'spheres': 'spheres'
  704. } as const;
  705. export type StructureComplexElementTypes = keyof typeof StructureComplexElementTypes
  706. const StructureComplexElementTypeTuples = PD.objectToOptions(StructureComplexElementTypes);
  707. type StructureComplexElement = typeof StructureComplexElement
  708. const StructureComplexElement = PluginStateTransform.BuiltIn({
  709. name: 'structure-complex-element',
  710. display: { name: 'Complex Element', description: 'Create a molecular structure from the specified model.' },
  711. from: SO.Molecule.Structure,
  712. to: SO.Molecule.Structure,
  713. params: { type: PD.Select<StructureComplexElementTypes>('atomic-sequence', StructureComplexElementTypeTuples, { isHidden: true }) }
  714. })({
  715. apply({ a, params }) {
  716. // TODO: update function.
  717. let query: StructureQuery, label: string;
  718. switch (params.type) {
  719. case 'polymer': query = StructureSelectionQueries.polymer.query; label = 'Polymer'; break;
  720. case 'protein': query = StructureSelectionQueries.protein.query; label = 'Protein'; break;
  721. case 'nucleic': query = StructureSelectionQueries.nucleic.query; label = 'Nucleic'; break;
  722. case 'water': query = Queries.internal.water(); label = 'Water'; break;
  723. case 'branched': query = StructureSelectionQueries.branchedPlusConnected.query; label = 'Branched'; break;
  724. case 'ligand': query = StructureSelectionQueries.ligandPlusConnected.query; label = 'Ligand'; break;
  725. case 'non-standard': query = StructureSelectionQueries.nonStandardPolymer.query; label = 'Non-standard'; break;
  726. case 'coarse': query = StructureSelectionQueries.coarse.query; label = 'Coarse'; break;
  727. case 'atomic-sequence': query = Queries.internal.atomicSequence(); label = 'Sequence'; break;
  728. case 'atomic-het': query = Queries.internal.atomicHet(); label = 'HET Groups/Ligands'; break;
  729. case 'spheres': query = Queries.internal.spheres(); label = 'Coarse Spheres'; break;
  730. default: throw new Error(`${params.type} is a not valid complex element.`);
  731. }
  732. const result = query(new QueryContext(a.data));
  733. const s = Sel.unionStructure(result);
  734. if (s.elementCount === 0) return StateObject.Null;
  735. return new SO.Molecule.Structure(s, { label, description: Structure.elementDescription(s) });
  736. },
  737. dispose({ b }) {
  738. b?.data.customPropertyDescriptors.dispose();
  739. }
  740. });
  741. type StructureComponent = typeof StructureComponent
  742. const StructureComponent = PluginStateTransform.BuiltIn({
  743. name: 'structure-component',
  744. display: { name: 'Component', description: 'A molecular structure component.' },
  745. from: SO.Molecule.Structure,
  746. to: SO.Molecule.Structure,
  747. params: StructureComponentParams
  748. })({
  749. apply({ a, params, cache }) {
  750. return createStructureComponent(a.data, params, cache as any);
  751. },
  752. update: ({ a, b, oldParams, newParams, cache }) => {
  753. return updateStructureComponent(a.data, b, oldParams, newParams, cache as any);
  754. },
  755. dispose({ b }) {
  756. b?.data.customPropertyDescriptors.dispose();
  757. }
  758. });
  759. type CustomModelProperties = typeof CustomModelProperties
  760. const CustomModelProperties = PluginStateTransform.BuiltIn({
  761. name: 'custom-model-properties',
  762. display: { name: 'Custom Model Properties' },
  763. isDecorator: true,
  764. from: SO.Molecule.Model,
  765. to: SO.Molecule.Model,
  766. params: (a, ctx: PluginContext) => {
  767. return ctx.customModelProperties.getParams(a?.data);
  768. }
  769. })({
  770. apply({ a, params }, ctx: PluginContext) {
  771. return Task.create('Custom Props', async taskCtx => {
  772. await attachModelProps(a.data, ctx, taskCtx, params);
  773. return new SO.Molecule.Model(a.data, { label: a.label, description: a.description });
  774. });
  775. },
  776. update({ a, b, oldParams, newParams }, ctx: PluginContext) {
  777. return Task.create('Custom Props', async taskCtx => {
  778. b.data = a.data;
  779. b.label = a.label;
  780. b.description = a.description;
  781. for (const name of oldParams.autoAttach) {
  782. const property = ctx.customModelProperties.get(name);
  783. if (!property) continue;
  784. a.data.customProperties.reference(property.descriptor, false);
  785. }
  786. await attachModelProps(a.data, ctx, taskCtx, newParams);
  787. return StateTransformer.UpdateResult.Updated;
  788. });
  789. },
  790. dispose({ b }) {
  791. b?.data.customProperties.dispose();
  792. }
  793. });
  794. async function attachModelProps(model: Model, ctx: PluginContext, taskCtx: RuntimeContext, params: ReturnType<CustomModelProperties['createDefaultParams']>) {
  795. const propertyCtx = { runtime: taskCtx, assetManager: ctx.managers.asset };
  796. const { autoAttach, properties } = params;
  797. for (const name of Object.keys(properties)) {
  798. const property = ctx.customModelProperties.get(name);
  799. const props = properties[name];
  800. if (autoAttach.includes(name) || property.isHidden) {
  801. try {
  802. await property.attach(propertyCtx, model, props, true);
  803. } catch (e) {
  804. ctx.log.warn(`Error attaching model prop '${name}': ${e}`);
  805. }
  806. } else {
  807. property.set(model, props);
  808. }
  809. }
  810. }
  811. type CustomStructureProperties = typeof CustomStructureProperties
  812. const CustomStructureProperties = PluginStateTransform.BuiltIn({
  813. name: 'custom-structure-properties',
  814. display: { name: 'Custom Structure Properties' },
  815. isDecorator: true,
  816. from: SO.Molecule.Structure,
  817. to: SO.Molecule.Structure,
  818. params: (a, ctx: PluginContext) => {
  819. return ctx.customStructureProperties.getParams(a?.data.root);
  820. }
  821. })({
  822. apply({ a, params }, ctx: PluginContext) {
  823. return Task.create('Custom Props', async taskCtx => {
  824. await attachStructureProps(a.data.root, ctx, taskCtx, params);
  825. return new SO.Molecule.Structure(a.data, { label: a.label, description: a.description });
  826. });
  827. },
  828. update({ a, b, oldParams, newParams }, ctx: PluginContext) {
  829. if (a.data !== b.data) return StateTransformer.UpdateResult.Recreate;
  830. return Task.create('Custom Props', async taskCtx => {
  831. b.data = a.data;
  832. b.label = a.label;
  833. b.description = a.description;
  834. for (const name of oldParams.autoAttach) {
  835. const property = ctx.customStructureProperties.get(name);
  836. if (!property) continue;
  837. a.data.customPropertyDescriptors.reference(property.descriptor, false);
  838. }
  839. await attachStructureProps(a.data.root, ctx, taskCtx, newParams);
  840. return StateTransformer.UpdateResult.Updated;
  841. });
  842. },
  843. dispose({ b }) {
  844. b?.data.customPropertyDescriptors.dispose();
  845. }
  846. });
  847. async function attachStructureProps(structure: Structure, ctx: PluginContext, taskCtx: RuntimeContext, params: ReturnType<CustomStructureProperties['createDefaultParams']>) {
  848. const propertyCtx = { runtime: taskCtx, assetManager: ctx.managers.asset };
  849. const { autoAttach, properties } = params;
  850. for (const name of Object.keys(properties)) {
  851. const property = ctx.customStructureProperties.get(name);
  852. const props = properties[name];
  853. if (autoAttach.includes(name) || property.isHidden) {
  854. try {
  855. await property.attach(propertyCtx, structure, props, true);
  856. } catch (e) {
  857. ctx.log.warn(`Error attaching structure prop '${name}': ${e}`);
  858. }
  859. } else {
  860. property.set(structure, props);
  861. }
  862. }
  863. }
  864. type ShapeFromPly = typeof ShapeFromPly
  865. const ShapeFromPly = PluginStateTransform.BuiltIn({
  866. name: 'shape-from-ply',
  867. display: { name: 'Shape from PLY', description: 'Create Shape from PLY data' },
  868. from: SO.Format.Ply,
  869. to: SO.Shape.Provider,
  870. params(a) {
  871. return { };
  872. }
  873. })({
  874. apply({ a, params }) {
  875. return Task.create('Create shape from PLY', async ctx => {
  876. const shape = await shapeFromPly(a.data, params).runInContext(ctx);
  877. const props = { label: 'Shape' };
  878. return new SO.Shape.Provider(shape, props);
  879. });
  880. }
  881. });