1 | // $Id: BamHeader.cc 3360 2014-11-24 01:22:23Z peter $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2012, 2013, 2014 Peter Johansson |
---|
5 | |
---|
6 | This file is part of the yat library, http://dev.thep.lu.se/yat |
---|
7 | |
---|
8 | The yat library is free software; you can redistribute it and/or |
---|
9 | modify it under the terms of the GNU General Public License as |
---|
10 | published by the Free Software Foundation; either version 3 of the |
---|
11 | License, or (at your option) any later version. |
---|
12 | |
---|
13 | The yat library is distributed in the hope that it will be useful, |
---|
14 | but 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 | |
---|
22 | #include <config.h> |
---|
23 | |
---|
24 | #include "BamHeader.h" |
---|
25 | |
---|
26 | #include "yat/utility/Exception.h" |
---|
27 | |
---|
28 | #include <cassert> |
---|
29 | #include <string> |
---|
30 | #include <sstream> |
---|
31 | |
---|
32 | namespace theplu { |
---|
33 | namespace yat { |
---|
34 | namespace omic { |
---|
35 | |
---|
36 | BamHeader::BamHeader(void) |
---|
37 | : header_(NULL) |
---|
38 | { |
---|
39 | } |
---|
40 | |
---|
41 | |
---|
42 | void BamHeader::parse_region(const std::string& reg, int& id, int& begin, |
---|
43 | int& end) const |
---|
44 | { |
---|
45 | assert(header_); |
---|
46 | #if YAT_HAVE_HTSLIB |
---|
47 | const char* b = reg.c_str(); |
---|
48 | const char* e = hts_parse_reg(b, &begin, &end); |
---|
49 | // If begin > end suggests something is wrong and that is also how |
---|
50 | // bam_parse_region (below in samtools impl) used to detect error |
---|
51 | // and return non-zero. |
---|
52 | if (begin<=end) { |
---|
53 | std::string chr = reg.substr(0, e-b); |
---|
54 | try { |
---|
55 | id = tid(chr); |
---|
56 | return; |
---|
57 | } |
---|
58 | catch (utility::runtime_error& e) { |
---|
59 | ; // throw below instead |
---|
60 | } |
---|
61 | } |
---|
62 | #else |
---|
63 | if (!bam_parse_region(header_, reg.c_str(), &id, &begin, &end)) |
---|
64 | return; |
---|
65 | #endif |
---|
66 | std::ostringstream ss; |
---|
67 | ss << "invalid region: '" << reg << "'"; |
---|
68 | throw utility::runtime_error(ss.str()); |
---|
69 | } |
---|
70 | |
---|
71 | |
---|
72 | uint32_t BamHeader::target_length(size_t tid) const |
---|
73 | { |
---|
74 | assert(tid < static_cast<size_t>(n_targets())); |
---|
75 | return header_->target_len[tid]; |
---|
76 | } |
---|
77 | |
---|
78 | |
---|
79 | const char* BamHeader::target_name(size_t tid) const |
---|
80 | { |
---|
81 | assert(tid < static_cast<size_t>(n_targets())); |
---|
82 | return header_->target_name[tid]; |
---|
83 | } |
---|
84 | |
---|
85 | |
---|
86 | int32_t BamHeader::tid(const std::string& name) const |
---|
87 | { |
---|
88 | #if YAT_HAVE_HTSLIB |
---|
89 | int res = bam_name2id(header_, name.c_str()); |
---|
90 | if (res==-1) { |
---|
91 | std::string msg("invalid segment name:"); |
---|
92 | msg += name; |
---|
93 | throw utility::runtime_error(msg); |
---|
94 | } |
---|
95 | return res; |
---|
96 | #else |
---|
97 | if (!header_->hash) { |
---|
98 | // We would like to call something like bam_init_header_hash, |
---|
99 | // but unfortunately that function is not defined in any header |
---|
100 | // (only in two source files). Instead we call bam_parse_region |
---|
101 | // because a documented side-effect of that function is that |
---|
102 | // hash get populated. |
---|
103 | int tid, b, e; |
---|
104 | bam_parse_region(header_, name.c_str(), &tid, &b, &e); |
---|
105 | assert(header_->hash); |
---|
106 | assert(tid == bam_get_tid(header_, name.c_str())); |
---|
107 | return tid; |
---|
108 | } |
---|
109 | return bam_get_tid(header_, name.c_str()); |
---|
110 | #endif |
---|
111 | } |
---|
112 | |
---|
113 | |
---|
114 | int32_t BamHeader::n_targets(void) const |
---|
115 | { |
---|
116 | assert(header_); |
---|
117 | return header_->n_targets; |
---|
118 | } |
---|
119 | |
---|
120 | }}} |
---|