Debian Codegraph @

src/dr/evolution/io/MigrateTreeImporter.java

raw symbols SCIP
1 /*
2 * MigrateTreeImporter.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.io;
27
28 import dr.evolution.tree.FlexibleNode;
29 import dr.evolution.tree.MutableTree;
30 import dr.evolution.tree.NodeRef;
31 import dr.evolution.tree.Tree;
32 import dr.evolution.util.Taxon;
33 import dr.evolution.util.TaxonList;
34
35 import java.io.IOException;
36 import java.io.Reader;
37 import java.util.HashMap;
38
39 /**
40 * @author Alexei Drummond
41 */
42 public class MigrateTreeImporter extends NexusImporter {
43
44 public static final String POP = "pop";
45 public static final String TO_POP = "toPop";
46 public static final String FROM_POP = "fromPop";
47
48 public MigrateTreeImporter(Reader reader) {
49 super(reader);
50 }
51
52 public Tree[] importTrees(TaxonList taxonList) throws IOException, ImportException {
53 Tree[] trees = super.importTrees(taxonList);
54
55 for (Tree tree : trees) {
56
57 // convert toPops to pops
58 for (int i = 0; i < tree.getNodeCount(); i++) {
59 NodeRef node = tree.getNode(i);
60
61 Object toPop = tree.getNodeAttribute(node, TO_POP);
62 Object pop = tree.getNodeAttribute(node, POP);
63
64 if (toPop != null && pop != null && !toPop.equals(pop)) {
65
66 String nodeName = node.getNumber() + "";
67 if (tree.isExternal(node)) {
68 nodeName = tree.getTaxonId(node.getNumber());
69 }
70 if (tree.isRoot(node)) nodeName = "root";
71
72 throw new RuntimeException(TO_POP + " = " + toPop + ", " + POP + " = " + pop + " in node " + nodeName);
73 }
74
75 if (pop == null && toPop != null) {
76 ((MutableTree) tree).setNodeAttribute(node, POP, toPop);
77 }
78 }
79 // convert fromPops to pops
80 for (int i = 0; i < tree.getNodeCount(); i++) {
81 NodeRef node = tree.getNode(i);
82
83 if (!tree.isRoot(node)) {
84
85 NodeRef parent = tree.getParent(node);
86 Object fromPop = tree.getNodeAttribute(node, FROM_POP);
87 Object pop = tree.getNodeAttribute(parent, POP);
88
89 if (fromPop != null && pop != null && !fromPop.equals(pop)) {
90 throw new RuntimeException(FROM_POP + " = " + fromPop + ", " + POP + " = " + pop);
91 }
92
93 if (pop == null && fromPop != null) {
94 ((MutableTree) tree).setNodeAttribute(parent, POP, fromPop);
95 }
96 }
97 }
98
99 // fill gaps with parent pops
100 fillInternalGaps(tree, tree.getRoot());
101 fillExternalGaps(tree);
102 }
103
104 return trees;
105 }
106
107 private void fillExternalGaps(Tree tree) {
108
109 for (int i = 0; i < tree.getExternalNodeCount(); i++) {
110 NodeRef node = tree.getExternalNode(i);
111 if (tree.getNodeAttribute(node, POP) == null) {
112 ((MutableTree) tree).setNodeAttribute(node, POP,
113 tree.getNodeAttribute(tree.getParent(node), POP));
114 }
115 }
116 }
117
118 private Object fillInternalGaps(Tree tree, NodeRef node) {
119
120 if (!tree.isExternal(node)) {
121 Object left = fillInternalGaps(tree, tree.getChild(node, 0));
122 Object right = fillInternalGaps(tree, tree.getChild(node, 1));
123
124 if (tree.getNodeAttribute(node, POP) == null) {
125
126 if (left == null && right == null) {
127 throw new RuntimeException("left and right are both null for node " + node.getNumber());
128 }
129
130 if (left == null) left = right;
131 if (right == null) right = left;
132
133 if (left.equals(right)) {
134 ((MutableTree) tree).setNodeAttribute(node, POP, left);
135 //System.out.println("Setting pop to " + left + " in node " + node.getNumber());
136 } else {
137 throw new RuntimeException(left + "!=" + right + " in children of node " + node.getNumber());
138 }
139 }
140 }
141 return tree.getNodeAttribute(node, POP);
142 }
143
144 /**
145 * Reads a branch in. This could be a node or a tip (calls readNode or readTip
146 * accordingly). It then reads the branch length and SimpleNode that will
147 * point at the new node or tip.
148 */
149 FlexibleNode readBranch(HashMap<String, Taxon> translationList) throws IOException, ImportException {
150 double length = 0.0;
151 FlexibleNode branch;
152
153 clearLastMetaComment();
154
155 if (nextCharacter() == '(') {
156 // is an internal node
157 branch = readInternalNode(translationList);
158
159 } else {
160 // is an external node
161 branch = readExternalNode(translationList);
162 }
163
164 if (getLastDelimiter() != ':' && getLastDelimiter() != ',' && getLastDelimiter() != ')') {
165 String label = readToken(",():;");
166 if (label.length() > 0) {
167 branch.setAttribute("label", label);
168 }
169 }
170
171 if (getLastDelimiter() == ':') {
172 length = readDouble(" ,():;");
173
174 if (getLastMetaComment() != null) {
175 parseMigrationString(getLastMetaComment(), branch);
176 clearLastMetaComment();
177 }
178
179 }
180
181 branch.setLength(length);
182
183 return branch;
184 }
185
186 /**
187 * Reads a node in. This could be a polytomy. Calls readBranch on each branch
188 * in the node.
189 */
190 FlexibleNode readInternalNode(HashMap<String, Taxon> translationList) throws IOException, ImportException {
191 FlexibleNode node = new FlexibleNode();
192
193 // read the opening '('
194 readCharacter();
195
196 // read the first child
197
198 FlexibleNode firstChild = readBranch(translationList);
199
200 node.addChild(firstChild);
201
202 // an internal node must have at least 2 children
203 if (getLastDelimiter() != ',') {
204 throw new BadFormatException("Missing ',' in tree in TREES block");
205 }
206
207 // read subsequent children
208 do {
209 node.addChild(readBranch(translationList));
210
211 } while (getLastDelimiter() == ',');
212
213 // should have had a closing ')'
214 if (getLastDelimiter() != ')') {
215 throw new BadFormatException("Missing closing ')' in tree in TREES block");
216 }
217
218 readToken(":(),;");
219
220 if (getLastMetaComment() != null) {
221 parseMigrationString(getLastMetaComment(), node);
222 clearLastMetaComment();
223 }
224
225 // find the next delimiter
226 return node;
227 }
228
229 /**
230 * Reads an external node in.
231 */
232 FlexibleNode readExternalNode(HashMap<String, Taxon> translationList) throws ImportException, IOException {
233 FlexibleNode node = new FlexibleNode();
234
235 String label = readToken(":(),;");
236
237 Taxon taxon;
238
239 if (translationList.size() > 0) {
240 taxon = translationList.get(label);
241
242 if (taxon == null) {
243 // taxon not found in taxon list...
244 throw new UnknownTaxonException("Taxon in tree, '" + label + "' is unknown");
245 }
246 } else {
247 taxon = new Taxon(label);
248 }
249
250 if (getLastMetaComment() != null) {
251 parseMigrationString(getLastMetaComment(), node);
252 clearLastMetaComment();
253 }
254
255 node.setTaxon(taxon);
256
257 int pop = Integer.parseInt(label.split("\\.")[0]);
258
259 node.setAttribute(POP, (pop - 1));
260
261 return node;
262 }
263
264 private void parseMigrationString(String migrationString, FlexibleNode branch) {
265
266 //System.out.println("Parse migration, string='" + migrationString + "'");
267
268 String[] migrations = migrationString.split(";");
269 for (String migration : migrations) {
270 parseMigration(migration, branch);
271 }
272 }
273
274 private void parseMigration(String migration, FlexibleNode branch) {
275
276 String[] parts = migration.split(" ");
277 if (!parts[0].equals("M")) throw new RuntimeException(parts[0] + " should be M");
278 int from = Integer.parseInt(parts[1]);
279 parts = parts[2].split(":");
280 int to = Integer.parseInt(parts[0]);
281 double time = Double.parseDouble(parts[1]);
282
283 if (branch.getAttribute(TO_POP) == null) branch.setAttribute(TO_POP, to);
284
285 branch.setAttribute(FROM_POP, from);
286 }
287
288
289 }
290