DNA-sim

git clone git://git.codymlewis.com/DNA-sim.git
Log | Files | Refs | README | LICENSE

Strand.cpp (1499B)


      1 #include <cstdlib>
      2 #include <map>
      3 #include <sstream>
      4 #include "Strand.h"
      5 namespace DNA_sim {
      6   Strand::Strand() {}
      7   Strand::Strand(std::string input) {
      8     set_strand(input);
      9   }
     10   Strand::~Strand() {}
     11   std::string Strand::stats() const {
     12     std::map<Codon, int> codon_count;
     13     for(std::size_t i = 0; i < codons.size(); ++i) {
     14       if(codon_count.find(codons[i]) == codon_count.end()) {
     15         codon_count[codons[i]] = 1;
     16       } else {
     17         codon_count[codons[i]]++;
     18       }
     19     }
     20     std::string result_stats = "Codon:\tCount:\n";
     21     for(std::map<Codon, int>::iterator it = codon_count.begin(); it != codon_count.end(); ++it) {
     22       std::stringstream ss;
     23       ss << it->second;
     24       ss >> result_stats;
     25       result_stats = it->first.get_bases() + "\t" + result_stats;
     26       result_stats += "\n";
     27     }
     28     return result_stats;
     29   }
     30   std::string Strand::get_strand() const {
     31     std::string strand_str = "";
     32     for(std::size_t i = 0; i < codons.size(); ++i) {
     33       strand_str += codons[i].get_bases();
     34     }
     35     return strand_str;
     36   }
     37   bool Strand::set_strand(std::string input) {
     38     for(std::size_t i = 0; i < input.length(); i += 3) {
     39       codons.push_back(Codon(input.substr(i, 3)));
     40     }
     41     return true;
     42   }
     43   std::istream& operator>>(std::istream& in, Strand& str) {
     44     std::string input;
     45     in >> input;
     46     str.set_strand(input);
     47     return in;
     48   }
     49   std::ostream& operator<<(std::ostream& out, const Strand& str) {
     50     out << str.get_strand();
     51     return out;
     52   }
     53 }