util.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. const EmptyAtomIds = new Set<string>()
  34. export function getAtomIdForAtomRole(moleculeType: MoleculeType, atomRole: AtomRole) {
  35. const m = MoleculeTypeAtomRoleId[moleculeType]
  36. if (m !== undefined) {
  37. const a = m[atomRole]
  38. if (a !== undefined) return a
  39. }
  40. return EmptyAtomIds
  41. }
  42. export function residueLabel(model: Model, rI: number) {
  43. const { residues, chains, residueAtomSegments, chainAtomSegments } = model.atomicHierarchy
  44. const { label_comp_id, label_seq_id } = residues
  45. const { label_asym_id } = chains
  46. const cI = chainAtomSegments.index[residueAtomSegments.offsets[rI]]
  47. return `${label_asym_id.value(cI)} ${label_comp_id.value(rI)} ${label_seq_id.value(rI)}`
  48. }
  49. export function elementLabel(model: Model, index: ElementIndex) {
  50. const { atoms, residues, chains, residueAtomSegments, chainAtomSegments } = model.atomicHierarchy
  51. const { label_atom_id } = atoms
  52. const { auth_seq_id, label_comp_id } = residues
  53. const { auth_asym_id } = chains
  54. const residueIndex = residueAtomSegments.index[index]
  55. const chainIndex = chainAtomSegments.index[residueIndex]
  56. return `[${label_comp_id.value(residueIndex)}]${auth_seq_id.value(residueIndex)}:${auth_asym_id.value(chainIndex)}.${label_atom_id.value(index)}`
  57. }
  58. // const centerPos = Vec3.zero()
  59. // const centerMin = Vec3.zero()
  60. // export function getCenterAndRadius(centroid: Vec3, unit: Unit, indices: ArrayLike<number>) {
  61. // const pos = unit.conformation.position
  62. // const { elements } = unit
  63. // Vec3.set(centroid, 0, 0, 0)
  64. // for (let i = 0, il = indices.length; i < il; ++i) {
  65. // pos(elements[indices[i]], centerPos)
  66. // Vec3.add(centroid, centroid, centerPos)
  67. // Vec3.min(centerMin, centerMin, centerPos)
  68. // }
  69. // Vec3.scale(centroid, centroid, 1/indices.length)
  70. // return Vec3.distance(centerMin, centroid)
  71. // }
  72. const matrixPos = Vec3.zero()
  73. export function getPositionMatrix(unit: Unit, indices: ArrayLike<number>) {
  74. const pos = unit.conformation.position
  75. const mat = Matrix.create(3, indices.length)
  76. const { elements } = unit
  77. for (let i = 0, il = indices.length; i < il; ++i) {
  78. pos(elements[indices[i]], matrixPos)
  79. Vec3.toArray(matrixPos, mat.data, i * 3)
  80. }
  81. return mat
  82. }