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

Last change on this file since 3417 was 3417, checked in by Peter, 4 years ago

updating copyright statements

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.6 KB
Line 
1#ifndef theplu_yat_omic_bam_header
2#define theplu_yat_omic_bam_header
3
4// $Id: BamHeader.h 3417 2015-05-25 01:35:59Z peter $
5
6/*
7  Copyright (C) 2012, 2013, 2014, 2015 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       \throw utility::runtime_error on failure
82
83       \since new in yat 0.11
84     */
85    void parse_region(const std::string& reg, int& tid, int& begin,
86                      int& end) const;
87
88    /**
89       \brief Access value in \c \@PG lines.
90
91       A program group line in the header typically looks like
92
93       \code @PG  ID:bwa  PN:bwa  VN:0.6.1-r104 \endcode
94
95       and for this line \c program_group("bwa", "VN") returns
96       \c "0.6.1-r104"
97
98       \return value for \a key for line with ID \a id.
99
100       \since New in yat 0.13
101    */
102    const std::string& program_group(const std::string& id,
103                                     const std::string& key) const;
104
105    /**
106       \brief Access value in \c \@RG lines.
107
108       A read group line in the header typically looks like
109
110       \code @RG  ID:foo  PL:ILLUMINA SM:Tumour \endcode
111
112       and for this line \c read_group("foo", "SM") returns \c "Tumour"
113
114       \return value for \a key for line with ID \a id.
115
116       \since New in yat 0.13
117    */
118    //
119    const std::string& read_group(const std::string& id,
120                                  const std::string& key) const;
121
122    /**
123       \brief Exchanges the content in \c *this and \a other
124
125       \since New in yat 0.13
126     */
127    void swap(BamHeader& other);
128
129    /**
130       Name of chromosome with ID \a tid
131     */
132    const char* target_name(size_t tid) const;
133
134    /**
135       Length of chromosome with ID \a tid
136     */
137    uint32_t target_length(size_t tid) const;
138
139    /**
140       \return text in header
141
142       \since New in yat 0.13
143     */
144    std::string text(void) const;
145
146    /**
147       \brief set text in header
148
149       This function parses \a txt and updates fields.
150
151       \since New in yat 0.13
152     */
153    void text(const std::string& txt);
154
155    /**
156       \brief inverse of target_name(size_t)
157
158       If compiled against libbam and \a name does not exist,
159       behavious is undefined. If compiled against htslib, a
160       utility::runtime_error is thrown.
161
162       \since new in yat 0.11
163     */
164    int32_t tid(const std::string& name) const;
165
166    /**
167       \return Number of chromosomes
168     */
169    int32_t n_targets(void) const;
170
171    /**
172       \brief assignment operator
173     */
174    BamHeader& operator=(const BamHeader& rhs);
175
176  private:
177#ifndef YAT_HAVE_HTSLIB
178    typedef bam_header_t bam_hdr_t;
179#endif
180    bam_hdr_t* header_;
181    typedef std::map<std::string, std::string> strMap;
182    typedef std::map<std::string, strMap> strMap2;
183    mutable strMap2 read_group_;
184    mutable strMap2 program_group_;
185
186    friend class InBamFile;
187    friend class OutBamFile;
188
189    const std::string& group(strMap2& map, const std::string& type,
190                             const std::string& id,
191                             const std::string& key) const;
192
193    // using compiler generated copy and assignment
194  };
195
196  /**
197     Exchanges the content in the headers.
198
199     \since New in yat 0.13
200
201     \relates BamHeader
202   */
203  void swap(BamHeader& lhs, BamHeader& rhs);
204
205}}}
206#endif
Note: See TracBrowser for help on using the repository browser.