ProteoWizard
ProteinList_FilterTest.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 2012 Vanderbilt University - Nashville, TN 37232
8//
9// Licensed under the Apache License, Version 2.0 (the "License");
10// you may not use this file except in compliance with the License.
11// You may obtain a copy of the License at
12//
13// http://www.apache.org/licenses/LICENSE-2.0
14//
15// Unless required by applicable law or agreed to in writing, software
16// distributed under the License is distributed on an "AS IS" BASIS,
17// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18// See the License for the specific language governing permissions and
19// limitations under the License.
20//
21
22
27#include <boost/assign.hpp>
28
29
30using namespace pwiz;
31using namespace pwiz::proteome;
32using namespace pwiz::analysis;
33using namespace pwiz::util;
34using namespace boost::assign;
35using boost::logic::tribool;
36
37
38ostream* os_ = 0;
39
40
41void printProteinList(const ProteinList& pl, ostream& os)
42{
43 os << "size: " << pl.size() << endl;
44
45 for (size_t i=0, end=pl.size(); i<end; i++)
46 {
47 ProteinPtr protein = pl.protein(i, false);
48 os << protein->index << " "
49 << protein->id << " "
50 << endl;
51 }
52}
53
54
56{
57 shared_ptr<ProteinListSimple> pl(new ProteinListSimple);
58
59 for (size_t i=0; i<10; ++i)
60 {
61 ProteinPtr protein(new Protein("Pro" + lexical_cast<string>(i+1), i, "", string(16, 'A'+i)));
62 pl->proteins.push_back(protein);
63 }
64
65 if (os_)
66 {
67 *os_ << "original protein list:\n";
68 printProteinList(*pl, *os_);
69 *os_ << endl;
70 }
71
72 return pl;
73}
74
75
77{
78 mutable bool pastMaxIndex;
79
81
82 virtual tribool accept(const Protein& protein) const
83 {
84 if (protein.index>5) pastMaxIndex = true;
85
86 return (protein.index==1 ||
87 protein.index==3 ||
88 protein.index==5);
89 }
90
91 virtual bool done() const
92 {
93 return pastMaxIndex;
94 }
95};
96
97
99{
100 if (os_) *os_ << "testSelectedIndices:\n";
101
103
104 if (os_)
105 {
106 printProteinList(filter, *os_);
107 *os_ << endl;
108 }
109
110 unit_assert_operator_equal(3, filter.size());
111 unit_assert_operator_equal("Pro2", filter.protein(0)->id);
112 unit_assert_operator_equal("Pro4", filter.protein(1)->id);
113 unit_assert_operator_equal("Pro6", filter.protein(2)->id);
114}
115
116
118{
119 if (os_) *os_ << "testIndexSet:\n";
120
121 IntegerSet indexSet;
122 indexSet.insert(3,5);
123 indexSet.insert(7);
124 indexSet.insert(9);
125
127
128 if (os_)
129 {
130 printProteinList(filter, *os_);
131 *os_ << endl;
132 }
133
134 unit_assert_operator_equal(5, filter.size());
135 unit_assert_operator_equal("Pro4", filter.protein(0)->id);
136 unit_assert_operator_equal("Pro5", filter.protein(1)->id);
137 unit_assert_operator_equal("Pro6", filter.protein(2)->id);
138 unit_assert_operator_equal("Pro8", filter.protein(3)->id);
139 unit_assert_operator_equal("Pro10", filter.protein(4)->id);
140}
141
142
144{
145 if (os_) *os_ << "testIdSet:\n";
146
147 set<string> idSet;
148 idSet += "Pro2", "Pro3", "Pro4", "Pro7";
149
151
152 if (os_)
153 {
154 printProteinList(filter, *os_);
155 *os_ << endl;
156 }
157
158 unit_assert_operator_equal(4, filter.size());
159 unit_assert_operator_equal("Pro2", filter.protein(0)->id);
160 unit_assert_operator_equal("Pro3", filter.protein(1)->id);
161 unit_assert_operator_equal("Pro4", filter.protein(2)->id);
162 unit_assert_operator_equal("Pro7", filter.protein(3)->id);
163}
164
165
166void test()
167{
170 testIndexSet(pl);
171 testIdSet(pl);
172}
173
174
175int main(int argc, char* argv[])
176{
177 TEST_PROLOG(argc, argv)
178
179 try
180 {
181 if (argc>1 && !strcmp(argv[1],"-v")) os_ = &cout;
182 test();
183 }
184 catch (exception& e)
185 {
186 TEST_FAILED(e.what())
187 }
188 catch (...)
189 {
190 TEST_FAILED("Caught unknown exception.")
191 }
192
194}
195
196
int main(int argc, char *argv[])
void testIndexSet(ProteinListPtr pl)
ProteinListPtr createProteinList()
void printProteinList(const ProteinList &pl, ostream &os)
void testIdSet(ProteinListPtr pl)
void testSelectedIndices(ProteinListPtr pl)
ostream * os_
ProteinList filter, for creating Protein sub-lists.
virtual ProteinPtr protein(size_t index, bool getSequence=true) const =0
virtual size_t size() const =0
a virtual container of integers, accessible via an iterator interface, stored as union of intervals
void insert(Interval interval)
insert an interval of integers into the virtual container
boost::shared_ptr< Protein > ProteinPtr
boost::shared_ptr< ProteinList > ProteinListPtr
virtual bool done() const
return true iff done accepting chromatograms; this allows early termination of the iteration through ...
virtual tribool accept(const Protein &protein) const
return true iff Protein is accepted
client-implemented filter predicate – called during construction of ProteinList_Filter to create the ...
#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