Debian Codegraph @

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

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