Files
DEC/individual/genome/Genotype.cpp
2024-10-03 18:43:04 +07:00

120 lines
4.1 KiB
C++

#include "AbstractGenome.h"
#include <iostream>
#include <sstream>
#include <cmath>
//Genotype::Genotype(const Genotype& _fDiplGenome, const Genotype& _mDiplGenome){
//}
HaploidGenotype Genotype::recombinantHaploidGenotype(ChromosomeRearrangementStrategy* strategy) const {
return strategy->buildRecombinantGenotype(this);
}
std::ostream& operator<< (std::ostream& os, const Genotype& g){
os<<"F";
for(unsigned int i = 0; i < g.fatherGenome.chromosomes.size(); i++){
os<<"ch"<<(i+1)<<":";
const std::vector<Gene> genesF = g.fatherGenome.chromosomes.at(i).getGenesAsVector();
const std::vector<Gene> genesM = g.motherGenome.chromosomes.at(i).getGenesAsVector();
for(unsigned int j = 0; j < genesF.size(); j++){
os<<"\t";
os<<genesF.at(j).getName();//<<", ";
if(genesF.at(j).getGeneType() == Gene::Continious){
os<<"\t"<<genesF.at(j).getGeneValueCont();//<<"\t";
os<<"\t"<<genesM.at(j).getGeneValueCont();//<<"\t";
}
else{
os<<"\t"<<genesF.at(j).getGeneValueDiscr();//<<"\t";
os<<"\t"<<genesM.at(j).getGeneValueDiscr();//<<"\t";
}
os<<"\t";
}
}
return os;
}
std::string Genotype::toSimpleString() const {
std::stringstream os;
for(unsigned int i = 0; i < this->fatherGenome.chromosomes.size(); i++){
const std::vector<Gene> genesF = this->fatherGenome.chromosomes.at(i).getGenesAsVector();
const std::vector<Gene> genesM = this->motherGenome.chromosomes.at(i).getGenesAsVector();
for(unsigned int j = 0; j < genesF.size(); j++){
if(genesF.at(j).getGeneType() == Gene::Continious){
os<<genesF.at(j).getGeneValueCont()<<"\t";
os<<genesM.at(j).getGeneValueCont()<<"\t";
}
else{
os<<genesF.at(j).getGeneValueDiscr()<<"\t";
os<<genesM.at(j).getGeneValueDiscr()<<"\t";
}
//os<<"\t";
}
}
return os.str();
}
std::string Genotype::toMaxModuleAB() const {
std::stringstream os;
for(unsigned int i = 0; i < this->fatherGenome.chromosomes.size(); i++){
const std::vector<Gene> genesF = this->fatherGenome.chromosomes.at(i).getGenesAsVector();
const std::vector<Gene> genesM = this->motherGenome.chromosomes.at(i).getGenesAsVector();
for(unsigned int j = 0; j < genesF.size(); j++){
if(genesF.at(j).getGeneType() == Gene::Continious){
if(fabs(genesF.at(j).getGeneValueCont()) > fabs(genesM.at(j).getGeneValueCont())){
os<<genesF.at(j).getGeneValueCont()<<"\t";
}
else{
os<<genesM.at(j).getGeneValueCont()<<"\t";
}
}
else{
os<<genesF.at(j).getGeneValueDiscr()<<"\t";
os<<genesM.at(j).getGeneValueDiscr()<<"\t";
}
//os<<"\t";
}
}
return os.str();
}
std::string Genotype::getRawGene(unsigned int fatherMother, unsigned int chromoNum, unsigned int geneNum) const{
const HaploidGenotype* genome = (fatherMother == 0) ? &(this->fatherGenome) : &(this->motherGenome);
return genome->getChromosome(chromoNum).getGenesAsVector().at(geneNum).getSequence();
}
void Genotype::doRawMutationSequence(unsigned int fatherMother, unsigned int chromoNum, unsigned int geneNum, std::string newSeq){
if(fatherMother==2){
this->fatherGenome.chromosomes.at(chromoNum).genes.at(geneNum).setGeneValue(newSeq);
this->motherGenome.chromosomes.at(chromoNum).genes.at(geneNum).setGeneValue(newSeq);
}
}
std::string Genotype::toSimpleFasta(bool onlyMother) const {
std::stringstream os;
for(unsigned int i = 0; i < this->motherGenome.chromosomes.size(); i++){
const std::vector<Gene> genesF = this->fatherGenome.chromosomes.at(i).getGenesAsVector();
const std::vector<Gene> genesM = this->motherGenome.chromosomes.at(i).getGenesAsVector();
os<<"> "<<this->motherGenome.chromosomes.at(i).getName()<<"\n";
for(unsigned int j = 0; j < genesM.size(); j++){
if(genesM.at(i).getGeneType() == Gene::Sequence){
os<<genesM.at(j).getSequence();
}
else if(genesM.at(j).getGeneType() == Gene::Continious){
os<<genesF.at(j).getGeneValueCont()<<"\t";
os<<genesM.at(j).getGeneValueCont()<<"\t";
}
else{
os<<genesF.at(j).getGeneValueDiscr()<<"\t";
os<<genesM.at(j).getGeneValueDiscr()<<"\t";
}
//os<<"\t";
}
}
return os.str();
}