3 /* Copyright 2006-2010 Emmanuel Paradis
5 /* This file is part of the R-package `ape'. */
6 /* See the file ../COPYING for licensing issues. */
10 #define DINDEX(i, j) n*(i - 1) - i*(i - 1)/2 + j - i - 1
11 /* works if i < j strictly, and i = 1, ...;
12 see give_index() below */
14 int give_index(int i, int j, int n)
16 if (i > j) return(DINDEX(j, i));
17 else return(DINDEX(i, j));
20 double sum_dist_to_i(int n, double *D, int i)
21 /* returns the sum of all distances D_ij between i and j
22 with j = 1...n and j != i */
24 /* we use the fact that the distances are arranged sequentially
25 in the lower triangle, e.g. with n = 6 the 15 distances are
26 stored as (the C indices are indicated):
37 so that we sum the values of the ith column--1st loop--and those of
38 (i - 1)th row (labelled 'i')--2nd loop */
44 /* the expression below CANNOT be factorized
45 because of the integer operations (it took
46 me a while to find out...) */
47 start = n*(i - 1) - i*(i - 1)/2;
49 for (j = start; j < end; j++) sum += D[j];
54 for (j = 1; j <= i - 1; j++) {
63 void nj(double *D, int *N, int *edge1, int *edge2, double *edge_length)
65 double *S, Sdist, Ndist, *new_dist, A, B, smallest_S, x, y;
66 int n, i, j, k, ij, smallest, OTU1, OTU2, cur_nod, o_l, *otu_label;
75 S = (double*)R_alloc(n + 1, sizeof(double));
76 new_dist = (double*)R_alloc(n*(n - 1)/2, sizeof(double));
77 otu_label = (int*)R_alloc(n + 1, sizeof(int));
79 for (i = 1; i <= n; i++) otu_label[i] = i; /* otu_label[0] is not used */
85 for (i = 1; i <= n; i++)
86 S[i] = sum_dist_to_i(n, D, i); /* S[0] is not used */
91 for (i = 1; i < n; i++) {
92 for (j = i + 1; j <= n; j++) {
93 A = B*D[ij] - S[i] - S[j];
104 edge2[k] = otu_label[OTU1];
105 edge2[k + 1] = otu_label[OTU2];
106 edge1[k] = edge1[k + 1] = cur_nod;
108 /* get the distances between all OTUs but the 2 selected ones
110 a) get the sum for both
111 b) compute the distances for the new OTU */
115 for (i = 1; i <= n; i++) {
116 if (i == OTU1 || i == OTU2) continue;
117 x = D[give_index(i, OTU1, n)]; /* dist between OTU1 and i */
118 y = D[give_index(i, OTU2, n)]; /* dist between OTU2 and i */
119 new_dist[ij] = (x + y - A)/2;
122 /* compute the branch lengths */
123 B = (S[OTU1] - S[OTU2])/B; /* don't need B anymore */
124 edge_length[k] = (A + B)/2;
125 edge_length[k + 1] = (A - B)/2;
127 /* update before the next loop
128 (we are sure that OTU1 < OTU2) */
130 for (i = OTU1; i > 1; i--)
131 otu_label[i] = otu_label[i - 1];
133 for (i = OTU2; i < n; i++)
134 otu_label[i] = otu_label[i + 1];
135 otu_label[1] = cur_nod;
137 for (i = 1; i < n; i++) {
138 if (i == OTU1 || i == OTU2) continue;
139 for (j = i + 1; j <= n; j++) {
140 if (j == OTU1 || j == OTU2) continue;
141 new_dist[ij] = D[DINDEX(i, j)];
147 for (i = 0; i < n*(n - 1)/2; i++) D[i] = new_dist[i];
153 for (i = 0; i < 3; i++) {
154 edge1[*N*2 - 4 - i] = cur_nod;
155 edge2[*N*2 - 4 - i] = otu_label[i + 1];
158 edge_length[*N*2 - 4] = (D[0] + D[1] - D[2])/2;
159 edge_length[*N*2 - 5] = (D[0] + D[2] - D[1])/2;
160 edge_length[*N*2 - 6] = (D[2] + D[1] - D[0])/2;