double E_rate(const vector<vector<double> >& P, int c,const substitution::MultiModel& smodel) { double R=0; for(int m=0;m<smodel.n_base_models();m++) R += P[c][m]*smodel.base_model(m).rate(); return R; }
void estimate_tree(const alignment& A, SequenceTree& T, substitution::MultiModel& smodel, const vector<int>& parameters) { //------- Estimate branch lengths -------------// log_branch_likelihood score2(A,smodel,T,parameters); // Initialize starting point optimize::Vector start(0.1, T.n_branches() + parameters.size()); for(int b=0;b<T.n_branches();b++) start[b] = log(T.branch(b).length()); for(int i=0;i<parameters.size();i++) start[i+T.n_branches()] = smodel.get_parameter_value_as<Double>( parameters[i] ); // optimize::Vector end = search_gradient(start,score); // optimize::Vector end = search_basis(start,score); optimize::Vector end = search_gradient(start,score2,1e-3); for(int b=0;b<T.n_branches();b++) T.branch(b).set_length(exp(end[b])); for(int i=0;i<parameters.size();i++) smodel.set_parameter_value(parameters[i], end[i+T.n_branches()]); smodel.set_rate(1); }
double getSimilarity(double t,substitution::MultiModel& SM) { double S = 0; for(int m=0;m<SM.n_base_models();m++) { // We can't use models with more than one part Matrix Q = SM.transition_p(t,0,m); double Sm = 0; for(int i=0;i<Q.size1();i++) Sm += SM.base_model(m).frequencies()[i]*Q(i,i); S += Sm * SM.distribution()[m]; } return S; }
void analyze_rates(const alignment& A,const SequenceTree& T, const substitution::MultiModel& smodel) { if (smodel.n_base_models() == 1) return; data_partition DP("DP",A,T,smodel); vector< vector<double> > rate_probs = substitution::get_model_probabilities_by_alignment_column(DP); vector<double> prior_bin_f = smodel.distribution(); vector<double> post_bin_f = get_post_rate_probs(rate_probs); show_rate_probs(cout,rate_probs,smodel); double prior_rate=0; double post_rate=0; for(int i=0;i<prior_bin_f.size();i++) { prior_rate += prior_bin_f[i]*smodel.base_model(i).rate(); post_rate += post_bin_f[i]*smodel.base_model(i).rate(); } for(int i=0;i<smodel.n_base_models();i++) cout<<" rate"<<i<<" = "<<smodel.base_model(i).rate(); cout<<endl<<endl; cout<<" Prior rate = "<<prior_rate<<endl;; for(int i=0;i<smodel.n_base_models();i++) cout<<" prior_bin_f"<<i<<" = "<<prior_bin_f[i]; cout<<endl<<endl; cout<<" Posterior rate = "<<post_rate<<endl;; for(int i=0;i<smodel.n_base_models();i++) cout<<" post_bin_f"<<i<<" = "<<post_bin_f[i]; cout<<endl<<endl; for(int i=0;i<smodel.n_base_models();i++) cout<<" odds_ratio"<<i<<" = "<<post_bin_f[i]/prior_bin_f[i]; cout<<endl<<endl; }
void show_frequencies(std::ostream& o,const substitution::MultiModel& MModel) { const alphabet& a = MModel.Alphabet(); if (MModel.n_base_models() == 1) { const valarray<double>& f = MModel.base_model(0).frequencies(); for(int i=0;i<a.size();i++) o<<"f"<<a.lookup(i)<<" = "<<f[i]<<"\n"; } else { for(int i=0;i<a.size();i++) { double total = 0; for(int m=0;m<MModel.n_base_models();m++) { const valarray<double>& f = MModel.base_model(m).frequencies(); o<<"f"<<a.lookup(i)<<m+1<<" = "<<f[i]<<" "; total += MModel.distribution()[m] * f[i]; } o<<"f"<<a.lookup(i)<<" = "<<total<<"\n"; } } }
Multi_Likelihood_Cache::Multi_Likelihood_Cache(const substitution::MultiModel& MM) :C(0), M(MM.n_base_models()), S(MM.n_states()) { }