channel.h
Go to the documentation of this file.
1 
8 #pragma once
9 #ifdef _MSC_VER
10 #pragma warning(push)
11 #pragma warning(disable:4290)// MSVC warns that it ignores the exception specification.
12 #endif
13 
14 #include <string>
15 #include <cctype>
16 #include <algorithm>
17 #include <vector>
18 #include <iterator>
19 #include "interop/util/assert.h"
20 #include "interop/util/string.h"
21 #include "interop/util/exception.h"
24 
25 namespace illumina { namespace interop { namespace logic { namespace utils
26 {
32  inline std::string normalize(const std::string &channel)
33  {
34  return util::to_lower(channel);
35  }
42  template<class I, class O>
43  void normalize(I beg, I end, O out)
44  {
45  for(;beg != end;++beg, ++out) *out = normalize(*beg);
46  }
54  template<class I>
55  std::string join(I beg, I end, const std::string& token)
56  {
57  std::string joined;
58  if(beg != end)
59  {
60  joined = *beg;
61  ++beg;
62  }
63  for(;beg != end;++beg) joined += token + *beg;
64  return joined;
65  }
72  inline std::string join(const std::vector<std::string>& values, const std::string& token)
73  {
74  return join(values.begin(), values.end(), token);
75  }
82  template<class I>
83  std::vector<std::string> expected_order(I beg, I end) INTEROP_THROW_SPEC(( model::invalid_channel_exception ))
84  {
85  std::vector<std::string> expected;
86  expected.reserve(std::distance(beg, end));
87  normalize(beg, end, std::back_inserter(expected));
88  std::stable_sort(expected.begin(), expected.end());
89  expected.erase(std::unique(expected.begin(), expected.end()), expected.end());
90  if(expected.size() != static_cast<size_t>(std::distance(beg, end)))
91  {
92  INTEROP_THROW( model::invalid_channel_exception, "Duplicate channel names in the RunInfo");
93  }
94  std::string norm = join(expected.begin(), expected.end(), ",");
95  if(norm == "a,c,g,t") return expected;
96  if(norm == "green,red")
97  {
98  std::swap(expected[0], expected[1]);
99  return expected;
100  }
101  if(norm == "1,2")
102  {
103  return expected;
104  }
105  if(norm == "blue,green")
106  {
107  return expected;
108  }
109  INTEROP_THROW( model::invalid_channel_exception, "Invalid channel names: " << norm);
110  }
111 
117  inline std::vector<std::string> expected_order(const std::vector<std::string>& channels)
118  {
119  return expected_order(channels.begin(), channels.end());
120  }
126  inline void expected2actual(const std::vector<std::string>& channels, std::vector<size_t>& map)
127  {
128  std::vector<std::string> normed;
129  normed.reserve(channels.size());
130  normalize(channels.begin(), channels.end(), std::back_inserter(normed));
131  std::vector<std::string> expected = expected_order(normed);
132  map.resize(normed.size());
133  INTEROP_ASSERTMSG(expected.size() == normed.size(), expected.size() << " == " << normed.size());
134  for(size_t i=0;i<normed.size();++i)
135  {
136  INTEROP_ASSERT(i < map.size());
137  map[i] = std::distance(normed.begin(), std::find(normed.begin(), normed.end(), expected[i]));
138  }
139  }
140 
146  inline void actual2expected(const std::vector<std::string>& channels, std::vector<size_t>& map)
147  {
148  std::vector<std::string> normed;
149  normed.reserve(channels.size());
150  normalize(channels.begin(), channels.end(), std::back_inserter(normed));
151  std::vector<std::string> expected = expected_order(normed);
152  map.resize(normed.size());
153  INTEROP_ASSERTMSG(expected.size() == normed.size(), expected.size() << " == " << normed.size());
154  for(size_t i=0;i<normed.size();++i)
155  {
156  map[i] = std::distance(expected.begin(), std::find(expected.begin(), expected.end(), normed[i]));
157  }
158  }
159 
165  inline std::vector<size_t> expected2actual_map(const std::vector<std::string>& channels)
166  {
167  std::vector<size_t> map;
168  expected2actual(channels, map);
169  return map;
170  }
176  inline std::vector<size_t> actual2expected_map(const std::vector<std::string>& channels)
177  {
178  std::vector<size_t> map;
179  actual2expected(channels, map);
180  return map;
181  }
188  std::vector<std::string>& channels)
189  {
190  switch(instrument)
191  {
192  case constants::MiniSeq:
193  case constants::NextSeq:
194  case constants::NovaSeq:
195  channels.clear();
196  channels.reserve(2);
197  channels.push_back("Red");
198  channels.push_back("Green");
199  break;
200  case constants::iSeq:
201  channels.clear();
202  channels.reserve(2);
203  channels.push_back("1");
204  channels.push_back("2");
205  break;
206  case constants::HiSeq:
207  case constants::MiSeq:
208  case constants::HiScan:
209  channels.clear();
210  channels.reserve(4);
211  channels.push_back("A");
212  channels.push_back("C");
213  channels.push_back("G");
214  channels.push_back("T");
215  break;
216  default:
217  break;
218  };
219  }
225  inline std::vector<std::string> update_channel_from_instrument_type(const constants::instrument_type instrument)
226  {
227  std::vector<std::string> channels;
228  update_channel_from_instrument_type(instrument, channels);
229  return channels;
230  }
231 
232 
233 }}}}
234 #ifdef _MSC_VER
235 #pragma warning(pop)
236 #endif
237 
#define INTEROP_ASSERTMSG(TST, MSG)
Definition: assert.h:32
void update_channel_from_instrument_type(const constants::instrument_type instrument, std::vector< std::string > &channels)
Definition: channel.h:187
Definition: enum_description.h:15
std::vector< size_t > expected2actual_map(const std::vector< std::string > &channels)
Definition: channel.h:165
#define INTEROP_THROW_SPEC(SPEC)
Definition: exception_specification.h:15
#define INTEROP_THROW(EXCEPTION, MESSAGE)
Definition: exception.h:18
Definition: enums.h:311
#define INTEROP_ASSERT(TST)
Definition: assert.h:21
void actual2expected(const std::vector< std::string > &channels, std::vector< size_t > &map)
Definition: channel.h:146
std::string join(I beg, I end, const std::string &token)
Definition: channel.h:55
std::vector< size_t > actual2expected_map(const std::vector< std::string > &channels)
Definition: channel.h:176
void expected2actual(const std::vector< std::string > &channels, std::vector< size_t > &map)
Definition: channel.h:126
std::vector< std::string > expected_order(I beg, I end) INTEROP_THROW_SPEC((model
Definition: channel.h:83
instrument_type
Definition: enums.h:309
std::string to_lower(const std::string &input)
Definition: string.h:98
std::string normalize(const std::string &channel)
Definition: channel.h:32