atomic-derived.ts 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /**
  2. * Copyright (c) 2018-2021 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { AtomicData, AtomNumber } from '../atomic';
  7. import { AtomicIndex, AtomicDerivedData, AtomicSegments } from '../atomic/hierarchy';
  8. import { ElementIndex, ResidueIndex } from '../../indexing';
  9. import { MoleculeType, getMoleculeType, getComponentType, PolymerType, getPolymerType, isPolymer, ElementSymbol } from '../../types';
  10. import { getAtomIdForAtomRole } from '../../../../../mol-model/structure/util';
  11. import { ChemicalComponentMap } from '../common';
  12. import { isProductionMode } from '../../../../../mol-util/debug';
  13. export function getAtomicDerivedData(data: AtomicData, segments: AtomicSegments, index: AtomicIndex, chemicalComponentMap: ChemicalComponentMap): AtomicDerivedData {
  14. const { label_comp_id, type_symbol, _rowCount: atomCount } = data.atoms;
  15. const { _rowCount: residueCount } = data.residues;
  16. const { offsets } = segments.residueAtomSegments;
  17. const atomicNumber = new Uint8Array(atomCount);
  18. for (let i = 0; i < atomCount; ++i) {
  19. atomicNumber[i] = AtomNumber(type_symbol.value(i));
  20. }
  21. const traceElementIndex = new Int32Array(residueCount);
  22. const directionFromElementIndex = new Int32Array(residueCount);
  23. const directionToElementIndex = new Int32Array(residueCount);
  24. const moleculeType = new Uint8Array(residueCount);
  25. const polymerType = new Uint8Array(residueCount);
  26. const moleculeTypeMap = new Map<string, MoleculeType>();
  27. const polymerTypeMap = new Map<string, PolymerType>();
  28. for (let i = 0 as ResidueIndex; i < residueCount; ++i) {
  29. const compId = label_comp_id.value(offsets[i]);
  30. const chemCompMap = chemicalComponentMap;
  31. let molType: MoleculeType;
  32. let polyType: PolymerType;
  33. if (moleculeTypeMap.has(compId)) {
  34. molType = moleculeTypeMap.get(compId)!;
  35. polyType = polymerTypeMap.get(compId)!;
  36. } else {
  37. let type: string;
  38. if (chemCompMap.has(compId)) {
  39. type = chemCompMap.get(compId)!.type;
  40. } else {
  41. if (!isProductionMode) console.info('chemComp not found', compId);
  42. type = getComponentType(compId);
  43. }
  44. molType = getMoleculeType(type, compId);
  45. // TODO if unknown molecule type, use atom names to guess molecule type
  46. polyType = getPolymerType(type, molType);
  47. moleculeTypeMap.set(compId, molType);
  48. polymerTypeMap.set(compId, polyType);
  49. }
  50. moleculeType[i] = molType;
  51. polymerType[i] = polyType;
  52. const traceAtomId = getAtomIdForAtomRole(polyType, 'trace');
  53. let traceIndex = index.findAtomsOnResidue(i, traceAtomId);
  54. if (traceIndex === -1) {
  55. const coarseAtomId = getAtomIdForAtomRole(polyType, 'coarseBackbone');
  56. traceIndex = index.findAtomsOnResidue(i, coarseAtomId);
  57. if (traceIndex === -1 && isPolymer(molType)) {
  58. traceIndex = index.findElementOnResidue(i, ElementSymbol('C'));
  59. }
  60. }
  61. traceElementIndex[i] = traceIndex;
  62. const directionFromAtomId = getAtomIdForAtomRole(polyType, 'directionFrom');
  63. directionFromElementIndex[i] = index.findAtomsOnResidue(i, directionFromAtomId);
  64. const directionToAtomId = getAtomIdForAtomRole(polyType, 'directionTo');
  65. directionToElementIndex[i] = index.findAtomsOnResidue(i, directionToAtomId);
  66. }
  67. return {
  68. atom: {
  69. atomicNumber: atomicNumber as unknown as ArrayLike<number>
  70. },
  71. residue: {
  72. traceElementIndex: traceElementIndex as unknown as ArrayLike<ElementIndex | -1>,
  73. directionFromElementIndex: directionFromElementIndex as unknown as ArrayLike<ElementIndex | -1>,
  74. directionToElementIndex: directionToElementIndex as unknown as ArrayLike<ElementIndex | -1>,
  75. moleculeType: moleculeType as unknown as ArrayLike<MoleculeType>,
  76. polymerType: polymerType as unknown as ArrayLike<PolymerType>,
  77. }
  78. };
  79. }