1 | #ifndef theplu_yat_omic_bam_read_iterator |
---|
2 | #define theplu_yat_omic_bam_read_iterator |
---|
3 | |
---|
4 | // $Id: BamReadIterator.h 2896 2012-12-10 21:52:30Z 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 | |
---|
31 | namespace theplu { |
---|
32 | namespace yat { |
---|
33 | namespace 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 | All reads iterated over overlap with defined region, in other |
---|
83 | words, iter->tid == \a tid; iter->pos() < \a end and iter->end() |
---|
84 | > start |
---|
85 | |
---|
86 | \param in file to iterate over |
---|
87 | \param tid segment (chromosome) to iterate over |
---|
88 | \param start left most position of specified region |
---|
89 | \param end right most position of specified region |
---|
90 | */ |
---|
91 | BamReadIterator(InBamFile& in, int32_t tid, int32_t start, int32_t end); |
---|
92 | private: |
---|
93 | friend class boost::iterator_core_access; |
---|
94 | BamReadIterator::reference dereference(void) const; |
---|
95 | bool equal(const BamReadIterator& other) const; |
---|
96 | void increment(void); |
---|
97 | |
---|
98 | /* |
---|
99 | Interface class for the actor that handles difference between |
---|
100 | an iterator that iterates the entire file and one that uses |
---|
101 | index to iterate over a specific region. |
---|
102 | */ |
---|
103 | class Actor |
---|
104 | { |
---|
105 | public: |
---|
106 | explicit Actor(InBamFile*); |
---|
107 | virtual ~Actor(void); |
---|
108 | virtual void increment(void)=0; |
---|
109 | InBamFile* in_; |
---|
110 | BamRead read_; |
---|
111 | }; |
---|
112 | |
---|
113 | // class used when region is entire file |
---|
114 | class AllActor : public Actor |
---|
115 | { |
---|
116 | public: |
---|
117 | AllActor(void); |
---|
118 | explicit AllActor(InBamFile&); |
---|
119 | void increment(void); |
---|
120 | }; |
---|
121 | |
---|
122 | // class used when iterating over a sub-region of the bam file |
---|
123 | class IndexActor : public Actor |
---|
124 | { |
---|
125 | public: |
---|
126 | IndexActor(InBamFile& bf, int32_t tid, int32_t begin, int32_t end); |
---|
127 | void increment(void); |
---|
128 | private: |
---|
129 | boost::shared_ptr<__bam_iter_t> iter_; |
---|
130 | }; |
---|
131 | |
---|
132 | boost::shared_ptr<Actor> actor_; |
---|
133 | |
---|
134 | struct IndexDestroyer |
---|
135 | { |
---|
136 | void operator()(bam_iter_t i) const |
---|
137 | { |
---|
138 | bam_iter_destroy(i); |
---|
139 | } |
---|
140 | }; |
---|
141 | }; |
---|
142 | }}} |
---|
143 | #endif |
---|