/** * tree - get the coalescent * * @param N - sample size * @param theta - mutation rate * @param get_newick - get newick representation * * @param Tb - time for population "bottleneck" (can also model * expansion; see parameter f) * * @param f - fraction of population size at Tb compared to current * size N_0; in other words, N_b = f * N_0. When f>1 a bottleneck is * modelled, when f<1 an expansion * * @return SEXP vector containing TMRCA, TBL, S_n and possibly newick * representation of tree * */ SEXP tree(SEXP N, SEXP theta, SEXP get_newick, SEXP Tb, SEXP f) { SEXP ans, NEWICK; SEXP TMRCA = PROTECT(allocVector(REALSXP, 1)); SEXP TBL = PROTECT(allocVector(REALSXP, 1)); SEXP NMUT = PROTECT(allocVector(INTSXP, 1)); struct Node *n; n = coalescent_tree(asInteger(N), asReal(theta), asReal(Tb), asReal(f)); char *newick_ptr = "NA"; if ((boolean) asInteger(get_newick)) newick_ptr = newick(n); PROTECT(NEWICK = allocVector(STRSXP, 1)); SET_STRING_ELT(NEWICK, 0, mkChar(newick_ptr)); ans = PROTECT(allocVector(VECSXP, 4)); NMUT = ScalarInteger(segregating_sites(n)); TMRCA = ScalarReal(tmrca(n)); TBL = ScalarReal(total_branch_length(n)); SET_VECTOR_ELT(ans, 0, TMRCA); SET_VECTOR_ELT(ans, 1, TBL); SET_VECTOR_ELT(ans, 2, NMUT); SET_VECTOR_ELT(ans, 3, NEWICK); UNPROTECT(5); // Clean up memory remove_tree(n); return (ans); }
string newick(int i,int n,int* & P,int* & Suc1,int* & Suc2,string* & Labels,double* & B){ ostringstream b; b<<B[i]; if (i>=n) return Labels[i]+":"+b.str(); else{ int s1=Suc1[i]; int s2=Suc2[i]; string l1=newick(s1,n,P,Suc1,Suc2,Labels,B); string l2=newick(s2,n,P,Suc1,Suc2,Labels,B); if (P[i]!=-1){ return "("+l1+","+l2+")"+Labels[i]+":"+b.str(); } else{ return "("+l1+","+l2+")"+Labels[i]+";\n"; } } }
/** * newick - get newick string representation for a node * * @param n - node for which newick representation is desired * * @return recursively generate newick string representation of node * and its descendants */ char *newick(struct Node *n) { char *retval; if (isleaf(n)) { retval = malloc (sizeof (char) * NEWICK_BUFSIZE); snprintf(retval, NEWICK_BUFSIZE, "%i:%.4f", n->id, n->parent->time - n->time); } else { char *left, *right; left = newick(n->left); right = newick(n->right); int BUFSIZE = strlen(left) + strlen(right) + 12; retval = malloc (sizeof (char) * BUFSIZE); if (isroot(n)) snprintf(retval, BUFSIZE, "(%s,%s):%.i;", left, right, n->id); else snprintf(retval, BUFSIZE, "(%s,%s):%.4f", left, right, n->parent->time - n->time); free(left); free(right); } return retval; }
double ribi::NewickVector::CalculateProbability( const std::string& newick_str, const double theta) { assert(Newick().IsNewick(newick_str)); assert(theta > 0.0); NewickVector newick(newick_str); NewickStorage<NewickVector> storage(newick); return CalculateProbabilityInternal( newick, theta, storage); }
double ribi::BinaryNewickVector::CalculateProbability( const std::string& newick_str, const double theta ) noexcept { assert(Newick().IsNewick(newick_str)); assert(Newick().IsUnaryNewick(Newick().StringToNewick(newick_str)) || Newick().IsBinaryNewick(Newick().StringToNewick(newick_str))); assert(theta > 0.0); BinaryNewickVector newick(newick_str); NewickStorage<BinaryNewickVector> storage(newick); return CalculateProbabilityInternal( newick, theta, storage); }
string newick(int i,int terminate,Pr* pr,Node** nodes){; ostringstream b; if (i>0){ b<<nodes[i]->B; } if (i>=pr->nbINodes) return nodes[i]->L+":"+b.str(); else{ string newLabel="("; for (vector<int>::iterator iter=nodes[i]->suc.begin(); iter!=nodes[i]->suc.end(); iter++) { int s = *iter; string l=newick(s,terminate,pr,nodes); if (iter==nodes[i]->suc.begin()) newLabel+=l; else newLabel+=","+l; } if (i!=terminate) { return newLabel+")"+nodes[i]->L+":"+b.str(); } else{ return newLabel+")"+nodes[i]->L+";\n"; } } }