model.ts 35 KB

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