source: trunk/yat/omic/BamHeader.h @ 3410

Last change on this file since 3410 was 3410, checked in by Peter, 8 years ago

add two new functions read_group and program_group to access data in BamHeader?. closes #817

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.7 KB
Line 
1#ifndef theplu_yat_omic_bam_header
2#define theplu_yat_omic_bam_header
3
4// $Id: BamHeader.h 3410 2015-04-21 23:05:43Z peter $
5
6/*
7  Copyright (C) 2012, 2013, 2014 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 "config_bam.h"
26
27#include "yat/utility/config_public.h"
28
29#include YAT_SAM_HEADER
30
31#include <map>
32#include <string>
33
34namespace theplu {
35namespace yat {
36namespace omic {
37
38#ifndef YAT_HAVE_HTSLIB
39  namespace detail {
40    bam_header_t * bam_hdr_dup(const bam_header_t* other);
41  }
42#endif
43
44  /**
45     \brief Wrapper around bam_hdr_t struct.
46
47     Class is typically created via InBamFile::header().
48
49     It is possible to copy and assign a BamHeader, but note that a
50     BamHeader does not own underlying data. The underlying data is
51     owned by the InBamFile and the BamHeader is thus invalid after
52     the corresponding InBamFile has been destroyed.
53
54     \since New in yat 0.10
55   */
56  class BamHeader
57  {
58  public:
59    /**
60       \brief Default constructor
61     */
62    BamHeader(void);
63
64    /**
65       \brief Destructor
66     */
67    ~BamHeader(void);
68
69    /**
70       \brief Copy constructor
71     */
72    BamHeader(const BamHeader&);
73
74    /**
75       Parse a region in the format: 'chr2:100,000-200,000 and return
76       values in variables \a tid, \a begin and \a end. \a reg is
77       1-based and \a begin and \a end are 0-based, i.e.,
78       "chr2:100,000-200,000" will set \a begin = 99999 and \a end =
79       200000.
80
81       \see bam_parse_region
82
83       \throw utility::runtime_error on failure
84
85       \since new in yat 0.11
86     */
87    void parse_region(const std::string& reg, int& tid, int& begin,
88                      int& end) const;
89
90    /**
91       \brief Access value in \c \@PG lines.
92
93       A program group line in the header typically looks like
94
95       \code @PG  ID:bwa  PN:bwa  VN:0.6.1-r104 \endcode
96
97       and for this line \c program_group("bwa", "VN") returns
98       \c "0.6.1-r104"
99
100       \return value for \a key for line with ID \a id.
101
102       \since New in yat 0.13
103    */
104    const std::string& program_group(const std::string& id,
105                                     const std::string& key) const;
106
107    /**
108       \brief Access value in \c \@RG lines.
109
110       A read group line in the header typically looks like
111
112       \code @RG  ID:foo  PL:ILLUMINA SM:Tumour \endcode
113
114       and for this line \c read_group("foo", "SM") returns \c "Tumour"
115
116       \return value for \a key for line with ID \a id.
117
118       \since New in yat 0.13
119    */
120    //
121    const std::string& read_group(const std::string& id,
122                                  const std::string& key) const;
123
124    /**
125       \brief Exchanges the content in \c *this and \a other
126
127       \since New in yat 0.13
128     */
129    void swap(BamHeader& other);
130
131    /**
132       Name of chromosome with ID \a tid
133     */
134    const char* target_name(size_t tid) const;
135
136    /**
137       Length of chromosome with ID \a tid
138     */
139    uint32_t target_length(size_t tid) const;
140
141    /**
142       \return text in header
143
144       \since New in yat 0.13
145     */
146    std::string text(void) const;
147
148    /**
149       \brief set text in header
150
151       This function parses \a txt and updates fields.
152
153       \since New in yat 0.13
154     */
155    void text(const std::string& txt);
156
157    /**
158       \brief inverse of target_name(size_t)
159
160       If compiled against libbam and \a name does not exist,
161       behavious is undefined. If compiled against htslib, a
162       utility::runtime_error is thrown.
163
164       \since new in yat 0.11
165     */
166    int32_t tid(const std::string& name) const;
167
168    /**
169       \return Number of chromosomes
170     */
171    int32_t n_targets(void) const;
172
173    /**
174       \brief assignment operator
175     */
176    BamHeader& operator=(const BamHeader& rhs);
177
178  private:
179#ifndef YAT_HAVE_HTSLIB
180    typedef bam_header_t bam_hdr_t;
181#endif
182    bam_hdr_t* header_;
183    typedef std::map<std::string, std::string> strMap;
184    typedef std::map<std::string, strMap> strMap2;
185    mutable strMap2 read_group_;
186    mutable strMap2 program_group_;
187
188    friend class InBamFile;
189    friend class OutBamFile;
190
191    const std::string& group(strMap2& map, const std::string& type,
192                             const std::string& id,
193                             const std::string& key) const;
194
195    // using compiler generated copy and assignment
196  };
197
198  /**
199     Exchanges the content in the headers.
200
201     \since New in yat 0.13
202
203     \relates BamHeader
204   */
205  void swap(BamHeader& lhs, BamHeader& rhs);
206
207}}}
208#endif
Note: See TracBrowser for help on using the repository browser.