1 | // $Id: BamFile.cc 2897 2012-12-11 07:27:40Z peter $ |
---|
2 | // |
---|
3 | // Copyright (C) 2012 Peter Johansson |
---|
4 | // |
---|
5 | // This program is free software; you can redistribute it and/or modify |
---|
6 | // it under the terms of the GNU General Public License as published by |
---|
7 | // the Free Software Foundation; either version 3 of the License, or |
---|
8 | // (at your option) any later version. |
---|
9 | // |
---|
10 | // This program is distributed in the hope that it will be useful, but |
---|
11 | // WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
13 | // General Public License for more details. |
---|
14 | // |
---|
15 | // You should have received a copy of the GNU General Public License |
---|
16 | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
17 | |
---|
18 | #include <config.h> |
---|
19 | |
---|
20 | #include "BamFile.h" |
---|
21 | #include "BamHeader.h" |
---|
22 | |
---|
23 | #include "yat/utility/Exception.h" |
---|
24 | |
---|
25 | #include <bam.h> |
---|
26 | |
---|
27 | #include <stdexcept> |
---|
28 | #include <string> |
---|
29 | #include <sstream> |
---|
30 | |
---|
31 | namespace theplu { |
---|
32 | namespace yat { |
---|
33 | namespace omic { |
---|
34 | |
---|
35 | InBamFile::InBamFile(void) |
---|
36 | : super_t(), index_(NULL) |
---|
37 | {} |
---|
38 | |
---|
39 | |
---|
40 | InBamFile::InBamFile(const std::string& fn) |
---|
41 | : super_t(), index_(NULL) |
---|
42 | { |
---|
43 | open(fn); |
---|
44 | } |
---|
45 | |
---|
46 | |
---|
47 | InBamFile::~InBamFile(void) |
---|
48 | { |
---|
49 | bam_index_destroy(index_); |
---|
50 | } |
---|
51 | |
---|
52 | |
---|
53 | const BamHeader& InBamFile::header(void) const |
---|
54 | { |
---|
55 | return header_; |
---|
56 | } |
---|
57 | |
---|
58 | |
---|
59 | const bam_index_t* InBamFile::index(void) const |
---|
60 | { |
---|
61 | if (!index_) { |
---|
62 | index_ = bam_index_load(filename().c_str()); |
---|
63 | if (!index_) { |
---|
64 | std::ostringstream ss; |
---|
65 | ss << "cannot load bam index '" << filename() << ".bai'"; |
---|
66 | throw utility::runtime_error(ss.str()); |
---|
67 | } |
---|
68 | } |
---|
69 | return index_; |
---|
70 | } |
---|
71 | |
---|
72 | |
---|
73 | void InBamFile::open(const std::string& fn) |
---|
74 | { |
---|
75 | open_base(fn, "rb", NULL); |
---|
76 | header_.header_ = sf_->header; |
---|
77 | assert(header_.header_); |
---|
78 | } |
---|
79 | |
---|
80 | |
---|
81 | bool InBamFile::read(BamRead& read) |
---|
82 | { |
---|
83 | int result_ = samread(sf_, read.bam_); |
---|
84 | if (result_<-1) { |
---|
85 | std::ostringstream ss; |
---|
86 | ss << "read(1): invalid bam file '" << filename() << "'"; |
---|
87 | if (result_ == -2) |
---|
88 | ss << " truncated file"; |
---|
89 | else |
---|
90 | ss << " error: " << result_; |
---|
91 | throw utility::runtime_error(ss.str()); |
---|
92 | } |
---|
93 | return result_>=0; |
---|
94 | } |
---|
95 | |
---|
96 | |
---|
97 | bool InBamFile::read(BamRead& read, bam_iter_t iter) |
---|
98 | { |
---|
99 | assert(sf_->type & 1); // no random access from sam files |
---|
100 | int result_ = bam_iter_read(sf_->x.bam, iter, read.bam_); |
---|
101 | if (result_<-1) { |
---|
102 | std::ostringstream ss; |
---|
103 | ss << "read(2): invalid bam file '" << filename() << "'"; |
---|
104 | if (result_ == -2) |
---|
105 | ss << " truncated file"; |
---|
106 | else |
---|
107 | ss << " error: " << result_; |
---|
108 | throw utility::runtime_error(ss.str()); |
---|
109 | } |
---|
110 | return result_>=0; |
---|
111 | } |
---|
112 | |
---|
113 | |
---|
114 | OutBamFile::OutBamFile(void) |
---|
115 | : super_t() {} |
---|
116 | |
---|
117 | |
---|
118 | OutBamFile::OutBamFile(const std::string& fn, const BamHeader& header) |
---|
119 | { |
---|
120 | open(fn, header); |
---|
121 | } |
---|
122 | |
---|
123 | |
---|
124 | void OutBamFile::open(const std::string& fn, const BamHeader& h) |
---|
125 | { |
---|
126 | open_base(fn, "wb", h.header_); |
---|
127 | } |
---|
128 | |
---|
129 | |
---|
130 | void OutBamFile::write(const BamRead& read) |
---|
131 | { |
---|
132 | samwrite(sf_, read.bam_); |
---|
133 | } |
---|
134 | |
---|
135 | }}} |
---|