| 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.evomodelxml.tree; |
| 27 |
|
| 28 |
import dr.util.FileHelpers; |
| 29 |
import dr.xml.*; |
| 30 |
import dr.evolution.io.NexusImporter; |
| 31 |
import dr.evolution.io.Importer; |
| 32 |
import dr.evolution.tree.Tree; |
| 33 |
import dr.evolution.util.TaxonList; |
| 34 |
import dr.evomodel.tree.EmpiricalTreeDistributionModel; |
| 35 |
|
| 36 |
import java.io.*; |
| 37 |
import java.util.logging.Logger; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public class EmpiricalTreeDistributionModelParser extends AbstractXMLObjectParser { |
| 45 |
|
| 46 |
public static final String RATE_ATTRIBUTE_NAME = "rateAttribute"; |
| 47 |
public static final String STARTING_TREE = "startingTree"; |
| 48 |
public static final String ITERATE = "iterate"; |
| 49 |
|
| 50 |
public String getParserName() { |
| 51 |
return EmpiricalTreeDistributionModel.EMPIRICAL_TREE_DISTRIBUTION_MODEL; |
| 52 |
} |
| 53 |
|
| 54 |
public String getParserDescription() { |
| 55 |
return "Read a list of trees from a NEXUS file."; |
| 56 |
} |
| 57 |
|
| 58 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 59 |
final String fileName = xo.getStringAttribute(FILE_NAME); |
| 60 |
|
| 61 |
int startingTree = xo.getAttribute(STARTING_TREE, -1); |
| 62 |
|
| 63 |
boolean iterate = xo.getAttribute(ITERATE, false); |
| 64 |
if (iterate && startingTree < 0) { |
| 65 |
startingTree = 0; |
| 66 |
} |
| 67 |
|
| 68 |
Logger.getLogger("dr.evomodel").info("Creating the empirical tree distribution model, '" + xo.getId() + "'"); |
| 69 |
|
| 70 |
TaxonList taxa = (TaxonList)xo.getChild(TaxonList.class); |
| 71 |
|
| 72 |
final File file = FileHelpers.getFile(fileName); |
| 73 |
|
| 74 |
Tree[] trees = null; |
| 75 |
NexusImporter importer = null; |
| 76 |
try { |
| 77 |
FileReader reader = new FileReader(file); |
| 78 |
importer = new NexusImporter(reader); |
| 79 |
if (!iterate) { |
| 80 |
trees = importer.importTrees(taxa, true); |
| 81 |
reader.close(); |
| 82 |
} |
| 83 |
} catch (FileNotFoundException e) { |
| 84 |
throw new XMLParseException(e.getMessage()); |
| 85 |
} catch (IOException e) { |
| 86 |
throw new XMLParseException(e.getMessage()); |
| 87 |
} catch (Importer.ImportException e) { |
| 88 |
throw new XMLParseException(e.getMessage()); |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
if (iterate) { |
| 93 |
Logger.getLogger("dr.evomodel").info(" Iterate over each tree from file, " + fileName); |
| 94 |
return new EmpiricalTreeDistributionModel(importer, startingTree); |
| 95 |
} else { |
| 96 |
Logger.getLogger("dr.evomodel").info(" Randomly jump between " + trees.length + " trees from file, " + fileName); |
| 97 |
return new EmpiricalTreeDistributionModel(trees, startingTree); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
public static final String FILE_NAME = "fileName"; |
| 102 |
|
| 103 |
|
| 104 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 105 |
return new XMLSyntaxRule[]{ |
| 106 |
AttributeRule.newIntegerRule(STARTING_TREE, true), |
| 107 |
AttributeRule.newBooleanRule(ITERATE, true), |
| 108 |
new StringAttributeRule(FILE_NAME, |
| 109 |
"The name of a NEXUS tree file"), |
| 110 |
|
| 111 |
|
| 112 |
new ElementRule(TaxonList.class), |
| 113 |
}; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
public Class getReturnType() { |
| 118 |
return EmpiricalTreeDistributionModel.class; |
| 119 |
} |
| 120 |
} |