Skip to content

Commit

Permalink
added a bunch of failure guards around pll functions
Browse files Browse the repository at this point in the history
  • Loading branch information
pierrebarbera committed Jun 24, 2020
1 parent d5bdc53 commit d33919d
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 39 deletions.
18 changes: 11 additions & 7 deletions src/core/pll/epa_pll_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ void link_tree_msa( pll_utree_t * tree,

auto clv_index = map_value->second;
// associate the sequence with the tip by calculating the tips clv buffers
if ( not pll_set_tip_states(partition, clv_index, model.charmap(), s.sequence().c_str()) ) {
if( not pll_set_tip_states( partition, clv_index, model.charmap(), s.sequence().c_str() ) ) {
LOG_ERR << "Setting tip states failed for sequence: " << s.header();
LOG_ERR << "message: " << pll_errmsg;
throw std::invalid_argument{"Bad sequence (see errors above)"};
throw std::invalid_argument { "Bad sequence (see errors above)" };
}

// clear this ref taxon from the map
Expand Down Expand Up @@ -104,11 +104,15 @@ void precompute_clvs( pll_utree_t const * const tree,
&num_matrices,
&num_ops);

pll_update_prob_matrices(partition,
&param_indices[0], // use model 0
&matrix_indices[0],// matrices to update
&branch_lengths[0],
num_matrices); // how many should be updated
if( not pll_update_prob_matrices(
partition,
&param_indices[ 0 ], // use model 0
&matrix_indices[ 0 ], // matrices to update
&branch_lengths[ 0 ],
num_matrices ) // how many should be updated
) {
throw std::runtime_error { std::string( pll_errmsg ) };
}

/* use the operations array to compute all num_ops inner CLVs. Operations
will be carried out sequentially starting from operation 0 towrds num_ops-1 */
Expand Down
14 changes: 10 additions & 4 deletions src/core/pll/optimize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -603,15 +603,21 @@ void optimize(raxml::Model& model,
}
}

void compute_and_set_empirical_frequencies(pll_partition_t * partition, raxml::Model& model)
void compute_and_set_empirical_frequencies( pll_partition_t* partition, raxml::Model& model )
{
auto empirical_freqs = pllmod_msa_empirical_frequencies( partition );

if( not empirical_freqs ) {
throw std::runtime_error { std::string( pll_errmsg ) };
}

pll_set_frequencies( partition, 0, empirical_freqs );

for ( size_t i = 0; i < model.num_submodels(); ++i ) {
model.base_freqs(i, std::vector<double>(partition->frequencies[i],
partition->frequencies[i] + partition->states));
for( size_t i = 0; i < model.num_submodels(); ++i ) {
model.base_freqs( i,
std::vector< double >( partition->frequencies[ i ],
partition->frequencies[ i ] + partition->states )
);
}

free( empirical_freqs );
Expand Down
26 changes: 16 additions & 10 deletions src/core/pll/pll_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -405,11 +405,14 @@ void reset_triplet_lengths( pll_unode_t * toward_pendant,
double branch_lengths[3] = {half_original, half_original, DEFAULT_BRANCH_LENGTH};
unsigned int matrix_indices[3] = {0, 1, 2};
std::vector<unsigned int> param_indices(partition->rate_cats, 0);
pll_update_prob_matrices( partition,
&param_indices[0],
matrix_indices,
branch_lengths,
3);

if( not pll_update_prob_matrices( partition,
&param_indices[ 0 ],
matrix_indices,
branch_lengths,
3 ) ) {
throw std::runtime_error { std::string( pll_errmsg ) };
}
}
}

Expand Down Expand Up @@ -511,11 +514,14 @@ pll_utree_t* make_utree_struct(pll_unode_t * root, const unsigned int num_nodes)


unsigned int inner_count = 0;
pll_utree_traverse( root,
PLL_TREE_TRAVERSE_POSTORDER,
cb_trav_no_tips,
nodes + tip_count,
&inner_count);

if( not pll_utree_traverse( root,
PLL_TREE_TRAVERSE_POSTORDER,
cb_trav_no_tips,
nodes + tip_count,
&inner_count ) ) {
throw std::runtime_error { std::string( pll_errmsg ) };
}

assert(num_nodes == tip_count + inner_count);

Expand Down
32 changes: 19 additions & 13 deletions src/tree/Tiny_Tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,18 @@ static void precompute_sites_static(char nt,
};
}

pll_compute_edge_loglikelihood( partition,
new_tip->clv_index,
PLL_SCALE_BUFFER_NONE,
inner->clv_index,
inner->scaler_index,
inner->pmatrix_index,
&param_indices[0],
&result[0]);
auto logl = pll_compute_edge_loglikelihood( partition,
new_tip->clv_index,
PLL_SCALE_BUFFER_NONE,
inner->clv_index,
inner->scaler_index,
inner->pmatrix_index,
&param_indices[ 0 ],
&result[ 0 ] );

if( logl == -std::numeric_limits< double >::infinity() ) {
throw std::runtime_error { "Tree Log-Likelihood -INF!" };
}
}


Expand Down Expand Up @@ -121,11 +125,13 @@ Tiny_Tree::Tiny_Tree( pll_unode_t * edge_node,

// use branch lengths to compute the probability matrices
std::vector<unsigned int> param_indices(reference_tree.partition()->rate_cats, 0);
pll_update_prob_matrices( partition_.get(),
&param_indices[0],
matrix_indices,
branch_lengths,
3);
if( not pll_update_prob_matrices( partition_.get(),
&param_indices[ 0 ],
matrix_indices,
branch_lengths,
3 ) ) {
throw std::runtime_error { std::string( pll_errmsg ) };
}

// use update_partials to compute the clv pointing toward the new tip
pll_update_partials(partition_.get(), &op, 1);
Expand Down
4 changes: 0 additions & 4 deletions src/tree/Tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,5 @@ double Tree::ref_tree_logl()
&param_indices[0],
nullptr);

// if ( logl == -std::numeric_limits<double>::infinity() ) {
// throw std::runtime_error{pll_errmsg};
// }

return logl;
}
4 changes: 3 additions & 1 deletion src/tree/tiny_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@ pll_partition_t * make_tiny_partition(Tree& reference_tree,
3, // number of scale buffers (one per possible inner node)
old_partition->attributes);

assert(tiny);
if( not tiny ) {
throw std::runtime_error { std::string( pll_errmsg ) };
}

unsigned int i;
free(tiny->rates);
Expand Down

0 comments on commit d33919d

Please sign in to comment.