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

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

add two tests for bam functionality

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