script.ts 5.7 KB

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