ProteoWizard
MZRTField.hpp
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#ifndef _MZRTFIELD_HPP_
25#define _MZRTFIELD_HPP_
26
27
31#include "boost/shared_ptr.hpp"
32#include "boost/concept/assert.hpp"
33#include "boost/concept/usage.hpp"
34#include <set>
35#include <vector>
36
37
38namespace pwiz {
39namespace analysis {
40
41
42using chemistry::MZTolerance;
43
44
45namespace {
46
47///
48/// lexicographic ordering, by m/z then retention time
49///
50template <typename T>
51struct LessThan_MZRT
52{
53 typedef boost::shared_ptr<T> TPtr;
54
55 bool operator()(const T& a, const T& b) const
56 {
57 if (a.mz < b.mz) return true;
58 if (b.mz < a.mz) return false;
59 return (a.retentionTime < b.retentionTime); // rare
60 }
61
62 bool operator()(const TPtr& a, const TPtr& b) const
63 {
64 return (*this)(*a, *b);
65 }
66};
67
68
69///
70/// struct for Boost concept checking
71///
72template <typename T>
73struct HasMZRT
74{
75 BOOST_CONCEPT_USAGE(HasMZRT)
76 {
77 T t; // default construction
78 double a = t.mz; // must have member 'mz'
79 a = t.retentionTime; // must have member 'retentionTime'
80 const T& c = t;
81 a = c.retentionTimeMin(); // must have member 'double retentionTimeMin() const'
82 a = c.retentionTimeMax(); // must have member 'double retentionTimeMin() const'
83 }
84};
85
86} // namespace
87
88
89///
90/// MZRTField is a std::set of boost::shared_ptrs, stored as a binary tree
91/// ordered by LessThan_MZRT
92///
93template <typename T>
94struct MZRTField : public std::set< boost::shared_ptr<T>, LessThan_MZRT<T> >
95{
96 //BOOST_CONCEPT_ASSERT((HasMZRT<T>));
97
98 typedef boost::shared_ptr<T> TPtr;
99
100 /// find all objects with a given m/z, within a given m/z tolerance,
101 /// satisfying the 'matches' predicate
102 template <typename RTMatches>
103 std::vector<TPtr>
104 find(double mz, MZTolerance mzTolerance, RTMatches matches) const;
105
106 /// remove an object via a shared reference, rather than an iterator into the set
107 void remove(const TPtr& p);
108};
109
110
113
114
115PWIZ_API_DECL std::ostream& operator<<(std::ostream& os, const PeakelField& peakelField);
116PWIZ_API_DECL std::ostream& operator<<(std::ostream& os, const FeatureField& featureField);
117
118
119/// predicate always returns true
120template <typename T>
122{
123 bool operator()(const T& t) const {return true;}
124};
125
126
127/// predicate returns true iff the object's retention time range contains the specified
128/// retention time
129template <typename T>
131{
132 RTMatches_Contains(double rt, double rtTolerance = 0) : rt_(rt), rtTolerance_(rtTolerance) {}
133
134 bool operator()(const T& t) const
135 {
136 return rt_>t.retentionTimeMin()-rtTolerance_ && rt_<t.retentionTimeMax()+rtTolerance_;
137 }
138
139 private:
140 double rt_;
142};
143
144
145/// predicate returns true iff the object's retention time range is completely contained within
146/// the range of the specified reference object, up to the specified tolerance
147template <typename T>
149{
150 RTMatches_IsContainedIn(const T& reference, double rtTolerance = 0)
151 : reference_(reference), rtTolerance_(rtTolerance) {}
152
153 bool operator()(const T& t) const
154 {
155 return t.retentionTimeMin() > reference_.retentionTimeMin() - rtTolerance_ &&
156 t.retentionTimeMax() < reference_.retentionTimeMax() + rtTolerance_;
157 }
158
159 private:
160 const T& reference_;
162};
163
164
165template <typename T>
166template <typename RTMatches>
167std::vector< boost::shared_ptr<T> >
168MZRTField<T>::find(double mz, MZTolerance mzTolerance, RTMatches matches) const
169{
170 TPtr target(new T);
171
172 // use binary search to get a std::set iterator range
173
174 target->mz = mz - mzTolerance;
175 typename MZRTField<T>::const_iterator begin = this->lower_bound(target);
176
177 target->mz = mz + mzTolerance;
178 typename MZRTField<T>::const_iterator end = this->upper_bound(target);
179
180 // linear copy_if within range
181
182 std::vector<TPtr> result;
183
184 for (typename MZRTField<T>::const_iterator it=begin; it!=end; ++it)
185 if (matches(**it))
186 result.push_back(*it);
187
188 return result;
189}
190
191
192template <typename T>
193void MZRTField<T>::remove(const boost::shared_ptr<T>& p)
194{
195 std::pair<typename MZRTField<T>::iterator, typename MZRTField<T>::iterator>
196 range = this->equal_range(p); // uses LessThan_MZRT
197
198 typename MZRTField<T>::iterator
199 found = std::find(range.first, range.second, p); // uses shared_ptr::operator==
200
201 if (found == range.second) throw std::runtime_error("[MZRTField::remove()] TPtr not found.");
202
203 this->erase(found);
204}
205
206
207} // namespace analysis
208} // namespace pwiz
209
210
211#endif // _MZRTFIELD_HPP_
212
#define PWIZ_API_DECL
Definition Export.hpp:32
MZRTField< pwiz::data::peakdata::Feature > FeatureField
PWIZ_API_DECL std::ostream & operator<<(std::ostream &os, PepxmlRecordReader &prr)
MZRTField< pwiz::data::peakdata::Peakel > PeakelField
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
boost::shared_ptr< T > TPtr
Definition MZRTField.hpp:98
predicate always returns true
bool operator()(const T &t) const
predicate returns true iff the object's retention time range contains the specified retention time
RTMatches_Contains(double rt, double rtTolerance=0)
bool operator()(const T &t) const
predicate returns true iff the object's retention time range is completely contained within the range...
RTMatches_IsContainedIn(const T &reference, double rtTolerance=0)
struct for expressing m/z tolerance in either amu or ppm