structure.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /**
  2. * Copyright (c) 2017 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. */
  6. import * as B from 'benchmark'
  7. import * as util from 'util'
  8. import * as fs from 'fs'
  9. import CIF from 'mol-io/reader/cif'
  10. import { Structure, Model, Queries as Q, Atom, AtomGroup, AtomSet, Selection, Symmetry } from 'mol-model/structure'
  11. import { Segmentation } from 'mol-data/int'
  12. import to_mmCIF from 'mol-model/structure/export/mmcif'
  13. require('util.promisify').shim();
  14. const readFileAsync = util.promisify(fs.readFile);
  15. async function readData(path: string) {
  16. if (path.match(/\.bcif$/)) {
  17. const input = await readFileAsync(path)
  18. const data = new Uint8Array(input.byteLength);
  19. for (let i = 0; i < input.byteLength; i++) data[i] = input[i];
  20. return data;
  21. } else {
  22. return readFileAsync(path, 'utf8');
  23. }
  24. }
  25. function *test() {
  26. yield 10;
  27. return 15;
  28. }
  29. async function runIt<T>(itP: () => IterableIterator<T>) {
  30. const it = itP();
  31. while (true) {
  32. const { value, done } = it.next();
  33. if (done) return value;
  34. }
  35. }
  36. runIt(test).then(r => console.log('rerdasdasda', r))
  37. export async function readCIF(path: string) {
  38. console.time('readData');
  39. const input = await readData(path)
  40. console.timeEnd('readData');
  41. console.time('parse');
  42. const comp = typeof input === 'string' ? CIF.parseText(input) : CIF.parseBinary(input);
  43. const parsed = await comp();
  44. console.timeEnd('parse');
  45. if (parsed.isError) {
  46. throw parsed;
  47. }
  48. const data = parsed.result.blocks[0];
  49. console.time('schema')
  50. const mmcif = CIF.schema.mmCIF(data);
  51. console.timeEnd('schema')
  52. console.time('buildModels')
  53. const models = Model.create({ kind: 'mmCIF', data: mmcif });
  54. console.timeEnd('buildModels')
  55. const structures = models.map(Structure.ofModel);
  56. return { mmcif, models, structures };
  57. }
  58. export namespace PropertyAccess {
  59. function baseline(model: Model) {
  60. const atom_site = model.sourceData.data.atom_site;
  61. const id = atom_site.id.value;
  62. let s = 0;
  63. for (let i = 0, _i = atom_site._rowCount; i < _i; i++) {
  64. s += id(i);
  65. }
  66. return s;
  67. }
  68. function sumProperty(structure: Structure, p: Atom.Property<number>) {
  69. const { atoms, units } = structure;
  70. const unitIds = AtomSet.unitIds(atoms);
  71. const l = Atom.Location();
  72. let s = 0;
  73. for (let i = 0, _i = unitIds.length; i < _i; i++) {
  74. l.unit = units[unitIds[i]];
  75. const set = AtomSet.unitGetByIndex(atoms, i);
  76. for (let j = 0, _j = AtomGroup.size(set); j < _j; j++) {
  77. l.atom = AtomGroup.getAt(set, j);
  78. s += p(l);
  79. }
  80. }
  81. return s;
  82. }
  83. function sumPropertySegmented(structure: Structure, p: Atom.Property<number>) {
  84. const { atoms, units } = structure;
  85. const unitIds = AtomSet.unitIds(atoms);
  86. const l = Atom.Location();
  87. let s = 0;
  88. let vA = 0, cC = 0, rC = 0;
  89. for (let i = 0, _i = unitIds.length; i < _i; i++) {
  90. const unit = units[unitIds[i]];
  91. l.unit = unit;
  92. const set = AtomSet.unitGetByIndex(atoms, i);
  93. const chainsIt = Segmentation.transientSegments(unit.hierarchy.chainSegments, set.atoms);
  94. const residues = unit.hierarchy.residueSegments;
  95. while (chainsIt.hasNext) {
  96. cC++;
  97. const chainSegment = chainsIt.move();
  98. const residuesIt = Segmentation.transientSegments(residues, set.atoms, chainSegment);
  99. while (residuesIt.hasNext) {
  100. rC++;
  101. const residueSegment = residuesIt.move();
  102. // l.atom = OrdSet.getAt(set, residueSegment.start);
  103. // console.log(unit.hierarchy.residues.auth_comp_id.value(unit.residueIndex[l.atom]), l.atom, OrdSet.getAt(set, residueSegment.end))
  104. for (let j = residueSegment.start, _j = residueSegment.end; j < _j; j++) {
  105. l.atom = AtomGroup.getAt(set, j);
  106. vA++;
  107. s += p(l);
  108. }
  109. }
  110. }
  111. }
  112. console.log('seg atom count', vA, cC, rC);
  113. return s;
  114. }
  115. function sumPropertyResidue(structure: Structure, p: Atom.Property<number>) {
  116. const { atoms, units } = structure;
  117. const unitIds = AtomSet.unitIds(atoms);
  118. const l = Atom.Location();
  119. let s = 0;
  120. for (let i = 0, _i = unitIds.length; i < _i; i++) {
  121. const unit = units[unitIds[i]];
  122. l.unit = unit;
  123. const set = AtomSet.unitGetByIndex(atoms, i);
  124. const residuesIt = Segmentation.transientSegments(unit.hierarchy.residueSegments, set.atoms);
  125. while (residuesIt.hasNext) {
  126. l.atom = AtomGroup.getAt(set, residuesIt.move().start);
  127. s += p(l);
  128. }
  129. }
  130. return s;
  131. }
  132. function sumPropertyAtomSetIt(structure: Structure, p: Atom.Property<number>) {
  133. const { atoms, units } = structure;
  134. let s = 0;
  135. const atomsIt = AtomSet.atoms(atoms);
  136. const l = Atom.Location();
  137. while (atomsIt.hasNext) {
  138. const a = atomsIt.move();
  139. l.unit = units[Atom.unit(a)];
  140. l.atom = Atom.index(a);
  141. s += p(l);
  142. }
  143. return s;
  144. }
  145. // function sumPropertySegmentedMutable(structure: Structure, p: Property<number>) {
  146. // const { atoms, units } = structure;
  147. // const unitIds = AtomSet.unitIds(atoms);
  148. // const l = Property.createLocation();
  149. // let s = 0;
  150. // for (let i = 0, _i = unitIds.length; i < _i; i++) {
  151. // const unit = units[unitIds[i]];
  152. // l.unit = unit;
  153. // const set = AtomSet.unitGetByIndex(atoms, i);
  154. // const chainsIt = Segmentation.transientSegments(unit.hierarchy.chainSegments, set);
  155. // const residuesIt = Segmentation.transientSegments(unit.hierarchy.residueSegments, set);
  156. // while (chainsIt.hasNext) {
  157. // const chainSegment = chainsIt.move();
  158. // residuesIt.updateRange(chainSegment);
  159. // while (residuesIt.hasNext) {
  160. // const residueSegment = residuesIt.move();
  161. // for (let j = residueSegment.start, _j = residueSegment.end; j < _j; j++) {
  162. // l.atom = OrdSet.getAt(set, j);
  163. // s += p(l);
  164. // }
  165. // }
  166. // }
  167. // }
  168. // return s;
  169. // }
  170. function sumDirect(structure: Structure) {
  171. const { atoms, units } = structure;
  172. const unitIds = AtomSet.unitIds(atoms);
  173. let s = 0;
  174. for (let i = 0, _i = unitIds.length; i < _i; i++) {
  175. const unitId = unitIds[i];
  176. const unit = units[unitId];
  177. const set = AtomSet.unitGetByIndex(atoms, i);
  178. //const { residueIndex, chainIndex } = unit;
  179. const p = unit.conformation.atomId.value;
  180. for (let j = 0, _j = AtomGroup.size(set); j < _j; j++) {
  181. const aI = AtomGroup.getAt(set, j);
  182. s += p(aI);
  183. }
  184. }
  185. return s;
  186. }
  187. // function concatProperty(structure: Structure, p: Property<string>) {
  188. // const { atoms, units } = structure;
  189. // const unitIds = AtomSet.unitIds(atoms);
  190. // const l = Property.createLocation(structure);
  191. // let s = [];
  192. // for (let i = 0, _i = unitIds.length; i < _i; i++) {
  193. // const unitId = unitIds[i];
  194. // l.unit = units[unitId];
  195. // const set = AtomSet.unitGetByIndex(atoms, i);
  196. // const { residueIndex, chainIndex } = l.unit;
  197. // for (let j = 0, _j = OrdSet.size(set); j < _j; j++) {
  198. // const aI = OrdSet.getAt(set, j);
  199. // l.atom = aI;
  200. // l.residueIndex = residueIndex[aI];
  201. // l.chainIndex = chainIndex[aI];
  202. // s[s.length] = p(l);
  203. // }
  204. // }
  205. // return s;
  206. // }
  207. export function write(s: Structure) {
  208. console.log(to_mmCIF('test', s));
  209. }
  210. export function testAssembly(id: string, s: Structure) {
  211. console.time('assembly')
  212. const a = Symmetry.buildAssembly(s, '1');
  213. console.timeEnd('assembly')
  214. fs.writeFileSync(`e:/test/molstar/${id}_assembly.bcif`, to_mmCIF(id, a, true));
  215. console.log('exported');
  216. //write(a);
  217. }
  218. export async function run() {
  219. //const { structures, models, mmcif } = await readCIF('./examples/1cbs_full.bcif');
  220. const { structures, models } = await readCIF('e:/test/quick/3j3q_full.bcif');
  221. //const { structures, models, mmcif } = await readCIF('e:/test/quick/1cbs_updated.cif');
  222. //const { structures, models/*, mmcif*/ } = await readCIF('e:/test/quick/5j7v_updated.cif');
  223. //console.log(mmcif.pdbx_struct_oper_list.matrix.toArray());
  224. // console.log(mmcif.pdbx_struct_oper_list.vector.toArray());
  225. // testAssembly('5j7v', structures[0]);
  226. // throw '';
  227. // console.log(models[0].symmetry.assemblies);
  228. //const { structures, models } = await readCIF('e:/test/molstar/3j3q.bcif');
  229. // fs.writeFileSync('e:/test/molstar/3j3q.bcif', to_mmCIF('test', structures[0], true));
  230. // return;
  231. // console.log(toMmCIFString('test', structures[0]));
  232. // return;
  233. console.log('bs', baseline(models[0]));
  234. console.log('sp', sumProperty(structures[0], l => l.unit.model.conformation.atomId.value(l.atom)));
  235. console.log(sumPropertySegmented(structures[0], l => l.unit.model.conformation.atomId.value(l.atom)));
  236. //console.log(sumPropertySegmentedMutable(structures[0], l => l.unit.model.conformation.atomId.value(l.atom)));
  237. console.log(sumPropertyAtomSetIt(structures[0], l => l.unit.model.conformation.atomId.value(l.atom)));
  238. //console.log(sumProperty(structures[0], Property.cachedAtomColumn(m => m.conformation.atomId)));
  239. console.log(sumDirect(structures[0]));
  240. console.log('r', sumPropertyResidue(structures[0], l => l.unit.hierarchy.residues.auth_seq_id.value(l.unit.residueIndex[l.atom])));
  241. console.time('atom.x');
  242. console.log('atom.x', sumProperty(structures[0], Q.props.atom.x));
  243. console.timeEnd('atom.x');
  244. console.time('__x')
  245. console.log('__x', sumProperty(structures[0], l => l.unit.conformation.x[l.atom]));
  246. console.timeEnd('__x')
  247. //const authSeqId = Atom.property(l => l.unit.hierarchy.residues.auth_seq_id.value(l.unit.residueIndex[l.atom]));
  248. //const auth_seq_id = Q.props.residue.auth_seq_id;
  249. const auth_comp_id = Q.props.residue.auth_comp_id;
  250. //const auth_asym_id = Q.props.chain.auth_asym_id;
  251. //const set = new Set(['A', 'B', 'C', 'D']);
  252. //const q = Q.generators.atomGroups({ atomTest: l => auth_seq_id(l) < 3 });
  253. const q = Q.generators.atoms({ atomTest: Q.pred.eq(Q.props.residue.auth_comp_id, 'ALA') });
  254. const P = Q.props
  255. //const q0 = Q.generators.atoms({ atomTest: l => auth_comp_id(l) === 'ALA' });
  256. const q1 = Q.generators.atoms({ residueTest: l => auth_comp_id(l) === 'ALA' });
  257. const q2 = Q.generators.atoms({ residueTest: l => auth_comp_id(l) === 'ALA', groupBy: Q.props.residue.key });
  258. const q3 = Q.generators.atoms({
  259. chainTest: Q.pred.inSet(P.chain.auth_asym_id, ['A', 'B', 'C', 'D']),
  260. residueTest: Q.pred.eq(P.residue.auth_comp_id, 'ALA')
  261. });
  262. q(structures[0]);
  263. //console.log(to_mmCIF('test', Selection.union(q0r)));
  264. console.time('q1')
  265. q1(structures[0]);
  266. console.timeEnd('q1')
  267. console.time('q1')
  268. q1(structures[0]);
  269. console.timeEnd('q1')
  270. console.time('q2')
  271. const q2r = q2(structures[0]);
  272. console.timeEnd('q2')
  273. console.log(Selection.structureCount(q2r));
  274. //console.log(q1(structures[0]));
  275. //const col = models[0].conformation.atomId.value;
  276. const suite = new B.Suite();
  277. suite
  278. //.add('test q', () => q1(structures[0]))
  279. //.add('test q', () => q(structures[0]))
  280. .add('test q1', () => q1(structures[0]))
  281. .add('test q3', () => q3(structures[0]))
  282. //.add('test int', () => sumProperty(structures[0], l => col(l.atom)))
  283. // .add('sum residue', () => sumPropertyResidue(structures[0], l => l.unit.hierarchy.residues.auth_seq_id.value(l.unit.residueIndex[l.atom])))
  284. // .add('baseline', () => baseline(models[0]))
  285. // .add('direct', () => sumDirect(structures[0]))
  286. //.add('normal int', () => sumProperty(structures[0], l => l.unit.model.conformation.atomId.value(l.atom)))
  287. //.add('atom set it int', () => sumPropertyAtomSetIt(structures[0], l => l.unit.conformation.atomId.value(l.atom)))
  288. // .add('segmented faster int', () => sumPropertySegmented(structures[0], l => l.unit.conformation.atomId.value(l.atom)))
  289. // .add('faster int', () => sumProperty(structures[0], l => l.unit.conformation.atomId.value(l.atom)))
  290. //.add('segmented faster _x', () => sumPropertySegmented(structures[0], l => l.unit.conformation.__x[l.atom]))
  291. //.add('faster _x', () => sumProperty(structures[0], l => l.unit.conformation.__x[l.atom] + l.unit.conformation.__y[l.atom] + l.unit.conformation.__z[l.atom]))
  292. //.add('segmented mut faster int', () => sumPropertySegmentedMutable(structures[0], l => l.unit.conformation.atomId.value(l.atom)))
  293. //.add('normal shortcut int', () => sumProperty(structures[0], l => l.conformation.atomId.value(l.atom)))
  294. //.add('cached int', () => sumProperty(structures[0], Property.cachedAtomColumn(m => m.conformation.atomId)))
  295. //.add('concat str', () => concatProperty(structures[0], l => l.unit.model.hierarchy.atoms.auth_atom_id.value(l.atom)))
  296. //.add('cached concat str', () => concatProperty(structures[0], Property.cachedAtomColumn(m => m.hierarchy.atoms.auth_atom_id)))
  297. .on('cycle', (e: any) => console.log(String(e.target)))
  298. .run();
  299. }
  300. }
  301. PropertyAccess.run();