ProteoWizard
MZRTFieldTest.cpp
Go to the documentation of this file.
1//
2// $Id$
3//
4//
5// Original author: Darren Kessner <darren@proteowizard.org>
6//
7// Copyright 2009 Center for Applied Molecular Medicine
8// University of Southern California, Los Angeles, CA
9//
10// Licensed under the Apache License, Version 2.0 (the "License");
11// you may not use this file except in compliance with the License.
12// You may obtain a copy of the License at
13//
14// http://www.apache.org/licenses/LICENSE-2.0
15//
16// Unless required by applicable law or agreed to in writing, software
17// distributed under the License is distributed on an "AS IS" BASIS,
18// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19// See the License for the specific language governing permissions and
20// limitations under the License.
21//
22
23
24#include "MZRTField.hpp"
27#include <cstring>
28
29
30using namespace pwiz::util;
31using namespace pwiz::analysis;
32using namespace pwiz::chemistry;
33using namespace pwiz::data::peakdata;
34
35
36ostream* os_ = 0;
37
38
40{
41 if (os_) *os_ << "testPredicate()\n";
42
43 PeakelPtr a(new Peakel(Peak(1.0, 1.0)));
44 PeakelPtr b(new Peakel(Peak(2.0, 1.0)));
45 PeakelPtr c(new Peakel(Peak(1.0, 2.0)));
46
47 LessThan_MZRT<Peakel> lt;
48
49 // a < c < b
50
51 unit_assert(lt(*a,*b));
52 unit_assert(lt(a,b));
53 unit_assert(!lt(b,a));
54 unit_assert(lt(a,c));
55 unit_assert(!lt(c,a));
56 unit_assert(lt(c,b));
57 unit_assert(!lt(b,c));
58
59 unit_assert(!lt(a,a));
60}
61
62
64{
65 Feature a,b;
66 a.mz = 1;
67 b.mz = 2;
68
69 LessThan_MZRT<Feature> lt;
70 unit_assert(lt(a,b));
71 unit_assert(!lt(b,a));
72 unit_assert(!lt(a,a));
73}
74
75
76struct Goober
77{
78 // minimum requirements to instantiate MZRTField<>;
79 // removing any of these will produce compiler error via Boost concept checking
80 // with the struct HasMZRT
81
82 double mz;
84 double retentionTimeMin() const;
85 double retentionTimeMax() const;
86};
87
88
90{
91 MZRTField<Goober> gooberField;
92}
93
94
95struct Simple
96{
97 double mz;
99 double rtMin;
100 double rtMax;
101 double retentionTimeMin() const {return rtMin;}
102 double retentionTimeMax() const {return rtMax;}
103
104 Simple(double _mz = 0, double _rtMin = 0, double _rtMax = 0)
105 : mz(_mz),
106 retentionTime((_rtMin+_rtMax)/2),
107 rtMin(_rtMin), rtMax(_rtMax)
108 {}
109};
110
111
112typedef shared_ptr<Simple> SimplePtr;
113
114
116{
117 // rt\mz 400 410 420 430 440
118 // 660 a c
119 // 661 a c
120 // 662 c
121 // 663
122 // 664 b
123 // 665 b d
124 // 666 b d
125 // 667 b d
126 // 668 b
127 // 669
128
129 SimplePtr a(new Simple(400, 660, 661));
130 SimplePtr b(new Simple(400, 664, 668));
131 SimplePtr c(new Simple(420, 660, 662));
132 SimplePtr d(new Simple(420, 665, 667));
133
134 MZRTField<Simple> simpleField;
135 simpleField.insert(a);
136 simpleField.insert(b);
137 simpleField.insert(c);
138 simpleField.insert(d);
139
140 vector<SimplePtr> result = simpleField.find(420, 1, RTMatches_Any<Simple>());
141 unit_assert(result.size()==2 && result[0]==c && result[1]==d);
142
143 result = simpleField.find(410, 11, RTMatches_Contains<Simple>(666,0));
144 unit_assert(result.size()==2 && result[0]==b && result[1]==d);
145
146 result = simpleField.find(420, 1, RTMatches_IsContainedIn<Simple>(*b));
147 unit_assert(result.size()==1 && result[0]==d);
148
149 result = simpleField.find(400, 1, RTMatches_IsContainedIn<Simple>(*d, 1.5));
150 unit_assert(result.size()==1 && result[0]==b);
151}
152
153
155{
156 if (os_) *os_ << "testPeakelField()\n";
157
158 PeakelPtr a(new Peakel(Peak(1.0, 1.0)));
159 PeakelPtr b(new Peakel(Peak(2.0, 1.0)));
160 PeakelPtr c(new Peakel(Peak(1.0, 2.0)));
161
162 PeakelField pf;
163
164 pf.insert(a);
165 pf.insert(b);
166 pf.insert(c);
167
168 if (os_) *os_ << pf << endl;
169
170 unit_assert(pf.size() == 3);
171
172 PeakelField::const_iterator it = pf.begin();
173 unit_assert(*it == a);
174
175 // note that std::set allows only const access
176 // however, we can modify **it
177 (*it)->peaks.push_back(Peak());
178 (*it)->peaks.clear();
179
180 ++it;
181 unit_assert(*it == c);
182
183 ++it;
184 unit_assert(*it == b);
185
186 // find()
187
188 if (os_) *os_ << "testPeakelField(): find()\n";
189
190 vector<PeakelPtr> v = pf.find(1.5, .6, RTMatches_Contains<Peakel>(1, .5));
191
192 if (os_)
193 {
194 *os_ << "find(): " << v.size() << endl;
195 for (vector<PeakelPtr>::const_iterator it=v.begin(); it!=v.end(); ++it)
196 *os_ << **it << endl;
197 }
198
199 unit_assert(v.size()==2 && v[0]==a && v[1]==b);
200 v = pf.find(1.5, .4, RTMatches_Contains<Peakel>(1, .5));
201 unit_assert(v.empty());
202 v = pf.find(2, .1, RTMatches_Contains<Peakel>(1, .1));
203 unit_assert(v.size()==1 && v[0]==b);
204
205 MZTolerance fiveppm(5, MZTolerance::PPM);
206 v = pf.find(1.000001, fiveppm, RTMatches_Contains<Peakel>(1, 10));
207 unit_assert(v.size()==2 && v[0]==a && v[1]==c);
208 v = pf.find(1.000006, fiveppm, RTMatches_Contains<Peakel>(1, 10));
209 unit_assert(v.empty());
210
211 // remove()
212
213 if (os_) *os_ << "testPeakelField(): remove()\n";
214
215 pf.remove(a);
216 unit_assert(pf.size() == 2);
217 it = pf.begin();
218 unit_assert(*it == c);
219 ++it;
220 unit_assert(*it == b);
221
222 bool caught = false;
223 try {
224 pf.remove(a);
225 }
226 catch (exception& e) {
227 if (os_) *os_ << "Caught exception correctly: " << e.what() << endl;
228 caught = true;
229 }
230 unit_assert(caught);
231
232 pf.remove(b);
233 unit_assert(pf.size() == 1);
234 it = pf.begin();
235 unit_assert(*it == c);
236
237 pf.remove(c);
238 unit_assert(pf.empty());
239
240 if (os_) *os_ << endl;
241}
242
243
244struct MyPred
245{
246 void operator()(double mz)
247 {
248 cout << "MyPred: " << mz << endl;
249 }
250};
251
252
254{
255 if (os_) *os_ << "testFeatureField()\n";
256
257 FeatureField ff;
258
259 FeaturePtr a(new Feature);
260 a->mz=1; a->retentionTime=1;
261
262 FeaturePtr b(new Feature);
263 b->mz=2; b->retentionTime=1;
264
265 FeaturePtr c(new Feature);
266 c->mz=1; c->retentionTime=2;
267
268 ff.insert(a);
269 ff.insert(b);
270 ff.insert(c);
271
272 if (os_) *os_ << ff << endl;
273
274 MZTolerance fiveppm(5, MZTolerance::PPM);
275 vector<FeaturePtr> v = ff.find(1.000001, fiveppm, RTMatches_Contains<Feature>(1, 10));
276 unit_assert(v.size()==2 && v[0]==a && v[1]==c);
277 v = ff.find(1.000006, fiveppm, RTMatches_Contains<Feature>(1, 10));
278 unit_assert(v.empty());
279}
280
281
291
292
293int main(int argc, char* argv[])
294{
295 TEST_PROLOG(argc, argv)
296
297 try
298 {
299 if (argc>1 && !strcmp(argv[1],"-v")) os_ = &cout;
300 test();
301 }
302 catch (exception& e)
303 {
304 TEST_FAILED(e.what())
305 }
306 catch (...)
307 {
308 TEST_FAILED("Caught unknown exception.")
309 }
310
312}
313
void testPredicate_Feature()
int main(int argc, char *argv[])
void testFeatureField()
void testFind()
void testPredicate()
void testPeakelField()
shared_ptr< Simple > SimplePtr
void testConceptChecking()
ostream * os_
void test()
boost::shared_ptr< Feature > FeaturePtr
Definition PeakData.hpp:292
boost::shared_ptr< Peakel > PeakelPtr
Definition PeakData.hpp:242
double retentionTime
double retentionTimeMax() const
double retentionTimeMin() const
double mz
void operator()(double mz)
double mz
double retentionTimeMin() const
Simple(double _mz=0, double _rtMin=0, double _rtMax=0)
double retentionTime
double retentionTimeMax() const
double rtMax
double rtMin
MZRTField is a std::set of boost::shared_ptrs, stored as a binary tree ordered by LessThan_MZRT.
Definition MZRTField.hpp:95
std::vector< TPtr > find(double mz, MZTolerance mzTolerance, RTMatches matches) const
find all objects with a given m/z, within a given m/z tolerance, satisfying the 'matches' predicate
void remove(const TPtr &p)
remove an object via a shared reference, rather than an iterator into the set
represents some generic metadata about a peak detected in a signal
predicate always returns true
predicate returns true iff the object's retention time range contains the specified retention time
predicate returns true iff the object's retention time range is completely contained within the range...
struct for expressing m/z tolerance in either amu or ppm
struct for an eluted peak (PEAK ELution)
Definition PeakData.hpp:212
#define unit_assert(x)
Definition unit.hpp:85
#define TEST_EPILOG
Definition unit.hpp:183
#define TEST_FAILED(x)
Definition unit.hpp:177
#define TEST_PROLOG(argc, argv)
Definition unit.hpp:175