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

Last change on this file since 2983 was 2983, checked in by Peter, 9 years ago

fixes #751. throw if OutBamFile::write fails

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