Difference between revisions of "Tutorial:Reading a PDB file with the complex molecular object: the System"

From MSL-Libraries
Jump to navigationJump to search
(Created page with 'An example on how to read and write atoms from/to a PDB file into using an '''System''' can be found in the example program example_System_usage.cpp in the…')
 
(Program description)
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
An example on how to read and write atoms from/to a PDB file into using an '''[[MSL Objects:System|System]]''' can be found in the example program example_System_usage.cpp in the examples/ subdirectory.
+
This is an example on how to read and write atoms from/to a PDB file into using an '''[[MSL Objects:System|System]]''' – the more complex and powerful molecular container in MSL – following the example program example_read_write_PDBs_with_the_System.cpp in the examples/ subdirectory.
 +
 
 +
{| border="0" cellpadding="2" style="background:#FFFF99"
 +
|
 +
'''NOTE:''' the programs is very similar to the example illustrated in the [[Tutorial:Reading a PDB file with the simpler molecular object: the AtomContainer | analogous tutorial with the AtomContainer]].  Where the two differ it is highlighted with a note.
 +
|}
  
 
[http://mslib.svn.sourceforge.net/viewvc/mslib/trunk/examples/example_read_write_PDBs_with_the_System.cpp?view=markup Complete source of example_read_write_PDBs_with_the_System.cpp]
 
[http://mslib.svn.sourceforge.net/viewvc/mslib/trunk/examples/example_read_write_PDBs_with_the_System.cpp?view=markup Complete source of example_read_write_PDBs_with_the_System.cpp]
Line 9: Line 14:
  
 
=== To run the program ===
 
=== To run the program ===
Go to the main directory and run the command
+
Go to the main directory and run the command ('''note''', the location of the exampleFiles subdirectory needs to be provided as an argument)
 
<source lang="text">
 
<source lang="text">
 
% bin/example_read_write_PDBs_with_the_System exampleFiles/
 
% bin/example_read_write_PDBs_with_the_System exampleFiles/
Line 15: Line 20:
  
 
=== Program description ===
 
=== Program description ===
 +
 +
 
* First the container is created.  The PDB is read using the ''readPdb'' function, which takes the file name (string object).
 
* First the container is created.  The PDB is read using the ''readPdb'' function, which takes the file name (string object).
 
<source lang="cpp">
 
<source lang="cpp">
Line 31: Line 38:
 
A: {1}ALA ILE {3}ALA
 
A: {1}ALA ILE {3}ALA
 
</source>
 
</source>
('''NOTE:''' here the System behave differently from the AtomContainer, which would print all atoms).
+
{| border="0" cellpadding="2" style="background:#FFFF99"
 +
|
 +
'''NOTE:''' here the System behave differently from the AtomContainer.  The << operator prints all atoms in the AtomContainer.
 +
|}
 +
 
  
 
* Atoms can also printed (or otherwise accessed) one by one using the [] operator, as if the container was an atom array.  
 
* Atoms can also printed (or otherwise accessed) one by one using the [] operator, as if the container was an atom array.  
Line 46: Line 57:
 
...
 
...
 
</source>
 
</source>
 +
{| border="0" cellpadding="2" style="background:#FFFF99"
 +
|
 
'''NOTE:''' unlike in the AtomContainer, the [] and () operators are NOT redundant in the System.  The () operator would returns Chain objects instead of Atom objects).
 
'''NOTE:''' unlike in the AtomContainer, the [] and () operators are NOT redundant in the System.  The () operator would returns Chain objects instead of Atom objects).
 +
|}
 +
{| border="0" cellpadding="2" style="background:#FFFF99"
 +
|
 +
'''NOTE 2:''' unlike the AtomContainer, the number of atoms in the System is given by the function ''atomSize''.  The ''size'' function returns the number of chains.
 +
|}
  
'''NOTE 2:''' the number of atoms is given by the function ''atomSize'' in the System, the ''size'' function returns the number of chains.
 
  
 
* Atoms can be accessed by ''id'' ("chain,resnum,name", for example "A,2,CD1") with the ''getAtom'' function
 
* Atoms can be accessed by ''id'' ("chain,resnum,name", for example "A,2,CD1") with the ''getAtom'' function
Line 76: Line 93:
 
120 }
 
120 }
 
</source>
 
</source>
 +
 +
----
 +
'''[[Tutorial|Back to the tutorial page]]'''
 +
 +
[[Category:Tutorial]]

Latest revision as of 13:47, 18 March 2010

This is an example on how to read and write atoms from/to a PDB file into using an System – the more complex and powerful molecular container in MSL – following the example program example_read_write_PDBs_with_the_System.cpp in the examples/ subdirectory.

NOTE: the programs is very similar to the example illustrated in the analogous tutorial with the AtomContainer. Where the two differ it is highlighted with a note.

Complete source of example_read_write_PDBs_with_the_System.cpp

To compile

% make bin/example_read_write_PDBs_with_the_System

To run the program

Go to the main directory and run the command (note, the location of the exampleFiles subdirectory needs to be provided as an argument)

% bin/example_read_write_PDBs_with_the_System exampleFiles/

Program description

  • First the container is created. The PDB is read using the readPdb function, which takes the file name (string object).
60	System container;
61	if (!container.readPdb(file)) {
		// reading failed, error handling code here
68	}
  • The sequence be printed out using the << operator
73	cout << container << endl;

which produces

A: {1}ALA ILE {3}ALA

NOTE: here the System behave differently from the AtomContainer. The << operator prints all atoms in the AtomContainer.


  • Atoms can also printed (or otherwise accessed) one by one using the [] operator, as if the container was an atom array.
79	for (unsigned int i=0; i<container.atomSize(); 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) +
...

NOTE: unlike in the AtomContainer, the [] and () operators are NOT redundant in the System. The () operator would returns Chain objects instead of Atom objects).

NOTE 2: unlike the AtomContainer, the number of atoms in the System is given by the function atomSize. The size function returns the number of chains.


  • 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 [] string operator (NOTE: again, the [] and () operators are NOT redundant)

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	}

Back to the tutorial page