| 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.beagle.tools.parsers; |
| 27 |
|
| 28 |
import dr.app.beagle.tools.BeagleSequenceSimulator; |
| 29 |
import dr.app.beagle.tools.Partition; |
| 30 |
import dr.evolution.alignment.Alignment; |
| 31 |
import dr.evolution.alignment.SimpleAlignment; |
| 32 |
import dr.evolution.datatype.Codons; |
| 33 |
import dr.evolution.datatype.Nucleotides; |
| 34 |
import dr.xml.*; |
| 35 |
|
| 36 |
import java.util.ArrayList; |
| 37 |
import java.util.logging.Logger; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public class BeagleSequenceSimulatorParser extends AbstractXMLObjectParser { |
| 44 |
|
| 45 |
public static final String BEAGLE_SEQUENCE_SIMULATOR = "beagleSequenceSimulator"; |
| 46 |
public static final String PARALLEL = "parallel"; |
| 47 |
public static final String OUTPUT_ANCESTRAL_SEQUENCES = "outputAncestralSequences"; |
| 48 |
public static final String OUTPUT = "output"; |
| 49 |
|
| 50 |
public String getParserName() { |
| 51 |
return BEAGLE_SEQUENCE_SIMULATOR; |
| 52 |
} |
| 53 |
|
| 54 |
@Override |
| 55 |
public String getParserDescription() { |
| 56 |
return "Beagle sequence simulator"; |
| 57 |
} |
| 58 |
|
| 59 |
@Override |
| 60 |
public Class<Alignment> getReturnType() { |
| 61 |
return Alignment.class; |
| 62 |
} |
| 63 |
|
| 64 |
@Override |
| 65 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
return new XMLSyntaxRule[]{ |
| 70 |
AttributeRule.newBooleanRule(PARALLEL, true, "Whether to use multiple Beagle instances for simulation, default is false (sequential execution)."), |
| 71 |
new StringAttributeRule(OUTPUT, "Possible output formats", |
| 72 |
|
| 73 |
SimpleAlignment.OutputType.values(), |
| 74 |
false), |
| 75 |
|
| 76 |
new ElementRule(Partition.class, 1, Integer.MAX_VALUE) |
| 77 |
}; |
| 78 |
} |
| 79 |
|
| 80 |
@Override |
| 81 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 82 |
|
| 83 |
String msg = ""; |
| 84 |
boolean parallel = false; |
| 85 |
boolean outputAncestralSequences = false; |
| 86 |
|
| 87 |
if (xo.hasAttribute(PARALLEL)) { |
| 88 |
parallel = xo.getBooleanAttribute(PARALLEL); |
| 89 |
} |
| 90 |
|
| 91 |
if (xo.hasAttribute(OUTPUT_ANCESTRAL_SEQUENCES)) { |
| 92 |
outputAncestralSequences = xo.getBooleanAttribute(OUTPUT_ANCESTRAL_SEQUENCES); |
| 93 |
} |
| 94 |
|
| 95 |
SimpleAlignment.OutputType output = SimpleAlignment.OutputType.FASTA; |
| 96 |
if (xo.hasAttribute(OUTPUT)) { |
| 97 |
output = SimpleAlignment.OutputType.parseFromString( |
| 98 |
xo.getStringAttribute(OUTPUT)); |
| 99 |
} |
| 100 |
|
| 101 |
int siteCount = 0; |
| 102 |
int to = 0; |
| 103 |
for (int i = 0; i < xo.getChildCount(); i++) { |
| 104 |
Partition partition = (Partition) xo.getChild(i); |
| 105 |
|
| 106 |
to = partition.to + 1; |
| 107 |
if (to > siteCount) { |
| 108 |
siteCount = to; |
| 109 |
} |
| 110 |
|
| 111 |
} |
| 112 |
|
| 113 |
ArrayList<Partition> partitionsList = new ArrayList<Partition>(); |
| 114 |
for (int i = 0; i < xo.getChildCount(); i++) { |
| 115 |
|
| 116 |
Partition partition = (Partition) xo.getChild(i); |
| 117 |
|
| 118 |
if (partition.from > siteCount) { |
| 119 |
throw new XMLParseException( |
| 120 |
"Illegal 'from' attribute in " + PartitionParser.PARTITION + " element"); |
| 121 |
} |
| 122 |
|
| 123 |
if (partition.to > siteCount) { |
| 124 |
throw new XMLParseException( |
| 125 |
"Illegal 'to' attribute in " + PartitionParser.PARTITION + " element"); |
| 126 |
} |
| 127 |
|
| 128 |
if (partition.to == -1) { |
| 129 |
partition.to = siteCount - 1; |
| 130 |
} |
| 131 |
|
| 132 |
if (partition.getRootSequence() != null) { |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
int partitionSiteCount = (partition.to - partition.from) +1; |
| 137 |
|
| 138 |
if (partition.getRootSequence().getLength() != 3 * partitionSiteCount && partition.getFreqModel().getDataType() instanceof Codons) { |
| 139 |
|
| 140 |
throw new RuntimeException("Root codon sequence " + "for partition "+ (i+1) +" has " |
| 141 |
+ partition.getRootSequence().getLength() + " characters " |
| 142 |
+ "expecting " + 3 * partitionSiteCount + " characters"); |
| 143 |
|
| 144 |
} else if (partition.getRootSequence().getLength() != partitionSiteCount && partition.getFreqModel().getDataType() instanceof Nucleotides) { |
| 145 |
|
| 146 |
throw new RuntimeException("Root nuleotide sequence "+ "for partition "+ (i+1) +" has " |
| 147 |
+ partition.getRootSequence().getLength() + " characters " |
| 148 |
+ "expecting " + partitionSiteCount + " characters"); |
| 149 |
|
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
} |
| 155 |
|
| 156 |
partitionsList.add(partition); |
| 157 |
} |
| 158 |
|
| 159 |
msg += "\n\t" + partitionsList.size() + " partitions with a total of "; |
| 160 |
msg += siteCount + ((siteCount > 1) ? " replications " : " replication"); |
| 161 |
if (msg.length() > 0) { |
| 162 |
Logger.getLogger("dr.app.beagle.tools").info("\nUsing Beagle Sequence Simulator: " + msg + "\n"); |
| 163 |
} |
| 164 |
|
| 165 |
BeagleSequenceSimulator s = new BeagleSequenceSimulator(partitionsList); |
| 166 |
SimpleAlignment alignment = s.simulate(parallel, outputAncestralSequences); |
| 167 |
|
| 168 |
alignment.setOutputType(output); |
| 169 |
|
| 170 |
return alignment; |
| 171 |
} |
| 172 |
|
| 173 |
} |