Debian Codegraph @

src/dr/app/realtime/DistanceMatrixInsertionTest.java

raw symbols SCIP
1 /*
2 * TaxonInsertionTest.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.app.realtime;
27
28 import dr.app.beast.BeastParser;
29 import dr.app.util.Arguments;
30 import dr.evolution.alignment.PatternList;
31 import dr.evolution.alignment.Patterns;
32 import dr.evolution.distance.DistanceMatrix;
33 import dr.evolution.tree.Tree;
34 import dr.evomodel.treedatalikelihood.BeagleDataLikelihoodDelegate;
35 import dr.evomodel.treedatalikelihood.DataLikelihoodDelegate;
36 import dr.evomodel.treedatalikelihood.MultiPartitionDataLikelihoodDelegate;
37 import dr.evomodel.treedatalikelihood.TreeDataLikelihood;
38 import dr.inference.markovchain.MarkovChain;
39 import dr.inference.mcmc.MCMC;
40 import dr.inference.model.Likelihood;
41 import dr.xml.XMLParseException;
42 import dr.xml.XMLParser;
43 import org.xml.sax.SAXException;
44
45 import javax.swing.*;
46 import javax.xml.parsers.ParserConfigurationException;
47 import java.awt.*;
48 import java.io.FileNotFoundException;
49 import java.io.FileReader;
50 import java.io.IOException;
51 import java.util.ArrayList;
52 import java.util.List;
53 import java.util.Locale;
54
55 /**
56 * @author Guy Baele
57 */
58 public class DistanceMatrixInsertionTest extends JPanel {
59
60 private final boolean VERBOSE = true;
61 private final boolean PARSER_WARNINGS = true;
62 private final boolean STRICT_XML = false;
63
64 public DistanceMatrixInsertionTest(String beastXMLFileName, CheckPointUpdaterApp.UpdateChoice choice) {
65
66 super(new GridLayout(1,0));
67
68 //no additional parsers, we don't need BEAGLE at the moment just yet
69 XMLParser parser = new BeastParser(new String[]{beastXMLFileName}, null, VERBOSE, PARSER_WARNINGS, STRICT_XML, null);
70 try {
71 FileReader fileReader = new FileReader(beastXMLFileName);
72
73 //get the MCMC object
74 MCMC mcmc = (MCMC) parser.parse(fileReader, MCMC.class);
75 MarkovChain mc = mcmc.getMarkovChain();
76
77 //check the Tree(Data)Likelihoods in the connected set of likelihoods
78 //focus on TreeDataLikelihood, which has getTree() to get the tree for each likelihood
79 //also get the DataLikelihoodDelegate from TreeDataLikelihood
80 ArrayList<TreeDataLikelihood> likelihoods = new ArrayList<TreeDataLikelihood>();
81 ArrayList<Tree> trees = new ArrayList<Tree>();
82 ArrayList<DataLikelihoodDelegate> delegates = new ArrayList<DataLikelihoodDelegate>();
83 for (Likelihood likelihood : Likelihood.CONNECTED_LIKELIHOOD_SET) {
84 if (likelihood instanceof TreeDataLikelihood) {
85 likelihoods.add((TreeDataLikelihood)likelihood);
86 trees.add(((TreeDataLikelihood) likelihood).getTree());
87 delegates.add(((TreeDataLikelihood) likelihood).getDataLikelihoodDelegate());
88 }
89 }
90
91 //suggested to go through TreeDataLikelihoodParser and give it an extra option to create a HashMap
92 //keyed by the tree; am currently not overly fond of this approach
93 ArrayList<PatternList> patternLists = new ArrayList<PatternList>();
94 for (DataLikelihoodDelegate del : delegates) {
95 if (del instanceof BeagleDataLikelihoodDelegate) {
96 patternLists.add(((BeagleDataLikelihoodDelegate) del).getPatternList());
97 } else if (del instanceof MultiPartitionDataLikelihoodDelegate) {
98 MultiPartitionDataLikelihoodDelegate mpdld = (MultiPartitionDataLikelihoodDelegate)del;
99 List<PatternList> list = mpdld.getPatternLists();
100 for (PatternList pList : list) {
101 patternLists.add(pList);
102 }
103 }
104 }
105
106 if (patternLists.size() == 0) {
107 throw new RuntimeException("No patterns detected. Please make sure the XML file is BEAST 1.9 compatible.");
108 }
109
110 //aggregate all patterns to create distance matrix
111 Patterns patterns = new Patterns(patternLists.get(0));
112 if (patternLists.size() > 1) {
113 for (int i = 1; i < patternLists.size(); i++) {
114 patterns.addPatterns(patternLists.get(i));
115 }
116 }
117
118 String[] columnNames = new String[patterns.getTaxonCount()+1];
119 columnNames[0] = "";
120 for (int i = 0; i < patterns.getTaxonCount(); i++) {
121 columnNames[i+1] = patterns.getTaxonId(i);
122 }
123
124 //set the patterns for the distance matrix
125 DistanceMatrix matrix = choice.getMatrix();
126
127 Object[][] data = new Object[patterns.getTaxonCount()][patterns.getTaxonCount()+1];
128
129 //perform actual tests on the constructed distance matrix
130 for (int i = 0; i < matrix.getRowCount(); i++) {
131 data[i][0] = new String(columnNames[i+1]);
132 for (int j = 1; j < matrix.getColumnCount()+1; j++) {
133 data[i][j] = new Double(matrix.getElement(i, j-1));
134 }
135 }
136
137 final JTable table = new JTable(data, columnNames);
138 //table.setPreferredScrollableViewportSize(new Dimension(800, 600));
139 table.setFillsViewportHeight(true);
140 table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
141
142 for (int i = 0; i < patterns.getTaxonCount(); i++) {
143 table.getColumnModel().getColumn(i).setPreferredWidth(100);
144 }
145
146 //Create the scroll pane and add the table to it.
147 JScrollPane scrollPane = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
148
149 //Add the scroll pane to this panel.
150 add(scrollPane);
151
152 } catch (FileNotFoundException fnf) {
153 System.out.println(fnf);
154 } catch (IOException io) {
155 System.out.println(io);
156 io.printStackTrace();
157 } catch (SAXException sax) {
158 System.out.println(sax);
159 } catch (XMLParseException xml) {
160 System.out.println(xml);
161 } catch (ParserConfigurationException pce) {
162 System.out.println(pce);
163 }
164 }
165
166 public static void main(String[] args) {
167
168 // There is a major issue with languages that use the comma as a decimal separator.
169 // To ensure compatibility between programs in the package, enforce the US locale.
170 Locale.setDefault(Locale.US);
171
172 Arguments arguments = new Arguments(
173 new Arguments.Option[]{
174 new Arguments.StringOption("BEAST_XML", "FILENAME", "Specify a BEAST XML file"),
175 new Arguments.StringOption("update_choice", "UPDATECHOICE", "Specify a function by which to update the tree"),
176 new Arguments.Option("help", "Print this information and stop")
177 });
178
179 try {
180 arguments.parseArguments(args);
181 } catch (Arguments.ArgumentException ae) {
182 System.out.println();
183 System.out.println(ae.getMessage());
184 System.out.println();
185 //printUsage(arguments);
186 System.exit(1);
187 }
188
189 String inputFile = null;
190
191 if (arguments.hasOption("BEAST_XML")) {
192 inputFile = arguments.getStringOption("BEAST_XML");
193 } else {
194 throw new RuntimeException("No BEAST XML file specified.");
195 }
196
197 String choice = "";
198 if (arguments.hasOption("update_choice")) {
199 choice = arguments.getStringOption("update_choice");
200 } else {
201 throw new RuntimeException("Update mechanism needs to be specified.");
202 }
203 CheckPointUpdaterApp.UpdateChoice chosen = null;
204 for (CheckPointUpdaterApp.UpdateChoice ch : CheckPointUpdaterApp.UpdateChoice.values()) {
205 if (choice.equals(ch.getName())) {
206 chosen = ch;
207 break;
208 }
209 }
210 if (chosen == null) {
211 throw new RuntimeException("Incorrect update mechanism specified.");
212 }
213
214 //Create and set up the window.
215 JFrame frame = new JFrame("Test distance matrix");
216 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
217
218 DistanceMatrixInsertionTest test = new DistanceMatrixInsertionTest(inputFile, chosen);
219 test.setOpaque(true);
220 frame.setContentPane(test);
221
222 //Display the window.
223 frame.pack();
224 frame.setVisible(true);
225
226 //System.exit(0);
227
228 }
229
230 }