Debian Codegraph @

src/dr/evomodel/alloppnet/parsers/MulSpeciesBindingsParser.java

raw symbols SCIP
1 /*
2 * MulSpeciesBindingsParser.java
3 *
4 * Copyright (c) 2002-2017 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.evomodel.alloppnet.parsers;
27
28 import dr.evomodel.alloppnet.speciation.AlloppSpeciesBindings;
29 import dr.evomodel.alloppnet.speciation.MulSpeciesBindings;
30 import dr.evomodel.tree.TreeModel;
31 import dr.xml.*;
32
33 import java.util.ArrayList;
34 import java.util.List;
35
36
37 /**
38 * Parser for list of possibly-allopolyploid species containing individuals
39 * in turn containing taxons (which are diploid genomes). This is similar
40 * to AlloppSpeciesBindingsParser but makes a MulSpeciesBindings not a
41 * AlloppSpeciesBindings. XML only different in using "mulSpecies" not
42 * "alloppspecies", and no "minGeneNodeHeight"..
43 *
44 * @author Graham Jones
45 * Date: 20/12/2011
46 */
47
48
49
50
51 /*
52 *
53 * Parses something like this:
54
55 <mulSpecies id="mulSpecies">
56 <sp id="Alpha" ploidylevel = 2>
57 <individual id = "1">
58 <taxon idref="1_Alpha" />
59 </individual>
60 <individual id = "2">
61 <taxon idref="2_Alpha" />
62 </individual>
63 <individual id = "3">
64 <taxon idref="3_Alpha" />
65 </individual>
66 </sp>
67 <sp id="Beta" ploidylevel = 4>
68 <individual id = "4">
69 <taxon idref="4_Beta_A" />
70 <taxon idref="4_Beta_B" />
71 </individual>
72 <individual id = "5">
73 <taxon idref="5_Beta_A" />
74 <taxon idref="5_Beta_B" />
75 </individual>
76 </sp>
77 ...
78 (more species, then genetrees)
79 </mulSpecies>
80
81 */
82
83
84
85
86 // I have adapted code from SpeciesBindingsParser.
87 // I changed 'ploidy' to 'popfactor' to reduce confusion with the ploidy level
88 // of a species which is independent of which gene is considered.
89 // Use of popfactors is untested. Maybe chloroplast data would use it.
90
91
92
93 public class MulSpeciesBindingsParser extends AbstractXMLObjectParser {
94 public static final String MUL_SPECIES = "mulSpecies";
95 public static final String GENE_TREES = "geneTrees";
96 public static final String GTREE = "gtree";
97 public static final String POPFACTOR = "popfactor";
98
99
100 public String getParserName() {
101 return MUL_SPECIES;
102 }
103
104 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
105
106 List<AlloppSpeciesBindings.ApSpInfo> apsp = new ArrayList<AlloppSpeciesBindings.ApSpInfo>();
107 for (int k = 0; k < xo.getChildCount(); ++k) {
108 final Object child = xo.getChild(k);
109 if (child instanceof AlloppSpeciesBindings.ApSpInfo) {
110 apsp.add((AlloppSpeciesBindings.ApSpInfo) child);
111 }
112 }
113 final XMLObject xogt = xo.getChild(GENE_TREES);
114 final int nTrees = xogt.getChildCount();
115 final TreeModel[] trees = new TreeModel[nTrees];
116 double[] popFactors = new double[nTrees];
117
118 for (int nt = 0; nt < trees.length; ++nt) {
119 Object child = xogt.getChild(nt);
120 if (!(child instanceof TreeModel)) {
121 assert child instanceof XMLObject;
122 popFactors[nt] = ((XMLObject) child).getDoubleAttribute(POPFACTOR);
123 child = ((XMLObject) child).getChild(TreeModel.class);
124
125 } else {
126 popFactors[nt] = -1;
127 }
128 trees[nt] = (TreeModel) child;
129 }
130
131 try {
132 return new MulSpeciesBindings(apsp.toArray(new AlloppSpeciesBindings.ApSpInfo[apsp.size()]),
133 trees, popFactors);
134 } catch (Error e) {
135 throw new XMLParseException(e.getMessage());
136 }
137 }
138
139
140 // I have adapted code from SpeciesBindingsParser
141 // I changed 'Ploidy' to 'PopFactors' to reduce confusion -
142 // the only use I can think of for popfactors in an AlloppNetwork is chloroplast data
143 ElementRule treeWithPopFactors = new ElementRule(GTREE,
144 new XMLSyntaxRule[]{AttributeRule.newDoubleRule(POPFACTOR),
145 new ElementRule(TreeModel.class)}, 0, Integer.MAX_VALUE);
146
147
148 @Override
149 public XMLSyntaxRule[] getSyntaxRules() {
150 return new XMLSyntaxRule[]{
151 new ElementRule(AlloppSpeciesBindings.ApSpInfo.class, 2, Integer.MAX_VALUE),
152 new ElementRule(GENE_TREES,
153 new XMLSyntaxRule[]{
154 new ElementRule(TreeModel.class, 0, Integer.MAX_VALUE),
155 treeWithPopFactors
156 }),
157 };
158 }
159
160
161 @Override
162 public String getParserDescription() {
163 return "Binds taxa to gene trees with information about possibly allopolyploid species.";
164 }
165
166 @Override
167 public Class getReturnType() {
168 return MulSpeciesBindings.class;
169 }
170 }
171