<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://msl-libraries.org/index.php?action=history&amp;feed=atom&amp;title=Tutorial%3APassing_atoms_between_objects%3A_the_AtomPointerVector</id>
	<title>Tutorial:Passing atoms between objects: the AtomPointerVector - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://msl-libraries.org/index.php?action=history&amp;feed=atom&amp;title=Tutorial%3APassing_atoms_between_objects%3A_the_AtomPointerVector"/>
	<link rel="alternate" type="text/html" href="http://msl-libraries.org/index.php?title=Tutorial:Passing_atoms_between_objects:_the_AtomPointerVector&amp;action=history"/>
	<updated>2026-04-09T06:35:59Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.32.6</generator>
	<entry>
		<id>http://msl-libraries.org/index.php?title=Tutorial:Passing_atoms_between_objects:_the_AtomPointerVector&amp;diff=588&amp;oldid=prev</id>
		<title>Senes: Created page with 'This is an example on how to pass atoms between objects that use or manipulated them, following the example program example_AtomPointerVector.cpp in the examples/ subdirectory.  …'</title>
		<link rel="alternate" type="text/html" href="http://msl-libraries.org/index.php?title=Tutorial:Passing_atoms_between_objects:_the_AtomPointerVector&amp;diff=588&amp;oldid=prev"/>
		<updated>2010-03-20T03:43:28Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;#039;This is an example on how to pass atoms between objects that use or manipulated them, following the example program example_AtomPointerVector.cpp in the examples/ subdirectory.  …&amp;#039;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;This is an example on how to pass atoms between objects that use or manipulated them, following the example program example_AtomPointerVector.cpp in the examples/ subdirectory.&lt;br /&gt;
&lt;br /&gt;
* Complete source of the '''[http://mslib.svn.sourceforge.net/viewvc/mslib/trunk/examples/example_AtomPointerVector.cpp?view=markup example_AtomPointerVector]''' program&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Background ===&lt;br /&gt;
&lt;br /&gt;
The '''[[MSL Objects:AtomPointerVector|AtomPointerVector]]''' is a vector of Atom pointers (vector&amp;lt;Atom*&amp;gt;) expanded with a few added functions by inheritance.  For example, it computes the geometric center of the atoms, it can save the current coordinates to a buffer and restore them later, and can calculate RMSD with another AtomPointerVector of the same size.&lt;br /&gt;
&lt;br /&gt;
Its main use in MSL is as a way for objects that manipulate or interact with Atoms to communicate.&lt;br /&gt;
&lt;br /&gt;
=== To compile ===&lt;br /&gt;
&amp;lt;source lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
% make bin/example_AtomPointerVector&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== To run the program ===&lt;br /&gt;
Go to the main directory and run the command&lt;br /&gt;
&amp;lt;source lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
% bin/example_AtomPointerVector&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Program description ===&lt;br /&gt;
* First the program creates a new AtomPointerVector from scratch.  We can use the vector's ''push_back'' functions and create new atoms by passing their atomId (chain, resnum, residue type and atom name) and the coordinates.  Here we create a tri-peptide (ALA, ILE, ALA);&lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
56	AtomPointerVector atoms;&lt;br /&gt;
57	atoms.push_back(new Atom(&amp;quot;A,1,ALA,N&amp;quot;,    2.143,  1.328,  0.000));&lt;br /&gt;
58	atoms.push_back(new Atom(&amp;quot;A,1,ALA,CA&amp;quot;,   1.539,  0.000,  0.000));&lt;br /&gt;
59	atoms.push_back(new Atom(&amp;quot;A,1,ALA,CB&amp;quot;,   2.095, -0.791,  1.207));&lt;br /&gt;
...&lt;br /&gt;
73	atoms.push_back(new Atom(&amp;quot;A,3,ALA,C&amp;quot;,   -5.297, -5.192, -0.000));&lt;br /&gt;
74	atoms.push_back(new Atom(&amp;quot;A,3,ALA,OT1&amp;quot;, -6.104, -4.223,  0.000));&lt;br /&gt;
75	atoms.push_back(new Atom(&amp;quot;A,3,ALA,OT2&amp;quot;, -5.649, -6.401, -0.000));&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The AtomPointerVectors supports printing of all atoms with the &amp;lt;&amp;lt; operator.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
80	cout &amp;lt;&amp;lt; atoms &amp;lt;&amp;lt; endl;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
which outputs:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
N    ALA    1  A [     2.143      1.328      0.000] (conf   1/  1) +&lt;br /&gt;
CA   ALA    1  A [     1.539      0.000      0.000] (conf   1/  1) +&lt;br /&gt;
...&lt;br /&gt;
OT1  ALA    3  A [    -6.104     -4.223      0.000] (conf   1/  1) +&lt;br /&gt;
OT2  ALA    3  A [    -5.649     -6.401     -0.000] (conf   1/  1) +&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The elements of an AtomPointerVector can be accessed with a for-loop.  Here we print each atom.  The [] operator returns the i-th element (an Atom pointer).  If we use the () operator (such as in the commented line 87), that would return an Atom.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
85	for (unsigned int i=0; i&amp;lt;atoms.size(); i++) {&lt;br /&gt;
86		cout &amp;lt;&amp;lt; &amp;quot;Atom &amp;quot; &amp;lt;&amp;lt; i &amp;lt;&amp;lt; &amp;quot;: &amp;quot; &amp;lt;&amp;lt; *(atoms[i]) &amp;lt;&amp;lt; endl; // the [] returns an Atom pointer&lt;br /&gt;
87		//cout &amp;lt;&amp;lt; &amp;quot;Atom &amp;quot; &amp;lt;&amp;lt; i &amp;lt;&amp;lt; &amp;quot;: &amp;quot;  &amp;lt;&amp;lt; atoms(i) &amp;lt;&amp;lt; endl; // alternative way, the () operator returns and Atom object (by reference)&lt;br /&gt;
88	}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
output:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
Atom 0: N    ALA    1  A [     2.143      1.328      0.000] (conf   1/  1) +&lt;br /&gt;
Atom 1: CA   ALA    1  A [     1.539      0.000      0.000] (conf   1/  1) +&lt;br /&gt;
...&lt;br /&gt;
Atom 17: OT1  ALA    3  A [    -6.104     -4.223      0.000] (conf   1/  1) +&lt;br /&gt;
Atom 18: OT2  ALA    3  A [    -5.649     -6.401     -0.000] (conf   1/  1) +&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
* The program then demostrates a couple of added feature of this enhanced vector&amp;lt;Atom*&amp;gt;.  First, the calculation of the geometric center of the atoms.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
96	cout &amp;lt;&amp;lt; &amp;quot;The geometric center of the atoms is &amp;quot; &amp;lt;&amp;lt; atoms.getGeometricCenter() &amp;lt;&amp;lt; endl;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
which outputs:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
The geometric center of the atoms is [    -2.248     -2.138     -0.260]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
* Then, the ability of taking snap-shots of the coordinates and saving them under labelled buffers that can be restored later.  In this example, the coordinates are saved, then the atoms are translated by 1Å in the X-axis (using the '''[[MSL Objects:Transforms|Transforms]]''' objec), then the original coordinates are recalled.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
107	atoms.saveCoor(&amp;quot;initialCoors&amp;quot;);&lt;br /&gt;
108&lt;br /&gt;
109	// use the Transforms object to translate the atoms&lt;br /&gt;
110	Transforms tr;&lt;br /&gt;
111	tr.translate(atoms, CartesianPoint(1.0, 0.0, 0.0));&lt;br /&gt;
112	cout &amp;lt;&amp;lt; &amp;quot;Atoms after translation by (1, 0, 0)&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
113	cout &amp;lt;&amp;lt; atoms &amp;lt;&amp;lt; endl;&lt;br /&gt;
114&lt;br /&gt;
115	atoms.applySavedCoor(&amp;quot;initialCoors&amp;quot;);&lt;br /&gt;
116	cout &amp;lt;&amp;lt; &amp;quot;Atoms after restoring the initial coordinates&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
117	cout &amp;lt;&amp;lt; atoms &amp;lt;&amp;lt; endl;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
which outputs:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
Atoms after translation by (1, 0, 0)&lt;br /&gt;
N    ALA    1  A [     3.143      1.328      0.000] (conf   1/  1) +&lt;br /&gt;
CA   ALA    1  A [     2.539      0.000      0.000] (conf   1/  1) +&lt;br /&gt;
...&lt;br /&gt;
OT1  ALA    3  A [    -5.104     -4.223      0.000] (conf   1/  1) +&lt;br /&gt;
OT2  ALA    3  A [    -4.649     -6.401      0.000] (conf   1/  1) +&lt;br /&gt;
&lt;br /&gt;
Atoms after restoring the initial coordinates&lt;br /&gt;
N    ALA    1  A [     2.143      1.328      0.000] (conf   1/  1) +&lt;br /&gt;
CA   ALA    1  A [     1.539      0.000      0.000] (conf   1/  1) +&lt;br /&gt;
...&lt;br /&gt;
OT1  ALA    3  A [    -6.104     -4.223      0.000] (conf   1/  1) +&lt;br /&gt;
OT2  ALA    3  A [    -5.649     -6.401     -0.000] (conf   1/  1) +&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The main purpose of the AtomPointerVector is to connect objects that manipulate or act on Atoms.  For example, and AtomPointerVector can be passed to an emtpy System to populate it.  The System can pass its atoms to a Transforms objects to tranlate them.&lt;br /&gt;
&lt;br /&gt;
* First let's create a System with from the ''atoms'' AtomPointerVector.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
129	System sys(atoms);&lt;br /&gt;
130	cout &amp;lt;&amp;lt; &amp;quot;Print the System created from the AtomPointerVector&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
131	cout &amp;lt;&amp;lt; sys &amp;lt;&amp;lt; endl;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
which outputs the sequence of the System:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
Print the System created from the AtomPointerVector&lt;br /&gt;
A: {1}ALA ILE {3}ALA&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The System makes its own internal copies of the atoms received.  The atoms are identical but the memory addresses are different, as the following check will demonstrate:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
138	string atomId = atoms[0]-&amp;gt;getAtomId(); &lt;br /&gt;
139	Atom * pAtom = &amp;amp;(sys.getAtom(atomId)); &lt;br /&gt;
140	// print both atoms and their address, show they are different&lt;br /&gt;
141	cout &amp;lt;&amp;lt; &amp;quot;From the system         : &amp;quot; &amp;lt;&amp;lt; *pAtom &amp;lt;&amp;lt; &amp;quot; (memory address &amp;quot; &amp;lt;&amp;lt; pAtom &amp;lt;&amp;lt; &amp;quot;)&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
142	cout &amp;lt;&amp;lt; &amp;quot;From the original vector: &amp;quot; &amp;lt;&amp;lt; *(atoms[0]) &amp;lt;&amp;lt; &amp;quot; (memory address &amp;quot; &amp;lt;&amp;lt; atoms[0] &amp;lt;&amp;lt; &amp;quot;)&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
which outputs:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
From the system         : N    ALA    1  A [     2.143      1.328      0.000] (conf   1/  1) + (memory address 0x210d620)&lt;br /&gt;
From the original vector: N    ALA    1  A [     2.143      1.328      0.000] (conf   1/  1) + (memory address 0x20fc930)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* The program then illustrates how the System and Transforms can communicate using an AtomPointerVector.  The System can temporarily pass its atoms to the Transforms to apply a translation.  The ''getAtomPointers()'' returns an AtomPointerVector of the System's Atoms.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
156	tr.translate(sys.getAtomPointers(), CartesianPoint(3.0, 0.0, 0.0));&lt;br /&gt;
...&lt;br /&gt;
163	cout &amp;lt;&amp;lt; &amp;quot;Print the System atoms after a translation by (3, 0, 0)&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
164	cout &amp;lt;&amp;lt; sys.getAtomPointers() &amp;lt;&amp;lt; endl;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
which outputs:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
Print the System atoms after a translation by (3, 0, 0)&lt;br /&gt;
N    ALA    1  A [     5.143      1.328      0.000] (conf   1/  1) +&lt;br /&gt;
CA   ALA    1  A [     4.539      0.000      0.000] (conf   1/  1) +&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Before we quit this program, we need to take care of the memory.  If an AtomPointerVector is created by an object, the garbage collection is normally done automatically by the object's distruptor.  However, inn this program we created Atom pointers manually at the beginning of the program with the ''new'' operator, so we need to delete them.  The ''deletePoitners()'' function takes care of freeing the memory and clearing the vector to size zero.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
170	atoms.deletePointers();&lt;br /&gt;
171	cout &amp;lt;&amp;lt; &amp;quot;Final cleanup, the atom pointer vector has now size &amp;quot; &amp;lt;&amp;lt; atoms.size() &amp;lt;&amp;lt; &amp;quot; and all allocated memory has been freed&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
which outputs:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
Final cleanup, the atom pointer vector has now size 0 and all allocated memory has been freed&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
'''[[Tutorial|Back to the tutorial page]]'''&lt;br /&gt;
&lt;br /&gt;
[[Category:Tutorial]]&lt;/div&gt;</summary>
		<author><name>Senes</name></author>
		
	</entry>
</feed>