source: trunk/yat/omic/BamReadIterator.cc @ 2897

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

prefer throwing yat::utility::runtime_error. refs #729

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.6 KB
Line 
1// $Id: BamReadIterator.cc 2897 2012-12-11 07:27:40Z 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 "yat/utility/Exception.h"
23
24#include <cstddef>
25
26namespace theplu {
27namespace yat {
28namespace omic {
29
30  BamReadIterator::BamReadIterator(void)
31  {
32    actor_ = boost::shared_ptr<Actor>(new AllActor);
33  }
34
35
36  BamReadIterator::BamReadIterator(InBamFile& in)
37  {
38    actor_ = boost::shared_ptr<Actor>(new AllActor(in));
39    increment();
40  }
41
42
43  BamReadIterator::BamReadIterator(InBamFile& bf, int32_t tid, int32_t start,
44                                   int32_t end)
45  {
46    actor_ = boost::shared_ptr<Actor>(new IndexActor(bf, tid, start, end));
47    assert(actor_);
48    increment();
49  }
50
51
52  BamReadIterator::reference BamReadIterator::dereference(void) const
53  {
54    return actor_->read_;
55  }
56
57
58  bool BamReadIterator::equal(const BamReadIterator& other) const
59  {
60    return actor_->in_ == other.actor_->in_;
61  }
62
63
64  void BamReadIterator::increment(void)
65  {
66    actor_->increment();
67  }
68
69
70  ///// Actors //////
71
72  BamReadIterator::Actor::Actor(InBamFile* bf)
73    : in_(bf) {}
74
75
76  BamReadIterator::Actor::~Actor(void)
77  {}
78
79
80  BamReadIterator::AllActor::AllActor(void)
81    : Actor(NULL) {}
82
83
84  BamReadIterator::AllActor::AllActor(InBamFile& bf)
85    : Actor(&bf) {}
86
87
88  void BamReadIterator::AllActor::increment(void)
89  {
90    if (!in_)
91      return;
92    try {
93      if (!in_->read(read_))
94        in_ = NULL;
95    }
96    catch (utility::runtime_error& e) {
97      in_ = NULL;
98      throw e;
99    }
100  }
101
102
103  BamReadIterator::IndexActor::IndexActor(InBamFile& bf, int32_t tid,
104                                          int32_t begin,int32_t end)
105    : Actor(&bf)
106  {
107    bam_iter_t it = bam_iter_query(bf.index(), tid, begin, end);
108    assert(it);
109    // iter_ takes ownership
110    iter_ = boost::shared_ptr<__bam_iter_t>(it, IndexDestroyer());
111    assert(iter_.get());
112  }
113
114
115  void BamReadIterator::IndexActor::increment(void)
116  {
117    if (!in_)
118      return;
119    assert(iter_.get());
120    try {
121      if (! in_->read(read_, iter_.get()))
122        in_ = NULL;
123    }
124    catch (utility::runtime_error& e) {
125      in_ = NULL;
126      throw e;
127    }
128  }
129
130}}}
Note: See TracBrowser for help on using the repository browser.