index.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. */
  6. import * as argparse from 'argparse'
  7. import fetch from 'node-fetch'
  8. require('util.promisify').shim();
  9. // import { Table } from 'mol-data/db'
  10. import CIF from 'mol-io/reader/cif'
  11. import { Model } from 'mol-model/structure'
  12. import { Run, Progress } from 'mol-task'
  13. async function parseCif(data: string|Uint8Array) {
  14. const comp = CIF.parse(data);
  15. const parsed = await Run(comp, p => console.log(Progress.format(p)), 250);
  16. if (parsed.isError) throw parsed;
  17. return parsed
  18. }
  19. async function getPdb(pdb: string) {
  20. const data = await fetch(`https://files.rcsb.org/download/${pdb}.cif`)
  21. const parsed = await parseCif(await data.text())
  22. return CIF.schema.mmCIF(parsed.result.blocks[0])
  23. }
  24. function atomLabel(model: Model, aI: number) {
  25. const { atoms, residues, chains, residueSegments, chainSegments } = model.hierarchy
  26. const { label_atom_id } = atoms
  27. const { label_comp_id, label_seq_id } = residues
  28. const { label_asym_id } = chains
  29. const rI = residueSegments.segmentMap[aI]
  30. const cI = chainSegments.segmentMap[aI]
  31. return `${label_asym_id.value(cI)} ${label_comp_id.value(rI)} ${label_seq_id.value(rI)} ${label_atom_id.value(aI)}`
  32. }
  33. function printBonds(model: Model) {
  34. const { count, offset, neighbor } = Model.bonds(model)
  35. for (let i = 0; i < count; ++i) {
  36. const start = offset[i];
  37. const end = offset[i + 1];
  38. for (let bI = start; bI < end; bI++) {
  39. console.log(`${atomLabel(model, i)} -- ${atomLabel(model, neighbor[bI])}`)
  40. }
  41. }
  42. }
  43. async function run(pdb: string) {
  44. const mmcif = await getPdb(pdb)
  45. const models = Model.create({ kind: 'mmCIF', data: mmcif });
  46. // const structure = Structure.ofModel(models[0])
  47. // console.log(structure)
  48. printBonds(models[0])
  49. }
  50. const parser = new argparse.ArgumentParser({
  51. addHelp: true,
  52. description: 'Print info about a structure, mainly to test and showcase the mol-model module'
  53. });
  54. parser.addArgument([ '--pdb', '-p' ], {
  55. help: 'Pdb entry id'
  56. });
  57. interface Args {
  58. pdb: string
  59. }
  60. const args: Args = parser.parseArgs();
  61. run(args.pdb)