Debian Codegraph @

src/dr/evolution/distance/PatristicDistanceMatrix.java

raw symbols SCIP
1 /*
2 * PatristicDistanceMatrix.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.evolution.distance;
27
28 import dr.evolution.tree.Tree;
29 import dr.evolution.tree.NodeRef;
30 import dr.evolution.io.NexusImporter;
31 import dr.evolution.io.Importer;
32 import dr.evolution.tree.TreeUtils;
33 import dr.matrix.Matrix;
34
35 import java.util.HashSet;
36 import java.util.Set;
37 import java.io.*;
38
39
40 /**
41 * @author Alexei Drummond
42 */
43 public class PatristicDistanceMatrix extends DistanceMatrix {
44
45 Tree tree;
46
47 public PatristicDistanceMatrix(Tree tree) {
48
49 this.tree = tree;
50
51 dimension = tree.getExternalNodeCount();
52 distancesKnown = false;
53
54 calculateDistances();
55 }
56
57 protected double calculatePairwiseDistance(final int taxon1, int taxon2) {
58
59 NodeRef node1 = tree.getExternalNode(taxon1);
60 NodeRef node2 = tree.getExternalNode(taxon2);
61
62 Set<String> nodes = new HashSet<String>();
63 nodes.add(tree.getNodeTaxon(node1).getId());
64 nodes.add(tree.getNodeTaxon(node2).getId());
65
66 NodeRef ancestor = TreeUtils.getCommonAncestorNode(tree, nodes);
67
68 if (ancestor == null) System.out.println("common ancestor is null!");
69
70 double height1 = height(tree, node1, ancestor);
71 double height2 = height(tree, node2, ancestor);
72
73 return height1 + height2;
74
75 }
76
77 public double height(Tree tree, NodeRef node, NodeRef ancestor) {
78
79 if (node == ancestor) return 0.0;
80
81 return tree.getBranchLength(node) + height(tree, tree.getParent(node), ancestor);
82
83 }
84
85 public String toString() {
86 try {
87 double[] dists = getUpperTriangle();
88
89 StringBuffer buffer = new StringBuffer();
90
91 buffer.append(" \t");
92 for (int k = 0; k < dimension; k++) {
93 buffer.append(tree.getNodeTaxon(tree.getExternalNode(k))).append("\t");
94 }
95
96 buffer.append("\n");
97 int i = 0;
98 for (int k = 0; k < dimension; k++) {
99 buffer.append(tree.getNodeTaxon(tree.getExternalNode(k)));
100 for (int j = 0; j <= k; j++) {
101 buffer.append("\t");
102 }
103 for (int j = 0; j < dimension-k-1; j++) {
104 buffer.append(String.valueOf(dists[i])).append("\t");
105 i += 1;
106 }
107 buffer.append("\n");
108 }
109 return buffer.toString();
110 } catch(Matrix.NotSquareException e) {
111 return e.toString();
112 }
113
114 }
115
116
117 public static void main(String[] args) throws IOException, Importer.ImportException {
118
119 File file = new File(args[0]);
120
121 File outFile = new File(args[1]);
122
123 PrintWriter writer = new PrintWriter(new FileWriter(outFile));
124
125 NexusImporter importer = new NexusImporter(new FileReader(file));
126
127 Tree[] trees = importer.importTrees(null);
128
129 System.out.println("Found " + trees.length + " trees.");
130
131 for (Tree tree : trees) {
132
133 PatristicDistanceMatrix matrix = new PatristicDistanceMatrix(tree);
134
135 writer.println(matrix.toString());
136
137 }
138 writer.flush();
139
140 writer.close();
141
142 }
143
144 }