geniomhe-oop-dnapp/include/Sequence.hpp

93 lines
1.9 KiB
C++
Raw Normal View History

2023-10-01 12:26:59 +02:00
#pragma once
#include <vector>
#include <fstream>
#include <sstream>
#include <stdexcept>
#include <string>
#include <unordered_map>
#include "GeneticCode.hpp"
#include "strutils.hpp"
class Sequence{
public:
Sequence(std::string sequence) {
this->sequence = sequence;
}
unsigned int get_length() {
return this->sequence.length();
}
std::string get_sequence() {
return this->sequence;
}
protected:
std::string sequence;
};
class ProteinSequence: public Sequence {
using Sequence::Sequence;
};
class RNASequence: public Sequence {
using Sequence::Sequence;
public:
ProteinSequence translate_to_protein() {
GeneticCode *code = GeneticCode::GetInstance();
std::string protein_sequence = "";
for (unsigned int i=0; i < this->sequence.length()-2; i+=3) {
std::string codon = this->sequence.substr(i, 3);
char amino_acid = code->get_amino_acid(codon);
protein_sequence += amino_acid;
}
return ProteinSequence(protein_sequence);
}
};
class DNASequence: public Sequence {
using Sequence::Sequence;
public:
DNASequence get_complement() {
std::string complement = "";
for (char &c: this->sequence) {
complement += DNASequence::complement(c);
}
return DNASequence(complement);
}
static char complement(char c) {
if (c == 'A') {
return 'T';
} else if (c == 'T') {
return 'A';
} else if (c == 'G') {
return 'C';
} else if (c == 'C') {
return 'G';
} else {
throw std::invalid_argument("Error: nucleotide " + std::to_string(c) + " has no complement");
}
}
RNASequence transcribe_to_rna() {
std::string rna_sequence = this->sequence.substr(0);
replace_all(rna_sequence, 'T', 'U');
return RNASequence(rna_sequence);
}
};