script.ts 6.2 KB

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