1 | |
---|
2 | // $Id: BamFile.cc 2993 2013-03-03 07:38:22Z peter $ |
---|
3 | |
---|
4 | /* |
---|
5 | Copyright (C) 2012, 2013 Peter Johansson |
---|
6 | |
---|
7 | This file is part of the yat library, http://dev.thep.lu.se/yat |
---|
8 | |
---|
9 | The yat library is free software; you can redistribute it and/or |
---|
10 | modify it under the terms of the GNU General Public License as |
---|
11 | published by the Free Software Foundation; either version 3 of the |
---|
12 | License, or (at your option) any later version. |
---|
13 | |
---|
14 | The yat library is distributed in the hope that it will be useful, |
---|
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
17 | General Public License for more details. |
---|
18 | |
---|
19 | You should have received a copy of the GNU General Public License |
---|
20 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
21 | */ |
---|
22 | |
---|
23 | #include <config.h> |
---|
24 | |
---|
25 | #include "BamFile.h" |
---|
26 | #include "BamHeader.h" |
---|
27 | #include "config_bam.h" |
---|
28 | |
---|
29 | #include "yat/utility/Exception.h" |
---|
30 | |
---|
31 | #include YAT_BAM_HEADER |
---|
32 | |
---|
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 | assert(header_.header_); |
---|
84 | } |
---|
85 | |
---|
86 | |
---|
87 | bool InBamFile::read(BamRead& read) |
---|
88 | { |
---|
89 | int result_ = samread(sf_, read.bam_); |
---|
90 | if (result_<-1) { |
---|
91 | std::ostringstream ss; |
---|
92 | ss << "read(1): invalid bam file '" << filename() << "'"; |
---|
93 | if (result_ == -2) |
---|
94 | ss << " truncated file"; |
---|
95 | else |
---|
96 | ss << " error: " << result_; |
---|
97 | throw utility::runtime_error(ss.str()); |
---|
98 | } |
---|
99 | return result_>=0; |
---|
100 | } |
---|
101 | |
---|
102 | |
---|
103 | bool InBamFile::read(BamRead& read, bam_iter_t iter) |
---|
104 | { |
---|
105 | assert(sf_->type & 1); // no random access from sam files |
---|
106 | int result_ = bam_iter_read(sf_->x.bam, iter, read.bam_); |
---|
107 | if (result_<-1) { |
---|
108 | std::ostringstream ss; |
---|
109 | ss << "read(2): invalid bam file '" << filename() << "'"; |
---|
110 | if (result_ == -2) |
---|
111 | ss << " truncated file"; |
---|
112 | else |
---|
113 | ss << " error: " << result_; |
---|
114 | throw utility::runtime_error(ss.str()); |
---|
115 | } |
---|
116 | return result_>=0; |
---|
117 | } |
---|
118 | |
---|
119 | |
---|
120 | OutBamFile::OutBamFile(void) |
---|
121 | : super_t() {} |
---|
122 | |
---|
123 | |
---|
124 | OutBamFile::OutBamFile(const std::string& fn, const BamHeader& header) |
---|
125 | { |
---|
126 | open(fn, header); |
---|
127 | } |
---|
128 | |
---|
129 | |
---|
130 | void OutBamFile::open(const std::string& fn, const BamHeader& h) |
---|
131 | { |
---|
132 | open_base(fn, "wb", h.header_); |
---|
133 | } |
---|
134 | |
---|
135 | |
---|
136 | void OutBamFile::write(const BamRead& read) |
---|
137 | { |
---|
138 | if (!samwrite(sf_, read.bam_)) { |
---|
139 | // FIXME if wanted perhaps we should throw an OutBamFile::error |
---|
140 | // instead that can hold \a read |
---|
141 | throw utility::runtime_error("OutBamFile::write failed"); |
---|
142 | } |
---|
143 | } |
---|
144 | |
---|
145 | }}} |
---|