model.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /**
  2. * Copyright (c) 2017-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 UUID from '../../../mol-util/uuid';
  8. import StructureSequence from './properties/sequence';
  9. import { AtomicHierarchy, AtomicConformation, AtomicRanges } from './properties/atomic';
  10. import { CoarseHierarchy, CoarseConformation } from './properties/coarse';
  11. import { Entities, ChemicalComponentMap, MissingResidues, StructAsymMap } from './properties/common';
  12. import { CustomProperties } from '../../custom-property';
  13. import { SaccharideComponentMap } from '../structure/carbohydrates/constants';
  14. import { ModelFormat } from '../../../mol-model-formats/format';
  15. import { calcModelCenter } from './util';
  16. import { Vec3 } from '../../../mol-math/linear-algebra';
  17. import { Mutable } from '../../../mol-util/type-helpers';
  18. import { Coordinates } from '../coordinates';
  19. import { Topology } from '../topology';
  20. import { Task } from '../../../mol-task';
  21. import { IndexPairBonds } from '../../../mol-model-formats/structure/property/bonds/index-pair';
  22. import { createModels } from '../../../mol-model-formats/structure/basic/parser';
  23. import { MmcifFormat } from '../../../mol-model-formats/structure/mmcif';
  24. import { ChainIndex } from './indexing';
  25. import { SymmetryOperator } from '../../../mol-math/geometry';
  26. import { ModelSymmetry } from '../../../mol-model-formats/structure/property/symmetry';
  27. import { Column } from '../../../mol-data/db';
  28. /**
  29. * Interface to the "source data" of the molecule.
  30. *
  31. * "Atoms" are integers in the range [0, atomCount).
  32. */
  33. export interface Model extends Readonly<{
  34. id: UUID,
  35. entryId: string,
  36. label: string,
  37. /** the name of the entry/file/collection the model is part of */
  38. entry: string,
  39. /**
  40. * corresponds to
  41. * - for IHM: `ihm_model_list.model_id`
  42. * - for standard mmCIF: `atom_site.pdbx_PDB_model_num`
  43. * - for models from coordinates: frame index
  44. */
  45. modelNum: number,
  46. /**
  47. * This is a hack to allow "model-index coloring"
  48. */
  49. trajectoryInfo: {
  50. index: number,
  51. size: number
  52. },
  53. sourceData: ModelFormat,
  54. entities: Entities,
  55. sequence: StructureSequence,
  56. atomicHierarchy: AtomicHierarchy,
  57. atomicConformation: AtomicConformation,
  58. atomicRanges: AtomicRanges,
  59. atomicChainOperatorMappinng: Map<ChainIndex, SymmetryOperator>,
  60. properties: {
  61. /** map that holds details about unobserved or zero occurrence residues */
  62. readonly missingResidues: MissingResidues,
  63. /** maps residue name to `ChemicalComponent` data */
  64. readonly chemicalComponentMap: ChemicalComponentMap
  65. /** maps residue name to `SaccharideComponent` data */
  66. readonly saccharideComponentMap: SaccharideComponentMap
  67. /** maps label_asym_id name to `StructAsym` data */
  68. readonly structAsymMap: StructAsymMap
  69. },
  70. customProperties: CustomProperties,
  71. /**
  72. * Not to be accessed directly, each custom property descriptor
  73. * defines property accessors that use this field to store the data.
  74. */
  75. _staticPropertyData: { [name: string]: any },
  76. _dynamicPropertyData: { [name: string]: any },
  77. coarseHierarchy: CoarseHierarchy,
  78. coarseConformation: CoarseConformation
  79. }> {
  80. } { }
  81. export namespace Model {
  82. export type Trajectory = ReadonlyArray<Model>
  83. export function trajectoryFromModelAndCoordinates(model: Model, coordinates: Coordinates): Trajectory {
  84. const trajectory: Mutable<Model.Trajectory> = [];
  85. const { frames } = coordinates;
  86. for (let i = 0, il = frames.length; i < il; ++i) {
  87. const f = frames[i];
  88. const m = {
  89. ...model,
  90. id: UUID.create22(),
  91. modelNum: i,
  92. atomicConformation: Coordinates.getAtomicConformation(f, model.atomicConformation.atomId),
  93. // TODO: add support for supplying sphere and gaussian coordinates in addition to atomic coordinates?
  94. // coarseConformation: coarse.conformation,
  95. customProperties: new CustomProperties(),
  96. _staticPropertyData: Object.create(null),
  97. _dynamicPropertyData: Object.create(null)
  98. };
  99. trajectory.push(m);
  100. }
  101. return trajectory;
  102. }
  103. export function trajectoryFromTopologyAndCoordinates(topology: Topology, coordinates: Coordinates): Task<Trajectory> {
  104. return Task.create('Create Trajectory', async ctx => {
  105. const model = (await createModels(topology.basic, topology.sourceData, ctx))[0];
  106. if (!model) throw new Error('found no model');
  107. const trajectory = trajectoryFromModelAndCoordinates(model, coordinates);
  108. const bondData = { pairs: topology.bonds, count: model.atomicHierarchy.atoms._rowCount };
  109. const indexPairBonds = IndexPairBonds.fromData(bondData);
  110. let index = 0;
  111. for (const m of trajectory) {
  112. IndexPairBonds.Provider.set(m, indexPairBonds);
  113. m.trajectoryInfo.index = index++;
  114. m.trajectoryInfo.size = trajectory.length;
  115. }
  116. return trajectory;
  117. });
  118. }
  119. const CenterProp = '__Center__';
  120. export function getCenter(model: Model): Vec3 {
  121. if (model._dynamicPropertyData[CenterProp]) return model._dynamicPropertyData[CenterProp];
  122. const center = calcModelCenter(model.atomicConformation, model.coarseConformation);
  123. model._dynamicPropertyData[CenterProp] = center;
  124. return center;
  125. }
  126. //
  127. export function hasCarbohydrate(model: Model): boolean {
  128. return model.properties.saccharideComponentMap.size > 0;
  129. }
  130. export function hasProtein(model: Model): boolean {
  131. const { subtype } = model.entities;
  132. for (let i = 0, il = subtype.rowCount; i < il; ++i) {
  133. if (subtype.value(i).startsWith('polypeptide')) return true;
  134. }
  135. return false;
  136. }
  137. export function hasNucleic(model: Model): boolean {
  138. const { subtype } = model.entities;
  139. for (let i = 0, il = subtype.rowCount; i < il; ++i) {
  140. const s = subtype.value(i);
  141. if (s.endsWith('ribonucleotide hybrid') || s.endsWith('ribonucleotide')) return true;
  142. }
  143. return false;
  144. }
  145. export function isFromPdbArchive(model: Model): boolean {
  146. if (!MmcifFormat.is(model.sourceData)) return false;
  147. const { db } = model.sourceData.data;
  148. return (
  149. db.database_2.database_id.isDefined ||
  150. // 4 character PDB id
  151. model.entryId.match(/^[1-9][a-z0-9]{3,3}$/i) !== null ||
  152. // long PDB id
  153. model.entryId.match(/^pdb_[0-9]{4,4}[1-9][a-z0-9]{3,3}$/i) !== null
  154. );
  155. }
  156. export function hasSecondaryStructure(model: Model): boolean {
  157. if (!MmcifFormat.is(model.sourceData)) return false;
  158. const { db } = model.sourceData.data;
  159. return (
  160. db.struct_conf.id.isDefined ||
  161. db.struct_sheet_range.id.isDefined
  162. );
  163. }
  164. const tmpAngles90 = Vec3.create(1.5707963, 1.5707963, 1.5707963); // in radians
  165. const tmpLengths1 = Vec3.create(1, 1, 1);
  166. export function hasCrystalSymmetry(model: Model): boolean {
  167. const spacegroup = ModelSymmetry.Provider.get(model)?.spacegroup;
  168. return !!spacegroup && !(
  169. spacegroup.num === 1 &&
  170. Vec3.equals(spacegroup.cell.anglesInRadians, tmpAngles90) &&
  171. Vec3.equals(spacegroup.cell.size, tmpLengths1)
  172. );
  173. }
  174. export function isFromXray(model: Model): boolean {
  175. if (!MmcifFormat.is(model.sourceData)) return false;
  176. const { db } = model.sourceData.data;
  177. for (let i = 0; i < db.exptl.method.rowCount; i++) {
  178. const v = db.exptl.method.value(i).toUpperCase();
  179. if (v.indexOf('DIFFRACTION') >= 0) return true;
  180. }
  181. return false;
  182. }
  183. export function isFromEm(model: Model): boolean {
  184. if (!MmcifFormat.is(model.sourceData)) return false;
  185. const { db } = model.sourceData.data;
  186. for (let i = 0; i < db.exptl.method.rowCount; i++) {
  187. const v = db.exptl.method.value(i).toUpperCase();
  188. if (v.indexOf('MICROSCOPY') >= 0) return true;
  189. }
  190. return false;
  191. }
  192. export function isFromNmr(model: Model): boolean {
  193. if (!MmcifFormat.is(model.sourceData)) return false;
  194. const { db } = model.sourceData.data;
  195. for (let i = 0; i < db.exptl.method.rowCount; i++) {
  196. const v = db.exptl.method.value(i).toUpperCase();
  197. if (v.indexOf('NMR') >= 0) return true;
  198. }
  199. return false;
  200. }
  201. export function hasXrayMap(model: Model): boolean {
  202. if (!MmcifFormat.is(model.sourceData)) return false;
  203. // Check exprimental method to exclude models solved with
  204. // 'ELECTRON CRYSTALLOGRAPHY' which also have structure factors
  205. if (!isFromXray(model)) return false;
  206. const { db } = model.sourceData.data;
  207. const { status_code_sf } = db.pdbx_database_status;
  208. return status_code_sf.isDefined && status_code_sf.value(0) === 'REL';
  209. }
  210. /**
  211. * Also checks for `content_type` of 'associated EM volume' to exclude cases
  212. * like 6TEK which are solved with 'X-RAY DIFFRACTION' but have an related
  213. * EMDB entry of type 'other EM volume'.
  214. */
  215. export function hasEmMap(model: Model): boolean {
  216. if (!MmcifFormat.is(model.sourceData)) return false;
  217. const { db } = model.sourceData.data;
  218. const { db_name, content_type } = db.pdbx_database_related;
  219. for (let i = 0, il = db.pdbx_database_related._rowCount; i < il; ++i) {
  220. if (db_name.value(i).toUpperCase() === 'EMDB' && content_type.value(i) === 'associated EM volume') {
  221. return true;
  222. }
  223. }
  224. return false;
  225. }
  226. export function hasDensityMap(model: Model): boolean {
  227. if (!MmcifFormat.is(model.sourceData)) return false;
  228. return hasXrayMap(model) || hasEmMap(model);
  229. }
  230. export function probablyHasDensityMap(model: Model): boolean {
  231. if (!MmcifFormat.is(model.sourceData)) return false;
  232. const { db } = model.sourceData.data;
  233. return hasDensityMap(model) || (
  234. // check if from pdb archive but missing relevant meta data
  235. isFromPdbArchive(model) && (
  236. !db.exptl.method.isDefined ||
  237. (isFromXray(model) && (
  238. !db.pdbx_database_status.status_code_sf.isDefined ||
  239. db.pdbx_database_status.status_code_sf.valueKind(0) === Column.ValueKind.Unknown
  240. )) ||
  241. (isFromEm(model) && (
  242. !db.pdbx_database_related.db_name.isDefined
  243. ))
  244. )
  245. );
  246. }
  247. }