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

Last change on this file since 2980 was 2980, checked in by Peter, 9 years ago

add syntax-check rule (included in maintainer-check), add two missing config.h includes, and prefer tests named with suffix '.test'

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