| 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.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 |
} |