ProteoWizard
ProteinList_FilterTest.cpp
Go to the documentation of this file.
1 //
2 // $Id: ProteinList_FilterTest.cpp 4129 2012-11-20 00:05:37Z chambm $
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 
23 #include "ProteinList_Filter.hpp"
27 #include <boost/assign.hpp>
28 
29 
30 using namespace pwiz;
31 using namespace pwiz::proteome;
32 using namespace pwiz::analysis;
33 using namespace pwiz::util;
34 using namespace boost::assign;
35 using boost::logic::tribool;
36 
37 
38 ostream* os_ = 0;
39 
40 
41 void 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 
80  SelectedIndexPredicate() : pastMaxIndex(false) {}
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 
166 void test()
167 {
170  testIndexSet(pl);
171  testIdSet(pl);
172 }
173 
174 
175 int 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