util.ts 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /**
  2. * Copyright (c) 2018-2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { Model, ResidueIndex, ElementIndex } from './model';
  7. import { MoleculeType, AtomRole, MoleculeTypeAtomRoleId, getMoleculeType } from './model/types';
  8. import { Vec3 } from 'mol-math/linear-algebra';
  9. import { Unit } from './structure';
  10. import Matrix from 'mol-math/linear-algebra/matrix/matrix';
  11. export function getCoarseBegCompId(unit: Unit.Spheres | Unit.Gaussians, element: ElementIndex) {
  12. const entityKey = unit.coarseElements.entityKey[element]
  13. const seq = unit.model.sequence.byEntityKey[entityKey]
  14. const seq_id_begin = unit.coarseElements.seq_id_begin.value(element)
  15. return seq.compId.value(seq_id_begin - 1) // 1-indexed
  16. }
  17. export function getElementMoleculeType(unit: Unit, element: ElementIndex): MoleculeType {
  18. switch (unit.kind) {
  19. case Unit.Kind.Atomic:
  20. return unit.model.atomicHierarchy.derived.residue.moleculeType[unit.residueIndex[element]]
  21. case Unit.Kind.Spheres:
  22. case Unit.Kind.Gaussians:
  23. // TODO add unit.model.coarseHierarchy.derived.residue.moleculeType
  24. const compId = getCoarseBegCompId(unit, element)
  25. const cc = unit.model.properties.chemicalComponentMap.get(compId)
  26. if (cc) return getMoleculeType(cc.type, compId)
  27. }
  28. return MoleculeType.unknown
  29. }
  30. export function getAtomicMoleculeType(model: Model, rI: ResidueIndex): MoleculeType {
  31. return model.atomicHierarchy.derived.residue.moleculeType[rI]
  32. }
  33. export function getAtomIdForAtomRole(moleculeType: MoleculeType, atomRole: AtomRole) {
  34. const m = MoleculeTypeAtomRoleId[moleculeType]
  35. if (m !== undefined) {
  36. const a = m[atomRole]
  37. if (a !== undefined) return a
  38. }
  39. return ''
  40. }
  41. export function residueLabel(model: Model, rI: number) {
  42. const { residues, chains, residueAtomSegments, chainAtomSegments } = model.atomicHierarchy
  43. const { label_comp_id, label_seq_id } = residues
  44. const { label_asym_id } = chains
  45. const cI = chainAtomSegments.index[residueAtomSegments.offsets[rI]]
  46. return `${label_asym_id.value(cI)} ${label_comp_id.value(rI)} ${label_seq_id.value(rI)}`
  47. }
  48. // const centerPos = Vec3.zero()
  49. // const centerMin = Vec3.zero()
  50. // export function getCenterAndRadius(centroid: Vec3, unit: Unit, indices: ArrayLike<number>) {
  51. // const pos = unit.conformation.position
  52. // const { elements } = unit
  53. // Vec3.set(centroid, 0, 0, 0)
  54. // for (let i = 0, il = indices.length; i < il; ++i) {
  55. // pos(elements[indices[i]], centerPos)
  56. // Vec3.add(centroid, centroid, centerPos)
  57. // Vec3.min(centerMin, centerMin, centerPos)
  58. // }
  59. // Vec3.scale(centroid, centroid, 1/indices.length)
  60. // return Vec3.distance(centerMin, centroid)
  61. // }
  62. const matrixPos = Vec3.zero()
  63. export function getPositionMatrix(unit: Unit, indices: ArrayLike<number>) {
  64. const pos = unit.conformation.position
  65. const mat = Matrix.create(3, indices.length)
  66. const { elements } = unit
  67. for (let i = 0, il = indices.length; i < il; ++i) {
  68. pos(elements[indices[i]], matrixPos)
  69. Vec3.toArray(matrixPos, mat.data, i * 3)
  70. }
  71. return mat
  72. }