Changeset 2999


Ignore:
Timestamp:
Mar 14, 2013, 2:30:14 PM (9 years ago)
Author:
Peter
Message:

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

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/bam_header.cc

    r2960 r2999  
    5959  if (!suite.add(std::string("1")==hdr.target_name(0)))
    6060    suite.err() << "error\n";
     61  int32_t tid = hdr.tid("1");
     62  suite.out() << "tid: " << tid << "\n";
     63  if (!suite.add(tid==0))
     64    suite.err() << "error\n";
     65
     66  int begin, end;
     67  hdr.parse_region("GL000197.1", tid, begin, end);
     68  suite.out() << tid << " " << begin << " " << end << "\n";
     69  suite.add(tid==35);
     70  hdr.parse_region("GL000197.1:1000-2000", tid, begin, end);
     71  suite.out() << tid << " " << begin << " " << end << "\n";
     72  if (!suite.add(tid==35))
     73    suite.err() << "incorrect tid\n";
     74  if (!suite.add(begin==999))
     75    suite.err() << "incorrect begin\n";
     76  if (!suite.add(end==2000))
     77    suite.err() << "incorrect end\n";
     78
    6179  suite.out() << hdr.target_name(35) << "\n";
    6280  if (!suite.add(std::string("GL000197.1")==hdr.target_name(35)))
  • trunk/yat/omic/BamHeader.cc

    r2993 r2999  
    1 
    21// $Id$
    32
     
    2524#include "BamHeader.h"
    2625
     26#include "yat/utility/Exception.h"
     27
    2728#include <cassert>
     29#include <string>
     30#include <sstream>
    2831
    2932namespace theplu {
     
    3437    : header_(NULL)
    3538  {
     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());
    3651  }
    3752
     
    5166
    5267
     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
    5386  int32_t BamHeader::n_targets(void) const
    5487  {
  • trunk/yat/omic/BamHeader.h

    r2993 r2999  
    55
    66/*
    7   Copyright (C) 2012 Peter Johansson
     7  Copyright (C) 2012, 2013 Peter Johansson
    88
    99  This file is part of the yat library, http://dev.thep.lu.se/yat
     
    2727#include YAT_BAM_HEADER
    2828
     29#include <string>
     30
    2931namespace theplu {
    3032namespace yat {
     
    4749
    4850    /**
     51       Parse a region in the format: 'chr2:100,000-200,000 and return
     52       values in variables \a tid, \a begin and \a end. \a reg is
     53       1-based and \a begin and \a end are 0-based, i.e.,
     54       "chr2:100,000-200,000" will set \a begin = 99000 and \a end =
     55       200000.
     56
     57       \see bam_parse_region
     58
     59       \throw utility::runtime_error on failure
     60
     61       \since new in yat 0.11
     62     */
     63    void parse_region(const std::string& reg, int& tid, int& begin,
     64                      int& end) const;
     65
     66    /**
    4967       Name of chromosome with ID \a tid
    5068     */
     
    5573     */
    5674    uint32_t target_length(size_t tid) const;
     75
     76    /**
     77       \brief inverse of target_name(size_t)
     78
     79       \note If \a name does not exist, behaviour is unexpected.
     80
     81       \since new in yat 0.11
     82     */
     83    int32_t tid(const std::string& name) const;
    5784
    5885    /**
Note: See TracChangeset for help on using the changeset viewer.