model.ts 30 KB

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