q_metric.h
Go to the documentation of this file.
1 
16 #pragma once
17 
18 #include <cstring>
19 #include <numeric>
20 #include "interop/util/exception.h"
25 
26 namespace illumina { namespace interop { namespace model { namespace metrics
27 {
31  {
32  public:
34  typedef ::uint16_t bin_type;
35  public:
42  q_score_bin(const bin_type lower = 0, const bin_type upper = 0, const bin_type value = 0) :
43  m_lower(lower),
44  m_upper(upper),
45  m_value(value)
46  { }
47 
48  public:
54  template<class T>
55  q_score_bin &operator=(const T &val)
56  {
57  m_lower = val.m_lower;
58  m_upper = val.m_upper;
59  m_value = val.m_value;
60  return *this;
61  }
62 
63  public:
76  bin_type lower() const
77  { return m_lower; }
78 
83  bin_type upper() const
84  { return m_upper; }
85 
90  bin_type value() const
91  { return m_value; }
94  private:
95  bin_type m_lower;
96  bin_type m_upper;
97  bin_type m_value;
98  template<class MetricType, int Version>
99  friend
100  struct io::generic_layout;
101  };
102 
106  {
107  public:
108  enum
109  {
112  };
114  typedef std::vector<q_score_bin> qscore_bin_vector_type;
117  public:
121  { }
122 
127  q_score_header(const qscore_bin_vector_type &bins) :
128  m_qscore_bins(bins)
129  { }
130 
144  {
145  INTEROP_BOUNDS_CHECK(n, m_qscore_bins.size(), "Index out of bounds");
146  return m_qscore_bins[n];
147  }
148 
153  const qscore_bin_vector_type &get_bins() const
154  {
155  return m_qscore_bins;
156  }
157 
162  qscore_bin_vector_type &bins()
163  {
164  return m_qscore_bins;
165  }
166 
171  size_t bin_count() const
172  { return m_qscore_bins.size(); }
177  size_t q_val_count() const
178  { return m_qscore_bins.empty() ? static_cast<size_t>(MAX_Q_BINS) : m_qscore_bins.size(); }
179 
186  size_t index_for_q_value(const size_t qval) const
187  {
188  if (m_qscore_bins.size() == 0) return qval - 1;
189  size_t index = 0;
190  while (index < m_qscore_bins.size() && binAt(index).value() < qval) index++;
191  return index;
192  }
199  size_t binCount() const
200  { return m_qscore_bins.size(); }
201 
207  {
208  return q_score_header();
209  }
210 
216  const q_score_bin &binAt(const size_t n) const
217  {
218  INTEROP_ASSERT(n < binCount());
219  return m_qscore_bins[n];
220  }
223  void clear()
224  {
225  m_qscore_bins.clear();
227  }
228 
229  protected:
231  qscore_bin_vector_type m_qscore_bins;
232  private:
233  template<class MetricType, int Version>
234  friend
235  struct io::generic_layout;
236  };
237 
248  {
249  public:
250  enum
251  {
256  };
257  public:
258  enum
259  {
262  };
269  typedef std::vector< ::uint32_t > uint32_vector;
272  typedef std::vector< ::uint32_t > uint_vector;
275  typedef std::vector< ::uint64_t > uint64_vector;
278  typedef ::uint32_t *uint_pointer_t;
279  public:
283  metric_base::base_cycle_metric(0, 0, 0)
284  { }
289  q_metric(const header_type& header) :
290  metric_base::base_cycle_metric(0, 0, 0),
291  m_qscore_hist(header.bin_count()==0?static_cast<size_t>(MAX_Q_BINS):header.bin_count())
292  { }
293 
302  const uint_t tile,
303  const uint_t cycle,
304  const uint32_vector &qscore_hist) :
305  metric_base::base_cycle_metric(lane, tile, cycle),
306  m_qscore_hist(qscore_hist)
307  {
308  }
309 
319  const uint_t tile,
320  const uint_t cycle,
321  const uint_pointer_t qscore_hist,
322  const uint_t count) :
323  metric_base::base_cycle_metric(lane, tile, cycle),
324  m_qscore_hist(qscore_hist, qscore_hist + count)
325  {
326  }
327 
328  public:
336  void set(const uint_t lane,
337  const uint_t tile,
338  const uint_t cycle,
339  const uint32_vector& qscore_hist)
340  {
341  metric_base::base_cycle_metric::set_base(lane, tile, cycle);
343  m_qscore_hist_cumulative.resize(qscore_hist.size(), 0);
344  }
345 
346  public:
362  {
363  INTEROP_BOUNDS_CHECK(n, m_qscore_hist.size(), "Index out of bounds");
364  return m_qscore_hist[n];
365  }
366 
371  const uint32_vector &qscore_hist() const
372  {
373  return m_qscore_hist;
374  }
375 
380  size_t size() const
381  {
382  return m_qscore_hist.size();
383  }
384 
390  {
391  return std::accumulate(m_qscore_hist.begin(), m_qscore_hist.end(), 0);
392  }
393 
398  ::uint64_t sum_qscore_cumulative() const
399  {
400  return std::accumulate(m_qscore_hist_cumulative.begin(),
401  m_qscore_hist_cumulative.end(),
402  static_cast< ::uint64_t >(0));
403  }
404 
420  const qscore_bin_vector_type &bins) const
421  {
422  uint_t total_count = 0;
423  for (size_t i = 0; i < bins.size(); i++)
424  {
425  if (bins[i].value() >= qscore)
426  total_count += m_qscore_hist[i];
427  }
428  return total_count;
429  }
430 
442  uint_t total_over_qscore(const size_t qscore_index) const
443  {
444  uint_t total_count = 0;
445  if(qscore_index <= m_qscore_hist.size())
446  total_count = std::accumulate(m_qscore_hist.begin() + qscore_index, m_qscore_hist.end(), 0);
447  return total_count;
448  }
449 
459  ::uint64_t total_over_qscore_cumulative(const size_t qscore_index) const
460  {
461  INTEROP_ASSERT(m_qscore_hist_cumulative.size() > 0);
462  ::uint64_t total_count = 0;
463  if(qscore_index <= m_qscore_hist_cumulative.size())
464  total_count = std::accumulate(m_qscore_hist_cumulative.begin() + qscore_index,
465  m_qscore_hist_cumulative.end(),
466  static_cast< ::uint64_t >(0));
467  return total_count;
468  }
469 
481  float percent_over_qscore(const size_t qscore_index) const
482  {
483  const float total = static_cast<float>(sum_qscore());
484  if (total == 0.0f) return std::numeric_limits<float>::quiet_NaN();
485  const uint_t total_count = total_over_qscore(qscore_index);
486  return 100 * total_count / total;
487  }
488 
498  float percent_over_qscore_cumulative(const size_t qscore_index) const
499  {
500  INTEROP_ASSERT(m_qscore_hist_cumulative.size() > 0);
501  const ::uint64_t total = sum_qscore_cumulative();
502  if (total == 0) return std::numeric_limits<float>::quiet_NaN();
503  const ::uint64_t total_count = total_over_qscore_cumulative(qscore_index);
504  return 100.0f * total_count / total;
505  }
506 
519  ::uint64_t total_over_qscore_cumulative(const uint_t qscore,
520  const qscore_bin_vector_type &bins) const
521  {
522  INTEROP_ASSERT(m_qscore_hist_cumulative.size() > 0);
523  ::uint64_t total_count = 0;
524  for (size_t i = 0; i < bins.size(); i++)
525  {
526  if (bins[i].value() >= qscore)
527  total_count += m_qscore_hist_cumulative[i];
528  }
529  return total_count;
530  }
531 
546  float percent_over_qscore(const uint_t qscore_index,
547  const qscore_bin_vector_type &bins) const
548  {
549  const float total = static_cast<float>(sum_qscore());
550  if (total == 0.0f) return std::numeric_limits<float>::quiet_NaN();
551  const uint_t total_count = total_over_qscore(qscore_index, bins);
552  return 100.0f * total_count / total;
553  }
554 
568  const qscore_bin_vector_type &bins) const
569  {
570  INTEROP_ASSERT(m_qscore_hist_cumulative.size() > 0);
571  const ::uint64_t total = sum_qscore_cumulative();
572  if (total == 0) return std::numeric_limits<float>::quiet_NaN();
573  const ::uint64_t total_count = total_over_qscore_cumulative(qscore, bins);
574  return 100.0f * total_count / total;
575  }
576 
587  uint_t median(const qscore_bin_vector_type &bins = qscore_bin_vector_type()) const
588  {
589  const uint_t total = sum_qscore();
590  const uint_t position = total % 2 == 0 ? total / 2 + 1 : (total + 1) / 2;
591  uint_t i = 0;
592  uint_t sum = 0;
593  for (; i < m_qscore_hist.size(); i++)
594  {
595  sum += m_qscore_hist[i];
596  if (sum >= position)
597  {
598  if (bins.size() == 0 || m_qscore_hist.size() == MAX_Q_BINS) return i + 1;
599  if (i < bins.size()) return bins[i].value();
600  break;
601  }
602  }
603  return std::numeric_limits<uint_t>::max();
604  }
605 
610  bool is_cumulative_empty() const
611  {
612  return m_qscore_hist_cumulative.empty();
613  }
621  void accumulate(const q_metric &metric)
622  {
623  uint64_vector::const_iterator beg = metric.m_qscore_hist_cumulative.begin(),
624  end = metric.m_qscore_hist_cumulative.end();
625  m_qscore_hist_cumulative.assign(m_qscore_hist.begin(), m_qscore_hist.end());
626  if (&metric != this)
627  {
628  for (uint64_vector::iterator cur = m_qscore_hist_cumulative.begin(); beg != end; ++beg, ++cur)
629  *cur += *beg;
630  }
631  }
632 
637  template<typename T>
638  void accumulate_into(std::vector<T> &distribution) const
639  {
640  INTEROP_ASSERT(distribution.size() == m_qscore_hist.size());
641  if (distribution.size() != m_qscore_hist.size()) return;
642  typename std::vector<T>::iterator it = distribution.begin();
643  for (typename uint32_vector::const_iterator cur = m_qscore_hist.begin(), end = m_qscore_hist.end();
644  cur != end; ++it, ++cur)
645  {
646  (*it) += (*cur);
647  }
648  }
653  void compress(const header_type& header)
654  {
655  if(size() == header.bin_count() || header.bin_count() == 0) return;
656  for(size_t i=0;i<header.bin_count();++i)
657  {
658  m_qscore_hist[i] = m_qscore_hist[static_cast<size_t>(header.bin_at(i).value()-1)];
659  }
660  m_qscore_hist.resize(header.bin_count());
661  }
662 
668  uint_t qscoreHist(const size_t n) const
669  {
670  INTEROP_ASSERT(n < m_qscore_hist.size());
671  return m_qscore_hist[n];
672  }
673 
679  const uint32_vector &qscoreHist() const
680  {
681  return m_qscore_hist;
682  }
683 
684  public:
689  static const char *prefix()
690  { return "Q"; }
691 
692  protected:
694  uint32_vector m_qscore_hist;
695  private:
696  uint64_vector m_qscore_hist_cumulative;
697 
698  template<class MetricType, int Version>
699  friend
700  struct io::generic_layout;
701  };
702 }}}}
703 
704 
q_score_bin(const bin_type lower=0, const bin_type upper=0, const bin_type value=0)
Definition: q_metric.h:42
size_t index_for_q_value(const size_t qval) const
Definition: q_metric.h:186
size_t q_val_count() const
Definition: q_metric.h:177
uint_t total_over_qscore(const size_t qscore_index) const
Definition: q_metric.h:442
Definition: enum_description.h:15
uint_t qscoreHist(const size_t n) const
Definition: q_metric.h:668
bin_type upper() const
Definition: q_metric.h:83
uint_t tile() const
Definition: base_metric.h:284
std::vector< ::uint32_t > uint32_vector
Definition: q_metric.h:269
#define INTEROP_THROW_SPEC(SPEC)
Definition: exception_specification.h:15
::uint32_t * uint_pointer_t
Definition: q_metric.h:278
::uint64_t total_over_qscore_cumulative(const uint_t qscore, const qscore_bin_vector_type &bins) const
Definition: q_metric.h:519
bool is_cumulative_empty() const
Definition: q_metric.h:610
float percent_over_qscore(const size_t qscore_index) const
Definition: q_metric.h:481
static q_score_header default_header()
Definition: q_metric.h:206
float percent_over_qscore(const uint_t qscore_index, const qscore_bin_vector_type &bins) const
Definition: q_metric.h:546
std::vector< ::uint64_t > uint64_vector
Definition: q_metric.h:275
qscore_bin_vector_type & bins()
Definition: q_metric.h:162
size_t size() const
Definition: q_metric.h:380
q_score_header header_type
Definition: q_metric.h:264
base_cycle_metric(const uint_t lane=0, const uint_t tile=0, const uint_t cycle=0)
Definition: base_cycle_metric.h:90
uint_t cycle() const
Definition: base_cycle_metric.h:131
q_metric(const uint_t lane, const uint_t tile, const uint_t cycle, const uint_pointer_t qscore_hist, const uint_t count)
Definition: q_metric.h:318
#define INTEROP_ASSERT(TST)
Definition: assert.h:21
uint_t sum_qscore() const
Definition: q_metric.h:389
bin_type lower() const
Definition: q_metric.h:76
size_t binCount() const
Definition: q_metric.h:199
bin_type value() const
Definition: q_metric.h:90
Definition: enums.h:291
void compress(const header_type &header)
Definition: q_metric.h:653
header_type::qscore_bin_vector_type qscore_bin_vector_type
Definition: q_metric.h:266
const uint32_vector & qscoreHist() const
Definition: q_metric.h:679
uint_t qscore_hist(const size_t n) const INTEROP_THROW_SPEC((model
Definition: q_metric.h:361
::uint32_t uint_t
Definition: base_metric.h:77
const qscore_bin_vector_type & get_bins() const
Definition: q_metric.h:153
void accumulate_into(std::vector< T > &distribution) const
Definition: q_metric.h:638
Definition: generic_layout.h:24
q_score_header(const qscore_bin_vector_type &bins)
Definition: q_metric.h:127
q_metric()
Definition: q_metric.h:282
q_metric(const uint_t lane, const uint_t tile, const uint_t cycle, const uint32_vector &qscore_hist)
Definition: q_metric.h:301
uint_t lane() const
Definition: base_metric.h:269
q_score_bin bin_t
Definition: q_metric.h:116
uint_t total_over_qscore(const uint_t qscore, const qscore_bin_vector_type &bins) const
Definition: q_metric.h:419
void set_base(const uint_t lane, const uint_t tile)
Definition: base_cycle_metric.h:101
const q_score_bin & binAt(const size_t n) const
Definition: q_metric.h:216
const q_score_bin & bin_at(const size_t n) const INTEROP_THROW_SPEC((model
Definition: q_metric.h:143
qscore_bin_vector_type m_qscore_bins
Definition: q_metric.h:231
void clear()
Definition: q_metric.h:223
const uint32_vector & qscore_hist() const
Definition: q_metric.h:371
Definition: enums.h:301
std::vector< q_score_bin > qscore_bin_vector_type
Definition: q_metric.h:114
q_score_bin & operator=(const T &val)
Definition: q_metric.h:55
::uint64_t total_over_qscore_cumulative(const size_t qscore_index) const
Definition: q_metric.h:459
float percent_over_qscore_cumulative(const size_t qscore_index) const
Definition: q_metric.h:498
uint32_vector m_qscore_hist
Definition: q_metric.h:694
::uint64_t sum_qscore_cumulative() const
Definition: q_metric.h:398
q_metric(const header_type &header)
Definition: q_metric.h:289
std::vector< ::uint32_t > uint_vector
Definition: q_metric.h:272
#define INTEROP_BOUNDS_CHECK(VALUE, RANGE, MESSAGE)
Definition: exception.h:24
static const char * prefix()
Definition: q_metric.h:689
float percent_over_qscore_cumulative(const uint_t qscore, const qscore_bin_vector_type &bins) const
Definition: q_metric.h:567
::uint16_t bin_type
Definition: q_metric.h:34
void set(const uint_t lane, const uint_t tile, const uint_t cycle, const uint32_vector &qscore_hist)
Definition: q_metric.h:336
uint_t median(const qscore_bin_vector_type &bins=qscore_bin_vector_type()) const
Definition: q_metric.h:587
void accumulate(const q_metric &metric)
Definition: q_metric.h:621
size_t bin_count() const
Definition: q_metric.h:171