1 | // $Id: BamFile.cc 3182 2014-03-24 22:28:02Z 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 "BamFile.h" |
---|
25 | #include "BamHeader.h" |
---|
26 | #include "config_bam.h" |
---|
27 | |
---|
28 | #include "yat/utility/Exception.h" |
---|
29 | |
---|
30 | #include YAT_BAM_HEADER |
---|
31 | |
---|
32 | #include <cassert> |
---|
33 | #include <stdexcept> |
---|
34 | #include <string> |
---|
35 | #include <sstream> |
---|
36 | |
---|
37 | namespace theplu { |
---|
38 | namespace yat { |
---|
39 | namespace omic { |
---|
40 | |
---|
41 | InBamFile::InBamFile(void) |
---|
42 | : super_t(), index_(NULL) |
---|
43 | {} |
---|
44 | |
---|
45 | |
---|
46 | InBamFile::InBamFile(const std::string& fn) |
---|
47 | : super_t(), index_(NULL) |
---|
48 | { |
---|
49 | open(fn); |
---|
50 | } |
---|
51 | |
---|
52 | |
---|
53 | InBamFile::~InBamFile(void) |
---|
54 | { |
---|
55 | bam_index_destroy(index_); |
---|
56 | } |
---|
57 | |
---|
58 | |
---|
59 | const BamHeader& InBamFile::header(void) const |
---|
60 | { |
---|
61 | return header_; |
---|
62 | } |
---|
63 | |
---|
64 | |
---|
65 | const bam_index_t* InBamFile::index(void) const |
---|
66 | { |
---|
67 | if (!index_) { |
---|
68 | index_ = bam_index_load(filename().c_str()); |
---|
69 | if (!index_) { |
---|
70 | std::ostringstream ss; |
---|
71 | ss << "cannot load bam index '" << filename() << ".bai'"; |
---|
72 | throw utility::runtime_error(ss.str()); |
---|
73 | } |
---|
74 | } |
---|
75 | return index_; |
---|
76 | } |
---|
77 | |
---|
78 | |
---|
79 | void InBamFile::open(const std::string& fn) |
---|
80 | { |
---|
81 | open_base(fn, "rb", NULL); |
---|
82 | header_.header_ = sf_->header; |
---|
83 | if (header_.header_ == NULL) { |
---|
84 | std::ostringstream ss; |
---|
85 | ss << "failed to read header from '" << filename() << "'"; |
---|
86 | throw utility::runtime_error(ss.str()); |
---|
87 | } |
---|
88 | } |
---|
89 | |
---|
90 | |
---|
91 | bool InBamFile::read(BamRead& read) |
---|
92 | { |
---|
93 | int result_ = samread(sf_, read.bam_); |
---|
94 | if (result_<-1) { |
---|
95 | std::ostringstream ss; |
---|
96 | ss << "InBamFile::read(1): invalid bam file '" << filename() << "'"; |
---|
97 | if (result_ == -2) |
---|
98 | ss << " truncated file"; |
---|
99 | else |
---|
100 | ss << " error: " << result_; |
---|
101 | throw utility::runtime_error(ss.str()); |
---|
102 | } |
---|
103 | return result_>=0; |
---|
104 | } |
---|
105 | |
---|
106 | |
---|
107 | bool InBamFile::read(BamRead& read, bam_iter_t iter) |
---|
108 | { |
---|
109 | assert(sf_->type & 1); // no random access from sam files |
---|
110 | int result_ = bam_iter_read(sf_->x.bam, iter, read.bam_); |
---|
111 | if (result_<-1) { |
---|
112 | std::ostringstream ss; |
---|
113 | ss << "read(2): invalid bam file '" << filename() << "'"; |
---|
114 | if (result_ == -2) |
---|
115 | ss << " truncated file"; |
---|
116 | else |
---|
117 | ss << " error: " << result_; |
---|
118 | throw utility::runtime_error(ss.str()); |
---|
119 | } |
---|
120 | return result_>=0; |
---|
121 | } |
---|
122 | |
---|
123 | |
---|
124 | OutBamFile::OutBamFile(void) |
---|
125 | : super_t() {} |
---|
126 | |
---|
127 | |
---|
128 | OutBamFile::OutBamFile(const std::string& fn, const BamHeader& header) |
---|
129 | { |
---|
130 | open(fn, header); |
---|
131 | } |
---|
132 | |
---|
133 | |
---|
134 | void OutBamFile::open(const std::string& fn, const BamHeader& h) |
---|
135 | { |
---|
136 | open_base(fn, "wb", h.header_); |
---|
137 | } |
---|
138 | |
---|
139 | |
---|
140 | void OutBamFile::write(const BamRead& read) |
---|
141 | { |
---|
142 | if (samwrite(sf_, read.bam_)<=0) { |
---|
143 | // FIXME if wanted perhaps we should throw an OutBamFile::error |
---|
144 | // instead that can hold \a read |
---|
145 | throw utility::runtime_error("OutBamFile::write failed"); |
---|
146 | } |
---|
147 | } |
---|
148 | |
---|
149 | }}} |
---|