ProteoWizard
SpectrumList_SorterTest.cpp
Go to the documentation of this file.
1//
2// $Id$
3//
4//
5// Original author: Matt Chambers <matt.chambers <a.t> vanderbilt.edu>
6//
7// Copyright 2008 Spielberg Family Center for Applied Proteomics
8// Cedars-Sinai Medical Center, Los Angeles, California 90048
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
27#include <cstring>
30#include "boost/logic/tribool.hpp"
31
32
33using namespace pwiz::util;
34using namespace pwiz::cv;
35using namespace pwiz::msdata;
36using namespace pwiz::analysis;
37using boost::logic::tribool;
38
39ostream* os_ = 0;
40
41
43{
44 virtual tribool less(const SpectrumIdentity& lhs,
45 const SpectrumIdentity& rhs) const
46 {
47 return boost::logic::indeterminate;
48 }
49
50 virtual tribool less(const Spectrum& lhs,
51 const Spectrum& rhs) const
52 {
53 if (lhs.id.empty())
54 return boost::logic::indeterminate;
56 }
57};
58
60{
61 virtual tribool less(const SpectrumIdentity& lhs,
62 const SpectrumIdentity& rhs) const
63 {
64 return boost::logic::indeterminate;
65 }
66
67 virtual tribool less(const Spectrum& lhs,
68 const Spectrum& rhs) const
69 {
70 CVParam lhsMSLevel = lhs.cvParam(MS_ms_level);
71 CVParam rhsMSLevel = rhs.cvParam(MS_ms_level);
72 if (lhsMSLevel.empty() || rhsMSLevel.empty())
73 return boost::logic::indeterminate;
74 return lhsMSLevel.valueAs<int>() < rhsMSLevel.valueAs<int>();
75 }
76};
77
78
79void test()
80{
81 MSData msd;
83
84 SpectrumListPtr originalList = msd.run.spectrumListPtr;
85
86 SpectrumListPtr defaultArrayLengthSortedList(
88
89 SpectrumListPtr msLevelUnstableSortedList(
90 new SpectrumList_Sorter(originalList, MSLevelSorter()));
91
92 SpectrumListPtr msLevelStableSortedList(
93 new SpectrumList_Sorter(originalList, MSLevelSorter(), true));
94
95 SpectrumListPtr sillySortedList(
96 new SpectrumList_Sorter(msLevelStableSortedList, DefaultArrayLengthSorter()));
97
98 if (os_)
99 {
100 *os_ << "Original spectrum list (" << originalList->size() << "):\n";
101 TextWriter write(*os_);
102 write(*originalList);
103 *os_ << endl;
104 }
105
106 if (os_)
107 {
108 *os_ << "Default array length sorted spectrum list (" << defaultArrayLengthSortedList->size() << "):\n";
109 TextWriter write(*os_);
110 write(*defaultArrayLengthSortedList);
111 *os_ << endl;
112 }
113
114 if (os_)
115 {
116 *os_ << "MS level unstable sorted spectrum list (" << msLevelUnstableSortedList->size() << "):\n";
117 TextWriter write(*os_);
118 write(*msLevelUnstableSortedList);
119 *os_ << endl;
120 }
121
122 if (os_)
123 {
124 *os_ << "MS level stable sorted spectrum list (" << msLevelStableSortedList->size() << "):\n";
125 TextWriter write(*os_);
126 write(*msLevelStableSortedList);
127 *os_ << endl;
128 }
129
130 if (os_)
131 {
132 *os_ << "Silly (nested) sorted spectrum list (" << sillySortedList->size() << "):\n";
133 TextWriter write(*os_);
134 write(*sillySortedList);
135 *os_ << endl;
136 }
137
138 unit_assert_operator_equal(originalList->size(), defaultArrayLengthSortedList->size());
139 unit_assert_operator_equal(originalList->size(), msLevelUnstableSortedList->size());
140 unit_assert_operator_equal(originalList->size(), msLevelStableSortedList->size());
141 unit_assert_operator_equal(originalList->size(), sillySortedList->size());
142
143 SpectrumPtr s;
144
145 // assert that the original list is unmodified
146 unit_assert_operator_equal("scan=19", originalList->spectrumIdentity(0).id);
147 unit_assert_operator_equal(0, originalList->spectrumIdentity(0).index);
148 unit_assert_operator_equal("scan=20", originalList->spectrumIdentity(1).id);
149 unit_assert_operator_equal(1, originalList->spectrumIdentity(1).index);
150 unit_assert_operator_equal("scan=21", originalList->spectrumIdentity(2).id);
151 unit_assert_operator_equal(2, originalList->spectrumIdentity(2).index);
152 unit_assert_operator_equal("scan=22", originalList->spectrumIdentity(3).id);
153 unit_assert_operator_equal(3, originalList->spectrumIdentity(3).index);
154 s = originalList->spectrum(0);
155 unit_assert_operator_equal("scan=19", s->id);
156 unit_assert_operator_equal(0, s->index);
157
158 // validate the default array length sorted list (ascending order, scan=19 and scan=22 are interchangeable)
159 unit_assert_operator_equal("scan=21", defaultArrayLengthSortedList->spectrumIdentity(0).id);
160 unit_assert_operator_equal(0, defaultArrayLengthSortedList->spectrumIdentity(0).index);
161 unit_assert_operator_equal("scan=20", defaultArrayLengthSortedList->spectrumIdentity(1).id);
162 unit_assert_operator_equal(1, defaultArrayLengthSortedList->spectrumIdentity(1).index);
163 unit_assert_operator_equal(2, defaultArrayLengthSortedList->spectrumIdentity(2).index);
164 unit_assert_operator_equal(3, defaultArrayLengthSortedList->spectrumIdentity(3).index);
165 s = defaultArrayLengthSortedList->spectrum(0);
166 unit_assert_operator_equal("scan=21", s->id);
167 unit_assert_operator_equal(0, s->index);
168 s = defaultArrayLengthSortedList->spectrum(1);
169 unit_assert_operator_equal("scan=20", s->id);
170 unit_assert_operator_equal(1, s->index);
171 s = defaultArrayLengthSortedList->spectrum(2);
172 unit_assert_operator_equal(2, s->index);
173 s = defaultArrayLengthSortedList->spectrum(3);
174 unit_assert_operator_equal(3, s->index);
175 for (size_t i=1, end=defaultArrayLengthSortedList->size(); i < end; ++i)
176 unit_assert(defaultArrayLengthSortedList->spectrum(i)->defaultArrayLength >=
177 defaultArrayLengthSortedList->spectrum(i-1)->defaultArrayLength);
178
179 // validate the MS level unstable sorted list (scan=19, scan=21, and scan=22 are interchangeable)
180 unit_assert_operator_equal(0, msLevelUnstableSortedList->spectrumIdentity(0).index);
181 unit_assert_operator_equal(1, msLevelUnstableSortedList->spectrumIdentity(1).index);
182 unit_assert_operator_equal(2, msLevelUnstableSortedList->spectrumIdentity(2).index);
183 unit_assert_operator_equal("scan=20", msLevelUnstableSortedList->spectrumIdentity(3).id);
184 unit_assert_operator_equal(3, msLevelUnstableSortedList->spectrumIdentity(3).index);
185 s = msLevelUnstableSortedList->spectrum(0);
186 unit_assert_operator_equal(0, s->index);
187 s = msLevelUnstableSortedList->spectrum(1);
188 unit_assert_operator_equal(1, s->index);
189 s = msLevelUnstableSortedList->spectrum(2);
190 unit_assert_operator_equal(2, s->index);
191 s = msLevelUnstableSortedList->spectrum(3);
192 unit_assert_operator_equal("scan=20", s->id);
193 unit_assert_operator_equal(3, s->index);
194
195 // validate the MS level stable sorted list (scan=19, scan=21, and scan=22 should stay in order)
196 unit_assert_operator_equal("scan=19", msLevelStableSortedList->spectrumIdentity(0).id);
197 unit_assert_operator_equal(0, msLevelStableSortedList->spectrumIdentity(0).index);
198 unit_assert_operator_equal("scan=21", msLevelStableSortedList->spectrumIdentity(1).id);
199 unit_assert_operator_equal(1, msLevelStableSortedList->spectrumIdentity(1).index);
200 unit_assert_operator_equal("sample=1 period=1 cycle=23 experiment=1", msLevelStableSortedList->spectrumIdentity(2).id);
201 unit_assert_operator_equal(2, msLevelStableSortedList->spectrumIdentity(2).index);
202 unit_assert_operator_equal("scan=20", msLevelStableSortedList->spectrumIdentity(3).id);
203 unit_assert_operator_equal(3, msLevelStableSortedList->spectrumIdentity(3).index);
204 s = msLevelStableSortedList->spectrum(0);
205 unit_assert_operator_equal("scan=19", s->id);
206 unit_assert_operator_equal(0, s->index);
207 s = msLevelStableSortedList->spectrum(1);
208 unit_assert_operator_equal("scan=21", s->id);
209 unit_assert_operator_equal(1, s->index);
210 s = msLevelStableSortedList->spectrum(2);
211 unit_assert_operator_equal("sample=1 period=1 cycle=23 experiment=1", s->id);
212 unit_assert_operator_equal(2, s->index);
213 s = msLevelStableSortedList->spectrum(3);
214 unit_assert_operator_equal("scan=20", s->id);
215 unit_assert_operator_equal(3, s->index);
216
217 // validate the silly (nested) sorted list
218 unit_assert_operator_equal("scan=21", sillySortedList->spectrumIdentity(0).id);
219 unit_assert_operator_equal(0, sillySortedList->spectrumIdentity(0).index);
220 unit_assert_operator_equal("scan=20", sillySortedList->spectrumIdentity(1).id);
221 unit_assert_operator_equal(1, sillySortedList->spectrumIdentity(1).index);
222 unit_assert_operator_equal(2, sillySortedList->spectrumIdentity(2).index);
223 unit_assert_operator_equal(3, sillySortedList->spectrumIdentity(3).index);
224 s = sillySortedList->spectrum(0);
225 unit_assert_operator_equal("scan=21", s->id);
226 unit_assert_operator_equal(0, s->index);
227 s = sillySortedList->spectrum(1);
228 unit_assert_operator_equal("scan=20", s->id);
229 unit_assert_operator_equal(1, s->index);
230 s = sillySortedList->spectrum(2);
231 unit_assert_operator_equal(2, s->index);
232 s = sillySortedList->spectrum(3);
233 unit_assert_operator_equal(3, s->index);
234 for (size_t i=1, end=sillySortedList->size(); i < end; ++i)
235 unit_assert(sillySortedList->spectrum(i)->defaultArrayLength >=
236 sillySortedList->spectrum(i-1)->defaultArrayLength);
237}
238
239
240int main(int argc, char* argv[])
241{
242 TEST_PROLOG(argc, argv)
243
244 try
245 {
246 if (argc>1 && !strcmp(argv[1],"-v")) os_ = &cout;
247 test();
248 }
249 catch (exception& e)
250 {
251 TEST_FAILED(e.what())
252 }
253 catch (...)
254 {
255 TEST_FAILED("Caught unknown exception.")
256 }
257
259}
int main(int argc, char *argv[])
ostream * os_
Provides a custom-sorted spectrum list.
MS_ms_level
ms level: Stages of ms achieved in a multi stage mass spectrometry experiment.
Definition cv.hpp:2139
PWIZ_API_DECL void initializeTiny(MSData &msd)
boost::shared_ptr< SpectrumList > SpectrumListPtr
Definition MSData.hpp:711
boost::shared_ptr< Spectrum > SpectrumPtr
Definition MSData.hpp:573
virtual tribool less(const SpectrumIdentity &lhs, const SpectrumIdentity &rhs) const
return values: true: lhs < rhs false: lhs >= rhs indeterminate: need to see the full Spectrum object ...
virtual tribool less(const Spectrum &lhs, const Spectrum &rhs) const
return values: true: lhs < rhs false: lhs >= rhs indeterminate: need a more detailed Spectrum object ...
virtual tribool less(const Spectrum &lhs, const Spectrum &rhs) const
return values: true: lhs < rhs false: lhs >= rhs indeterminate: need a more detailed Spectrum object ...
virtual tribool less(const SpectrumIdentity &lhs, const SpectrumIdentity &rhs) const
return values: true: lhs < rhs false: lhs >= rhs indeterminate: need to see the full Spectrum object ...
client-implemented sort predicate – called during construction of SpectrumList_Sorter to sort the und...
represents a tag-value pair, where the tag comes from the controlled vocabulary
value_type valueAs() const
templated value access with type conversion
CVParam cvParam(CVID cvid) const
finds cvid in the container:
This is the root element of ProteoWizard; it represents the mzML element, defined as: intended to cap...
Definition MSData.hpp:850
Run run
a run in mzML should correspond to a single, consecutive and coherent set of scans on an instrument.
Definition MSData.hpp:886
SpectrumListPtr spectrumListPtr
all mass spectra and the acquisitions underlying them are described and attached here....
Definition MSData.hpp:827
The structure that captures the generation of a peak list (including the underlying acquisitions)
Definition MSData.hpp:506
size_t defaultArrayLength
default length of binary data arrays contained in this element.
Definition MSData.hpp:508
Identifying information for a spectrum.
Definition MSData.hpp:471
std::string id
a unique identifier for this spectrum. It should be expected that external files may use this identif...
Definition MSData.hpp:476
#define unit_assert(x)
Definition unit.hpp:85
#define TEST_EPILOG
Definition unit.hpp:183
#define TEST_FAILED(x)
Definition unit.hpp:177
#define unit_assert_operator_equal(expected, actual)
Definition unit.hpp:92
#define TEST_PROLOG(argc, argv)
Definition unit.hpp:175