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

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

conform GPL header to same style as in other yat files

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