| 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.FlexibleTree; |
| 30 |
import dr.evolution.tree.Tree; |
| 31 |
import dr.evolution.util.Taxon; |
| 32 |
import dr.evolution.util.TaxonList; |
| 33 |
|
| 34 |
import java.io.EOFException; |
| 35 |
import java.io.IOException; |
| 36 |
import java.io.Reader; |
| 37 |
import java.io.StringReader; |
| 38 |
import java.util.ArrayList; |
| 39 |
import java.util.HashMap; |
| 40 |
import java.util.Map; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
public class NewickImporter extends Importer implements TreeImporter { |
| 50 |
public static final String COMMENT = "comment"; |
| 51 |
|
| 52 |
public class BranchMissingException extends ImportException { |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
private static final long serialVersionUID = 777435104809244693L; |
| 57 |
|
| 58 |
public BranchMissingException() { |
| 59 |
super(); |
| 60 |
} |
| 61 |
|
| 62 |
public BranchMissingException(String msg) { |
| 63 |
super("Branch missing: " + msg); |
| 64 |
System.err.println(msg); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public NewickImporter(Reader reader) { |
| 72 |
super(reader); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
public NewickImporter(String treeString) { |
| 79 |
this(new StringReader(treeString)); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
public Tree importTree(TaxonList taxonList) throws IOException, ImportException { |
| 86 |
setCommentDelimiters('[', ']', '\0', '\0', '&'); |
| 87 |
|
| 88 |
try { |
| 89 |
skipUntil("("); |
| 90 |
unreadCharacter('('); |
| 91 |
|
| 92 |
final FlexibleNode root = readInternalNode(taxonList); |
| 93 |
if (getLastMetaComment() != null) { |
| 94 |
root.setAttribute(COMMENT, getLastMetaComment()); |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
Map<Taxon, Integer> taxonNumberMap = null; |
| 101 |
if (taxonList != null) { |
| 102 |
taxonNumberMap = new HashMap<Taxon, Integer>(); |
| 103 |
int i = 0; |
| 104 |
for (Taxon taxon : taxonList) { |
| 105 |
taxonNumberMap.put(taxon, i); |
| 106 |
i++; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
return new FlexibleTree(root, false, true, taxonNumberMap); |
| 111 |
|
| 112 |
} catch (EOFException e) { |
| 113 |
throw new ImportException("incomplete tree"); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
public Tree[] importTrees(TaxonList taxonList) throws IOException, ImportException { |
| 121 |
boolean done = false; |
| 122 |
ArrayList<FlexibleTree> array = new ArrayList<FlexibleTree>(); |
| 123 |
|
| 124 |
do { |
| 125 |
|
| 126 |
try { |
| 127 |
|
| 128 |
skipUntil("("); |
| 129 |
unreadCharacter('('); |
| 130 |
|
| 131 |
FlexibleNode root = readInternalNode(taxonList); |
| 132 |
FlexibleTree tree = new FlexibleTree(root, false, true); |
| 133 |
array.add(tree); |
| 134 |
|
| 135 |
if (taxonList == null) { |
| 136 |
taxonList = tree; |
| 137 |
} |
| 138 |
|
| 139 |
if (readCharacter() != ';') { |
| 140 |
throw new BadFormatException("Expecting ';' after tree"); |
| 141 |
} |
| 142 |
|
| 143 |
} catch (EOFException e) { |
| 144 |
done = true; |
| 145 |
} |
| 146 |
} while (!done); |
| 147 |
|
| 148 |
Tree[] trees = new Tree[array.size()]; |
| 149 |
array.toArray(trees); |
| 150 |
|
| 151 |
return trees; |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
public boolean hasTree() throws IOException, ImportException { |
| 158 |
try { |
| 159 |
skipUntil("("); |
| 160 |
unreadCharacter('('); |
| 161 |
} catch (EOFException e) { |
| 162 |
lastTree = null; |
| 163 |
return false; |
| 164 |
} |
| 165 |
|
| 166 |
return true; |
| 167 |
} |
| 168 |
|
| 169 |
private Tree lastTree = null; |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
public Tree importNextTree() throws IOException, ImportException { |
| 176 |
FlexibleTree tree = null; |
| 177 |
|
| 178 |
try { |
| 179 |
skipUntil("("); |
| 180 |
unreadCharacter('('); |
| 181 |
|
| 182 |
FlexibleNode root = readInternalNode(lastTree); |
| 183 |
|
| 184 |
tree = new FlexibleTree(root, false, true); |
| 185 |
|
| 186 |
} catch (EOFException e) { |
| 187 |
|
| 188 |
} |
| 189 |
|
| 190 |
lastTree = tree; |
| 191 |
|
| 192 |
return tree; |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
private FlexibleNode readBranch(TaxonList taxonList) throws IOException, ImportException { |
| 201 |
double length = 0.0; |
| 202 |
FlexibleNode branch; |
| 203 |
|
| 204 |
if (nextCharacter() == '(') { |
| 205 |
|
| 206 |
branch = readInternalNode(taxonList); |
| 207 |
|
| 208 |
} else { |
| 209 |
|
| 210 |
branch = readExternalNode(taxonList); |
| 211 |
} |
| 212 |
|
| 213 |
final String comment = getLastMetaComment(); |
| 214 |
if (comment != null) { |
| 215 |
branch.setAttribute(COMMENT, comment); |
| 216 |
clearLastMetaComment(); |
| 217 |
} |
| 218 |
|
| 219 |
if (getLastDelimiter() == ':') { |
| 220 |
length = readDouble(",():;"); |
| 221 |
} |
| 222 |
|
| 223 |
branch.setLength(length); |
| 224 |
|
| 225 |
return branch; |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
private FlexibleNode readInternalNode(TaxonList taxonList) throws IOException, ImportException { |
| 233 |
FlexibleNode node = new FlexibleNode(); |
| 234 |
|
| 235 |
|
| 236 |
final char ch = readCharacter(); |
| 237 |
assert ch == '('; |
| 238 |
|
| 239 |
|
| 240 |
node.addChild(readBranch(taxonList)); |
| 241 |
|
| 242 |
if (getLastDelimiter() != ',') { |
| 243 |
java.util.logging.Logger.getLogger("dr.evolution.io").warning("Internal node only has a single child."); |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
while (getLastDelimiter() == ',') { |
| 249 |
node.addChild(readBranch(taxonList)); |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
if (getLastDelimiter() != ')') { |
| 254 |
throw new BadFormatException("Missing closing ')' in tree"); |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
try { |
| 259 |
String label = readToken(",():;"); |
| 260 |
if (label.length() > 0) { |
| 261 |
node.setAttribute("label", label); |
| 262 |
} |
| 263 |
} catch (IOException ioe) { |
| 264 |
|
| 265 |
|
| 266 |
} |
| 267 |
|
| 268 |
return node; |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
private FlexibleNode readExternalNode(TaxonList taxonList) throws IOException, ImportException { |
| 275 |
FlexibleNode node = new FlexibleNode(); |
| 276 |
|
| 277 |
String label = readToken(":(),;"); |
| 278 |
|
| 279 |
Taxon taxon; |
| 280 |
|
| 281 |
if (taxonList != null) { |
| 282 |
|
| 283 |
int index = taxonList.getTaxonIndex(label); |
| 284 |
if (index != -1) { |
| 285 |
taxon = taxonList.getTaxon(index); |
| 286 |
} else { |
| 287 |
throw new UnknownTaxonException("Taxon in tree, '" + label + "' is unknown"); |
| 288 |
} |
| 289 |
} else { |
| 290 |
|
| 291 |
taxon = new Taxon(label); |
| 292 |
} |
| 293 |
|
| 294 |
node.setTaxon(taxon); |
| 295 |
return node; |
| 296 |
} |
| 297 |
|
| 298 |
} |