Changeset 3477


Ignore:
Timestamp:
Mar 9, 2016, 9:26:38 AM (8 years ago)
Author:
Peter
Message:

add functionality to access which RG IDs that are present in a BamHeader?

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/bam_header.cc

    r3412 r3477  
    133133    }
    134134
     135    typedef BamHeader::read_group_iterator iterator;
     136    iterator it = hdr.read_group_begin();
     137    if (it == hdr.read_group_end()) {
     138      suite.add(false);
     139      suite.err() << "it == hdr.read_group_end()\n";
     140    }
     141    else {
     142      if (!suite.add(*it == "foo"))
     143        suite.err() << "*it != foo\n";
     144      std::map<std::string, std::string> m = hdr.read_group(*it);
     145      std::string val = m["SM"];
     146      if (!suite.add(val == "Tumour"))
     147        suite.err() << "incorrect val: " << val << " expected Tumour\n";
     148      val = m["PL"];
     149      if (!suite.add(val == "ILLUMINA"))
     150        suite.err() << "incorrect val: " << val << " expected ILLUMINA\n";
     151    }
     152
    135153    std::string bwa_version = hdr.program_group("bwa", "VN");
    136154    if (bwa_version!="0.6.1-r104") {
  • trunk/yat/omic/BamHeader.cc

    r3423 r3477  
    8181                                      const std::string& id,
    8282                                      const std::string& key) const
     83  {
     84    update_group(table, type);
     85    strMap2::const_iterator line_table = table.find(id);
     86    if (line_table == table.end()) {
     87      std::string msg = "@" + type + ": unknown ID: '" + id + "'";
     88      throw utility::runtime_error(msg);
     89    }
     90    strMap::const_iterator it = line_table->second.find(key);
     91    if (it == line_table->second.end()) {
     92      std::string msg = "@" + type + ": unknown KEY: '" + key + "'";
     93      throw utility::runtime_error(msg);
     94    }
     95    return it->second;
     96  }
     97
     98
     99  void BamHeader::update_group(strMap2& table, const std::string& type) const
    83100  {
    84101    assert(type.size()==2);
     
    101118      }
    102119    }
    103     strMap2::const_iterator line_table = table.find(id);
    104     if (line_table == table.end()) {
    105       std::string msg = "@" + type + ": unknown ID: '" + id + "'";
    106       throw utility::runtime_error(msg);
    107     }
    108     strMap::const_iterator it = line_table->second.find(key);
    109     if (it == line_table->second.end()) {
    110       std::string msg = "@" + type + ": unknown KEY: '" + key + "'";
    111       throw utility::runtime_error(msg);
    112     }
    113     return it->second;
    114120  }
    115121
     
    156162  {
    157163    return group(read_group_, "RG", id, key);
     164  }
     165
     166
     167  const std::map<std::string, std::string>&
     168  BamHeader::read_group(const std::string& id) const
     169  {
     170    update_group(read_group_, "RG");
     171    strMap2::const_iterator line_table = read_group_.find(id);
     172    if (line_table == read_group_.end()) {
     173      std::string msg = "@RG: unknown ID: '" + id + "'";
     174      throw utility::runtime_error(msg);
     175    }
     176    return line_table->second;
     177  }
     178
     179
     180  BamHeader::read_group_iterator BamHeader::read_group_begin(void) const
     181  {
     182    update_group(read_group_, "RG");
     183    BamHeader::strMap2::const_iterator it(read_group_.begin());
     184    return utility::pair_first_iterator(it);
     185  }
     186
     187
     188  BamHeader::read_group_iterator BamHeader::read_group_end(void) const
     189  {
     190    update_group(read_group_, "RG");
     191    BamHeader::strMap2::const_iterator it(read_group_.end());
     192    return utility::pair_first_iterator(it);
    158193  }
    159194
  • trunk/yat/omic/BamHeader.h

    r3417 r3477  
    2727#include "yat/utility/config_public.h"
    2828
     29#include "yat/utility/stl_utility.h"
     30
    2931#include YAT_SAM_HEADER
     32
     33#include <boost/iterator/transform_iterator.hpp>
    3034
    3135#include <map>
     
    5660  class BamHeader
    5761  {
     62    typedef std::map<std::string, std::string> strMap;
     63    typedef std::map<std::string, strMap> strMap2;
    5864  public:
    5965    /**
     
    112118       and for this line \c read_group("foo", "SM") returns \c "Tumour"
    113119
     120       To see which IDs are available in the header, iterate through
     121       the set of IDs with read_group_begin() (and read_group_end()).
     122
    114123       \return value for \a key for line with ID \a id.
    115124
    116125       \since New in yat 0.13
    117126    */
    118     //
    119127    const std::string& read_group(const std::string& id,
    120128                                  const std::string& key) const;
     129
     130    /**
     131       \returns a map describing an \@RG line such as
     132
     133       \code @RG  ID:foo  PL:ILLUMINA SM:Tumour \endcode
     134
     135       for which the returned map contains two entries: one with key
     136       \c PL and value \c ILLUMINA, and one with key \c SM and value
     137       \c Tumour.
     138
     139       \since New in yat 0.14
     140     */
     141    const std::map<std::string, std::string>&
     142    read_group(const std::string& id) const;
     143
     144    /**
     145       read_group_iterator is used to iterate through read groups
     146
     147       \see read_group_begin
     148       \see read_group_end
     149
     150       \since New in yat 0.14
     151     */
     152    typedef boost::transform_iterator<
     153      utility::PairFirst<const strMap2::value_type>,
     154      strMap2::const_iterator>
     155    read_group_iterator;
     156
     157    /**
     158       \return first of available RG IDs
     159
     160       \since New in yat 0.14
     161     */
     162    read_group_iterator read_group_begin(void) const;
     163
     164    /**
     165       \return end of range of available RG IDs
     166
     167       \see read_group_begin()
     168
     169       \since New in yat 0.14
     170     */
     171    read_group_iterator read_group_end(void) const;
    121172
    122173    /**
     
    179230#endif
    180231    bam_hdr_t* header_;
    181     typedef std::map<std::string, std::string> strMap;
    182     typedef std::map<std::string, strMap> strMap2;
    183232    mutable strMap2 read_group_;
    184233    mutable strMap2 program_group_;
     
    191240                             const std::string& key) const;
    192241
     242    void update_group(strMap2& map, const std::string& type) const;
     243
    193244    // using compiler generated copy and assignment
    194245  };
Note: See TracChangeset for help on using the changeset viewer.