yat/utility/CigarIterator.h

Code
Comments
Other
Rev Date Author Line
3336 24 Oct 14 peter 1 #ifndef theplu_yat_utility_cigar_iterator
3336 24 Oct 14 peter 2 #define theplu_yat_utility_cigar_iterator
3329 12 Oct 14 peter 3
3329 12 Oct 14 peter 4 // $Id$
3329 12 Oct 14 peter 5
3329 12 Oct 14 peter 6 /*
3792 12 Apr 19 peter 7   Copyright (C) 2014, 2015, 2016, 2018 Peter Johansson
3329 12 Oct 14 peter 8
3329 12 Oct 14 peter 9   This file is part of the yat library, http://dev.thep.lu.se/yat
3329 12 Oct 14 peter 10
3329 12 Oct 14 peter 11   The yat library is free software; you can redistribute it and/or
3329 12 Oct 14 peter 12   modify it under the terms of the GNU General Public License as
3329 12 Oct 14 peter 13   published by the Free Software Foundation; either version 3 of the
3329 12 Oct 14 peter 14   License, or (at your option) any later version.
3329 12 Oct 14 peter 15
3329 12 Oct 14 peter 16   The yat library is distributed in the hope that it will be useful,
3329 12 Oct 14 peter 17   but WITHOUT ANY WARRANTY; without even the implied warranty of
3329 12 Oct 14 peter 18   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
3329 12 Oct 14 peter 19   General Public License for more details.
3329 12 Oct 14 peter 20
3329 12 Oct 14 peter 21   You should have received a copy of the GNU General Public License
3751 17 Oct 18 peter 22   along with yat. If not, see <http://www.gnu.org/licenses/>.
3329 12 Oct 14 peter 23 */
3329 12 Oct 14 peter 24
3368 10 Feb 15 peter 25 #include "Cigar.h"
3368 10 Feb 15 peter 26
3385 13 Mar 15 peter 27 #include <boost/concept_check.hpp>
3329 12 Oct 14 peter 28 #include <boost/cstdint.hpp>
3329 12 Oct 14 peter 29 #include <boost/iterator/iterator_facade.hpp>
3329 12 Oct 14 peter 30 #include <boost/iterator/iterator_categories.hpp>
3385 13 Mar 15 peter 31 #include <boost/iterator/iterator_concepts.hpp>
3329 12 Oct 14 peter 32
3368 10 Feb 15 peter 33 #include <cstddef> // for size_t
3368 10 Feb 15 peter 34
3329 12 Oct 14 peter 35 namespace theplu {
3329 12 Oct 14 peter 36 namespace yat {
3336 24 Oct 14 peter 37 namespace utility {
3329 12 Oct 14 peter 38
3329 12 Oct 14 peter 39   /**
3329 12 Oct 14 peter 40      \brief Iterator over a CIGAR
3329 12 Oct 14 peter 41
3329 12 Oct 14 peter 42      A CIGAR string is typically represented as a compact array, i.e.,
3329 12 Oct 14 peter 43      an array \c MMMMMMDDMMMM is represented as \c 6M2D4M. This class
3329 12 Oct 14 peter 44      is a proxy that allows iterating over a CIGAR as though the array
3329 12 Oct 14 peter 45      was \c MMMMMMDDMMMM.
3329 12 Oct 14 peter 46
3370 10 Feb 15 peter 47      CigarIterator is a \readable_iterator (not mutable) and
3329 12 Oct 14 peter 48      models a \bidirectional_traversal_iterator. Its value_type is \c
3329 12 Oct 14 peter 49      uint8_t. Since no \c uint8_t is stored in CIGAR, the dereference
3329 12 Oct 14 peter 50      operator calculates the CIGAR element to return and returns by
3329 12 Oct 14 peter 51      value. CigarIterator is therefore an \input_iterator.
3329 12 Oct 14 peter 52
3510 21 Jul 16 peter 53      Type Requirement:
3510 21 Jul 16 peter 54      - \c BASE is a \readable_iterator
3510 21 Jul 16 peter 55      - \c BASE is a \bidirectional_traversal_iterator
3510 21 Jul 16 peter 56
3329 12 Oct 14 peter 57      \since New in yat 0.13
3329 12 Oct 14 peter 58    */
3336 24 Oct 14 peter 59   template<typename BASE>
3329 12 Oct 14 peter 60   class CigarIterator
3329 12 Oct 14 peter 61     : public boost::iterator_facade<
3336 24 Oct 14 peter 62     CigarIterator<BASE>, uint8_t
3336 24 Oct 14 peter 63     , boost::bidirectional_traversal_tag, const uint8_t>
3329 12 Oct 14 peter 64   {
3329 12 Oct 14 peter 65   public:
3329 12 Oct 14 peter 66     /**
3329 12 Oct 14 peter 67        \brief Default constructor
3329 12 Oct 14 peter 68      */
3329 12 Oct 14 peter 69     CigarIterator(void);
3329 12 Oct 14 peter 70
3329 12 Oct 14 peter 71     /**
3329 12 Oct 14 peter 72        Construct an iterator that points to element offset in \a p. If
3329 12 Oct 14 peter 73        \a p corresponds to \c 5M and offset is 2, the iterator points to
3329 12 Oct 14 peter 74        the third \c M.
3329 12 Oct 14 peter 75      */
3336 24 Oct 14 peter 76     explicit CigarIterator(BASE p, size_t offset=0);
3332 23 Oct 14 peter 77
3332 23 Oct 14 peter 78     /**
3332 23 Oct 14 peter 79        \return underlying iterator
3332 23 Oct 14 peter 80      */
3336 24 Oct 14 peter 81     BASE base(void) const;
3329 12 Oct 14 peter 82   private:
3329 12 Oct 14 peter 83     friend class boost::iterator_core_access;
3329 12 Oct 14 peter 84
3336 24 Oct 14 peter 85     BASE base_;
3329 12 Oct 14 peter 86     size_t index_;
3329 12 Oct 14 peter 87
3329 12 Oct 14 peter 88     void decrement(void);
3336 24 Oct 14 peter 89     uint8_t dereference(void) const;
3329 12 Oct 14 peter 90     bool equal(const CigarIterator& other) const;
3329 12 Oct 14 peter 91     void increment(void);
3329 12 Oct 14 peter 92
3329 12 Oct 14 peter 93     // using compiler generated copy
3329 12 Oct 14 peter 94     //CigarIterator(const CigarIterator& other);
3329 12 Oct 14 peter 95     //CigarIterator& operator=(const CigarIterator&);
3329 12 Oct 14 peter 96   };
3329 12 Oct 14 peter 97
3336 24 Oct 14 peter 98
3336 24 Oct 14 peter 99   //// implementation  /////////////
3336 24 Oct 14 peter 100
3336 24 Oct 14 peter 101   template<typename BASE>
3385 13 Mar 15 peter 102   CigarIterator<BASE>::CigarIterator(void)
3385 13 Mar 15 peter 103   {
3385 13 Mar 15 peter 104     BOOST_CONCEPT_ASSERT((boost_concepts::ReadableIterator<BASE>));
3385 13 Mar 15 peter 105     BOOST_CONCEPT_ASSERT((boost_concepts::BidirectionalTraversal<BASE>));
3385 13 Mar 15 peter 106   }
3336 24 Oct 14 peter 107
3336 24 Oct 14 peter 108
3336 24 Oct 14 peter 109   template<typename BASE>
3336 24 Oct 14 peter 110   CigarIterator<BASE>::CigarIterator(BASE b, size_t x)
3385 13 Mar 15 peter 111     : base_(b), index_(x)
3385 13 Mar 15 peter 112   {
3385 13 Mar 15 peter 113     BOOST_CONCEPT_ASSERT((boost_concepts::ReadableIterator<BASE>));
3385 13 Mar 15 peter 114     BOOST_CONCEPT_ASSERT((boost_concepts::BidirectionalTraversal<BASE>));
3385 13 Mar 15 peter 115   }
3336 24 Oct 14 peter 116
3336 24 Oct 14 peter 117
3336 24 Oct 14 peter 118   template<typename BASE>
3336 24 Oct 14 peter 119   BASE CigarIterator<BASE>::base(void) const
3336 24 Oct 14 peter 120   {
3336 24 Oct 14 peter 121     return base_;
3336 24 Oct 14 peter 122   }
3336 24 Oct 14 peter 123
3336 24 Oct 14 peter 124
3336 24 Oct 14 peter 125   template<typename BASE>
3336 24 Oct 14 peter 126   void CigarIterator<BASE>::decrement(void)
3336 24 Oct 14 peter 127   {
3336 24 Oct 14 peter 128     if (index_)
3336 24 Oct 14 peter 129       --index_;
3336 24 Oct 14 peter 130     else {
3336 24 Oct 14 peter 131       --base_;
3336 24 Oct 14 peter 132       index_ = bam_cigar_oplen(*base_)-1;
3336 24 Oct 14 peter 133     }
3336 24 Oct 14 peter 134   }
3336 24 Oct 14 peter 135
3336 24 Oct 14 peter 136
3336 24 Oct 14 peter 137   template<typename BASE>
3336 24 Oct 14 peter 138   uint8_t CigarIterator<BASE>::dereference(void) const
3336 24 Oct 14 peter 139   {
3336 24 Oct 14 peter 140     return bam_cigar_op(*base_);
3336 24 Oct 14 peter 141   }
3336 24 Oct 14 peter 142
3336 24 Oct 14 peter 143
3336 24 Oct 14 peter 144   template<typename BASE>
3336 24 Oct 14 peter 145   bool CigarIterator<BASE>::equal(const CigarIterator& other) const
3336 24 Oct 14 peter 146   {
3336 24 Oct 14 peter 147     return base_==other.base_ && index_==other.index_;
3336 24 Oct 14 peter 148   }
3336 24 Oct 14 peter 149
3336 24 Oct 14 peter 150
3336 24 Oct 14 peter 151   template<typename BASE>
3336 24 Oct 14 peter 152   void CigarIterator<BASE>::increment(void)
3336 24 Oct 14 peter 153   {
3336 24 Oct 14 peter 154     if (++index_ == bam_cigar_oplen(*base_)) {
3336 24 Oct 14 peter 155       index_=0;
3336 24 Oct 14 peter 156       ++base_;
3336 24 Oct 14 peter 157     }
3336 24 Oct 14 peter 158   }
3336 24 Oct 14 peter 159
3329 12 Oct 14 peter 160 }}}
3329 12 Oct 14 peter 161 #endif