File: atomBijection.C

package info (click to toggle)
ball 1.5.0%2Bgit20180813.37fc53c-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 239,848 kB
  • sloc: cpp: 326,149; ansic: 4,208; python: 2,303; yacc: 1,778; lex: 1,099; xml: 958; sh: 322; makefile: 93
file content (236 lines) | stat: -rw-r--r-- 6,301 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//

#include <BALL/STRUCTURE/atomBijection.h>

#include <BALL/STRUCTURE/geometricProperties.h>
#include <BALL/KERNEL/PTE.h>
#include <BALL/KERNEL/extractors.h>
#include <BALL/KERNEL/residue.h>
#include <BALL/DATATYPE/hashGrid.h>

using namespace std;

namespace BALL
{

	AtomBijection::AtomBijection(AtomContainer& A, AtomContainer& B, bool limit_to_selection)
		: std::vector<std::pair<Atom*, Atom*> >()
	{
		assignByName(A, B, limit_to_selection);
	}

	/* Calculate the root mean squared deviation */
	double AtomBijection::calculateRMSD() const
	{
		double sum_of_squares = 0.0;

		if (!empty())
		{
			for (Size i = 0; i < size(); ++i)
			{
				Vector3& r(operator [] (i).first->getPosition());
				sum_of_squares += r.getSquareDistance(operator [] (i).second->getPosition());
			}

			// calculate mean square deviation
			sum_of_squares = sqrt(sum_of_squares / (double)size());
		}

		// return RMSD
		return sum_of_squares;
	}

	Size AtomBijection::assignByName(AtomContainer& A, AtomContainer& B, bool limit_to_selection)
	{
		// Clear old bijection.
		clear();

		// Remember the names of A and their atom pointers.
		StringHashMap<Atom*> A_names;
		for (AtomIterator ai = A.beginAtom(); +ai; ++ai)
		{
			A_names.insert(std::pair<String, Atom*>(ai->getFullName(Atom::ADD_VARIANT_EXTENSIONS_AND_ID), &*ai));
		}

		// Iterate over all atoms of B and try to find an 
		// atom in A identical names.
		for (AtomIterator ai = B.beginAtom(); +ai; ++ai)
		{
			if (A_names.has(ai->getFullName(Atom::ADD_VARIANT_EXTENSIONS_AND_ID)))
			{
				if (   !limit_to_selection
						|| (ai->isSelected() || A_names[ai->getFullName(Atom::ADD_VARIANT_EXTENSIONS_AND_ID)]->isSelected()))
				{
					// We found two matching atoms. Remember them.
					push_back(AtomPair(A_names[ai->getFullName(Atom::ADD_VARIANT_EXTENSIONS_AND_ID)], &*ai));
				}
				// Throw away the hash map entry in order to avoid
				// 1:n mappings.
				A_names.erase(ai->getFullName(Atom::ADD_VARIANT_EXTENSIONS_AND_ID));
			}
		}

		// Check whether we could map anything. 
		// If not, try to map by atom name alone.
		if (size() == 0)
		{
			// Next stage: try to map by atom name only.
			A_names.clear();
			for (AtomIterator ai = A.beginAtom(); +ai; ++ai)
			{
				A_names.insert(std::pair<String, Atom*>(ai->getName(), &*ai));
			}
			clear();
			for (AtomIterator ai = B.beginAtom(); +ai; ++ai)
			{
				if (A_names.has(ai->getName()))
				{
					if (   !limit_to_selection
							|| (ai->isSelected() || A_names[ai->getName()]->isSelected()))
					{
						// We found two matching atoms. Remember them.
						push_back(std::pair<Atom*, Atom*>(A_names[ai->getName()], &*ai));
					}
					// Throw away the hash map entry in order to avoid
					// 1:n mappings.
					A_names.erase(ai->getName());
				}
			}
		}

		return size();
	}

	Size AtomBijection::assignTrivial(AtomContainer& A, AtomContainer& B, bool limit_to_selection)
	{
		// Delete old bijection.
		clear();

		// Map in order -- first atom of A onto
		// first atom of B and so on.
		AtomIterator ai(A.beginAtom());
		AtomIterator bi(B.beginAtom());
		for (; +ai && +bi; ++ai, ++bi)
		{
			if (   !limit_to_selection
					|| (ai->isSelected() || bi->isSelected()))
			{
				push_back(std::pair<Atom*, Atom*>(&*ai, &*bi));
			}
		}

		return size();
	}

	Size AtomBijection::assignCAlphaAtoms(AtomContainer& A, AtomContainer& B, bool limit_to_selection)
	{
		// Delete old bijection.
		clear();

		// Extract all residues in A and B
		ResidueList rla(residues(A));
		ResidueList rlb(residues(B));

		// Walk over the residues in parallel
		ResidueList::iterator ita(rla.begin());
		ResidueList::iterator itb(rlb.begin());
		for (; ita != rla.end() && itb != rlb.end(); ++ita, ++itb)
		{
			// If the two residues do have an atom named CA, push back the pair.
			Atom* caa = (*ita)->getAtom("CA");
			Atom* cab = (*itb)->getAtom("CA");
			if (    (caa != 0) && (cab != 0)
					&&  (   !limit_to_selection
					     || (caa->isSelected() || cab->isSelected())))
			{
				push_back(AtomPair(caa, cab));
			}
		}
		//
		return size();
	}

	Size AtomBijection::assignBackboneAtoms(AtomContainer& A, AtomContainer& B, bool limit_to_selection)
	{
		// Delete old bijection.
		clear();

		// Extract all residues in A and B
		ResidueList rla(residues(A));
		ResidueList rlb(residues(B));

		// Walk over the residues in parallel
		ResidueList::iterator ita(rla.begin());
		ResidueList::iterator itb(rlb.begin());
		for (; ita != rla.end() && itb != rlb.end(); ++ita, ++itb)
		{
			// If the two residues do have backbone atoms (CA, C, N, O, H)
			// map then onto each other.
			Atom* a = (*ita)->getAtom("CA");
			Atom* b = (*itb)->getAtom("CA");
			if (    (a != 0) && (b != 0)
				  &&  (   !limit_to_selection
					     || (a->isSelected() || b->isSelected())))
			{
				push_back(AtomPair(a, b));
			}
			a = (*ita)->getAtom("C");
			b = (*itb)->getAtom("C");
			if (    (a != 0) && (b != 0)
				  &&  (   !limit_to_selection
					     || (a->isSelected() || b->isSelected())))
			{
				push_back(AtomPair(a, b));
			}
			a = (*ita)->getAtom("N");
			b = (*itb)->getAtom("N");
			if (    (a != 0) && (b != 0)
					&&  (   !limit_to_selection
					     || (a->isSelected() || b->isSelected())))
			{
				push_back(AtomPair(a, b));
			}
			a = (*ita)->getAtom("O");
			b = (*itb)->getAtom("O");
			if (    (a != 0) && (b != 0)
			    &&  (   !limit_to_selection
					     || (a->isSelected() || b->isSelected())))
			{
				push_back(AtomPair(a, b));
			}
			a = (*ita)->getAtom("H");
			b = (*itb)->getAtom("H");
			if (    (a != 0) && (b != 0)
					&&  (   !limit_to_selection
					     || (a->isSelected() || b->isSelected())))
			{
				push_back(AtomPair(a, b));
			}
		}
		//
		return size();
	}

	Size AtomBijection::assignAtomsByProperty(AtomContainer& A, AtomContainer& B)
	{
		// Delete old bijection.
		clear();
		// Map in order -- first atom of A onto
		// first atom of B and so on.
		AtomIterator ai(A.beginAtom());
		AtomIterator bi(B.beginAtom());
		for (; +ai && +bi; ++ai, ++bi)
		{
			if ( ai->hasProperty("ATOMBIJECTION_RMSD_SELECTION") || bi->hasProperty("ATOMBIJECTION_RMSD_SELECTION"))
			{
				push_back(std::pair<Atom*, Atom*>(&*ai, &*bi));
			}
		}
		return size();
	}



} // namespace BALL