model.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /**
  2. * Copyright (c) 2019-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 { Model, Structure, StructureSymmetry } from '../../mol-model/structure';
  8. import { stringToWords } from '../../mol-util/string';
  9. import { SpacegroupCell, Spacegroup } from '../../mol-math/geometry';
  10. import { ParamDefinition as PD } from '../../mol-util/param-definition';
  11. import { Vec3 } from '../../mol-math/linear-algebra';
  12. import { RuntimeContext } from '../../mol-task';
  13. import { PluginContext } from '../../mol-plugin/context';
  14. import { Assembly, Symmetry } from '../../mol-model/structure/model/properties/symmetry';
  15. import { PluginStateObject as SO } from '../objects';
  16. import { ModelSymmetry } from '../../mol-model-formats/structure/property/symmetry';
  17. export namespace ModelStructureRepresentation {
  18. export function getParams(model?: Model, defaultValue?: 'deposited' | 'assembly' | 'symmetry' | 'symmetry-mates' | 'symmetry-assembly') {
  19. const symmetry = model && ModelSymmetry.Provider.get(model)
  20. const assemblyIds = symmetry ? symmetry.assemblies.map(a => [a.id, `${a.id}: ${stringToWords(a.details)}`] as [string, string]) : [];
  21. const showSymm = !symmetry ? true : !SpacegroupCell.isZero(symmetry.spacegroup.cell);
  22. const operatorOptions: [number, string][] = []
  23. if (symmetry) {
  24. const { operators } = symmetry.spacegroup
  25. for (let i = 0, il = operators.length; i < il; i++) {
  26. operatorOptions.push([i, `${i + 1}: ${Spacegroup.getOperatorXyz(operators[i])}`])
  27. }
  28. }
  29. const asymIdsOptions: [string, string][] = []
  30. if (model) {
  31. model.properties.structAsymMap.forEach(v => {
  32. const label = v.id === v.auth_id ? v.id : `${v.id} [auth ${v.auth_id}]`
  33. asymIdsOptions.push([v.id, label])
  34. })
  35. }
  36. const modes = {
  37. deposited: PD.EmptyGroup(),
  38. assembly: PD.Group({
  39. id: PD.Optional(model
  40. ? PD.Select(assemblyIds.length ? assemblyIds[0][0] : '', assemblyIds, { label: 'Asm Id', description: 'Assembly Id' })
  41. : PD.Text('', { label: 'Asm Id', description: 'Assembly Id (use empty for the 1st assembly)' }))
  42. }, { isFlat: true }),
  43. 'symmetry-mates': PD.Group({
  44. radius: PD.Numeric(5)
  45. }, { isFlat: true }),
  46. 'symmetry': PD.Group({
  47. ijkMin: PD.Vec3(Vec3.create(-1, -1, -1), { step: 1 }, { label: 'Min IJK', fieldLabels: { x: 'I', y: 'J', z: 'K' } }),
  48. ijkMax: PD.Vec3(Vec3.create(1, 1, 1), { step: 1 }, { label: 'Max IJK', fieldLabels: { x: 'I', y: 'J', z: 'K' } })
  49. }, { isFlat: true }),
  50. 'symmetry-assembly': PD.Group({
  51. generators: PD.ObjectList({
  52. operators: PD.ObjectList({
  53. index: PD.Select(0, operatorOptions),
  54. shift: PD.Vec3(Vec3(), { step: 1 }, { label: 'IJK', fieldLabels: { x: 'I', y: 'J', z: 'K' } })
  55. }, e => `${e.index + 1}_${e.shift.map(a => a + 5).join('')}`, {
  56. defaultValue: [] as { index: number, shift: Vec3 }[]
  57. }),
  58. asymIds: PD.MultiSelect([] as string[], asymIdsOptions)
  59. }, e => `${e.asymIds.length} asym ids, ${e.operators.length} operators`, {
  60. defaultValue: [] as { operators: { index: number, shift: Vec3 }[], asymIds: string[] }[]
  61. })
  62. }, { isFlat: true })
  63. };
  64. const options: [keyof typeof modes, string][] = [
  65. ['deposited', 'Deposited']
  66. ];
  67. if (assemblyIds) {
  68. options.push(['assembly', 'Assembly']);
  69. }
  70. if (showSymm) {
  71. options.push(['symmetry-mates', 'Symmetry Mates']);
  72. options.push(['symmetry', 'Symmetry (indices)']);
  73. options.push(['symmetry-assembly', 'Symmetry (assembly)']);
  74. }
  75. return {
  76. type: PD.MappedStatic(defaultValue || 'deposited', modes, { options })
  77. };
  78. }
  79. export type Params = PD.Values<ReturnType<typeof getParams>>['type']
  80. async function buildAssembly(plugin: PluginContext, ctx: RuntimeContext, model: Model, id?: string) {
  81. let asm: Assembly | undefined = void 0;
  82. const symmetry = ModelSymmetry.Provider.get(model)
  83. // if no id is specified, use the 1st assembly.
  84. if (!id && symmetry && symmetry.assemblies.length !== 0) {
  85. id = symmetry.assemblies[0].id;
  86. }
  87. if (!symmetry || symmetry.assemblies.length === 0) {
  88. if (id !== 'deposited') {
  89. plugin.log.warn(`Model '${model.entryId}' has no assembly, returning deposited structure.`);
  90. }
  91. } else {
  92. asm = Symmetry.findAssembly(model, id || '');
  93. if (!asm) {
  94. plugin.log.warn(`Model '${model.entryId}' has no assembly called '${id}', returning deposited structure.`);
  95. }
  96. }
  97. const base = Structure.ofModel(model);
  98. if (!asm) {
  99. const label = { label: 'Deposited', description: Structure.elementDescription(base) };
  100. return new SO.Molecule.Structure(base, label);
  101. }
  102. id = asm.id;
  103. const s = await StructureSymmetry.buildAssembly(base, id!).runInContext(ctx);
  104. const props = { label: `Assembly ${id}`, description: Structure.elementDescription(s) };
  105. return new SO.Molecule.Structure(s, props);
  106. }
  107. async function buildSymmetry(ctx: RuntimeContext, model: Model, ijkMin: Vec3, ijkMax: Vec3) {
  108. const base = Structure.ofModel(model);
  109. const s = await StructureSymmetry.buildSymmetryRange(base, ijkMin, ijkMax).runInContext(ctx);
  110. const props = { label: `Symmetry [${ijkMin}] to [${ijkMax}]`, description: Structure.elementDescription(s) };
  111. return new SO.Molecule.Structure(s, props);
  112. }
  113. async function buildSymmetryMates(ctx: RuntimeContext, model: Model, radius: number) {
  114. const base = Structure.ofModel(model);
  115. const s = await StructureSymmetry.builderSymmetryMates(base, radius).runInContext(ctx);
  116. const props = { label: `Symmetry Mates`, description: Structure.elementDescription(s) };
  117. return new SO.Molecule.Structure(s, props);
  118. }
  119. async function buildSymmetryAssembly(ctx: RuntimeContext, model: Model, generators: StructureSymmetry.Generators, symmetry: Symmetry) {
  120. const base = Structure.ofModel(model);
  121. const s = await StructureSymmetry.buildSymmetryAssembly(base, generators, symmetry).runInContext(ctx);
  122. const props = { label: `Symmetry Assembly`, description: Structure.elementDescription(s) };
  123. return new SO.Molecule.Structure(s, props);
  124. }
  125. export async function create(plugin: PluginContext, ctx: RuntimeContext, model: Model, params?: Params): Promise<SO.Molecule.Structure> {
  126. const symmetry = ModelSymmetry.Provider.get(model)
  127. if (!symmetry || !params || params.name === 'deposited') {
  128. const s = Structure.ofModel(model);
  129. return new SO.Molecule.Structure(s, { label: 'Deposited', description: Structure.elementDescription(s) });
  130. }
  131. if (params.name === 'assembly') {
  132. return buildAssembly(plugin, ctx, model, params.params.id)
  133. }
  134. if (params.name === 'symmetry') {
  135. return buildSymmetry(ctx, model, params.params.ijkMin, params.params.ijkMax)
  136. }
  137. if (params.name === 'symmetry-mates') {
  138. return buildSymmetryMates(ctx, model, params.params.radius)
  139. }
  140. if (params.name === 'symmetry-assembly') {
  141. return buildSymmetryAssembly(ctx, model, params.params.generators, symmetry)
  142. }
  143. throw new Error(`Unknown represetation type: ${(params as any).name}`);
  144. }
  145. }