Changeset 4290


Ignore:
Timestamp:
Feb 3, 2023, 4:17:58 PM (4 months ago)
Author:
Peter
Message:

add same interface for program_group as for read_group in class BamHeader?.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/bam_header.cc

    r4264 r4290  
    2323#include "yat/omic/BamFile.h"
    2424#include "yat/omic/BamHeader.h"
     25#include <set>
    2526#endif
     27
    2628
    2729using namespace theplu::yat;
     
    172174  int32_t n_chr = hdr.n_targets();
    173175
    174   // check that we can add another chromosomevia a @SG field
     176  // check that we can add another chromosome via a @SG field
    175177  std::stringstream ss(hdr.text());
    176178  std::string str3;
     
    253255    suite.out() << "expected exception with what(): " << e.what() << "\n";
    254256  }
     257
     258
     259  std::set<std::string>
     260    programs(hdr.program_group_begin(), hdr.program_group_end());
     261  for (const auto& prog : programs)
     262    suite.out() << prog << "\n";
     263  if (!programs.count("bwa") || !programs.count("samtools") ||
     264      programs.size()!=3) {
     265    suite.add(false);
     266    suite.err() << "incorrect programs\n";
     267  }
     268  hdr.program_group("bwa");
    255269}
    256270#endif
  • trunk/yat/omic/BamHeader.cc

    r4265 r4290  
    6767
    6868
     69  const std::map<std::string, std::string>&
     70  BamHeader::group(strMap2& map, const std::string& type,
     71                   const std::string& id) const
     72  {
     73    update_group(map, type);
     74    strMap2::const_iterator line_table = map.find(id);
     75    if (line_table == map.end()) {
     76      std::string msg = "@" + type + ": unknown ID: '" + id + "'";
     77      throw utility::runtime_error(msg);
     78    }
     79    return line_table->second;
     80  }
     81
     82
     83  BamHeader::read_group_iterator
     84  BamHeader::group_begin(strMap2& group, const std::string& type) const
     85  {
     86    update_group(group, type);
     87    BamHeader::strMap2::const_iterator it(group.begin());
     88    return utility::pair_first_iterator(it);
     89  }
     90
     91
     92  BamHeader::read_group_iterator
     93  BamHeader::group_end(strMap2& group, const std::string& type) const
     94  {
     95    update_group(group, type);
     96    BamHeader::strMap2::const_iterator it(group.end());
     97    return utility::pair_first_iterator(it);
     98  }
     99
     100
    69101  const std::string& BamHeader::group(strMap2& table,const std::string& type,
    70102                                      const std::string& id,
    71103                                      const std::string& key) const
    72104  {
    73     update_group(table, type);
    74     strMap2::const_iterator line_table = table.find(id);
    75     if (line_table == table.end()) {
    76       std::string msg = "@" + type + ": unknown ID: '" + id + "'";
    77       throw utility::runtime_error(msg);
    78     }
    79     strMap::const_iterator it = line_table->second.find(key);
    80     if (it == line_table->second.end()) {
     105    const std::map<std::string, std::string>& m = group(table, type, id);
     106    strMap::const_iterator it = m.find(key);
     107    if (it == m.end()) {
    81108      std::string msg = "@" + type + ": unknown KEY: '" + key + "'";
    82109      throw utility::runtime_error(msg);
     
    142169
    143170
     171  const std::map<std::string, std::string>&
     172  BamHeader::program_group(const std::string& id) const
     173  {
     174    return group(program_group_, "PG", id);
     175  }
     176
     177
     178  BamHeader::program_group_iterator BamHeader::program_group_begin(void) const
     179  {
     180    return group_begin(program_group_, "PG");
     181  }
     182
     183
     184  BamHeader::program_group_iterator BamHeader::program_group_end(void) const
     185  {
     186    return group_end(program_group_, "PG");
     187  }
     188
     189
    144190  const std::string& BamHeader::read_group(const std::string& id,
    145191                                           const std::string& key) const
     
    152198  BamHeader::read_group(const std::string& id) const
    153199  {
    154     update_group(read_group_, "RG");
    155     strMap2::const_iterator line_table = read_group_.find(id);
    156     if (line_table == read_group_.end()) {
    157       std::string msg = "@RG: unknown ID: '" + id + "'";
    158       throw utility::runtime_error(msg);
    159     }
    160     return line_table->second;
     200    return group(read_group_, "RG", id);
    161201  }
    162202
     
    164204  BamHeader::read_group_iterator BamHeader::read_group_begin(void) const
    165205  {
    166     update_group(read_group_, "RG");
    167     BamHeader::strMap2::const_iterator it(read_group_.begin());
    168     return utility::pair_first_iterator(it);
     206    return group_begin(read_group_, "RG");
    169207  }
    170208
     
    172210  BamHeader::read_group_iterator BamHeader::read_group_end(void) const
    173211  {
    174     update_group(read_group_, "RG");
    175     BamHeader::strMap2::const_iterator it(read_group_.end());
    176     return utility::pair_first_iterator(it);
     212    return group_end(read_group_, "RG");
    177213  }
    178214
  • trunk/yat/omic/BamHeader.h

    r4272 r4290  
    106106
    107107    /**
     108       \returns a map describing an \@PG line such as
     109
     110       \code @PG  ID:bwa  PN:bwa  VN:0.6.1-r104 \endcode
     111
     112       for which the returned map contains two entries: one with key
     113       \c PN and value \c bwa, and one with key \c VN and value
     114       \c 0.6.1-r104.
     115
     116       \since New in yat 0.21
     117     */
     118    const std::map<std::string, std::string>&
     119    program_group(const std::string& id) const;
     120
     121    /**
     122       program_group_iterator is used to iterate through program groups
     123
     124       program_group_iterator is a \readable_iterator and
     125       \bidirectional_traversal_iterator with value type \c
     126       std::string
     127
     128       \see program_group_begin
     129       \see program_group_end
     130
     131       \since New in yat 0.21
     132     */
     133    typedef boost::transform_iterator<
     134      utility::PairFirst<const strMap2::value_type>,
     135      strMap2::const_iterator>
     136    program_group_iterator;
     137
     138    /**
     139       \return first of available PG IDs
     140
     141       \since New in yat 0.21
     142     */
     143    program_group_iterator program_group_begin(void) const;
     144
     145    /**
     146       \return end of range of available PG IDs
     147
     148       \see program_group_begin()
     149
     150       \since New in yat 0.21
     151     */
     152    program_group_iterator program_group_end(void) const;
     153
     154    /**
    108155       \brief Access value in \c \@RG lines.
    109156
     
    240287    friend class OutBamFile;
    241288
     289    const std::map<std::string, std::string>&
     290    group(strMap2&, const std::string& type,
     291          const std::string& id) const;
     292
     293    read_group_iterator
     294    group_begin(strMap2&, const std::string& type) const;
     295
     296    read_group_iterator
     297    group_end(strMap2&, const std::string& type) const;
     298
    242299    const std::string& group(strMap2& map, const std::string& type,
    243300                             const std::string& id,
Note: See TracChangeset for help on using the changeset viewer.