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

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

avoid cassert in header file

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