source: trunk/yat/omic/BamFile.cc @ 3166

Last change on this file since 3166 was 3166, checked in by Peter, 8 years ago

throw OutBamFile::error when OutBamFile::write fails. closes #778

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.8 KB
Line 
1// $Id: BamFile.cc 3166 2014-01-17 02:23:05Z 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
37namespace theplu {
38namespace yat {
39namespace 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  OutBamFile::OutBamFile(const std::string& fn, const BamHeader& header,
135                         unsigned int c)
136  {
137    open(fn, header, c);
138  }
139
140
141  void OutBamFile::open(const std::string& fn, const BamHeader& h)
142  {
143    open_base(fn, "wb", h.header_);
144  }
145
146
147  void OutBamFile::open(const std::string& fn, const BamHeader& h,
148                        unsigned int compression)
149  {
150    std::string mode("wb");
151    if (compression > 9) {
152      std::stringstream oss;
153      oss << "OutBamFile::open( " << fn << ", <header>, " << compression
154          << "): incorrect compression level\n";
155      throw std::invalid_argument(oss.str());
156    }
157    mode.push_back('0'+compression);
158    open_base(fn, mode, h.header_);
159  }
160
161
162  void OutBamFile::write(const BamRead& read)
163  {
164    if (samwrite(sf_, read.bam_)<=0) {
165      throw OutBamFile::error(read);
166    }
167  }
168
169
170  // OutBamFile::error
171
172  OutBamFile::error::error(const BamRead& r)
173    : IO_error(""), read_(r) {}
174
175
176  OutBamFile::error::~error(void) throw() {}
177
178
179  const BamRead& OutBamFile::error::read(void) const
180  { return read_; }
181
182}}}
Note: See TracBrowser for help on using the repository browser.