1 | // $Id: BamReadIterator.cc 2895 2012-12-10 21:38:12Z peter $ |
---|
2 | // |
---|
3 | // Copyright (C) 2012 Peter Johansson |
---|
4 | // |
---|
5 | // This program is free software; you can redistribute it and/or modify |
---|
6 | // it under the terms of the GNU General Public License as published by |
---|
7 | // the Free Software Foundation; either version 3 of the License, or |
---|
8 | // (at your option) any later version. |
---|
9 | // |
---|
10 | // This program is distributed in the hope that it will be useful, but |
---|
11 | // WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
13 | // General Public License for more details. |
---|
14 | // |
---|
15 | // You should have received a copy of the GNU General Public License |
---|
16 | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
17 | |
---|
18 | #include "BamReadIterator.h" |
---|
19 | |
---|
20 | #include "BamFile.h" |
---|
21 | |
---|
22 | #include <cstddef> |
---|
23 | |
---|
24 | namespace theplu { |
---|
25 | namespace yat { |
---|
26 | namespace omic { |
---|
27 | |
---|
28 | BamReadIterator::BamReadIterator(void) |
---|
29 | { |
---|
30 | actor_ = boost::shared_ptr<Actor>(new AllActor); |
---|
31 | } |
---|
32 | |
---|
33 | |
---|
34 | BamReadIterator::BamReadIterator(InBamFile& in) |
---|
35 | { |
---|
36 | actor_ = boost::shared_ptr<Actor>(new AllActor(in)); |
---|
37 | increment(); |
---|
38 | } |
---|
39 | |
---|
40 | |
---|
41 | BamReadIterator::BamReadIterator(InBamFile& bf, int32_t tid, int32_t start, |
---|
42 | int32_t end) |
---|
43 | { |
---|
44 | actor_ = boost::shared_ptr<Actor>(new IndexActor(bf, tid, start, end)); |
---|
45 | assert(actor_); |
---|
46 | increment(); |
---|
47 | } |
---|
48 | |
---|
49 | |
---|
50 | BamReadIterator::reference BamReadIterator::dereference(void) const |
---|
51 | { |
---|
52 | return actor_->read_; |
---|
53 | } |
---|
54 | |
---|
55 | |
---|
56 | bool BamReadIterator::equal(const BamReadIterator& other) const |
---|
57 | { |
---|
58 | return actor_->in_ == other.actor_->in_; |
---|
59 | } |
---|
60 | |
---|
61 | |
---|
62 | void BamReadIterator::increment(void) |
---|
63 | { |
---|
64 | actor_->increment(); |
---|
65 | } |
---|
66 | |
---|
67 | |
---|
68 | ///// Actors ////// |
---|
69 | |
---|
70 | BamReadIterator::Actor::Actor(InBamFile* bf) |
---|
71 | : in_(bf) {} |
---|
72 | |
---|
73 | |
---|
74 | BamReadIterator::Actor::~Actor(void) |
---|
75 | {} |
---|
76 | |
---|
77 | |
---|
78 | BamReadIterator::AllActor::AllActor(void) |
---|
79 | : Actor(NULL) {} |
---|
80 | |
---|
81 | |
---|
82 | BamReadIterator::AllActor::AllActor(InBamFile& bf) |
---|
83 | : Actor(&bf) {} |
---|
84 | |
---|
85 | |
---|
86 | void BamReadIterator::AllActor::increment(void) |
---|
87 | { |
---|
88 | if (!in_) |
---|
89 | return; |
---|
90 | try { |
---|
91 | if (!in_->read(read_)) |
---|
92 | in_ = NULL; |
---|
93 | } |
---|
94 | catch (std::runtime_error& e) { |
---|
95 | in_ = NULL; |
---|
96 | throw e; |
---|
97 | } |
---|
98 | } |
---|
99 | |
---|
100 | |
---|
101 | BamReadIterator::IndexActor::IndexActor(InBamFile& bf, int32_t tid, |
---|
102 | int32_t begin,int32_t end) |
---|
103 | : Actor(&bf) |
---|
104 | { |
---|
105 | bam_iter_t it = bam_iter_query(bf.index(), tid, begin, end); |
---|
106 | assert(it); |
---|
107 | // iter_ takes ownership |
---|
108 | iter_ = boost::shared_ptr<__bam_iter_t>(it, IndexDestroyer()); |
---|
109 | assert(iter_.get()); |
---|
110 | } |
---|
111 | |
---|
112 | |
---|
113 | void BamReadIterator::IndexActor::increment(void) |
---|
114 | { |
---|
115 | if (!in_) |
---|
116 | return; |
---|
117 | assert(iter_.get()); |
---|
118 | try { |
---|
119 | if (! in_->read(read_, iter_.get())) |
---|
120 | in_ = NULL; |
---|
121 | } |
---|
122 | catch (std::runtime_error& e) { |
---|
123 | in_ = NULL; |
---|
124 | throw e; |
---|
125 | } |
---|
126 | } |
---|
127 | |
---|
128 | }}} |
---|