| 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.app.tools; |
| 27 |
|
| 28 |
import java.io.IOException; |
| 29 |
import java.io.PrintStream; |
| 30 |
import java.text.NumberFormat; |
| 31 |
import java.util.ArrayList; |
| 32 |
import java.util.Collections; |
| 33 |
import java.util.HashMap; |
| 34 |
import java.util.Iterator; |
| 35 |
import java.util.List; |
| 36 |
import java.util.Map; |
| 37 |
|
| 38 |
import dr.evolution.alignment.Alignment; |
| 39 |
import dr.evolution.datatype.DataType; |
| 40 |
import dr.evolution.io.TreeExporter; |
| 41 |
import dr.evolution.sequence.Sequence; |
| 42 |
import dr.evolution.tree.NodeRef; |
| 43 |
import dr.evolution.tree.Tree; |
| 44 |
import dr.evolution.util.Taxon; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public class NexusExporter implements TreeExporter { |
| 52 |
|
| 53 |
private final PrintStream out; |
| 54 |
private NumberFormat formatter = null; |
| 55 |
private String treePrefix = DEFAULT_TREE_PREFIX; |
| 56 |
private boolean sorted = false; |
| 57 |
private AttributeType writeAttributesAs = AttributeType.NODE_ATTRIBUTES; |
| 58 |
public static final String DEFAULT_TREE_PREFIX = "TREE"; |
| 59 |
public static final String SPECIAL_CHARACTERS_REGEX = ".*[\\s\\.;,\"\'].*"; |
| 60 |
|
| 61 |
public enum AttributeType { |
| 62 |
NODE_ATTRIBUTES, |
| 63 |
BRANCH_ATTRIBUTES |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public NexusExporter(PrintStream out) { |
| 74 |
this.out = out; |
| 75 |
this.writeAttributesAs = AttributeType.NODE_ATTRIBUTES; |
| 76 |
} |
| 77 |
|
| 78 |
public NexusExporter(PrintStream out, AttributeType writeAttributesAs) { |
| 79 |
this.out = out; |
| 80 |
this.writeAttributesAs = writeAttributesAs; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
public void setTreePrefix(String treePrefix) { |
| 89 |
this.treePrefix = treePrefix; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
public void setNumberFormat(NumberFormat format) { |
| 98 |
formatter = format; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
public void setSortedTranslationTable(boolean sorted) { |
| 105 |
this.sorted = sorted; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
public void exportTrees(Tree[] trees, boolean attributes, String[] treeNames) { |
| 114 |
if(!(treeNames==null) && trees.length != treeNames.length) { |
| 115 |
throw new RuntimeException("Number of trees and number of tree names is not the same"); |
| 116 |
} |
| 117 |
Map<String, Integer> idMap = writeNexusHeader(trees[0]); |
| 118 |
out.println("\t\t;"); |
| 119 |
for (int i = 0; i < trees.length; i++) { |
| 120 |
if(treeNames==null) { |
| 121 |
writeNexusTree(trees[i], treePrefix + i, attributes, idMap); |
| 122 |
} |
| 123 |
else { |
| 124 |
writeNexusTree(trees[i], treeNames[i], attributes, idMap); |
| 125 |
} |
| 126 |
} |
| 127 |
out.println("End;"); |
| 128 |
} |
| 129 |
|
| 130 |
public void exportTrees(Tree[] trees, boolean attributes) { |
| 131 |
exportTrees(trees, attributes, null); |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
public void exportTrees(Tree[] trees) { |
| 136 |
exportTrees(trees, true, null); |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
public void exportTree(Tree tree) { |
| 145 |
Map<String, Integer> idMap = writeNexusHeader(tree); |
| 146 |
out.println("\t\t;"); |
| 147 |
writeNexusTree(tree, treePrefix + 1, true, idMap); |
| 148 |
out.println("End;"); |
| 149 |
} |
| 150 |
|
| 151 |
public void writeNexusTree(Tree tree, String s, boolean attributes, Map<String, Integer> idMap) { |
| 152 |
|
| 153 |
String treeAttributes = "[&R] "; |
| 154 |
|
| 155 |
|
| 156 |
StringBuilder treeComment = null; |
| 157 |
{ |
| 158 |
Iterator<String> iter = tree.getAttributeNames(); |
| 159 |
if (iter != null) { |
| 160 |
while (iter.hasNext()) { |
| 161 |
final String name = iter.next(); |
| 162 |
final String value = tree.getAttribute(name).toString(); |
| 163 |
|
| 164 |
if( name.equals("weight") ) { |
| 165 |
treeAttributes = treeAttributes + "[&W " + value + " ] "; |
| 166 |
} |
| 167 |
else { |
| 168 |
if( treeComment == null ) { |
| 169 |
treeComment = new StringBuilder(" [&"); |
| 170 |
} else if( treeComment.length() > 2 ) { |
| 171 |
treeComment.append(", "); |
| 172 |
} |
| 173 |
|
| 174 |
treeComment.append(name).append("=").append(value); |
| 175 |
} |
| 176 |
} |
| 177 |
if( treeComment != null ) { |
| 178 |
treeComment.append("]"); |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
out.print("tree " + s + ((treeComment != null) ? treeComment.toString() : "") |
| 184 |
+ " = " + treeAttributes); |
| 185 |
|
| 186 |
writeNode(tree, tree.getRoot(), attributes, idMap); |
| 187 |
out.println(";"); |
| 188 |
} |
| 189 |
|
| 190 |
public Map<String, Integer> writeNexusHeader(Tree tree) { |
| 191 |
int taxonCount = tree.getTaxonCount(); |
| 192 |
List<String> names = new ArrayList<String>(); |
| 193 |
|
| 194 |
for (int i = 0; i < tree.getTaxonCount(); i++) { |
| 195 |
names.add(tree.getTaxonId(i)); |
| 196 |
} |
| 197 |
|
| 198 |
if (sorted) Collections.sort(names); |
| 199 |
|
| 200 |
out.println("#NEXUS"); |
| 201 |
out.println(); |
| 202 |
out.println("Begin taxa;"); |
| 203 |
out.println("\tDimensions ntax=" + taxonCount + ";"); |
| 204 |
out.println("\tTaxlabels"); |
| 205 |
for (String name : names) { |
| 206 |
if (name.matches(SPECIAL_CHARACTERS_REGEX)) { |
| 207 |
name = "'" + name + "'"; |
| 208 |
} |
| 209 |
out.println("\t\t" + name); |
| 210 |
} |
| 211 |
out.println("\t\t;"); |
| 212 |
out.println("End;"); |
| 213 |
out.println(""); |
| 214 |
out.println("Begin trees;"); |
| 215 |
|
| 216 |
|
| 217 |
out.println("\tTranslate"); |
| 218 |
Map<String, Integer> idMap = new HashMap<String, Integer>(); |
| 219 |
|
| 220 |
int k = 1; |
| 221 |
for (String name : names) { |
| 222 |
idMap.put(name, k); |
| 223 |
if (name.matches(SPECIAL_CHARACTERS_REGEX)) { |
| 224 |
name = "'" + name + "'"; |
| 225 |
} |
| 226 |
if (k < names.size()) { |
| 227 |
out.println("\t\t" + k + " " + name + ","); |
| 228 |
} else { |
| 229 |
out.println("\t\t" + k + " " + name); |
| 230 |
} |
| 231 |
k += 1; |
| 232 |
} |
| 233 |
return idMap; |
| 234 |
} |
| 235 |
|
| 236 |
private void writeNode(Tree tree, NodeRef node, boolean attributes, Map<String, Integer> idMap) { |
| 237 |
if (tree.isExternal(node)) { |
| 238 |
int k = node.getNumber() + 1; |
| 239 |
if (idMap != null) k = idMap.get(tree.getTaxonId(k - 1)); |
| 240 |
|
| 241 |
out.print(k); |
| 242 |
} else { |
| 243 |
out.print("("); |
| 244 |
writeNode(tree, tree.getChild(node, 0), attributes, idMap); |
| 245 |
for (int i = 1; i < tree.getChildCount(node); i++) { |
| 246 |
out.print(","); |
| 247 |
writeNode(tree, tree.getChild(node, i), attributes, idMap); |
| 248 |
} |
| 249 |
out.print(")"); |
| 250 |
} |
| 251 |
|
| 252 |
if (writeAttributesAs == AttributeType.BRANCH_ATTRIBUTES && !tree.isRoot(node)) { |
| 253 |
out.print(":"); |
| 254 |
} |
| 255 |
|
| 256 |
if (attributes) { |
| 257 |
Iterator<?> iter = tree.getNodeAttributeNames(node); |
| 258 |
if (iter != null) { |
| 259 |
boolean first = true; |
| 260 |
while (iter.hasNext()) { |
| 261 |
if (first) { |
| 262 |
out.print("[&"); |
| 263 |
first = false; |
| 264 |
} else { |
| 265 |
out.print(","); |
| 266 |
} |
| 267 |
String name = (String) iter.next(); |
| 268 |
out.print(name + "="); |
| 269 |
Object value = tree.getNodeAttribute(node, name); |
| 270 |
printValue(value); |
| 271 |
} |
| 272 |
out.print("]"); |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
if (writeAttributesAs == AttributeType.NODE_ATTRIBUTES && !tree.isRoot(node)) { |
| 277 |
out.print(":"); |
| 278 |
} |
| 279 |
|
| 280 |
if (!tree.isRoot(node)) { |
| 281 |
double length = tree.getBranchLength(node); |
| 282 |
if (formatter != null) { |
| 283 |
out.print(formatter.format(length)); |
| 284 |
} else { |
| 285 |
out.print(length); |
| 286 |
} |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
private void printValue(Object value) { |
| 291 |
if (value instanceof Object[]) { |
| 292 |
out.print("{"); |
| 293 |
Object[] values = (Object[]) value; |
| 294 |
for (int i = 0; i < values.length; i++) { |
| 295 |
if (i > 0) { |
| 296 |
out.print(","); |
| 297 |
} |
| 298 |
printValue(values[i]); |
| 299 |
} |
| 300 |
out.print("}"); |
| 301 |
} else if (value instanceof String) { |
| 302 |
out.print("\"" + value.toString() + "\""); |
| 303 |
} else { |
| 304 |
out.print(value.toString()); |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
public String exportAlignment(Alignment alignment) throws IOException, IllegalArgumentException { |
| 309 |
|
| 310 |
StringBuffer buffer = new StringBuffer(); |
| 311 |
|
| 312 |
DataType dataType = null; |
| 313 |
int seqLength = 0; |
| 314 |
|
| 315 |
for (int i = 0; i < alignment.getSequenceCount(); i++) { |
| 316 |
|
| 317 |
Sequence sequence = alignment.getSequence(i); |
| 318 |
|
| 319 |
if (sequence.getLength() > seqLength) { |
| 320 |
seqLength = sequence.getLength(); |
| 321 |
} |
| 322 |
|
| 323 |
if (dataType == null) { |
| 324 |
dataType = sequence.getDataType(); |
| 325 |
} else if (dataType != sequence.getDataType()) { |
| 326 |
throw new RuntimeException( |
| 327 |
"Sequences must have the same data type."); |
| 328 |
} |
| 329 |
|
| 330 |
} |
| 331 |
|
| 332 |
buffer.append("#NEXUS\n"); |
| 333 |
buffer.append("begin data;\n"); |
| 334 |
buffer.append("\tdimensions" + " " + "ntax=" + alignment.getTaxonCount() + " " + "nchar=" + seqLength + ";\n"); |
| 335 |
buffer.append("\tformat datatype=" + dataType.getDescription() |
| 336 |
+ " missing=" + DataType.UNKNOWN_CHARACTER + " gap=" |
| 337 |
+ DataType.GAP_CHARACTER + ";\n"); |
| 338 |
buffer.append("\tmatrix\n"); |
| 339 |
|
| 340 |
int maxRowLength = seqLength; |
| 341 |
for (int n = 0; n < Math.ceil((double) seqLength / maxRowLength); n++) { |
| 342 |
for (int i = 0; i < alignment.getSequenceCount(); i++) { |
| 343 |
|
| 344 |
Sequence sequence = alignment.getSequence(i); |
| 345 |
|
| 346 |
StringBuilder builder = new StringBuilder("\t"); |
| 347 |
|
| 348 |
appendTaxonName(sequence.getTaxon(), builder); |
| 349 |
|
| 350 |
String sequenceString = sequence.getSequenceString(); |
| 351 |
|
| 352 |
builder.append("\t").append( |
| 353 |
sequenceString.subSequence( |
| 354 |
n * maxRowLength, |
| 355 |
Math.min((n + 1) * maxRowLength, |
| 356 |
sequenceString.length()))); |
| 357 |
int shortBy = Math.min(Math.min(n * maxRowLength, seqLength) |
| 358 |
- sequence.getLength(), maxRowLength); |
| 359 |
|
| 360 |
if (shortBy > 0) { |
| 361 |
for (int j = 0; j < shortBy; j++) { |
| 362 |
builder.append(DataType.GAP_CHARACTER); |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
buffer.append(builder + "\n"); |
| 367 |
} |
| 368 |
} |
| 369 |
buffer.append(";\nend;"); |
| 370 |
|
| 371 |
return buffer.toString(); |
| 372 |
} |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
private StringBuilder appendTaxonName(Taxon taxon, StringBuilder builder) { |
| 381 |
|
| 382 |
String name = taxon.getId(); |
| 383 |
if (!name.matches(SPECIAL_CHARACTERS_REGEX)) { |
| 384 |
|
| 385 |
name = name.replace("\'", "\'\'"); |
| 386 |
builder.append("\'").append(name).append("\'"); |
| 387 |
return builder; |
| 388 |
} |
| 389 |
return builder.append(name); |
| 390 |
} |
| 391 |
|
| 392 |
} |