| 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.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 |
|
| 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 |
|
| 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 |
|
| 74 |
MCMC mcmc = (MCMC) parser.parse(fileReader, MCMC.class); |
| 75 |
MarkovChain mc = mcmc.getMarkovChain(); |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 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 |
|
| 92 |
|
| 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 |
|
| 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 |
|
| 125 |
DistanceMatrix matrix = choice.getMatrix(); |
| 126 |
|
| 127 |
Object[][] data = new Object[patterns.getTaxonCount()][patterns.getTaxonCount()+1]; |
| 128 |
|
| 129 |
|
| 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 |
|
| 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 |
|
| 147 |
JScrollPane scrollPane = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); |
| 148 |
|
| 149 |
|
| 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 |
|
| 169 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 223 |
frame.pack(); |
| 224 |
frame.setVisible(true); |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
} |
| 229 |
|
| 230 |
} |