SHORE API
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
dna_iterator.hpp
Go to the documentation of this file.
1 
2 /*
3  * Copyright 2008,2009,2010,2011,2012 Stephan Ossowski, Korbinian Schneeberger,
4  * Felix Ott, Joerg Hagmann, Alf Scotland, Sebastian Bender
5  *
6  * This file is part of SHORE.
7  *
8  * SHORE is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * SHORE is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with SHORE. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
25 
26 #ifndef SHORE_GENOME_DNA_ITERATOR_HPP__
27 #define SHORE_GENOME_DNA_ITERATOR_HPP__
28 
29 #include <boost/iterator/iterator_facade.hpp>
30 
33 
34 namespace shore {
35 
38 template<typename Iter,
39  typename Converted,
40  typename ConversionFunc=Converted (*)(const typename std::iterator_traits<Iter>::value_type)>
42 :public boost::iterator_facade<conversion_proxy<Iter,Converted,ConversionFunc>,
43  const Converted,
44  boost::random_access_traversal_tag,
45  const Converted>
46 {
47  public:
48 
49  typedef Iter iterator_type;
50  typedef Converted converted_type;
51  typedef ConversionFunc conversion_func_type;
52 
53  private:
54 
55  friend class boost::iterator_core_access;
56 
58 
59  iterator_type m_iter;
60  conversion_func_type m_conv;
61 
62  void increment()
63  {
64  ++m_iter;
65  }
66 
67  void decrement()
68  {
69  --m_iter;
70  }
71 
72  void advance(ptrdiff_t n)
73  {
74  m_iter+=n;
75  }
76 
77  ptrdiff_t distance_to(const this_type &other) const
78  {
79  return other.m_iter-m_iter;
80  }
81 
82  bool equal(const this_type &other) const
83  {
84  return other.m_iter==m_iter;
85  }
86 
87  converted_type dereference() const
88  {
89  return m_conv(*m_iter);
90  }
91 
92  public:
93 
94  conversion_proxy(conversion_func_type fun=conversion_func_type())
95  :m_conv(fun)
96  {}
97 
98  conversion_proxy(iterator_type iter,conversion_func_type fun=conversion_func_type())
99  :m_iter(iter),m_conv(fun)
100  {}
101 
103  iterator_type base() const
104  {
105  return m_iter;
106  }
107 };
108 
110 
113 {
114  shore::nuc::base operator()(const shore::nuc::base b) const;
115 };
116 
119 {
120  shore::nuc::packed_base operator()(const shore::nuc::packed_base b) const;
121 };
122 
125 {
126  char operator()(const char c) const;
127 };
128 
131 {
132  shore::nuc::base operator()(const uint64_t c) const;
133 };
134 
137 {
138  shore::nuc::base operator()(const uint64_t c) const;
139 };
140 
143 {
144  shore::nuc::packed_base operator()(const uint64_t c) const;
145 };
146 
149 {
150  shore::nuc::packed_base operator()(const uint64_t c) const;
151 };
152 
155 {
156  char operator()(const uint64_t c) const;
157 };
158 
161 {
162  char operator()(const uint64_t c) const;
163 };
164 
167 {
168  shore::nuc::base operator()(const char c) const;
169 };
170 
173 {
174  shore::nuc::base operator()(const char c) const;
175 };
176 
179 {
180  shore::nuc::packed_base operator()(const char c) const;
181 };
182 
185 {
186  shore::nuc::packed_base operator()(const char c) const;
187 };
188 
191 {
192  char operator()(const shore::nuc::base b) const;
193 };
194 
197 {
198  char operator()(const shore::nuc::base b) const;
199 };
200 
203 {
204  shore::nuc::packed_base operator()(const shore::nuc::base b) const;
205 };
206 
209 {
210  shore::nuc::packed_base operator()(const shore::nuc::base b) const;
211 };
212 
215 {
216  char operator()(const shore::nuc::packed_base b) const;
217 };
218 
221 {
222  char operator()(const shore::nuc::packed_base b) const;
223 };
224 
227 {
228  shore::nuc::base operator()(const shore::nuc::packed_base b) const;
229 };
230 
233 {
234  shore::nuc::base operator()(const shore::nuc::packed_base b) const;
235 };
236 
238 
240 template<typename Iter>
243 {
244  public:
245 
247 
249 
250  base_compl_iterator(const Iter it)
251  :proxy_type(it)
252  {}
253 };
254 
256 template<typename Iter>
259 {
260  public:
261 
263 
265 
266  packed_compl_iterator(const Iter it)
267  :proxy_type(it)
268  {}
269 };
270 
272 template<typename Iter>
275 {
276  public:
277 
279 
281 
282  char_compl_iterator(const Iter it)
283  :proxy_type(it)
284  {}
285 };
286 
288 
290 template<typename Iter>
293 {
294  public:
295 
297 
298  int2base_iterator() {}
299 
300  int2base_iterator(const Iter it)
301  :proxy_type(it)
302  {}
303 };
304 
306 template<typename Iter>
309 {
310  public:
311 
313 
315 
316  int2packed_iterator(const Iter it)
317  :proxy_type(it)
318  {}
319 };
320 
322 template<typename Iter>
325 {
326  public:
327 
329 
330  int2char_iterator() {}
331 
332  int2char_iterator(const Iter it)
333  :proxy_type(it)
334  {}
335 };
336 
338 template<typename Iter>
341 {
342  public:
343 
345 
347 
348  int2base_compl_iterator(const Iter it)
349  :proxy_type(it)
350  {}
351 };
352 
354 template<typename Iter>
357 {
358  public:
359 
361 
363 
364  int2packed_compl_iterator(const Iter it)
365  :proxy_type(it)
366  {}
367 };
368 
370 template<typename Iter>
373 {
374  public:
375 
377 
379 
380  int2char_compl_iterator(const Iter it)
381  :proxy_type(it)
382  {}
383 };
384 
386 
388 template<typename Iter>
391 {
392  public:
393 
395 
397 
398  base2packed_iterator(const Iter it)
399  :proxy_type(it)
400  {}
401 };
402 
404 template<typename Iter>
407 {
408  public:
409 
411 
412  base2char_iterator() {}
413 
414  base2char_iterator(const Iter it)
415  :proxy_type(it)
416  {}
417 };
418 
420 template<typename Iter>
423 {
424  public:
425 
427 
429 
430  base2packed_compl_iterator(const Iter it)
431  :proxy_type(it)
432  {}
433 };
434 
436 template<typename Iter>
439 {
440  public:
441 
443 
445 
446  base2char_compl_iterator(const Iter it)
447  :proxy_type(it)
448  {}
449 };
450 
452 
454 template<typename Iter>
457 {
458  public:
459 
461 
463 
464  packed2base_iterator(const Iter it)
465  :proxy_type(it)
466  {}
467 };
468 
470 template<typename Iter>
473 {
474  public:
475 
477 
479 
480  packed2char_iterator(const Iter it)
481  :proxy_type(it)
482  {}
483 };
484 
486 template<typename Iter>
489 {
490  public:
491 
493 
495 
496  packed2base_compl_iterator(const Iter it)
497  :proxy_type(it)
498  {}
499 };
500 
502 template<typename Iter>
505 {
506  public:
507 
509 
511 
512  packed2char_compl_iterator(const Iter it)
513  :proxy_type(it)
514  {}
515 };
516 
518 
520 template<typename Iter>
523 {
524  public:
525 
527 
528  char2base_iterator() {}
529 
530  char2base_iterator(const Iter it)
531  :proxy_type(it)
532  {}
533 };
534 
536 template<typename Iter>
539 {
540  public:
541 
543 
545 
546  char2packed_iterator(const Iter it)
547  :proxy_type(it)
548  {}
549 };
550 
552 template<typename Iter>
555 {
556  public:
557 
559 
561 
562  char2base_compl_iterator(const Iter it)
563  :proxy_type(it)
564  {}
565 };
566 
568 template<typename Iter>
571 {
572  public:
573 
575 
577 
578  char2packed_compl_iterator(const Iter it)
579  :proxy_type(it)
580  {}
581 };
582 
584 
589 :public boost::iterator_facade<
590 dna_iterator,const shore::nuc::base,boost::random_access_traversal_tag,const shore::nuc::base>
591 {
592  private:
593 
594  friend class boost::iterator_core_access;
595 
596  const char* m_pt;
599  bool m_complement;
600 
601 
602  void increment()
603  {
604  if(m_pt!=0)
605  ++m_pt;
606  else
607  ++m_it;
608  }
609 
610  void decrement()
611  {
612  if(m_pt!=0)
613  --m_pt;
614  else
615  --m_it;
616  }
617 
618  void advance(difference_type n)
619  {
620  if(m_pt!=0)
621  m_pt+=n;
622  else
623  m_it+=n;
624  }
625 
626  difference_type distance_to(const dna_iterator& other) const
627  {
628  if(m_pt!=0)
629  return other.m_pt-m_pt;
630  return other.m_it-m_it;
631  }
632 
633  bool equal(const dna_iterator& other) const
634  {
635  if(m_pt!=0)
636  return m_pt==other.m_pt;
637  return m_it==other.m_it;
638  }
639 
640  reference dereference() const
641  {
642  if(m_complement)
643  {
644  if(m_pt!=0)
645  return shore::nuc::comp(shore::nuc::dec(*m_pt));
646  return shore::nuc::comp(shore::nuc::unpack(static_cast<shore::nuc::packed_base>(
647  static_cast<shore::intpack::int_type>(*m_it))));
648  }
649  if(m_pt!=0)
650  return shore::nuc::dec(*m_pt);
651  return shore::nuc::unpack(static_cast<shore::nuc::packed_base>(
652  static_cast<shore::intpack::int_type>(*m_it)));
653  }
654 
655  public:
656 
657  dna_iterator()
658  :m_pt(0),m_complement(false)
659  {}
660 
661  dna_iterator(const char*const pt)
662  :m_pt(pt),m_complement(false)
663  {}
664 
666  :m_pt(0),m_it(it),m_complement(false)
667  {}
668 
671  void set_complement(const bool comp)
672  {
673  m_complement=comp;
674  }
675 
676  static dna_iterator begin(const std::string& s)
677  {
678  return dna_iterator(s.data());
679  }
680 
681  static dna_iterator end(const std::string& s)
682  {
683  return dna_iterator(s.data()+s.size());
684  }
685 };
686 
691 :public boost::iterator_facade<
692 packeddna_iterator,const shore::nuc::packed_base,boost::random_access_traversal_tag,const shore::nuc::packed_base>
693 {
694  private:
695 
696  friend class boost::iterator_core_access;
697 
698  const char* m_pt;
701  bool m_complement;
702 
703 
704  void increment()
705  {
706  if(m_pt!=0)
707  ++m_pt;
708  else
709  ++m_it;
710  }
711 
712  void decrement()
713  {
714  if(m_pt!=0)
715  --m_pt;
716  else
717  --m_it;
718  }
719 
720  void advance(difference_type n)
721  {
722  if(m_pt!=0)
723  m_pt+=n;
724  else
725  m_it+=n;
726  }
727 
728  difference_type distance_to(const packeddna_iterator& other) const
729  {
730  if(m_pt!=0)
731  return other.m_pt-m_pt;
732  return other.m_it-m_it;
733  }
734 
735  bool equal(const packeddna_iterator& other) const
736  {
737  if(m_pt!=0)
738  return m_pt==other.m_pt;
739  return m_it==other.m_it;
740  }
741 
742  reference dereference() const
743  {
744  if(m_complement)
745  {
746  if(m_pt!=0)
747  return shore::nuc::pack(shore::nuc::comp(shore::nuc::dec(*m_pt)));
748  return shore::nuc::pack(shore::nuc::comp(shore::nuc::unpack(
749  static_cast<shore::nuc::packed_base>(
750  static_cast<shore::intpack::int_type>(*m_it)))));
751  }
752  if(m_pt!=0)
753  return shore::nuc::pack(shore::nuc::dec(*m_pt));
754  return static_cast<shore::nuc::packed_base>(
755  static_cast<shore::intpack::int_type>(*m_it));
756  }
757 
758  public:
759 
761  :m_pt(0),m_complement(false)
762  {}
763 
764  packeddna_iterator(const char*const pt)
765  :m_pt(pt),m_complement(false)
766  {}
767 
769  :m_pt(0),m_it(it),m_complement(false)
770  {}
771 
774  void set_complement(const bool comp)
775  {
776  m_complement=comp;
777  }
778 
779  static packeddna_iterator begin(const std::string& s)
780  {
781  return packeddna_iterator(s.data());
782  }
783 
784  static packeddna_iterator end(const std::string& s)
785  {
786  return packeddna_iterator(s.data()+s.size());
787  }
788 };
789 
794 :public boost::iterator_facade<
795 chardna_iterator,const char,boost::random_access_traversal_tag,const char>
796 {
797  private:
798 
799  friend class boost::iterator_core_access;
800 
801  const char* m_pt;
804  bool m_complement;
805 
806 
807  void increment()
808  {
809  if(m_pt!=0)
810  ++m_pt;
811  else
812  ++m_it;
813  }
814 
815  void decrement()
816  {
817  if(m_pt!=0)
818  --m_pt;
819  else
820  --m_it;
821  }
822 
823  void advance(difference_type n)
824  {
825  if(m_pt!=0)
826  m_pt+=n;
827  else
828  m_it+=n;
829  }
830 
831  difference_type distance_to(const chardna_iterator& other) const
832  {
833  if(m_pt!=0)
834  return other.m_pt-m_pt;
835  return other.m_it-m_it;
836  }
837 
838  bool equal(const chardna_iterator& other) const
839  {
840  if(m_pt!=0)
841  return m_pt==other.m_pt;
842  return m_it==other.m_it;
843  }
844 
845  reference dereference() const
846  {
847  if(m_complement)
848  {
849  if(m_pt!=0)
851  return shore::nuc::enc(shore::nuc::comp(shore::nuc::unpack(
852  static_cast<shore::nuc::packed_base>(
853  static_cast<shore::intpack::int_type>(*m_it)))));
854  }
855  if(m_pt!=0)
856  return *m_pt;
857  return shore::nuc::enc(shore::nuc::unpack(
858  static_cast<shore::nuc::packed_base>(
859  static_cast<shore::intpack::int_type>(*m_it))));
860  }
861 
862  public:
863 
865  :m_pt(0),m_complement(false)
866  {}
867 
868  chardna_iterator(const char*const pt)
869  :m_pt(pt),m_complement(false)
870  {}
871 
873  :m_pt(0),m_it(it),m_complement(false)
874  {}
875 
878  void set_complement(const bool comp)
879  {
880  m_complement=comp;
881  }
882 };
883 
884 } // namespace
885 
886 #endif // SHORE_GENOME_DNA_ITERATOR_HPP__
887