Debian Codegraph @

src/dr/app/beagle/tools/parsers/BeagleSequenceSimulatorParser.java

raw symbols SCIP
1 /*
2 * BeagleSequenceSimulatorParser.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.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 * @author Filip Bielejec
41 * @version $Id$
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(), //TODO: this should ignore upper/lower cas
74 false),
75
76 new ElementRule(Partition.class, 1, Integer.MAX_VALUE)
77 };
78 }// END: getSyntaxRules
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 }// END: partitions loop
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 // TODO: what about 'every'?
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 }// END: dataType check
151
152 // System.exit(-1);
153
154 }// END: ancestralSequence check
155
156 partitionsList.add(partition);
157 }// END: partitions loop
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 }// END: parseXMLObject
172
173 }// END: class