1 | #ifndef theplu_yat_omic_bam_read_iterator |
---|
2 | #define theplu_yat_omic_bam_read_iterator |
---|
3 | |
---|
4 | // $Id: BamReadIterator.h 2986 2013-02-18 08:07:44Z 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 | #include "config_bam.h" |
---|
24 | |
---|
25 | #include YAT_SAM_HEADER |
---|
26 | |
---|
27 | #include <boost/iterator/iterator_facade.hpp> |
---|
28 | #include <boost/shared_ptr.hpp> |
---|
29 | |
---|
30 | #include <iterator> |
---|
31 | |
---|
32 | namespace theplu { |
---|
33 | namespace yat { |
---|
34 | namespace omic { |
---|
35 | |
---|
36 | // forward declaration |
---|
37 | class InBamFile; |
---|
38 | |
---|
39 | /** |
---|
40 | \brief class to iterate through a InBamFile |
---|
41 | |
---|
42 | Iterator is a single pass \input_iterator which means iterator |
---|
43 | can only be used read bam file. If creating two iterators working |
---|
44 | on the same InBamFile, the iterators are not independent of each |
---|
45 | other. Incrementing one, the behaviour of the other is undefined. |
---|
46 | |
---|
47 | Iterator can either be constructed to iterate over entire bam |
---|
48 | file or if a region is specified only over that region. When no |
---|
49 | more elements are available, iterator turns into a end-of-file |
---|
50 | iterator and is no longer dereferencable. |
---|
51 | |
---|
52 | \see InBamFile |
---|
53 | |
---|
54 | \since New in yat 0.10 |
---|
55 | */ |
---|
56 | class BamReadIterator |
---|
57 | : public boost::iterator_facade< |
---|
58 | BamReadIterator, const BamRead, std::input_iterator_tag |
---|
59 | > |
---|
60 | { |
---|
61 | public: |
---|
62 | /** |
---|
63 | \brief Contructs end of file iterator. |
---|
64 | */ |
---|
65 | BamReadIterator(void); |
---|
66 | |
---|
67 | /** |
---|
68 | Creates an iterator pointing to first BamRead in |
---|
69 | InBamFile. |
---|
70 | |
---|
71 | \param in file to iterate over |
---|
72 | */ |
---|
73 | explicit BamReadIterator(InBamFile& in); |
---|
74 | |
---|
75 | /** |
---|
76 | Iterator that walks over specified region. Iterator iterates |
---|
77 | over all BamReads in file \a in that overlap with specified |
---|
78 | region and then it turns into an end-of-region iterator. |
---|
79 | |
---|
80 | This function requires an index file is available, see |
---|
81 | InBamFile::index(). |
---|
82 | |
---|
83 | All reads iterated over overlap with defined region, in other |
---|
84 | words, iter->tid == \a tid; iter->pos() < \a end and iter->end() |
---|
85 | > start |
---|
86 | |
---|
87 | \param in file to iterate over |
---|
88 | \param tid segment (chromosome) to iterate over |
---|
89 | \param start left most position of specified region |
---|
90 | \param end right most position of specified region |
---|
91 | */ |
---|
92 | BamReadIterator(InBamFile& in, int32_t tid, int32_t start, int32_t end); |
---|
93 | private: |
---|
94 | friend class boost::iterator_core_access; |
---|
95 | BamReadIterator::reference dereference(void) const; |
---|
96 | bool equal(const BamReadIterator& other) const; |
---|
97 | void increment(void); |
---|
98 | |
---|
99 | /* |
---|
100 | Interface class for the actor that handles difference between |
---|
101 | an iterator that iterates the entire file and one that uses |
---|
102 | index to iterate over a specific region. |
---|
103 | */ |
---|
104 | class Actor |
---|
105 | { |
---|
106 | public: |
---|
107 | explicit Actor(InBamFile*); |
---|
108 | virtual ~Actor(void); |
---|
109 | virtual void increment(void)=0; |
---|
110 | InBamFile* in_; |
---|
111 | BamRead read_; |
---|
112 | }; |
---|
113 | |
---|
114 | // class used when region is entire file |
---|
115 | class AllActor : public Actor |
---|
116 | { |
---|
117 | public: |
---|
118 | AllActor(void); |
---|
119 | explicit AllActor(InBamFile&); |
---|
120 | void increment(void); |
---|
121 | }; |
---|
122 | |
---|
123 | // class used when iterating over a sub-region of the bam file |
---|
124 | class IndexActor : public Actor |
---|
125 | { |
---|
126 | public: |
---|
127 | IndexActor(InBamFile& bf, int32_t tid, int32_t begin, int32_t end); |
---|
128 | void increment(void); |
---|
129 | private: |
---|
130 | boost::shared_ptr<__bam_iter_t> iter_; |
---|
131 | }; |
---|
132 | |
---|
133 | boost::shared_ptr<Actor> actor_; |
---|
134 | |
---|
135 | struct IndexDestroyer |
---|
136 | { |
---|
137 | void operator()(bam_iter_t i) const |
---|
138 | { |
---|
139 | bam_iter_destroy(i); |
---|
140 | } |
---|
141 | }; |
---|
142 | }; |
---|
143 | }}} |
---|
144 | #endif |
---|