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

Last change on this file since 2883 was 2883, checked in by Peter, 10 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: 3.6 KB
Line 
1#ifndef theplu_yat_omic_bam_read_iterator
2#define theplu_yat_omic_bam_read_iterator
3
4// $Id: BamReadIterator.h 2883 2012-12-03 12:48:51Z peter $
5//
6// Copyright (C) 2012 Peter Johansson
7//
8// This program is free software; you can redistribute it and/or modify
9// it under the terms of the GNU General Public License as published by
10// the Free Software Foundation; either version 3 of the License, or
11// (at your option) any later version.
12//
13// This program is distributed in the hope that it will be useful, but
14// WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16// General Public License for more details.
17//
18// You should have received a copy of the GNU General Public License
19// along with this program. If not, see <http://www.gnu.org/licenses/>.
20
21#include "BamFile.h"
22#include "BamRead.h"
23
24#include <sam.h>
25
26#include <boost/iterator/iterator_facade.hpp>
27#include <boost/shared_ptr.hpp>
28
29#include <iterator>
30
31namespace theplu {
32namespace yat {
33namespace omic {
34
35  // forward declaration
36  class InBamFile;
37
38  /**
39     \brief class to iterate through a InBamFile
40
41     Iterator is a single pass \input_iterator which means iterator
42     can only be used read bam file. Also two BamReadIterator working
43     on the same InBamFile are not independent, i.e., incrementing one
44     iterator will also affect the other one.
45
46     Iterator can either be constructed to iterate over entire bam
47     file or if a region is specified only over that region. When no
48     more elements are available, iterator turns into a end-of-file
49     iterator and is no longer dereferencable.
50
51     \see InBamFile
52   */
53  class BamReadIterator
54    : public boost::iterator_facade<
55    BamReadIterator, const BamRead, std::input_iterator_tag
56    >
57  {
58  public:
59    /**
60       \brief Contructs end of file iterator.
61     */
62    BamReadIterator(void);
63
64    /**
65       Creates an iterator pointing to first BamRead in
66       InBamFile.
67
68       \param in file to iterate over
69     */
70    explicit BamReadIterator(InBamFile& in);
71
72    /**
73       Iterator that walks over specified region. Iterator iterates
74       over all BamReads in file \a in that overlap with specified
75       region and then it turns into an end-of-region iterator.
76
77       This function requires an index file is available, see
78       InBamFile::index().
79
80       \param in file to iterate over
81       \param tid segment (chromosome) to iterate over
82       \param start left most position of specified region
83       \param end right most position of specified region
84     */
85    BamReadIterator(InBamFile& in, int32_t tid, int32_t start, int32_t end);
86  private:
87    friend class boost::iterator_core_access;
88    BamReadIterator::reference dereference(void) const;
89    bool equal(const BamReadIterator& other) const;
90    void increment(void);
91
92    /**
93       Interface class for the actor that handles difference between
94       an iterator that iterates the entire file and one that uses
95       index to iterate over a specific region.
96     */
97    class Actor
98    {
99    public:
100      explicit Actor(InBamFile*);
101      virtual ~Actor(void);
102      virtual void increment(void)=0;
103      InBamFile* in_;
104      BamRead read_;
105    };
106
107    /// class used when region is entire file
108    class AllActor : public Actor
109    {
110    public:
111      AllActor(void);
112      explicit AllActor(InBamFile&);
113      void increment(void);
114    };
115
116    /// class used when iterating over a sub-region of the bam file
117    class IndexActor : public Actor
118    {
119    public:
120      IndexActor(InBamFile& bf, int32_t tid, int32_t begin, int32_t end);
121      void increment(void);
122    private:
123      boost::shared_ptr<__bam_iter_t> iter_;
124    };
125
126    boost::shared_ptr<Actor> actor_;
127
128    struct IndexDestroyer
129    {
130      void operator()(bam_iter_t i) const
131      {
132        bam_iter_destroy(i);
133      }
134    };
135  };
136}}}
137#endif
Note: See TracBrowser for help on using the repository browser.