ProteoWizard
PeptideID_dataFetcherTest.cpp
Go to the documentation of this file.
1 //
2 // $Id: PeptideID_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 
25 #include "Feature_dataFetcher.hpp"
26 #include "pwiz/utility/proteome/Ion.hpp"
28 #include <iomanip>
29 
30 using namespace pwiz::util;
31 using namespace pwiz::proteome;
32 using namespace pwiz::data::pepxml;
33 using namespace pwiz::eharmony;
34 
35 typedef boost::shared_ptr<PeptideID_dataFetcher> PidfPtr;
36 
37 ostream* os_ = 0;
38 
39 const char* samplePepXML =
40  "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
41  "<msms_pipeline_analysis>\n"
42  "<msms_run_summary>\n"
43  "<spectrum_query start_scan=\"1\" end_scan=\"2\" precursor_neutral_mass=\"1.0\" assumed_charge=\"1\" retention_time_sec=\"2.0\">\n"
44  "<search_result>\n"
45  "<search_hit peptide=\"BUCKLEMYSHOE\">\n"
46  "<analysis_result analysis=\"peptideprophet\">\n"
47  "<peptideprophet_result probability=\"0.900\" all_ntt_prob=\"(0,0,0.900)\">\n"
48  "<search_score_summary>\n"
49  "</search_score_summary>\n"
50  "</peptideprophet_result>\n"
51  "</analysis_result>\n"
52  "</search_hit>\n"
53  "</search_result>\n"
54  "</spectrum_query>\n"
55  "<spectrum_query start_scan=\"3\" end_scan=\"4\" precursor_neutral_mass=\"3.0\" assumed_charge=\"1\" retention_time_sec=\"4.0\">\n"
56  "<search_result>\n"
57  "<search_hit peptide=\"SHUTTHEDOOR\">\n"
58  "<analysis_result analysis=\"peptideprophet\">\n"
59  "<peptideprophet_result probability=\"0.900\" all_ntt_prob=\"(0,0,0.900)\">\n"
60  "<search_score_summary>\n"
61  "</search_score_summary>\n"
62  "</peptideprophet_result>\n"
63  "</analysis_result>\n"
64  "</search_hit>\n"
65  "</search_result>\n"
66  "</spectrum_query>\n"
67  "<spectrum_query start_scan=\"5\" end_scan=\"6\" precursor_neutral_mass=\"5.0\" assumed_charge=\"1\" retention_time_sec=\"6.0\">\n"
68  "<search_result>\n"
69  "<search_hit peptide=\"PICKUPSTICKS\">\n"
70  "<analysis_result analysis=\"peptideprophet\">\n"
71  "<peptideprophet_result probability=\"0.900\" all_ntt_prob=\"(0,0,0.900)\">\n"
72  "<search_score_summary>\n"
73  "</search_score_summary>\n"
74  "</peptideprophet_result>\n"
75  "</analysis_result>\n"
76  "</search_hit>\n"
77  "</search_result>\n"
78  "</spectrum_query>\n"
79  "<spectrum_query start_scan=\"7\" end_scan=\"8\" precursor_neutral_mass=\"7.0\" assumed_charge=\"1\" retention_time_sec=\"8.0\">\n"
80  "<search_result>\n"
81  "<search_hit peptide=\"LAYTHEMSTRAIGHT\">\n"
82  "<analysis_result analysis=\"peptideprophet\">\n"
83  "<peptideprophet_result probability=\"0.900\" all_ntt_prob=\"(0,0,0.900)\">\n"
84  "<search_score_summary>\n"
85  "</search_score_summary>\n"
86  "</peptideprophet_result>\n"
87  "</analysis_result>\n"
88  "</search_hit>\n"
89  "</search_result>\n"
90  "</spectrum_query>\n"
91  "</msms_run_summary>\n"
92  "</msms_pipeline_analysis>\n";
93 
94 
95 SpectrumQuery makeSpectrumQuery(double precursorNeutralMass, double rt, int charge, string sequence, double score, int startScan, int endScan)
96 {
97  SpectrumQuery spectrumQuery;
98  spectrumQuery.startScan = startScan;
99  spectrumQuery.endScan = endScan;
100  spectrumQuery.precursorNeutralMass = precursorNeutralMass;
101  spectrumQuery.assumedCharge = charge;
102  spectrumQuery.retentionTimeSec = rt;
103 
104  SearchResult searchResult;
105 
106  SearchHit searchHit;
107  searchHit.peptide = sequence;
108 
109 
110  AnalysisResult analysisResult;
111  analysisResult.analysis = "peptideprophet";
112 
113  PeptideProphetResult xresult;
114  xresult.probability = score;
115  xresult.allNttProb.push_back(0);
116  xresult.allNttProb.push_back(0);
117  xresult.allNttProb.push_back(score);
118 
119  analysisResult.peptideProphetResult = xresult;
120 
121  searchHit.analysisResult = analysisResult;
122  searchResult.searchHit = searchHit;
123 
124  spectrumQuery.searchResult = searchResult;
125 
126  return spectrumQuery;
127 
128 }
129 
131 {
132 
133  istringstream iss(samplePepXML);
134  PidfPtr pidf(new PeptideID_dataFetcher(iss));
135 
136  return pidf;
137 
138 }
139 /*
140 boost::shared_ptr<Feature> makeFeature(double mz, double retentionTime)
141 {
142  boost::shared_ptr<Feature> feature;
143  feature->mz = mz;
144  feature->retentionTime = retentionTime;
145 
146  return feature;
147  }*/
148 
149 struct IsSQ
150 {
151  IsSQ(const SpectrumQuery& sq) : _p(sq){}
153  bool operator()(boost::shared_ptr<SpectrumQuery> sq){ return *sq == _p;}
154 
155 };
156 
158 {
159  if (os_)
160  {
161  *os_ << "\ntestPeptideID_dataFetcherConstructor() ... \n";
162  *os_ << "\nSample pep.xml: \n";
163  *os_ << samplePepXML << endl;
164 
165  }
166 
167  // make the PeptideID_dataFetcher from input pep.xml
169 
170  // make the SpectrumQuery objects that we expect to be read into the PeptideID_dataFetcher
171  SpectrumQuery a = makeSpectrumQuery(1,2,1, "BUCKLEMYSHOE", 0.900, 1,2); // mz, rt, charge, sequence, score, start scan, end scan
172  SpectrumQuery b = makeSpectrumQuery(3,4,1,"SHUTTHEDOOR",0.900,3,4);
173  SpectrumQuery c = makeSpectrumQuery(5,6,1,"PICKUPSTICKS",0.900,5,6);
174  SpectrumQuery d = makeSpectrumQuery(7,8,1,"LAYTHEMSTRAIGHT",0.900,7,8);
175 
176  // Access SpectrumQuery objects that are in the PeptideID_dataFetcher at the coordinates we expect
177  vector<boost::shared_ptr<SpectrumQuery> > sq_a = pidf->getSpectrumQueries(Ion::mz(1,1),2);
178  vector<boost::shared_ptr<SpectrumQuery> > sq_b = pidf->getSpectrumQueries(Ion::mz(3,1),4);
179  vector<boost::shared_ptr<SpectrumQuery> > sq_c = pidf->getSpectrumQueries(Ion::mz(5,1),6);
180  vector<boost::shared_ptr<SpectrumQuery> > sq_d = pidf->getSpectrumQueries(Ion::mz(7,1),8);
181 
182  // Assert that all SpectrumQuery objects were found at expected coordinates
183  unit_assert(find_if(sq_a.begin(), sq_a.end(),IsSQ(a)) != sq_a.end());
184  unit_assert(find_if(sq_b.begin(), sq_b.end(),IsSQ(b)) != sq_b.end());
185  unit_assert(find_if(sq_c.begin(), sq_c.end(),IsSQ(c)) != sq_c.end());
186  unit_assert(find_if(sq_d.begin(), sq_d.end(),IsSQ(d)) != sq_d.end());
187 
188  if (os_)
189  {
190  *os_ << "\nSpectrumQuery objects read from sample pep.xml: \n";
191  ostringstream oss;
192  XMLWriter writer(oss);
193  a.write(writer);
194  b.write(writer);
195  c.write(writer);
196  d.write(writer);
197  *os_ << oss.str() << endl;
198  }
199 
200 }
201 
202 void testMerge()
203 {
204  if (os_) *os_ << "\ntestMerge() ... \n" << endl;
205 
206  SpectrumQuery b = makeSpectrumQuery(1,2,1, "BUCKLEMYSHOE", 0.900, 1,2);
207  SpectrumQuery c = makeSpectrumQuery(3,4,1, "SHUTTHEDOOR", 0.900, 3,4);
208 
209  vector<SpectrumQuery> v;
210  v.push_back(b);
211 
212  vector<SpectrumQuery> v2;
213  SpectrumQuery b2(b);
214  b2.retentionTimeSec = 4000;
215  v2.push_back(b2);
216  v2.push_back(c);
217 
218  MSMSPipelineAnalysis mspa_fiat;
219  mspa_fiat.msmsRunSummary.spectrumQueries = v;
220 
221  MSMSPipelineAnalysis mspa_chrysler;
222  mspa_chrysler.msmsRunSummary.spectrumQueries = v2;
223 
224  PeptideID_dataFetcher fiat(mspa_fiat);
225  PeptideID_dataFetcher chrysler(mspa_chrysler);
226 
227  fiat.merge(chrysler);
228 
229  // test that the merger correctly concatenated all SpectrumQuery objects
230  // vector<boost::shared_ptr<SpectrumQuery> > contents = fiat.getSpectrumQueries(Ion::mz(b.precursorNeutralMass, b.assumedCharge), b.retentionTimeSec);
231  vector<boost::shared_ptr<SpectrumQuery> > contents = fiat.getBin().getAllContents();
232  unit_assert(contents.size() == 2);
233  unit_assert(**contents.begin() == b);
234  unit_assert(*contents.back() == c);
235 
236 
237 
238 }
239 
240 int main(int argc, char* argv[])
241 {
242  try
243  {
244  if (argc>1 && !strcmp(argv[1],"-v")) os_ = &cout;
245 
247  testMerge();
248 
249  }
250 
251  catch (std::exception& e)
252  {
253  cerr << e.what() << endl;
254  return 1;
255 
256  }
257 
258  catch (...)
259  {
260  cerr << "Caught unknown exception.\n";
261  return 1;
262 
263  }
264 
265  return 0;
266 
267 }