DNA-sim

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

commit 22ebcdd38213098b9a8f59d51949b0acf3b83e68
parent 6d2b31be53f26d517d2c62f6b84313f135dde612
Author: Cody Lewis <luxdotsugi@gmail.com>
Date:   Sat, 14 Jul 2018 00:38:09 +1000

Got the statistics and input working

Diffstat:
DCodon.cpp | 80-------------------------------------------------------------------------------
DCodon.h | 38--------------------------------------
Asrc/Codon.cpp | 45+++++++++++++++++++++++++++++++++++++++++++++
Asrc/Codon.h | 37+++++++++++++++++++++++++++++++++++++
Asrc/Strand.cpp | 53+++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/Strand.h | 32++++++++++++++++++++++++++++++++
Asrc/main.cpp | 72++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/makefile | 17+++++++++++++++++
8 files changed, 256 insertions(+), 118 deletions(-)

diff --git a/Codon.cpp b/Codon.cpp @@ -1,80 +0,0 @@ -/** - * Codon.cpp - DNA_sim - * Author: Cody Lewis - * Date: 2018-07-07 - * Last Modified: 2018-07-07 - * Description: - * A Class for A codon (a collection of three mRNA bases) - */ -#include <cstdlib> -#include "Codon.h" -namespace DNA_sim { - // Constructors - Codon::Codon() { - bases.resize(3); - } - Codon::Codon(std::string input) { - Codon(); - set_bases(input); - } - // Destructor - Codon::~Codon() {} - // Queries - std::string Codon::get_bases() const { - std::string base_string; - for(std::size_t i = 0; i < bases.size(); ++i) { - switch (bases[i]) { - case Base::adenine: - base_string += 'A'; - break; - case Base::uracil: - base_string += 'U'; - break; - case Base::cytosine: - base_string += 'C'; - break; - case Base::guanine: - base_string += 'G'; - break; - default: - base_string += '_'; - } - } - return base_string; - } - // Mutators - bool Codon::set_bases(std::string input) { - int j = 0; - for(std::string::iterator it = input.begin(); it != input.end(); ++it) { - switch (*it) { - case 'A': - bases[j] = Base::adenine; - break; - case 'U': - bases[j] = Base::uracil; - break; - case 'C': - bases[j] = Base::cytosine; - break; - case 'G': - bases[j] = Base::guanine; - break; - default: - bases[j] = Base::adenine; - } - j++; - } - return true; - } - // Operator overloads - std::istream& operator>>(std::istream& in, Codon& cod) { - std::string input; - in >> input; - cod.set_bases(input); - return in; - } - std::ostream& operator<<(std::ostream& out, const Codon& cod) { - out << cod.get_bases(); - return out; - } -} diff --git a/Codon.h b/Codon.h @@ -1,38 +0,0 @@ -/** - * Codon.h - DNA_sim - * Author: Cody Lewis - * Date: 2018-07-07 - * Last Modified: 2018-07-07 - * Description: - * A Class for A codon (a collection of three mRNA bases) - */ -#ifndef CODON_h -#define CODON_h -#include <string> -#include <vector> -#include <iostream> -namespace DNA_sim { - class Codon { - private: - /* enum Base - * The invidual bases of mRNA - */ - enum Base { adenine, uracil, cytosine, guanine }; - std::vector<Base> bases; // 3 bases per codon - public: - Codon(); // Default constructor - Codon(std::string input); // Input constructor - ~Codon(); // Destructor - - // Queries - std::string get_bases() const; - - // Mutators - bool set_bases(std::string input); - // TODO: Add crossover - }; - // Operator Overloads - std::istream& operator>>(std::istream& in, Codon& cod); // input - std::ostream& operator<<(std::ostream& out, const Codon& cod); // output -} -#endif /* ifndef CODON_h */ diff --git a/src/Codon.cpp b/src/Codon.cpp @@ -0,0 +1,45 @@ +/** + * Codon.cpp - DNA_sim + * Author: Cody Lewis + * Date: 2018-07-07 + * Last Modified: 2018-07-07 + * Description: + * A Class for A codon (a collection of three mRNA bases) + */ +#include <cstdlib> +#include "Codon.h" +namespace DNA_sim { + // Constructors + Codon::Codon() { + bases.resize(3); + } + Codon::Codon(std::string input) { + Codon(); + set_bases(input); + } + // Destructor + Codon::~Codon() {} + // Queries + std::string Codon::get_bases() const { + return bases; + } + // Mutators + bool Codon::set_bases(std::string input) { + bases = input; + return true; + } + // Operator overloads + std::istream& operator>>(std::istream& in, Codon& cod) { + std::string input; + in >> input; + cod.set_bases(input); + return in; + } + std::ostream& operator<<(std::ostream& out, const Codon& cod) { + out << cod.get_bases(); + return out; + } + bool operator<(const Codon& lhs, const Codon& rhs) { + return lhs.get_bases() < rhs.get_bases(); + } +} diff --git a/src/Codon.h b/src/Codon.h @@ -0,0 +1,37 @@ +/** + * Codon.h - DNA_sim + * Author: Cody Lewis + * Date: 2018-07-07 + * Last Modified: 2018-07-07 + * Description: + * A Class for A codon (a collection of three mRNA bases) + */ +#ifndef CODON_h +#define CODON_h +#include <string> +#include <vector> +#include <iostream> +namespace DNA_sim { + class Codon { + private: + /* enum Base + * The invidual bases of mRNA + */ + std::string bases; // 3 bases per codon + public: + Codon(); // Default constructor + Codon(std::string input); // Input constructor + ~Codon(); // Destructor + + // Queries + std::string get_bases() const; + + // Mutators + bool set_bases(std::string input); + }; + // Operator Overloads + std::istream& operator>>(std::istream& in, Codon& cod); // input + std::ostream& operator<<(std::ostream& out, const Codon& cod); // output + bool operator<(const Codon& lhs, const Codon& rhs); +} +#endif /* ifndef CODON_h */ diff --git a/src/Strand.cpp b/src/Strand.cpp @@ -0,0 +1,53 @@ +#include <cstdlib> +#include <map> +#include <sstream> +#include "Strand.h" +namespace DNA_sim { + Strand::Strand() {} + Strand::Strand(std::string input) { + set_strand(input); + } + Strand::~Strand() {} + std::string Strand::stats() const { + std::map<Codon, int> codon_count; + for(std::size_t i = 0; i < codons.size(); ++i) { + if(codon_count.find(codons[i]) == codon_count.end()) { + codon_count[codons[i]] = 1; + } else { + codon_count[codons[i]]++; + } + } + std::string result_stats = "Codon:\tCount:\n"; + for(std::map<Codon, int>::iterator it = codon_count.begin(); it != codon_count.end(); ++it) { + std::stringstream ss; + ss << it->second; + ss >> result_stats; + result_stats = it->first.get_bases() + "\t" + result_stats; + result_stats += "\n"; + } + return result_stats; + } + std::string Strand::get_strand() const { + std::string strand_str = ""; + for(std::size_t i = 0; i < codons.size(); ++i) { + strand_str += codons[i].get_bases(); + } + return strand_str; + } + bool Strand::set_strand(std::string input) { + for(std::size_t i = 0; i < input.length(); i += 3) { + codons.push_back(Codon(input.substr(i, 3))); + } + return true; + } + std::istream& operator>>(std::istream& in, Strand& str) { + std::string input; + in >> input; + str.set_strand(input); + return in; + } + std::ostream& operator<<(std::ostream& out, const Strand& str) { + out << str.get_strand(); + return out; + } +} diff --git a/src/Strand.h b/src/Strand.h @@ -0,0 +1,32 @@ +/** + * Strand.h - DNA_sim + * Author: Cody Lewis + * Date: 2018-07-13 + * Description: + * A class for a strand of DNA (A collection of codons) + */ +#ifndef STRAND_h +#define STRAND_h +#include <vector> +#include <iostream> +#include "Codon.h" +namespace DNA_sim { + class Strand { + private: + std::vector<Codon> codons; // The Codon collection + public: + Strand(); + Strand(std::string input); // input constructor + ~Strand(); // Destructor + + // Queries + std::string stats() const; + std::string get_strand() const; + + // Mutators + bool set_strand(std::string input); + }; + std::istream& operator>>(std::istream& in, Strand& str); + std::ostream& operator<<(std::ostream& out, const Strand& str); +} +#endif /* ifndef STRANG_h */ diff --git a/src/main.cpp b/src/main.cpp @@ -0,0 +1,72 @@ +/** + * main.cpp - DNA_sim + * Author: Cody Lewis + * Description: + * The UI for the DNA_sim + */ +#include <iostream> +#include <cstdlib> +#include <fstream> +#include <vector> +#include "Strand.h" +void add_strand(std::vector<DNA_sim::Strand>& strands); +void view_stats(std::vector<DNA_sim::Strand>& strands); +void view_strands(std::vector<DNA_sim::Strand>& strands); +int main() { + int input; + std::vector<DNA_sim::Strand> strands; + bool exit = false; + while(!exit) { + std::cout << "Add Strand(1), View strand stats(2), View strands(3), Exit(9)" << std::endl; + std::cin >> input; + switch(input) { + case 1: + add_strand(strands); + break; + case 2: + view_stats(strands); + break; + case 3: + view_strands(strands); + break; + case 9: + exit = true; + break; + default: + std::cout << "Invalid input, please try again." << std::endl; + } + } + return 0; +} +void add_strand(std::vector<DNA_sim::Strand>& strands) { + std::string input; + std::cout << "Do you want to use a file (y/N) "; + std::cin >> input; + if(input == "y" || input == "Y") { + std::cout << "Specify a file name: "; + input.clear(); + std::cin >> input; + std::ifstream in_data; + in_data.open(input.c_str()); + input.clear(); + in_data >> input; // takes a single line + strands.push_back(DNA_sim::Strand(input)); + } else { + std::cout << "Input your strand: "; + input.clear(); + std::cin >> input; + } + strands.push_back(DNA_sim::Strand(input)); +} +void view_stats(std::vector<DNA_sim::Strand>& strands) { + for(std::size_t i = 0; i < strands.size(); ++i) { + std::cout << "Strand " << i << ":" << std::endl; + std::cout << strands[i].stats() << std::endl; + } +} +void view_strands(std::vector<DNA_sim::Strand>& strands) { + for(std::size_t i = 0; i < strands.size(); ++i) { + std::cout << "Strand " << i << ":" << std::endl; + std::cout << strands[i] << std::endl; + } +} diff --git a/src/makefile b/src/makefile @@ -0,0 +1,17 @@ +CC=g++ +CFLAGS=-c -g -Wall +LDFLAGS= +SOURCES=Codon.cpp Strand.cpp main.cpp +OBJECTS=$(SOURCES:.cpp=.o) +EXECUTABLE=DNA_sim + +all: $(SOURCES) $(EXECUTABLE) + +$(EXECUTABLE): $(OBJECTS) + $(CC) $(LDFLAGS) $(OBJECTS) -o $@ + +%.o : %.cpp + $(CC) $(CFLAGS) -c $< + +clean: + rm -rf *.o core