ProteoWizard
Feature_dataFetcherTest.cpp
Go to the documentation of this file.
1 //
2 // $Id: Feature_dataFetcherTest.cpp 2051 2010-06-15 18:39:13Z chambm $
3 //
4 //
5 // Original author: Kate Hoff <katherine.hoff@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 #include "Feature_dataFetcher.hpp"
26 
27 using namespace pwiz::data::peakdata;
28 using namespace pwiz::util;
29 using namespace pwiz::minimxml;
30 using namespace pwiz::eharmony;
31 
32 ostream* os_ = 0;
33 
34 FeaturePtr makeFeature(double mz, double retentionTime)
35 {
36  FeaturePtr feature(new Feature());
37  feature->mz = mz;
38  feature->retentionTime = retentionTime;
39 
40  return feature;
41 
42 }
43 
44 FeatureSequencedPtr makeFeatureSequenced(const FeaturePtr feature, string _ms1_5="", string _ms2="")
45 {
47  fs->feature = feature;
48  fs->ms1_5 = _ms1_5;
49  fs->ms2 = _ms2;
50 
51  return fs;
52 
53 }
54 
55 struct IsFS
56 {
57  IsFS(const FeatureSequenced& fs): _fs(fs){}
59  bool operator()(boost::shared_ptr<FeatureSequenced> fs_ptr){ return *fs_ptr == _fs;}
60 
61 };
62 
63 void test()
64 {
65 
66  if (os_) *os_ << "\ntest() ... \n\n";
67 
68  // make a vector of features
69  FeaturePtr a = makeFeature(1,2);
70  FeaturePtr b = makeFeature(3,4);
71  FeaturePtr c = makeFeature(5,6);
72 
73  // make FeatureSequenced objects
74  FeatureSequenced fs_a(a);
75  FeatureSequenced fs_b(b);
76  FeatureSequenced fs_c(c);
77 
78  vector<FeaturePtr> features;
79  features.push_back(a);
80  features.push_back(b);
81  features.push_back(c);
82 
83  // test vector<FeaturePtr> constructor
84  if (os_) *os_ << "constructing Feature_dataFetcher ... " << endl;
85  Feature_dataFetcher fdf(features);
86  if (os_) *os_ << "constructed. " << endl;
87 
88  if (os_) *os_ << "testing getFeatures ... " << endl;
89  vector<boost::shared_ptr<FeatureSequenced> > test_a = fdf.getFeatures(1,2);
90  vector<boost::shared_ptr<FeatureSequenced> > test_b = fdf.getFeatures(3,4);
91  vector<boost::shared_ptr<FeatureSequenced> > test_c = fdf.getFeatures(5,6);
92 
93  unit_assert(find_if(test_a.begin(), test_a.end(), IsFS(fs_a)) != test_a.end());
94  unit_assert(find_if(test_b.begin(), test_b.end(), IsFS(fs_b)) != test_b.end());
95  unit_assert(find_if(test_c.begin(), test_c.end(), IsFS(fs_c)) != test_c.end());
96 
97  if (os_)
98  {
99  *os_ << "testing vector<Feature> constructor ... \n";
100  ostringstream oss;
101  XMLWriter writer(oss);
102  vector<boost::shared_ptr<FeatureSequenced> >::iterator a_it = test_a.begin();
103  for(; a_it != test_a.end(); ++a_it) (*a_it)->feature->write(writer);
104  *os_ << oss.str() << endl;
105 
106  }
107 
108 
109  // write a FeatureFile
110  ostringstream oss;
111  XMLWriter writer(oss);
112  FeatureFile ff;
113  ff.features = features;
114  ff.write(writer);
115 
116  //read it to an istream
117  istringstream iss(oss.str());
118 
119  // test istream constructor
120  Feature_dataFetcher fdf_is(iss);
121 
122  vector<boost::shared_ptr<FeatureSequenced> > test_is_a = fdf_is.getFeatures(1,2);
123  vector<boost::shared_ptr<FeatureSequenced> > test_is_b = fdf_is.getFeatures(3,4);
124  vector<boost::shared_ptr<FeatureSequenced> > test_is_c = fdf_is.getFeatures(5,6);
125 
126  unit_assert(find_if(test_is_a.begin(), test_is_a.end(), IsFS(fs_a)) != test_is_a.end());
127  unit_assert(find_if(test_is_b.begin(), test_is_b.end(), IsFS(fs_b)) != test_is_b.end());
128  unit_assert(find_if(test_is_c.begin(), test_is_c.end(), IsFS(fs_c)) != test_is_c.end());
129 
130 
131  if (os_)
132  {
133  *os_ << "\ntesting istream constructor ... \n";
134 
135  }
136 
137 
138  return;
139 
140 }
141 
142 void testMerge()
143 {
144  if(os_) *os_ << "\ntestMerge()...\n" << endl;
145 
146  FeaturePtr a = makeFeature(5,1);
147  FeaturePtr b = makeFeature(20,9);
148 
150 
151  vector<FeaturePtr> v_a;
152  vector<FeaturePtr> v_b;
153 
154  v_a.push_back(a);
155  v_b.push_back(b);
156 
157  Feature_dataFetcher fdf_a(v_a);
158  Feature_dataFetcher fdf_b(v_b);
159 
160  fdf_a.merge(fdf_b);
161  vector<boost::shared_ptr<FeatureSequenced> > binContents = fdf_a.getFeatures(b->mz, b->retentionTime);
162 
163  unit_assert(binContents.size() > 0);
164 
165  if (os_)
166  {
167  *os_ << "Merged FeatureSequenced:\n " << endl;
168 
169  XMLWriter writer(*os_);
170  (*binContents.begin())->feature->write(writer);
171  *os_ << "ms1_5: " << (*binContents.begin())->ms1_5 << endl;
172  *os_ << "ms2: " << (*binContents.begin())->ms2 << endl;
173 
174  *os_ << "\nOriginal FeatureSequenced:\n " << endl;
175 
176  fs_b->feature->write(writer);
177  *os_ << "ms1_5: " << fs_b->ms1_5 << endl;
178  *os_ << "ms2: " << fs_b->ms2 << endl;
179 
180  }
181 
182  unit_assert((*binContents.back()) == *fs_b);
183 
184 }
185 
186 int main(int argc, char* argv[])
187 {
188 
189  try
190  {
191  if (argc>1 && !strcmp(argv[1],"-v")) os_ = &cout;
192  if (os_) *os_ << "Feature_dataFetcherTest ... \n";
193 
194  test();
195  testMerge();
196 
197  return 0;
198  }
199  catch (exception& e)
200  {
201  cerr << e.what() << endl;
202  }
203  catch (...)
204  {
205  cerr << "Caught unknown exception.\n";
206  }
207 
208  return 1;
209 }