Debian Codegraph @

src/dr/evoxml/RandomTaxaSampleParser.java

raw symbols SCIP
1 /*
2 * RandomTaxaSampleParser.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.evoxml;
27
28 import dr.evolution.util.Taxa;
29 import dr.evolution.util.Taxon;
30 import dr.math.MathUtils;
31 import dr.xml.*;
32
33 import java.io.File;
34 import java.io.FileWriter;
35 import java.io.IOException;
36 import java.io.Writer;
37 import java.util.ArrayList;
38 import java.util.logging.Logger;
39
40 public class RandomTaxaSampleParser extends AbstractXMLObjectParser {
41
42 public static final String RANDOM_TAXA_SAMPLE = "randomTaxaSample";
43 public static final String SAMPLE = "sample";
44 public static final String PRINT_TAXA = "printTaxa";
45 public static final String FILE_NAME = "fileName";
46
47 public String getParserDescription() {
48 return "Randomly samples n taxa from a collection of N taxa";
49 }
50
51 public Class getReturnType() {
52 return RandomTaxaSampleParser.class;
53 }
54
55 public XMLSyntaxRule[] getSyntaxRules() {
56 return rules;
57 }
58
59 private final XMLSyntaxRule[] rules = {
60 AttributeRule.newIntegerRule(SAMPLE),
61 new ElementRule(Taxa.class),
62 };
63
64 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
65
66 Taxa population = (Taxa) xo.getChild(Taxa.class);
67 int n = xo.getIntegerAttribute(SAMPLE);
68
69 int N = population.getTaxonCount();
70
71 if (n <= 0 || n > N) {
72 throw new XMLParseException("sample must be greater than 0 and less than or equal to the population size");
73 }
74
75 Taxa sample = new Taxa();
76
77 ArrayList<Integer> indexes = new ArrayList<Integer>(N);
78
79 for (int i = 0; i < N; i++)
80 indexes.add(i);
81
82 Logger.getLogger("dr.evolution").info("Generating a random taxa sample of size: " + n);
83
84 for (int i = 0; i < n; i++) {
85 int randomIndex = MathUtils.nextInt(indexes.size());
86
87 Taxon selectedTaxon = population.getTaxon(indexes.get(randomIndex));
88
89 sample.addTaxon(selectedTaxon);
90
91 indexes.remove(randomIndex);
92 }
93
94 if (xo.hasAttribute(PRINT_TAXA) && xo.getBooleanAttribute(PRINT_TAXA)) {
95 String fileName = null;
96 if (xo.hasAttribute(FILE_NAME)) {
97 fileName = xo.getStringAttribute(FILE_NAME);
98 }
99
100 if (fileName != null) {
101 try {
102 Writer write;
103
104 File file = new File(fileName);
105 String name = file.getName();
106 String parent = file.getParent();
107
108 if (!file.isAbsolute()) {
109 parent = System.getProperty("user.dir");
110 }
111
112 write = new FileWriter(new File(parent, name));
113
114 write.write("<taxa id=\"randomTaxaSample\">\n");
115
116 for (int i = 0; i < n; i++) {
117 write.write("\t<taxon idref=\"" + sample.getTaxonId(i) + "\"/>\n");
118 }
119
120 write.write("</taxa id=\"randomTaxaSample\">\n");
121
122 write.flush();
123 } catch (IOException fnfe) {
124 throw new XMLParseException("File '" + fileName + "' can not be opened for " + getParserName() + " element.");
125 }
126 } else {
127 Logger.getLogger("dr.evomodel").info("<taxa id=\"randomTaxaSample\">");
128
129 for (int i = 0; i < n; i++) {
130 Logger.getLogger("dr.evomodel").info("\t<taxon idref=\" " + sample.getTaxonId(i) + " \"> ");
131 }
132 Logger.getLogger("dr.evomodel").info("</taxa id=\"randomTaxaSample\">");
133
134 }
135
136 }
137
138 return sample;
139 }
140
141 public String getParserName() {
142 return RANDOM_TAXA_SAMPLE;
143 }
144
145 }