File: Weights_test.C

package info (click to toggle)
openms 1.11.1-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 436,688 kB
  • ctags: 150,907
  • sloc: cpp: 387,126; xml: 71,547; python: 7,764; ansic: 2,626; php: 2,499; sql: 737; ruby: 342; sh: 325; makefile: 128
file content (328 lines) | stat: -rw-r--r-- 10,032 bytes parent folder | download
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
// --------------------------------------------------------------------------
//                   OpenMS -- Open-Source Mass Spectrometry               
// --------------------------------------------------------------------------
// Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
// ETH Zurich, and Freie Universitaet Berlin 2002-2013.
// 
// This software is released under a three-clause BSD license:
//  * Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
//  * Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimer in the
//    documentation and/or other materials provided with the distribution.
//  * Neither the name of any author or any participating institution 
//    may be used to endorse or promote products derived from this software 
//    without specific prior written permission.
// For a full list of authors, refer to the file AUTHORS. 
// --------------------------------------------------------------------------
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING 
// INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// 
// --------------------------------------------------------------------------
// $Maintainer: Stephan Aiche $
// $Authors: Stephan Aiche $
// --------------------------------------------------------------------------

#include <OpenMS/CONCEPT/ClassTest.h>

#include <OpenMS/CHEMISTRY/MASSDECOMPOSITION/IMS/IMSAlphabet.h>
#include <OpenMS/DATASTRUCTURES/Map.h>
#include <OpenMS/CHEMISTRY/ResidueDB.h>
#include <OpenMS/CHEMISTRY/Residue.h>

#include <set>
///////////////////////////
#include <OpenMS/CHEMISTRY/MASSDECOMPOSITION/IMS/Weights.h>
///////////////////////////

using namespace OpenMS;
using namespace ims;
using namespace std;

/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////

START_TEST(Weights, "$Id: Weights_test.C 10915 2013-04-04 20:14:57Z aiche $")

/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////


Weights* ptr = 0;
Weights* null_ptr = 0;

Weights::alphabet_masses_type masses;
masses.push_back(71.0456);
masses.push_back(180.0312);
masses.push_back(1.0186);
masses.push_back(4284.36894);
masses.push_back(255.0);


START_SECTION(Weights())
{
	ptr = new Weights();
	TEST_NOT_EQUAL(ptr, null_ptr)
}
END_SECTION

START_SECTION(~Weights())
{
	delete ptr;
}
END_SECTION

START_SECTION((Weights(const alphabet_masses_type &masses, alphabet_mass_type prec)))
{
  Weights::alphabet_mass_type precision = 0.01;

  ptr = new Weights(masses, precision);
  TEST_NOT_EQUAL(ptr, null_ptr)
}
END_SECTION

START_SECTION((Weights(const Weights &other)))
{
  Weights copy(*ptr);
  TEST_NOT_EQUAL(&copy, null_ptr)

  // test equality of copy and ptr
  ABORT_IF(ptr->size()!=copy.size())

  for(Size i = 0 ; i < ptr->size() ; ++i)
  {
    TEST_EQUAL(ptr->getAlphabetMass(i), copy.getAlphabetMass(i))
    TEST_EQUAL(ptr->getWeight(i), copy.getWeight(i))
    TEST_EQUAL((*ptr)[i], copy[i])
  }
}
END_SECTION

START_SECTION((Weights& operator=(const Weights &weights_)))
{
  Weights copy;
  copy = *ptr;
  TEST_NOT_EQUAL(&copy, null_ptr)

  // test equality of copy and ptr
  ABORT_IF(ptr->size()!=copy.size())

  for(Size i = 0 ; i < ptr->size() ; ++i)
  {
    TEST_EQUAL(ptr->getAlphabetMass(i), copy.getAlphabetMass(i))
    TEST_EQUAL(ptr->getWeight(i), copy.getWeight(i))
    TEST_EQUAL((*ptr)[i], copy[i])
  }
}
END_SECTION

START_SECTION((size_type size() const ))
{
  TEST_EQUAL(ptr->size(), 5)
  Weights w;
  TEST_EQUAL(w.size(),0)
}
END_SECTION

START_SECTION((weight_type getWeight(size_type i) const ))
{
  TEST_EQUAL(ptr->getWeight(0), 7105)
  TEST_EQUAL(ptr->getWeight(1), 18003)
  TEST_EQUAL(ptr->getWeight(2), 102)
  TEST_EQUAL(ptr->getWeight(3), 428437);
  TEST_EQUAL(ptr->getWeight(4), 25500)
}
END_SECTION

START_SECTION((void setPrecision(alphabet_mass_type precision_)))
{
  ptr->setPrecision(0.1);

  TEST_EQUAL(ptr->getWeight(0), 710)
  TEST_EQUAL(ptr->getWeight(1), 1800)
  TEST_EQUAL(ptr->getWeight(2), 10)
  TEST_EQUAL(ptr->getWeight(3), 42844)
  TEST_EQUAL(ptr->getWeight(4), 2550)

  ptr->setPrecision(1);

  TEST_EQUAL(ptr->getWeight(0), 71)
  TEST_EQUAL(ptr->getWeight(1), 180)
  TEST_EQUAL(ptr->getWeight(2), 1)
  TEST_EQUAL(ptr->getWeight(3), 4284)
  TEST_EQUAL(ptr->getWeight(4), 255)

  ptr->setPrecision(0.0001);

  TEST_EQUAL(ptr->getWeight(0), 710456)
  TEST_EQUAL(ptr->getWeight(1), 1800312)
  TEST_EQUAL(ptr->getWeight(2), 10186)
  TEST_EQUAL(ptr->getWeight(3), 42843689)
  TEST_EQUAL(ptr->getWeight(4), 2550000)

  ptr->setPrecision(0.01);

  TEST_EQUAL(ptr->getWeight(0), 7105)
  TEST_EQUAL(ptr->getWeight(1), 18003)
  TEST_EQUAL(ptr->getWeight(2), 102)
  TEST_EQUAL(ptr->getWeight(3), 428437)
  TEST_EQUAL(ptr->getWeight(4), 25500)
}
END_SECTION

START_SECTION((alphabet_mass_type getPrecision() const ))
{
  TEST_EQUAL(ptr->getPrecision(), 0.01)
  ptr->setPrecision(0.00025);
  TEST_EQUAL(ptr->getPrecision(), 0.00025)
  ptr->setPrecision(0.01);
  TEST_EQUAL(ptr->getPrecision(), 0.01)
}
END_SECTION

START_SECTION((weight_type operator[](size_type i) const ))
{
  TEST_EQUAL((*ptr)[0], 7105)
  TEST_EQUAL((*ptr)[1], 18003)
  TEST_EQUAL((*ptr)[2], 102)
  TEST_EQUAL((*ptr)[3], 428437)
  TEST_EQUAL((*ptr)[4], 25500)
}
END_SECTION

START_SECTION((weight_type back() const ))
{
  TEST_EQUAL((*ptr)[4], 25500);
  TEST_EQUAL(ptr->back(), 25500);
}
END_SECTION

START_SECTION((alphabet_mass_type getAlphabetMass(size_type i) const ))
{
  // compare with the masses it was created from
  ABORT_IF(ptr->size() != masses.size())

  for(Size i = 0 ; i < ptr->size() ; ++i)
  {
    TEST_EQUAL(ptr->getAlphabetMass(i), masses[i])
  }
}
END_SECTION

START_SECTION((alphabet_mass_type getParentMass(const std::vector< unsigned int > &decomposition) const ))
{
  vector<unsigned int> base_decomposition(5,0);

  for(Size i = 0 ; i < ptr->size() ; ++i)
  {
    vector<unsigned int> decomposition(base_decomposition);
    decomposition[i] = 1;
    TEST_REAL_SIMILAR(ptr->getParentMass(decomposition), masses[i])
    decomposition[i] = 2;
    TEST_REAL_SIMILAR(ptr->getParentMass(decomposition), 2*masses[i])
  }

  vector<unsigned int> wrong_decomposition(3,0);
  TEST_EXCEPTION_WITH_MESSAGE(Exception::InvalidParameter, ptr->getParentMass(wrong_decomposition),"The passed decomposition has the wrong size. Expected 5 but got 3.")
}
END_SECTION

START_SECTION((void swap(size_type index1, size_type index2)))
{
  Weights copy_to_swap(*ptr);

  copy_to_swap.swap(0,1);
  TEST_EQUAL(ptr->getAlphabetMass(0), copy_to_swap.getAlphabetMass(1))
  TEST_EQUAL(ptr->getAlphabetMass(1), copy_to_swap.getAlphabetMass(0))

  TEST_EQUAL(ptr->getWeight(0), copy_to_swap.getWeight(1))
  TEST_EQUAL(ptr->getWeight(1), copy_to_swap.getWeight(0))

  copy_to_swap.swap(1,3);

  TEST_EQUAL(ptr->getAlphabetMass(0), copy_to_swap.getAlphabetMass(3))
  TEST_EQUAL(ptr->getAlphabetMass(3), copy_to_swap.getAlphabetMass(1))

  TEST_EQUAL(ptr->getWeight(0), copy_to_swap.getWeight(3))
  TEST_EQUAL(ptr->getWeight(3), copy_to_swap.getWeight(1))

}
END_SECTION

START_SECTION((bool divideByGCD()))
{
  // we use the example from the documentation here to demonstrate that
  // it works
  // For example, given alphabet weights 3.0, 5.0, 8.0 with precision 0.1, the
  // integer weights would be 30, 50, 80. After calling this method, the new
  // weights are 3, 5, 8 with precision 1.0 (since the gcd of 30, 50, and 80
  //  is 10).

  Weights::alphabet_masses_type masses;
  masses.push_back(3.0);
  masses.push_back(5.0);
  masses.push_back(8.0);

  Weights weights_to_test_gcd(masses, 0.1);
  TEST_EQUAL(weights_to_test_gcd.divideByGCD(), true)
  TEST_EQUAL(weights_to_test_gcd[0],3)
  TEST_EQUAL(weights_to_test_gcd[1],5)
  TEST_EQUAL(weights_to_test_gcd[2],8)

  TEST_EQUAL(weights_to_test_gcd.getPrecision(), 1.0)

  // calling it again should not change anything
  TEST_EQUAL(weights_to_test_gcd.divideByGCD(), false)


  Weights::alphabet_masses_type prime_masses;
  prime_masses.push_back(1.13);
  prime_masses.push_back(1.67);
  prime_masses.push_back(2.41);

  Weights prime_weights(prime_masses, 0.01);
  // we cannot find a GCD here
  TEST_EQUAL(prime_weights.divideByGCD(), false)


  Weights::alphabet_masses_type not_enough_masses;
  not_enough_masses.push_back(40);

  Weights not_enough_entries_weights(not_enough_masses,0.01);
  // we cannot divide by GCD if we only have 1 entry
  TEST_EQUAL(not_enough_entries_weights.divideByGCD(), false)
}
END_SECTION

START_SECTION((alphabet_mass_type getMinRoundingError() const ))
{
  TEST_REAL_SIMILAR(ptr->getMinRoundingError(), -6.6655113114361e-06) // for 255.0 -> 25500
  ptr->setPrecision(10);
  TEST_REAL_SIMILAR(ptr->getMinRoundingError(), -1.0) // for 1.0186 -> 0
  ptr->setPrecision(0.01);
}
END_SECTION

START_SECTION((alphabet_mass_type getMaxRoundingError() const ))
{
  TEST_REAL_SIMILAR(ptr->getMaxRoundingError(), 0.00137443549970554)
  ptr->setPrecision(10);
  TEST_REAL_SIMILAR(ptr->getMaxRoundingError(), 0.0196078431372549)
}
END_SECTION

/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
END_TEST