metric_file_stream.h
Go to the documentation of this file.
1 
10 #pragma once
11 #include "interop/util/exception.h"
16 
17 namespace illumina { namespace interop { namespace io
18 {
19 
31  template<class MetricSet>
33  {
34  return size_of_buffer(metrics);
35  }
43  template<class MetricSet>
44  size_t write_interop_to_buffer(const MetricSet& metrics, ::uint8_t* buffer, const size_t buffer_size)
46  {
47  std::ostringstream fout;
48  write_metrics(fout, metrics, metrics.version());
49  std::string str = fout.str();
50  if(buffer_size < str.length())
51  INTEROP_THROW(invalid_argument, "Buffer size too small: " << buffer_size << " < " << str.length());
52  size_t i=0;
53  for(;i<str.length();++i)
54  buffer[i] = static_cast< ::uint8_t >(str[i]);
55  return i;
56  }
65  template<class MetricSet>
66  void read_interop_from_buffer(::uint8_t* buffer, const size_t buffer_size, MetricSet& metrics) INTEROP_THROW_SPEC(
71  {
72  detail::membuf sbuf(reinterpret_cast<char*>(buffer), reinterpret_cast<char*>(buffer) + buffer_size);
73  std::istream in(&sbuf);
74  read_metrics(in, metrics, buffer_size, /*rebuild=*/ false);
75  }
85  template<class MetricSet>
86  void read_interop_from_string(const std::string& buffer, MetricSet& metrics, const bool rebuild=true) INTEROP_THROW_SPEC(
87  (interop::io::bad_format_exception,
88  interop::io::incomplete_file_exception,
89  model::index_out_of_bounds_exception) )
90  {
91  std::istringstream in(buffer);
92  read_metrics(in, metrics, buffer.length(), rebuild);
93  }
103  template<class MetricSet>
104  void write_interop_to_string(std::string& buffer
105  , const MetricSet& metrics, const ::int16_t version = -1) INTEROP_THROW_SPEC(
106  (interop::io::bad_format_exception,
107  interop::io::incomplete_file_exception,
108  model::index_out_of_bounds_exception))
109  {
110  std::ostringstream out;
111  write_metrics(out, metrics, version);
112  buffer = out.str();
113  }
122  template<class MetricSet>
123  size_t read_header_from_string(const std::string& buffer, MetricSet& metrics) INTEROP_THROW_SPEC(
127  {
128  std::istringstream in(buffer);
129  return read_header(in, metrics);
130  }
139  template<class MetricSet>
140  void write_header_to_string(std::string& buffer, const MetricSet& metrics, ::int16_t version=-1) INTEROP_THROW_SPEC(
141  (interop::io::bad_format_exception,
142  interop::io::incomplete_file_exception,
143  model::index_out_of_bounds_exception))
144  {
145  if(version == -1) version = metrics.version();
146  std::ostringstream out;
147  write_metric_header<typename MetricSet::metric_type>(out, version, metrics);
148  buffer = out.str();
149  }
164  template<class MetricSet>
165  void read_interop(const std::string& run_directory, MetricSet& metrics, const bool use_out=true) INTEROP_THROW_SPEC(
169  model::index_out_of_bounds_exception))
170  {
171 # if __GNUC__ <= 4 && __GNUC_MINOR__ <= 7
172  // ---------------------------------------------------------------------------------------------------------
173  // No operations to get GCC 4.4.x to compile correctly
174  const bool no_op = metrics.offset_map().find(0) != metrics.offset_map().end();
175  (void)no_op;
176  metrics.resize(metrics.size());
177  metrics.trim(metrics.size());
178  // ---------------------------------------------------------------------------------------------------------
179 # endif
180 
181  std::string file_name = interop_filename<MetricSet>(run_directory, use_out);
182  std::ifstream fin(file_name.c_str(), std::ios::binary);
183  if(!fin.good())
184  {
185  file_name = interop_filename<MetricSet>(run_directory, !use_out);
186  fin.open(file_name.c_str(), std::ios::binary);
187  }
188  if(!fin.good()) INTEROP_THROW(file_not_found_exception, "File not found: " << file_name);
189  read_metrics(fin, metrics, static_cast<size_t>(file_size(file_name)));
190  }
202  template<class MetricSet>
203  bool write_interop(const std::string& run_directory,
204  const MetricSet& metrics,
205  const bool use_out=true,
206  const ::int16_t version=-1)
210  {
211  if(metrics.empty() || metrics.version() == 0 )return true;
212  const std::string file_name = interop_filename<MetricSet>(run_directory, use_out);
213  std::ofstream fout(file_name.c_str(), std::ios::binary);
214  if(!fout.good())INTEROP_THROW(file_not_found_exception, "File not found: " << file_name);
215  write_metrics(fout, metrics, version);
216  return fout.good();
217  }
228  template<class MetricType>
229  void write_interop_header(const std::string& run_directory,
230  const ::int16_t version=-1,
231  const typename MetricType::header_type& header = typename MetricType::header_type(),
232  const bool use_out=true)
236  {
237  const std::string file_name = interop_filename<MetricType>(run_directory, use_out);
238  std::ofstream fout(file_name.c_str(), std::ios::binary);
239  if(!fout.good())INTEROP_THROW(file_not_found_exception, "File not found: " << file_name);
240  write_metric_header(fout, header, version);
241  }
250  template<class MetricSet>
251  bool interop_exists(const std::string& run_directory, MetricSet&, const bool use_out=true)
255  model::index_out_of_bounds_exception))
256  {
257  const std::string file_name = interop_filename<MetricSet>(run_directory, use_out);
258  std::ifstream fin(file_name.c_str(), std::ios::binary);
259  if(!fin.good()) return false;
260  return true;
261  }
274  template<class MetricSet>
275  void list_interop_filenames(std::vector<std::string>& files,
276  const std::string& run_directory,
277  const size_t last_cycle=0,
278  const bool use_out=true,
279  const bool add=false)
280  {
281  if(!add)
282  {
283  files.clear();
284  files.reserve(last_cycle+1);
285  }
286  else files.reserve(last_cycle+1+files.size());
287  files.push_back(interop_filename<MetricSet>(run_directory, use_out));
288  for(size_t cycle=1;cycle <= last_cycle;++cycle)
289  {
290  files.push_back(interop_filename<MetricSet>(run_directory, cycle, use_out));
291  }
292  }
308  template<class MetricSet>
309  void read_interop_by_cycle(const std::string& run_directory,
310  MetricSet& metrics,
311  const size_t last_cycle,
312  const bool use_out=true)
314  interop::io::bad_format_exception,
315  interop::io::incomplete_file_exception,
316  model::index_out_of_bounds_exception))
317  {
318  std::string incomplete_file_message;
319  for(size_t cycle=1;cycle <= last_cycle;++cycle)
320  {
321  const std::string file_name = interop_filename<MetricSet>(run_directory, cycle, use_out);
322  const int64_t file_size_in_bytes = file_size(file_name);
323  if(file_size_in_bytes < 0) continue;
324  std::ifstream fin(file_name.c_str(), std::ios::binary);
325  if(fin.good())
326  {
327  try
328  {
329  read_metrics(fin, metrics, static_cast<size_t>(file_size_in_bytes), false);
330  }
331  catch(const incomplete_file_exception& ex)
332  {
333  incomplete_file_message = ex.what();
334  }
335  }
336  }
337  metrics.rebuild_index();
338  if(incomplete_file_message != "")
339  throw incomplete_file_exception(incomplete_file_message);
340  }
350  template<class MetricSet>
351  bool interop_exists(const std::string& run_directory, MetricSet&, const size_t last_cycle, const bool use_out=true)
355  model::index_out_of_bounds_exception))
356  {
357  std::string file_name = interop_filename<MetricSet>(run_directory, use_out);
358  std::ifstream fin(file_name.c_str(), std::ios::binary);
359  if(fin.good()) return true;
360  for(size_t cycle=1;cycle <= last_cycle;++cycle)
361  {
362  file_name = interop_filename<MetricSet>(run_directory, cycle, use_out);
363  fin.open(file_name.c_str(), std::ios::binary);
364  if(fin.good()) return true;
365  }
366  return false;
367  }
374  template<class Metric>
375  bool is_deprecated(const int version)
376  {
377  typedef metric_format_factory<Metric> factory_t;
378  typedef typename factory_t::metric_format_map metric_format_map;
379  metric_format_map &format_map = factory_t::metric_formats();
380  if (format_map.find(version) == format_map.end())
381  return false;
382  if(format_map[version]->is_deprecated()) return true;
383  return false;
384  }
391  template<class MetricSet>
392  bool is_deprecated_set(MetricSet &metrics)
393  {
394  typedef typename MetricSet::metric_type metric_t;
395  return is_deprecated<metric_t>(metrics.version());
396  }
397 
398 }}}
399 
400 
metric_type
Definition: enums.h:284
Definition: enum_description.h:15
Definition: stream_exceptions.h:86
Definition: stream_membuf.h:27
#define INTEROP_THROW_SPEC(SPEC)
Definition: exception_specification.h:15
void write_metric_header(std::ostream &out, const ::int16_t version, const typename MetricType::header_type &header=typename MetricType::header_type())
Definition: metric_stream.h:287
size_t read_header_from_string(const std::string &buffer, MetricSet &metrics) INTEROP_THROW_SPEC((interop
Definition: metric_file_stream.h:123
#define INTEROP_THROW(EXCEPTION, MESSAGE)
Definition: exception.h:18
Definition: metric_format_factory.h:48
Definition: stream_exceptions.h:45
void write_interop_to_string(std::string &buffer, const MetricSet &metrics, const ::int16_t version=-1) INTEROP_THROW_SPEC((interop
Definition: metric_file_stream.h:104
void read_interop(const std::string &run_directory, MetricSet &metrics, const bool use_out=true) INTEROP_THROW_SPEC((io
Definition: metric_file_stream.h:165
size_t write_interop_to_buffer(const MetricSet &metrics,::uint8_t *buffer, const size_t buffer_size)
Definition: metric_file_stream.h:44
bool is_deprecated_set(MetricSet &metrics)
Definition: metric_file_stream.h:392
bool interop_exists(const std::string &run_directory, MetricSet &, const bool use_out=true) INTEROP_THROW_SPEC((io
Definition: metric_file_stream.h:251
void read_interop_by_cycle(const std::string &run_directory, MetricSet &metrics, const size_t last_cycle, const bool use_out=true) INTEROP_THROW_SPEC((interop
Definition: metric_file_stream.h:309
void write_header_to_string(std::string &buffer, const MetricSet &metrics,::int16_t version=-1) INTEROP_THROW_SPEC((interop
Definition: metric_file_stream.h:140
bool write_interop(const std::string &run_directory, const MetricSet &metrics, const bool use_out=true, const ::int16_t version=-1) INTEROP_THROW_SPEC((io
Definition: metric_file_stream.h:203
Definition: stream_exceptions.h:27
Definition: stream_exceptions.h:59
Definition: stream_exceptions.h:73
void list_interop_filenames(std::vector< std::string > &files, const std::string &run_directory, const size_t last_cycle=0, const bool use_out=true, const bool add=false)
Definition: metric_file_stream.h:275
void read_interop_from_string(const std::string &buffer, MetricSet &metrics, const bool rebuild=true) INTEROP_THROW_SPEC((interop
Definition: metric_file_stream.h:86
bool is_deprecated(const int version)
Definition: metric_file_stream.h:375
size_t compute_buffer_size(const MetricSet &metrics) INTEROP_THROW_SPEC((io
Definition: metric_file_stream.h:32
size_t size_of_buffer(const MetricSet &metric_set,::int16_t version=-1)
Definition: metric_stream.h:202
void write_interop_header(const std::string &run_directory, const ::int16_t version=-1, const typename MetricType::header_type &header=typename MetricType::header_type(), const bool use_out=true) INTEROP_THROW_SPEC((io
Definition: metric_file_stream.h:229
::int64_t file_size(const std::string &path)
Definition: filesystem.cpp:156
void read_interop_from_buffer(::uint8_t *buffer, const size_t buffer_size, MetricSet &metrics) INTEROP_THROW_SPEC((interop
Definition: metric_file_stream.h:66
void read_metrics(std::istream &in, MetricSet &metrics, const size_t file_size, const bool rebuild=true)
Definition: metric_stream.h:99
size_t read_header(std::istream &in, MetricSet &metrics)
Definition: metric_stream.h:73