model.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /**
  2. * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. * @author David Sehnal <david.sehnal@gmail.com>
  6. */
  7. import * as argparse from 'argparse'
  8. require('util.promisify').shim();
  9. // import { Table } from 'mol-data/db'
  10. import CIF from 'mol-io/reader/cif'
  11. import { Model, Structure, Element, Unit, Queries } from 'mol-model/structure'
  12. // import { Run, Progress } from 'mol-task'
  13. import { OrderedSet } from 'mol-data/int';
  14. import { Table } from 'mol-data/db';
  15. import { mmCIF_Database } from 'mol-io/reader/cif/schema/mmcif';
  16. import { openCif, downloadCif } from './helpers';
  17. import { BitFlags } from 'mol-util';
  18. import { SecondaryStructureType } from 'mol-model/structure/model/types';
  19. async function downloadFromPdb(pdb: string) {
  20. // `https://files.rcsb.org/download/${pdb}.cif`
  21. const parsed = await downloadCif(`http://www.ebi.ac.uk/pdbe/static/entry/${pdb}_updated.cif`, false);
  22. return CIF.schema.mmCIF(parsed.blocks[0]);
  23. }
  24. async function readPdbFile(path: string) {
  25. const parsed = await openCif(path);
  26. return CIF.schema.mmCIF(parsed.blocks[0]);
  27. }
  28. export function atomLabel(model: Model, aI: number) {
  29. const { atoms, residues, chains, residueSegments, chainSegments } = model.atomicHierarchy
  30. const { label_atom_id } = atoms
  31. const { label_comp_id, label_seq_id } = residues
  32. const { label_asym_id } = chains
  33. const rI = residueSegments.segmentMap[aI]
  34. const cI = chainSegments.segmentMap[aI]
  35. return `${label_asym_id.value(cI)} ${label_comp_id.value(rI)} ${label_seq_id.value(rI)} ${label_atom_id.value(aI)}`
  36. }
  37. export function residueLabel(model: Model, rI: number) {
  38. const { residues, chains, residueSegments, chainSegments } = model.atomicHierarchy
  39. const { label_comp_id, label_seq_id } = residues
  40. const { label_asym_id } = chains
  41. const cI = chainSegments.segmentMap[residueSegments.segments[rI]]
  42. return `${label_asym_id.value(cI)} ${label_comp_id.value(rI)} ${label_seq_id.value(rI)}`
  43. }
  44. export function printSecStructure(model: Model) {
  45. console.log('Secondary Structure\n=============');
  46. const { residues } = model.atomicHierarchy;
  47. const { type, key } = model.properties.secondaryStructure;
  48. const count = residues._rowCount;
  49. let rI = 0;
  50. while (rI < count) {
  51. let start = rI;
  52. while (rI < count && key[start] === key[rI]) rI++;
  53. rI--;
  54. if (BitFlags.has(type[start], SecondaryStructureType.Flag.Beta)) {
  55. console.log(`Sheet: ${residueLabel(model, start)} - ${residueLabel(model, rI)} (key ${key[start]})`);
  56. } else if (BitFlags.has(type[start], SecondaryStructureType.Flag.Helix)) {
  57. console.log(`Helix: ${residueLabel(model, start)} - ${residueLabel(model, rI)} (key ${key[start]})`);
  58. }
  59. rI++;
  60. }
  61. }
  62. export function printBonds(structure: Structure) {
  63. for (const unit of structure.units) {
  64. if (!Unit.isAtomic(unit)) continue;
  65. const elements = unit.elements;
  66. const { a, b } = unit.bonds;
  67. const { model } = unit;
  68. if (!a.length) continue;
  69. for (let bI = 0, _bI = a.length; bI < _bI; bI++) {
  70. const x = a[bI], y = b[bI];
  71. if (x >= y) continue;
  72. console.log(`${atomLabel(model, elements[x])} -- ${atomLabel(model, elements[y])}`);
  73. }
  74. }
  75. }
  76. export function printSequence(model: Model) {
  77. console.log('Sequence\n=============');
  78. const { byEntityKey } = model.sequence;
  79. for (const key of Object.keys(byEntityKey)) {
  80. const seq = byEntityKey[+key];
  81. console.log(`${seq.entityId} (${seq.num.value(0)}, ${seq.num.value(seq.num.rowCount - 1)}) (${seq.compId.value(0)}, ${seq.compId.value(seq.compId.rowCount - 1)})`);
  82. // for (let i = 0; i < seq.compId.rowCount; i++) {
  83. // console.log(`${seq.entityId} ${seq.num.value(i)} ${seq.compId.value(i)}`);
  84. // }
  85. }
  86. console.log();
  87. }
  88. export function printUnits(structure: Structure) {
  89. console.log('Units\n=============');
  90. const l = Element.Location();
  91. for (const unit of structure.units) {
  92. l.unit = unit;
  93. const elements = unit.elements;
  94. const size = OrderedSet.size(elements);
  95. if (Unit.isAtomic(l.unit)) {
  96. console.log(`Atomic unit ${unit.id} ${unit.conformation.operator.name}: ${size} elements`);
  97. } else if (Unit.isCoarse(l.unit)) {
  98. console.log(`Coarse unit ${unit.id} ${unit.conformation.operator.name} (${Unit.isSpheres(l.unit) ? 'spheres' : 'gaussians'}): ${size} elements.`);
  99. const props = Queries.props.coarse;
  100. const seq = l.unit.model.sequence;
  101. for (let j = 0, _j = Math.min(size, 3); j < _j; j++) {
  102. l.element = OrderedSet.getAt(elements, j);
  103. const residues: string[] = [];
  104. const start = props.seq_id_begin(l), end = props.seq_id_end(l);
  105. const compId = seq.byEntityKey[props.entityKey(l)].compId.value;
  106. for (let e = start; e <= end; e++) residues.push(compId(e));
  107. console.log(`${props.asym_id(l)}:${start}-${end} (${residues.join('-')}) ${props.asym_id(l)} [${props.x(l).toFixed(2)}, ${props.y(l).toFixed(2)}, ${props.z(l).toFixed(2)}]`);
  108. }
  109. if (size > 3) console.log(`...`);
  110. }
  111. }
  112. }
  113. export function printIHMModels(model: Model) {
  114. if (!model.coarseHierarchy.isDefined) return false;
  115. console.log('IHM Models\n=============');
  116. console.log(Table.formatToString(model.coarseHierarchy.models));
  117. }
  118. async function run(mmcif: mmCIF_Database) {
  119. const models = await Model.create({ kind: 'mmCIF', data: mmcif }).run();
  120. const structure = Structure.ofModel(models[0]);
  121. printSequence(models[0]);
  122. printIHMModels(models[0]);
  123. printUnits(structure);
  124. printBonds(structure);
  125. printSecStructure(models[0]);
  126. }
  127. async function runDL(pdb: string) {
  128. const mmcif = await downloadFromPdb(pdb)
  129. run(mmcif);
  130. }
  131. async function runFile(filename: string) {
  132. const mmcif = await readPdbFile(filename);
  133. run(mmcif);
  134. }
  135. const parser = new argparse.ArgumentParser({
  136. addHelp: true,
  137. description: 'Print info about a structure, mainly to test and showcase the mol-model module'
  138. });
  139. parser.addArgument([ '--download', '-d' ], {
  140. help: 'Pdb entry id'
  141. });
  142. parser.addArgument([ '--file', '-f' ], {
  143. help: 'filename'
  144. });
  145. interface Args {
  146. download?: string,
  147. file?: string
  148. }
  149. const args: Args = parser.parseArgs();
  150. if (args.download) runDL(args.download)
  151. else if (args.file) runFile(args.file)