script.ts 7.0 KB

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