ProteoWizard
ProteomeDataFileTest.cpp
Go to the documentation of this file.
1 //
2 // $Id: ProteomeDataFileTest.cpp 4129 2012-11-20 00:05:37Z chambm $
3 //
4 //
5 // Original author: Darren Kessner <darren@proteowizard.org>
6 //
7 // Copyright 2007 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 
24 #include "ProteomeDataFile.hpp"
25 #include "Diff.hpp"
26 #include "examples.hpp"
27 #include "Reader_FASTA.hpp"
31 #include <boost/iostreams/filtering_stream.hpp>
32 #include <boost/iostreams/filter/gzip.hpp>
33 #include <boost/iostreams/device/file_descriptor.hpp>
34 #include <boost/iostreams/copy.hpp>
35 
36 
37 using namespace pwiz::util;
38 using namespace pwiz::proteome;
39 using namespace pwiz::data;
40 using boost::shared_ptr;
41 
42 
43 ostream* os_ = 0;
44 
45 
46 string filenameBase_ = "temp.ProteomeDataFileTest";
47 
48 
50  const DiffConfig diffConfig)
51 {
52  if (os_) *os_ << "validateWriteRead()\n " << writeConfig << endl;
53 
54  string filename1 = filenameBase_ + "1.fasta";
55  string filename2 = filenameBase_ + "2.fasta";
56 
57  {
58  // create ProteomeData object in memory
59  ProteomeData tiny;
61 
62  // write to file #1 (static)
63  ProteomeDataFile::write(tiny, filename1, writeConfig);
64 
65  shared_ptr<Reader> reader;
66  if (writeConfig.format == ProteomeDataFile::Format_FASTA)
67  {
68  // Reader_FASTA creates the index in the read() call
69  Reader_FASTA::Config config;
70  config.indexed = writeConfig.indexed;
71  reader.reset(new Reader_FASTA(config));
72  }
73 
74  // read back into an ProteomeDataFile object
75  ProteomeDataFile pd1(filename1, *reader);
76 
77  // compare
78  Diff<ProteomeData, DiffConfig> diff(tiny, pd1, diffConfig);
79  if (diff && os_) *os_ << diff << endl;
80  unit_assert(!diff);
81 
82  // write to file #2 (member)
83  pd1.write(filename2, writeConfig);
84 
85  // read back into another ProteomeDataFile object
86  ProteomeDataFile pd2(filename2, *reader);
87 
88  // compare
89  diff(tiny, pd2);
90  if (diff && os_) *os_ << diff << endl;
91  unit_assert(!diff);
92 
93  // now give the gzip read a workout
94  bio::filtering_istream tinyGZ(bio::gzip_compressor() | bio::file_descriptor_source(filename1));
95  bio::copy(tinyGZ, bio::file_descriptor_sink(filename1+".gz", ios::out|ios::binary));
96 
97  ProteomeDataFile pd3(filename1+".gz", *reader);
98 
99  // compare
100  diff(tiny, pd3);
101  if (diff && os_) *os_ << diff << endl;
102  unit_assert(!diff);
103  }
104 
105  // remove temp files
106  bfs::remove(filename1);
107  bfs::remove(filename2);
108  bfs::remove(filename1 + ".gz");
109 
110  bool index1Exists = bfs::exists(filename1 + ".index");
111  bool index2Exists = bfs::exists(filename2 + ".index");
112  bool index3Exists = bfs::exists(filename1 + ".gz.index");
113 
114  bool indexShouldExist = writeConfig.indexed;
115  unit_assert(!indexShouldExist || index1Exists);
116  unit_assert(!indexShouldExist || index2Exists);
117  unit_assert(!indexShouldExist || index3Exists);
118 
119  if (index1Exists) bfs::remove(filename1 + ".index");
120  if (index2Exists) bfs::remove(filename2 + ".index");
121  if (index3Exists) bfs::remove(filename1 + ".gz.index");
122 }
123 
124 void test()
125 {
126  ProteomeDataFile::WriteConfig writeConfig;
127  DiffConfig diffConfig;
128 
129  // test FASTA with binary stream index
130  validateWriteRead(writeConfig, diffConfig);
131 
132  // test FASTA with memory index
133  writeConfig.indexed = false;
134  validateWriteRead(writeConfig, diffConfig);
135 }
136 
137 
138 class TestReader : public Reader
139 {
140  public:
141 
142  TestReader() : count(0) {}
143 
144  virtual std::string identify(const std::string& uri, shared_ptr<istream> uriStreamPtr) const
145  {
146  ++count;
147 
148  if (!bal::iends_with(uri, ".fasta"))
149  return "";
150 
151  string buf;
152  getline(*uriStreamPtr, buf);
153  if (buf[0] != '>')
154  return "";
155 
156  return getType();
157  }
158 
159  virtual void read(const std::string& uri,
160  shared_ptr<istream> uriStreamPtr,
161  ProteomeData& pd) const
162  {
163  ++count;
164  }
165 
166  const char *getType() const {return "testReader";} // satisfy inheritance
167 
168  mutable int count;
169 };
170 
171 
173 {
174  // create a file
175  string filename = filenameBase_ + ".fAsTa";
176  ofstream os(filename.c_str());
177  os << ">Id Description\nSEQUENCE\n";
178  os.close();
179 
180  // open the file with our Reader
181  TestReader reader;
182  ProteomeDataFile pd(filename, reader);
183 
184  // verify that our reader got called properly
185  unit_assert(reader.count == 2);
186 
187  // remove temp file
188  boost::filesystem::remove(filename);
189 
190  if (os_) *os_ << endl;
191 }
192 
193 
194 int main(int argc, char* argv[])
195 {
196  TEST_PROLOG(argc, argv)
197 
198  try
199  {
200  if (argc>1 && !strcmp(argv[1],"-v")) os_ = &cout;
201  test();
202  testReader();
203  }
204  catch (exception& e)
205  {
206  TEST_FAILED(e.what())
207  }
208  catch (...)
209  {
210  TEST_FAILED("Caught unknown exception.")
211  }
212 
214 }
215