Browse Source

example of how to loop over bonds and create atom label

arose 7 years ago
parent
commit
501f5a2280
2 changed files with 30 additions and 14 deletions
  1. 29 14
      src/apps/structure-info/index.ts
  2. 1 0
      src/mol-model/structure/structure/unit.ts

+ 29 - 14
src/apps/structure-info/index.ts

@@ -8,9 +8,10 @@ import * as argparse from 'argparse'
 import fetch from 'node-fetch'
 require('util.promisify').shim();
 
+// import { Table } from 'mol-data/db'
 import CIF from 'mol-io/reader/cif'
 import Computation from 'mol-util/computation'
-import { Model, Structure } from 'mol-model/structure'
+import { Model } from 'mol-model/structure'
 
 function showProgress(tag: string, p: Computation.Progress) {
     console.log(`[${tag}] ${p.message} ${p.isIndeterminate ? '' : (p.current / p.max * 100).toFixed(2) + '% '}(${p.elapsedMs | 0}ms)`)
@@ -22,35 +23,49 @@ async function parseCif(data: string|Uint8Array) {
         updateRateMs: 250,
         observer: p => showProgress(`cif parser ${typeof data === 'string' ? 'string' : 'binary'}`, p)
     });
-    console.time('parse cif')
     const parsed = await comp(ctx);
-    console.timeEnd('parse cif')
     if (parsed.isError) throw parsed;
     return parsed
 }
 
-export async function getPdb(pdb: string) {
-    console.log(`downloading ${pdb}...`)
+async function getPdb(pdb: string) {
     const data = await fetch(`https://files.rcsb.org/download/${pdb}.cif`)
-    console.log(`done downloading ${pdb}`)
-
     const parsed = await parseCif(await data.text())
     return CIF.schema.mmCIF(parsed.result.blocks[0])
 }
 
-async function run(pdb: string, out?: string) {
-    const mmcif = await getPdb(pdb)
+function atomLabel(model: Model, aI: number) {
+    const { atoms, residues, chains, residueSegments, chainSegments } = model.hierarchy
+    const { label_atom_id } = atoms
+    const { label_comp_id, label_seq_id } = residues
+    const { label_asym_id } = chains
+    const rI = residueSegments.segmentMap[aI]
+    const cI = chainSegments.segmentMap[aI]
+    return `${label_asym_id.value(cI)} ${label_comp_id.value(rI)} ${label_seq_id.value(rI)} ${label_atom_id.value(aI)}`
+}
 
-    const models = Model.create({ kind: 'mmCIF', data: mmcif });
-    const structure = Structure.ofModel(models[0])
+function printBonds(model: Model) {
+    const { count, offset, neighbor } = Model.bonds(model)
+    for (let i = 0; i < count; ++i) {
+        const start = offset[i];
+        const end = offset[i + 1];
+        for (let bI = start; bI < end; bI++) {
+            console.log(`${atomLabel(model, i)} -- ${atomLabel(model, neighbor[bI])}`)
+        }
+    }
+}
 
-    console.log(structure)
-    console.log(Model.bonds(models[0]))
+async function run(pdb: string) {
+    const mmcif = await getPdb(pdb)
+    const models = Model.create({ kind: 'mmCIF', data: mmcif });
+    // const structure = Structure.ofModel(models[0])
+    // console.log(structure)
+    printBonds(models[0])
 }
 
 const parser = new argparse.ArgumentParser({
   addHelp: true,
-  description: 'Print info about a structure'
+  description: 'Print info about a structure, mainly to test and showcase the mol-model module'
 });
 parser.addArgument([ '--pdb', '-p' ], {
     help: 'Pdb entry id'

+ 1 - 0
src/mol-model/structure/structure/unit.ts

@@ -33,6 +33,7 @@ interface Unit extends SymmetryOperator.ArrayMapping {
     readonly conformation: Model['conformation']
 
     // TODO: add velocity?
+    // AR: would fit into conformation, right?
 }
 
 namespace Unit {