source: trunk/yat/omic/BamReadIterator.h @ 2884

Last change on this file since 2884 was 2884, checked in by Peter, 10 years ago

add missing since tags. refs #729

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.6 KB
Line 
1#ifndef theplu_yat_omic_bam_read_iterator
2#define theplu_yat_omic_bam_read_iterator
3
4// $Id: BamReadIterator.h 2884 2012-12-04 02:17:22Z peter $
5//
6// Copyright (C) 2012 Peter Johansson
7//
8// This program 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// This program is distributed in the hope that it will be useful, but
14// WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16// General Public License for more details.
17//
18// You should have received a copy of the GNU General Public License
19// along with this program. If not, see <http://www.gnu.org/licenses/>.
20
21#include "BamFile.h"
22#include "BamRead.h"
23
24#include <sam.h>
25
26#include <boost/iterator/iterator_facade.hpp>
27#include <boost/shared_ptr.hpp>
28
29#include <iterator>
30
31namespace theplu {
32namespace yat {
33namespace omic {
34
35  // forward declaration
36  class InBamFile;
37
38  /**
39     \brief class to iterate through a InBamFile
40
41     Iterator is a single pass \input_iterator which means iterator
42     can only be used read bam file. Also two BamReadIterator working
43     on the same InBamFile are not independent, i.e., incrementing one
44     iterator will also affect the other one.
45
46     Iterator can either be constructed to iterate over entire bam
47     file or if a region is specified only over that region. When no
48     more elements are available, iterator turns into a end-of-file
49     iterator and is no longer dereferencable.
50
51     \see InBamFile
52
53     \since New in yat 0.10
54   */
55  class BamReadIterator
56    : public boost::iterator_facade<
57    BamReadIterator, const BamRead, std::input_iterator_tag
58    >
59  {
60  public:
61    /**
62       \brief Contructs end of file iterator.
63     */
64    BamReadIterator(void);
65
66    /**
67       Creates an iterator pointing to first BamRead in
68       InBamFile.
69
70       \param in file to iterate over
71     */
72    explicit BamReadIterator(InBamFile& in);
73
74    /**
75       Iterator that walks over specified region. Iterator iterates
76       over all BamReads in file \a in that overlap with specified
77       region and then it turns into an end-of-region iterator.
78
79       This function requires an index file is available, see
80       InBamFile::index().
81
82       \param in file to iterate over
83       \param tid segment (chromosome) to iterate over
84       \param start left most position of specified region
85       \param end right most position of specified region
86     */
87    BamReadIterator(InBamFile& in, int32_t tid, int32_t start, int32_t end);
88  private:
89    friend class boost::iterator_core_access;
90    BamReadIterator::reference dereference(void) const;
91    bool equal(const BamReadIterator& other) const;
92    void increment(void);
93
94    /**
95       Interface class for the actor that handles difference between
96       an iterator that iterates the entire file and one that uses
97       index to iterate over a specific region.
98     */
99    class Actor
100    {
101    public:
102      explicit Actor(InBamFile*);
103      virtual ~Actor(void);
104      virtual void increment(void)=0;
105      InBamFile* in_;
106      BamRead read_;
107    };
108
109    /// class used when region is entire file
110    class AllActor : public Actor
111    {
112    public:
113      AllActor(void);
114      explicit AllActor(InBamFile&);
115      void increment(void);
116    };
117
118    /// class used when iterating over a sub-region of the bam file
119    class IndexActor : public Actor
120    {
121    public:
122      IndexActor(InBamFile& bf, int32_t tid, int32_t begin, int32_t end);
123      void increment(void);
124    private:
125      boost::shared_ptr<__bam_iter_t> iter_;
126    };
127
128    boost::shared_ptr<Actor> actor_;
129
130    struct IndexDestroyer
131    {
132      void operator()(bam_iter_t i) const
133      {
134        bam_iter_destroy(i);
135      }
136    };
137  };
138}}}
139#endif
Note: See TracBrowser for help on using the repository browser.