| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 146 |
|
| 147 |
|
| 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 |
|
| 157 |
branch = readInternalNode(translationList); |
| 158 |
|
| 159 |
} else { |
| 160 |
|
| 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 |
|
| 188 |
|
| 189 |
|
| 190 |
FlexibleNode readInternalNode(HashMap<String, Taxon> translationList) throws IOException, ImportException { |
| 191 |
FlexibleNode node = new FlexibleNode(); |
| 192 |
|
| 193 |
|
| 194 |
readCharacter(); |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
FlexibleNode firstChild = readBranch(translationList); |
| 199 |
|
| 200 |
node.addChild(firstChild); |
| 201 |
|
| 202 |
|
| 203 |
if (getLastDelimiter() != ',') { |
| 204 |
throw new BadFormatException("Missing ',' in tree in TREES block"); |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
do { |
| 209 |
node.addChild(readBranch(translationList)); |
| 210 |
|
| 211 |
} while (getLastDelimiter() == ','); |
| 212 |
|
| 213 |
|
| 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 |
|
| 226 |
return node; |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
|
| 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 |
|
| 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 |
|
| 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 |
|