Changeset 2741


Ignore:
Timestamp:
Jun 8, 2012, 1:46:33 AM (9 years ago)
Author:
Peter
Message:

prefer vector and string as internals in omic::DNA

Location:
trunk/yat/omic
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/yat/omic/DNA.cc

    r2591 r2741  
    22
    33/*
    4   Copyright (C) 2010, 2011 Peter Johansson
     4  Copyright (C) 2010, 2011, 2012 Peter Johansson
    55
    66  This file is part of the yat library, http://dev.thep.lu.se/yat
     
    3030namespace omic {
    3131
    32   std::map<char, unsigned short> DNA::char2code_;
    33   std::vector<char> DNA::code2char_;
     32  std::vector<unsigned short> DNA::char2code_;
     33  std::string DNA::code2char_;
    3434
    3535  DNA::DNA(void)
    36     : code_(0) 
     36    : code_(0)
    3737  {
    3838    assert(code_<16);
     
    4242  DNA::DNA(char c)
    4343  {
    44     init();
    45     std::map<char, unsigned short>::const_iterator i = char2code_.find(c);
    46     if (i==char2code_.end()) {
     44    try {
     45      *this = c;
     46    }
     47    catch (std::invalid_argument& e) {
    4748      std::string msg("DNA('");
    4849      msg += c;
     
    5051      throw std::invalid_argument(msg);
    5152    }
    52     code_ = i->second;
    5353    assert(code_<16);
    5454  }
     
    6868      return;
    6969
    70     char2code_[' '] = 0;
     70    code2char_ = " ACMGRSVTWYHKDBN";
    7171
    72     char2code_['A'] = 1;
    73     char2code_['C'] = 2;
    74     char2code_['G'] = 4;
    75     char2code_['T'] = 8;
    76    
    77     char2code_['M'] = 1+2;
    78     char2code_['R'] = 1+4;
    79     char2code_['W'] = 1+8;
    80     char2code_['S'] = 2+4;
    81     char2code_['Y'] = 2+8;
    82     char2code_['K'] = 4+8;
    83    
    84     char2code_['B'] = 2+4+8;
    85     char2code_['D'] = 1+4+8;
    86     char2code_['H'] = 1+2+8;
    87     char2code_['V'] = 1+2+4;
    88    
    89     char2code_['N'] = 1+2+4+8;
    90 
    91     code2char_.resize(char2code_.size());
    92     for (std::map<char, unsigned short>::const_iterator it=char2code_.begin();
    93          it!=char2code_.end(); ++it) {
    94       code2char_[it->second] = it->first;
    95     }
     72    char2code_.resize(128,255);
     73    for (unsigned short i=0; i<code2char_.size(); ++i)
     74      char2code_[static_cast<size_t>(code2char_[i])] = i;
    9675  }
    9776
     
    133112    return code2char_[code_];
    134113  }
    135    
     114
    136115
    137116  bool operator==(const DNA& lhs, const DNA& rhs)
     
    175154  {
    176155    init();
    177     std::map<char, unsigned short>::const_iterator i = char2code_.find(c);
    178     if (i==char2code_.end()) {
    179       std::string msg("DNA assignment from '");
     156    code_ = char2code_[static_cast<size_t>(c)];
     157    if (code_==255) {
     158      std::string msg("DNA('");
    180159      msg += c;
    181       msg += "'";
     160      msg += "')";
    182161      throw std::invalid_argument(msg);
    183162    }
    184     code_ = i->second;
    185163    assert(code_<16);
    186164    return *this;
  • trunk/yat/omic/DNA.h

    r2677 r2741  
    55  $Id$
    66
    7   Copyright (C) 2010, 2011 Peter Johansson
     7  Copyright (C) 2010, 2011, 2012 Peter Johansson
    88
    99  This file is part of the yat library, http://dev.thep.lu.se/yat
     
    2626
    2727#include <iosfwd>
    28 #include <map>
    2928#include <string>
    3029#include <vector>
     
    139138  private:
    140139    unsigned short code_;
    141     static std::map<char, unsigned short> char2code_;
    142     static std::vector<char> code2char_;
     140    static std::vector<unsigned short> char2code_;
     141    static std::string code2char_;
    143142
    144143    void init(void) const;
Note: See TracChangeset for help on using the changeset viewer.