script.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /**
  2. * Copyright (c) 2017 molio 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 util from 'util'
  8. import * as fs from 'fs'
  9. require('util.promisify').shim();
  10. const readFileAsync = util.promisify(fs.readFile);
  11. import Gro from './reader/gro/parser'
  12. import CIF from './reader/cif/index'
  13. import { getSchema } from './reader/cif/schema/utils'
  14. const file = '1crn.gro'
  15. // const file = 'water.gro'
  16. // const file = 'test.gro'
  17. // const file = 'md_1u19_trj.gro'
  18. function showProgress(tag: string, p: Computation.Progress) {
  19. console.log(`[${tag}] ${p.message} ${p.isIndeterminate ? '' : (p.current / p.max * 100).toFixed(2) + '% '}(${p.elapsedMs | 0}ms)`)
  20. }
  21. async function runGro(input: string) {
  22. console.time('parseGro');
  23. const comp = Gro(input);
  24. const ctx = Computation.observable({ updateRateMs: 150, observer: p => showProgress('GRO', p) });
  25. const parsed = await comp(ctx);
  26. console.timeEnd('parseGro');
  27. if (parsed.isError) {
  28. console.log(parsed);
  29. return;
  30. }
  31. const groFile = parsed.result
  32. console.log('structure count: ', groFile.structures.length);
  33. const data = groFile.structures[0];
  34. // const header = groFile.blocks[0].getCategory('header')
  35. const { header, atoms } = data;
  36. console.log(JSON.stringify(header, null, 2));
  37. console.log('number of atoms:', atoms.count);
  38. console.log(`'${atoms.residueNumber.value(1)}'`)
  39. console.log(`'${atoms.residueName.value(1)}'`)
  40. console.log(`'${atoms.atomName.value(1)}'`)
  41. console.log(atoms.z.value(1))
  42. console.log(`'${atoms.z.value(1)}'`)
  43. const n = atoms.count;
  44. console.log('rowCount', n)
  45. console.time('getFloatArray x')
  46. const x = atoms.x.toArray({ array: Float32Array })
  47. console.timeEnd('getFloatArray x')
  48. console.log(x.length, x[0], x[x.length - 1])
  49. console.time('getFloatArray y')
  50. const y = atoms.y.toArray({ array: Float32Array })
  51. console.timeEnd('getFloatArray y')
  52. console.log(y.length, y[0], y[y.length - 1])
  53. console.time('getFloatArray z')
  54. const z = atoms.z.toArray({ array: Float32Array })
  55. console.timeEnd('getFloatArray z')
  56. console.log(z.length, z[0], z[z.length - 1])
  57. console.time('getIntArray residueNumber')
  58. const residueNumber = atoms.residueNumber.toArray({ array: Int32Array })
  59. console.timeEnd('getIntArray residueNumber')
  60. console.log(residueNumber.length, residueNumber[0], residueNumber[residueNumber.length - 1])
  61. }
  62. export async function _gro() {
  63. const input = await readFileAsync(`./examples/${file}`, 'utf8')
  64. runGro(input)
  65. }
  66. // _gro()
  67. async function runCIF(input: string | Uint8Array) {
  68. console.time('parseCIF');
  69. const comp = typeof input === 'string' ? CIF.parseText(input) : CIF.parseBinary(input);
  70. const ctx = Computation.observable({ updateRateMs: 250, observer: p => showProgress('CIF', p) });
  71. const parsed = await comp(ctx);
  72. console.timeEnd('parseCIF');
  73. if (parsed.isError) {
  74. console.log(parsed);
  75. return;
  76. }
  77. const data = parsed.result.blocks[0];
  78. const atom_site = data.categories._atom_site;
  79. console.log(atom_site.getField('Cartn_x')!.float(0));
  80. //console.log(atom_site.getField('label_atom_id')!.toStringArray());
  81. const mmcif = CIF.schema.mmCIF(data);
  82. console.log(mmcif.atom_site.Cartn_x.value(0));
  83. console.log(mmcif.entity.type.toArray());
  84. console.log(mmcif.pdbx_struct_oper_list.matrix.value(0));
  85. }
  86. export async function _cif() {
  87. let path = `./examples/1cbs_updated.cif`;
  88. path = '../test/3j3q.cif' // lets have a relative path for big test files
  89. const input = await readFileAsync(path, 'utf8')
  90. console.log('------------------');
  91. console.log('Text CIF:');
  92. runCIF(input);
  93. path = `./examples/1cbs_full.bcif`;
  94. // const path = 'c:/test/quick/3j3q.cif';
  95. const input2 = await readFileAsync(path)
  96. console.log('------------------');
  97. console.log('BinaryCIF:');
  98. const data = new Uint8Array(input2.byteLength);
  99. for (let i = 0; i < input2.byteLength; i++) data[i] = input2[i];
  100. runCIF(input2);
  101. }
  102. // _cif();
  103. async function runDic(input: string | Uint8Array) {
  104. console.time('parseDic');
  105. const comp = typeof input === 'string' ? CIF.parseText(input) : CIF.parseBinary(input);
  106. const ctx = Computation.observable({ updateRateMs: 250, observer: p => showProgress('DIC', p) });
  107. const parsed = await comp(ctx);
  108. console.timeEnd('parseDic');
  109. if (parsed.isError) {
  110. console.log(parsed);
  111. return;
  112. }
  113. const schema = getSchema(parsed.result.blocks[0])
  114. console.log(util.inspect(schema, {showHidden: false, depth: 3}))
  115. // console.log(util.inspect(Object.keys(schema).length, {showHidden: false, depth: 1}))
  116. }
  117. export async function _dic() {
  118. let path = './build/dics/mmcif_pdbx_v50.dic'
  119. const input = await readFileAsync(path, 'utf8')
  120. console.log('------------------');
  121. console.log('Text DIC:');
  122. runDic(input);
  123. }
  124. _dic();
  125. import Computation from './utils/computation'
  126. const comp = Computation.create(async ctx => {
  127. for (let i = 0; i < 0; i++) {
  128. await new Promise(res => setTimeout(res, 500));
  129. if (ctx.requiresUpdate) await ctx.update({ message: 'working', current: i, max: 2 });
  130. }
  131. return 42;
  132. });
  133. async function testComp() {
  134. const ctx = Computation.observable({ observer: p => showProgress('test', p) });
  135. const ret = await comp(ctx);
  136. console.log('computation returned', ret);
  137. }
  138. testComp();