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

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

New classes to wrap around functionality provided by samtools. refs #729

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