source: trunk/yat/omic/BamHeader.cc @ 2999

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

closes #739. Two new functions in BamHeader?: tid(string) and parse_region.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.3 KB
Line 
1// $Id: BamHeader.cc 2999 2013-03-14 13:30:14Z peter $
2
3/*
4  Copyright (C) 2012, 2013 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 "BamHeader.h"
25
26#include "yat/utility/Exception.h"
27
28#include <cassert>
29#include <string>
30#include <sstream>
31
32namespace theplu {
33namespace yat {
34namespace omic {
35
36  BamHeader::BamHeader(void)
37    : header_(NULL)
38  {
39  }
40
41
42  void BamHeader::parse_region(const std::string& reg, int& tid, int& begin,
43                               int& end) const
44  {
45    assert(header_);
46    if (!bam_parse_region(header_, reg.c_str(), &tid, &begin, &end))
47      return;
48    std::ostringstream ss;
49    ss << "invalid region: '" << reg << "'";
50    throw utility::runtime_error(ss.str());
51  }
52
53
54  uint32_t BamHeader::target_length(size_t tid) const
55  {
56    assert(tid < static_cast<size_t>(n_targets()));
57    return header_->target_len[tid];
58  }
59
60
61  const char* BamHeader::target_name(size_t tid) const
62  {
63    assert(tid < static_cast<size_t>(n_targets()));
64    return header_->target_name[tid];
65  }
66
67
68  int32_t BamHeader::tid(const std::string& name) const
69  {
70    if (!header_->hash) {
71      // We would like to call something like bam_init_header_hash,
72      // but unfortunately that function is not defined in any header
73      // (only in two source files). Instead we call bam_parse_region
74      // because a documented side-effect of that function is that
75      // hash get populated.
76      int tid, b, e;
77      bam_parse_region(header_, target_name(0), &tid, &b, &e);
78      assert(header_->hash);
79      assert(tid == bam_get_tid(header_, name.c_str()));
80      return tid;
81    }
82    return bam_get_tid(header_, name.c_str());
83  }
84
85
86  int32_t BamHeader::n_targets(void) const
87  {
88    assert(header_);
89    return header_->n_targets;
90  }
91
92}}}
Note: See TracBrowser for help on using the repository browser.