1 | #include "muscle.h" |
---|
2 | #include "seqvect.h" |
---|
3 | #include "distfunc.h" |
---|
4 | #include "clust.h" |
---|
5 | #include "clustsetdf.h" |
---|
6 | #include "tree.h" |
---|
7 | #include "clust.h" |
---|
8 | #include "distcalc.h" |
---|
9 | #include <math.h> |
---|
10 | |
---|
11 | static void TreeFromSeqVect_NJ(const DistFunc &DF, CLUSTER Cluster, Tree &tree) |
---|
12 | { |
---|
13 | ClustSetDF CSD(DF); |
---|
14 | |
---|
15 | Clust C; |
---|
16 | C.Create(CSD, Cluster); |
---|
17 | |
---|
18 | tree.FromClust(C); |
---|
19 | } |
---|
20 | |
---|
21 | static void TreeFromSeqVect_UPGMA(const DistFunc &DF, CLUSTER Cluster, Tree &tree) |
---|
22 | { |
---|
23 | LINKAGE Linkage = LINKAGE_Undefined; |
---|
24 | switch (Cluster) |
---|
25 | { |
---|
26 | case CLUSTER_UPGMA: |
---|
27 | Linkage = LINKAGE_Avg; |
---|
28 | break; |
---|
29 | case CLUSTER_UPGMAMin: |
---|
30 | Linkage = LINKAGE_Min; |
---|
31 | break; |
---|
32 | case CLUSTER_UPGMAMax: |
---|
33 | Linkage = LINKAGE_Max; |
---|
34 | break; |
---|
35 | case CLUSTER_UPGMB: |
---|
36 | Linkage = LINKAGE_Biased; |
---|
37 | break; |
---|
38 | default: |
---|
39 | Quit("TreeFromSeqVect_UPGMA, CLUSTER_%u not supported", Cluster); |
---|
40 | } |
---|
41 | |
---|
42 | DistCalcDF DC; |
---|
43 | DC.Init(DF); |
---|
44 | UPGMA2(DC, tree, Linkage); |
---|
45 | } |
---|
46 | |
---|
47 | static void SaveDF(const SeqVect &v, DistFunc &d, const char *FileName) |
---|
48 | { |
---|
49 | FILE *f = fopen(FileName, "w"); |
---|
50 | if (f == 0) |
---|
51 | Quit("Cannot create %s", FileName); |
---|
52 | |
---|
53 | unsigned n = v.GetSeqCount(); |
---|
54 | fprintf(f, "%u\n", n); |
---|
55 | for (unsigned i = 0; i < n; ++i) |
---|
56 | { |
---|
57 | fprintf(f, "%10.10s ", v.GetSeqName(i)); |
---|
58 | for (unsigned j = 0; j < i; ++j) |
---|
59 | fprintf(f, " %9g", d.GetDist(i, j)); |
---|
60 | fprintf(f, "\n"); |
---|
61 | } |
---|
62 | fclose(f); |
---|
63 | } |
---|
64 | |
---|
65 | void TreeFromSeqVect(const SeqVect &v, Tree &tree, CLUSTER Cluster, |
---|
66 | DISTANCE Distance, ROOT Root, const char *SaveFileName) |
---|
67 | { |
---|
68 | DistFunc DF; |
---|
69 | DistUnaligned(v, Distance, DF); |
---|
70 | if (SaveFileName != 0) |
---|
71 | SaveDF(v, DF, SaveFileName); |
---|
72 | if (CLUSTER_NeighborJoining == Cluster) |
---|
73 | TreeFromSeqVect_NJ(DF, Cluster, tree); |
---|
74 | else |
---|
75 | TreeFromSeqVect_UPGMA(DF, Cluster, tree); |
---|
76 | FixRoot(tree, Root); |
---|
77 | } |
---|