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