Tutorial:Reading a PDB file with the simpler molecular object: the AtomContainer
From MSL-Libraries
Revision as of 22:17, 16 March 2010 by Senes (talk | contribs) (Created page with 'An example on how to read and write atoms from/to a PDB file into using an '''AtomContainer''' can be found in the example program example_AtomConta…')
An example on how to read and write atoms from/to a PDB file into using an AtomContainer can be found in the example program example_AtomContainer_usage.cpp in the examples/ subdirectory.
Source of example_AtomContainer_usage.cpp
- First the container is created. The PDB is read using the readPdb function, which takes the file name (string object).
60 AtomContainer container;
61 if (!container.readPdb(file)) {
// reading failed, error handling code here
68 }
- All atoms can be printed out using the << operator.
73 cout << container << endl;
which produces a list of the atoms
N ALA 1 A [ 2.143 1.328 0.000] (conf 1/ 1) +
CA ALA 1 A [ 1.539 0.000 0.000] (conf 1/ 1) +
CB ALA 1 A [ 2.095 -0.791 1.207] (conf 1/ 1) +
...
- Atoms can also printed (or otherwise accessed) one by one using the [] operator, like if the container was an atom array
79 for (unsigned int i=0; i<container.size(); i++) {
80 cout << "Atom " << i << " is " << container[i] << endl;
81 }
which produces
Atom 0 is N ALA 1 A [ 2.143 1.328 0.000] (conf 1/ 1) +
Atom 1 is CA ALA 1 A [ 1.539 0.000 0.000] (conf 1/ 1) +
Atom 2 is CB ALA 1 A [ 2.095 -0.791 1.207] (conf 1/ 1) +
...
- Atoms can be accessed by id ("chain,resnum,name", for example "A,2,CD1") with the getAtom function
89 Atom & a = container.getAtom("A,2,CD1");
or using the () operator
96 Atom & b = container("A,2,CD1");
- The above functions do not allow for error checking. To check if the atom exists use the atomExists function, followed by the getLastFoundAtom to get the atom without a second lookup.
102 if (container.atomExists("A,2,CD1")) {
103 cout << "Atom A 2 CD1 exists" << endl;
104 // get the atom without a second lookup
105 Atom & c = container.getLastFoundAtom();
106 cout << c << endl;
107 } else {
108 cout << "Atom A 2 CD1 does no exist" << endl;
109 }
- Finally, to write all atoms to a PDB file use the writePdb function
116 if (!container.writePdb("/tmp/example0001_out.pdb")) {
// writing failed, error handling code here
120 }