Skip to content

Commit 618ea50

Browse files
committed
Fix Coala IO
1 parent daef62a commit 618ea50

6 files changed

Lines changed: 65 additions & 15 deletions

File tree

src/Bpp/Phyl/Io/BppOFrequencySetFormat.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -530,12 +530,12 @@ std::unique_ptr<FrequencySetInterface> BppOFrequencySetFormat::readFrequencySet(
530530
if (dynamic_cast<Coala*>(model.get()) == 0)
531531
throw Exception("MVAprotein frequency set needs a Coala model");
532532

533-
auto coala = unique_ptr<Coala>(dynamic_cast<Coala*>(model.release()));
533+
auto coala = shared_ptr<Coala>(dynamic_cast<Coala*>(model.release()));
534534
// map<string, string> unparsedParameterValuesNested(nestedReader.getUnparsedArguments());
535535

536536
auto mvaFS = make_unique<MvaFrequencySet>(dynamic_pointer_cast<const ProteicAlphabet>(alphabet));
537537
// mvaFS->setParamValues(args);
538-
mvaFS->initSet(*coala);
538+
mvaFS->initSet(coala);
539539

540540
pFS = std::move(mvaFS);
541541
}
@@ -724,6 +724,24 @@ void BppOFrequencySetFormat::writeFrequencySet(
724724
catch (bad_cast&)
725725
{}
726726

727+
// MV
728+
729+
try
730+
{
731+
auto pmvaFS = dynamic_cast<const MvaFrequencySet&>(freqset);
732+
if (comma)
733+
out << ", ";
734+
out << "model=";
735+
736+
BppOSubstitutionModelFormat bIO(BppOSubstitutionModelFormat::ALL, true, true, true, false, 0);
737+
738+
bIO.write(*(pmvaFS.getModel()), out, globalAliases, writtenNames);
739+
comma = true;
740+
}
741+
catch (bad_cast&)
742+
{}
743+
744+
727745

728746
// FullPerAA
729747
try

src/Bpp/Phyl/Io/BppOSubstitutionModelFormat.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -856,9 +856,15 @@ unique_ptr<SubstitutionModelInterface> BppOSubstitutionModelFormat::readSubstitu
856856
if (TextTools::isEmpty(nbrOfParametersPerBranch))
857857
throw Exception("'nbrAxes' argument missing to define the number of axis of the Correspondence Analysis.");
858858
// Now we create the Coala model
859-
model = make_unique<Coala>(alpha, *nestedModel, TextTools::to<unsigned int>(nbrOfParametersPerBranch));
860-
if (nData)
861-
model->setFreqFromData(*mData.at(nData));
859+
auto coala = make_unique<Coala>(alpha, *nestedModel, TextTools::to<unsigned int>(nbrOfParametersPerBranch));
860+
if (!nData)
861+
throw Exception("'data' argument missing to build the Correspondence Analysis.");
862+
else
863+
{
864+
coala->setNData(nData);
865+
coala->setFreqFromData(*mData.at(nData));
866+
}
867+
model = unique_ptr<SubstitutionModelInterface>(dynamic_cast<SubstitutionModelInterface*>(coala.release()));
862868
}
863869
else
864870
throw Exception("Model '" + modelName + "' is unknown, or does not fit proteic alphabet.");
@@ -1698,6 +1704,8 @@ void BppOSubstitutionModelFormat::write(const BranchModelInterface& model,
16981704
{
16991705
const Coala& coalaModel = dynamic_cast<const Coala&>(model);
17001706
out << "exch=" << coalaModel.getExch() << ", nbrAxes=" << coalaModel.getNbrOfAxes();
1707+
if (coalaModel.getNData() > 0)
1708+
out << ", data=" << coalaModel.getNData();
17011709
comma = true;
17021710
}
17031711
catch (bad_cast&)

src/Bpp/Phyl/Legacy/App/PhylogeneticsApplicationTools.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ void LegacyPhylogeneticsApplicationTools::setSubstitutionModelSet(
543543
{
544544
auto tmp2 = dynamic_pointer_cast<Coala>(tmp);
545545
if (tmp2)
546-
dynamic_pointer_cast<MvaFrequencySet>(rootFrequencies)->initSet(*tmp2);
546+
dynamic_pointer_cast<MvaFrequencySet>(rootFrequencies)->initSet(tmp2);
547547
else
548548
throw Exception("The MVAprotein frequencies set at the root can only be used if a Coala model is used on branches.");
549549
}

src/Bpp/Phyl/Model/FrequencySet/MvaFrequencySet.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,21 @@ MvaFrequencySet::MvaFrequencySet(shared_ptr<const ProteicAlphabet> alpha) :
1414
make_shared<CanonicalStateMap>(alpha, false),
1515
"MVA.",
1616
"MVAprotein"),
17+
model_(),
1718
tPpalAxes_(),
1819
rowCoords_(),
1920
nbrOfAxes_(0),
20-
model_(),
2121
columnWeights_(),
2222
paramValues_()
2323
{}
2424

25-
void MvaFrequencySet::initSet(const CoalaCore& coala)
25+
void MvaFrequencySet::initSet(std::shared_ptr<const Coala> coala)
2626
{
27-
setNbrOfAxes(coala.getNbrOfAxes());
28-
setTransposeMatrixOfPpalAxes(coala.getTppalAxesMatrix());
29-
setMatrixOfRowCoords(coala.getRowCoordinates());
30-
setVectorOfColumnWeights(coala.getColumnWeights());
27+
model_=coala;
28+
setNbrOfAxes(coala->getNbrOfAxes());
29+
setTransposeMatrixOfPpalAxes(coala->getTppalAxesMatrix());
30+
setMatrixOfRowCoords(coala->getRowCoordinates());
31+
setVectorOfColumnWeights(coala->getColumnWeights());
3132
defineParameters();
3233
updateFrequencies();
3334
}

src/Bpp/Phyl/Model/FrequencySet/MvaFrequencySet.h

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ class MvaFrequencySet :
2222
public virtual ProteinFrequencySetInterface,
2323
public AbstractFrequencySet
2424
{
25+
private:
26+
std::shared_ptr<const Coala> model_;
27+
2528
public:
2629
/**
2730
* @brief Constructor
@@ -48,7 +51,6 @@ class MvaFrequencySet :
4851
RowMatrix<double> tPpalAxes_;
4952
RowMatrix<double> rowCoords_;
5053
size_t nbrOfAxes_;
51-
std::string model_;
5254
std::vector<double> columnWeights_;
5355
std::map<std::string, std::string> paramValues_;
5456

@@ -58,10 +60,20 @@ class MvaFrequencySet :
5860
return std::dynamic_pointer_cast<const ProteicAlphabet>(getAlphabet());
5961
}
6062

63+
const Coala& model() const
64+
{
65+
return *model_;
66+
}
67+
68+
std::shared_ptr<const Coala> getModel() const
69+
{
70+
return model_;
71+
}
72+
6173
void setTransposeMatrixOfPpalAxes(const RowMatrix<double>& matrix) { tPpalAxes_ = matrix; }
6274
void setMatrixOfRowCoords(const RowMatrix<double>& matrix) { rowCoords_ = matrix; }
6375
void setNbrOfAxes(const size_t& nAxes) { nbrOfAxes_ = nAxes; }
64-
void setModelName(const std::string& modelName) { model_ = modelName; }
76+
// void setModelName(const std::string& modelName) { model_ = modelName; }
6577
void setVectorOfColumnWeights(const std::vector<double>& cw) { columnWeights_ = cw; }
6678
void setParamValues(std::map<std::string, std::string>& valuesSettings) {paramValues_ = valuesSettings;}
6779

@@ -71,7 +83,7 @@ class MvaFrequencySet :
7183
void fireParameterChanged(const ParameterList& parameters) override;
7284
void updateFrequencies();
7385

74-
void initSet(const CoalaCore& coala);
86+
void initSet(std::shared_ptr<const Coala> coala);
7587

7688
void computeReversePCA(const std::vector<double>& positions, std::vector<double>& tmpFreqs);
7789
void computeCoordsFirstSpacePCA(std::vector<double>& tmpFreqs, std::vector<double>& freqs);

src/Bpp/Phyl/Model/Protein/CoalaCore.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class CoalaCore
3838
std::vector<double> colWeights_;
3939
ParameterList paramValues_;
4040

41+
size_t nData_; // number of the used data (for output consistency)
4142
public:
4243
CoalaCore(size_t nbAxes = 0);
4344

@@ -51,6 +52,16 @@ class CoalaCore
5152
const RowMatrix<double>& getRowCoordinates() const { return R_; }
5253
const std::vector<double>& getColumnWeights() const { return colWeights_; }
5354

55+
size_t getNData() const
56+
{
57+
return nData_;
58+
}
59+
60+
void setNData(size_t n)
61+
{
62+
nData_=n;
63+
}
64+
5465
protected:
5566
/**
5667
* @brief Comoute COA from data and return the axis position parameters

0 commit comments

Comments
 (0)