Debian Codegraph @

src/dr/app/tools/GenerateRelaxedClockXMLByData.java

raw symbols SCIP
1 /*
2 * GenerateRelaxedClockXMLByData.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.tools;
27
28 import dr.app.beauti.util.XMLWriter;
29
30 import java.io.*;
31
32 /**
33 * Given data file format is
34 * 1st line : anything which is ignored
35 * 2nd line : one taxon name separator (e.g. space or tab) its sequence data
36 * 3rd line : ...
37 *
38 * @author Walter Xie
39 */
40 public class GenerateRelaxedClockXMLByData {
41
42 static final String path = "/Users/local/EC/dxie004/Documents/BEAST1Release/RR_Datasets/";
43 static final String startingTree = "(((((Ssc:65,Bta:65):16,((Cfa:46,Fca:46):28,Eca:74):7):11," +
44 "(((Rno:20,Mmu:20):65,Ocu:85):5,(((Hsa:5,Ptr:5):5,Ppy:10):13,Mml:23):67):2):81,Tvu1:173):137,Gga:310);";
45 static final double rescaleHeight = 0.5;
46
47 static public void main(String[] args) {
48 int taxonNum = 14;
49
50 for (int c = 1; c <= 100; c++) {
51
52 String inputFileName = "geneRRNP" + c + ".phy";
53 String[][] data = new String[taxonNum][2];
54
55 try {
56 System.out.println("Input data from " + path + inputFileName + "\n\n");
57 FileReader fileReader = new FileReader(path + inputFileName);
58
59 LineNumberReader lineNumberReader = new LineNumberReader(fileReader);
60
61 String line = "";
62 int lineNum = 0;
63 while ((line = lineNumberReader.readLine()) != null) {
64 lineNum = lineNumberReader.getLineNumber();
65 System.out.println("Line: " + lineNum + ": " + line);
66
67 if (lineNumberReader.getLineNumber() > 1) { // not need 1st row
68
69 String[] d = line.split("\\s+");
70
71 if (d.length != 2) throw new IOException("d " + d.length);
72
73 data[lineNum - 2][0] = d[0]; // taxon name
74 data[lineNum - 2][1] = d[1]; // alignment
75 }
76 }
77 if (lineNum != 15) throw new IOException("not have 15 line " + lineNum);
78
79 fileReader.close();
80
81 String outputFileName = "geneRRNP" + c + ".xml";
82 System.out.println("\n\nCreating xml : " + path + outputFileName);
83 XMLWriter w = new XMLWriter(new BufferedWriter(new FileWriter(new File(path + outputFileName))));
84
85 writeData(w, data);
86
87 writeRestXML(w, outputFileName);
88
89 w.close();
90 System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++\n\n");
91
92 } catch (FileNotFoundException e) {
93 e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
94 } catch (IOException e) {
95 e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
96 }
97 }
98 }
99
100
101 private static void writeRestXML(XMLWriter w, String outputFileName) throws IOException {
102
103 w.writeText("\n\t<!-- a Birth-Death speciation process (Gernhard 2008). -->\n" +
104 "\t<birthDeathModel id=\"birthDeath\" units=\"substitutions\">\n" +
105 "\t\t<birthMinusDeathRate>\n" +
106 "\t\t\t<parameter id=\"birthDiffRate\" value=\"1.0\" lower=\"0.0\" upper=\"1000000.0\"/>\n" +
107 "\t\t</birthMinusDeathRate>\n" +
108 "\t\t<relativeDeathRate>\n" +
109 "\t\t\t<parameter id=\"relativeDeathRate\" value=\"0.5\" lower=\"0.0\" upper=\"1.0\"/>\n" +
110 "\t\t</relativeDeathRate>\n" +
111 "\t</birthDeathModel>\n");
112
113 // w.writeText("\n\t<constantSize id=\"constant\" units=\"substitutions\">\n" +
114 // "\t\t<populationSize>\n" +
115 // "\t\t\t<parameter id=\"popSize\" value=\"0.077\" lower=\"0.0\" upper=\"Infinity\"/>\n" +
116 // "\t\t</populationSize>\n" +
117 // "\t</constantSize>\n");
118
119 w.flush();
120 w.writeText("\n" +
121 "\t<!-- Generate a random starting tree under the coalescent process -->\n" +
122 "\t<newick id=\"startingTree\" rescaleHeight=\"" + rescaleHeight + "\">\n");
123 w.writeText(startingTree);
124 w.writeText("\n" + "\t</newick>\n");
125
126 // w.writeText("\n<!-- Construct a rough-and-ready UPGMA tree as an starting tree -->\n" +
127 // "\t<upgmaTree id=\"startingTree\">\n" +
128 // "\t\t<distanceMatrix correction=\"JC\">\n" +
129 // "\t\t\t<patterns>\n"+
130 // "\t\t\t\t<alignment idref=\"alignment\"/>\n" +
131 // "\t\t\t</patterns>\n" +
132 // "\t\t</distanceMatrix>\n" +
133 // "\t</upgmaTree>\n");
134
135 w.flush();
136
137 w.writeText("\n" +
138 "\t<!-- Generate a tree model -->\n" +
139 "\t<treeModel id=\"treeModel\">\n" +
140 "\t\t<coalescentTree idref=\"startingTree\"/>\n" +
141 "\t\t<rootHeight>\n" +
142 "\t\t\t<parameter id=\"treeModel.rootHeight\"/>\n" +
143 "\t\t</rootHeight>\n" +
144 "\t\t<nodeHeights internalNodes=\"true\">\n" +
145 "\t\t\t<parameter id=\"treeModel.internalNodeHeights\"/>\n" +
146 "\t\t</nodeHeights>\n" +
147 "\t\t<nodeHeights internalNodes=\"true\" rootNode=\"true\">\n" +
148 "\t\t\t<parameter id=\"treeModel.allInternalNodeHeights\"/>\n" +
149 "\t\t</nodeHeights>\n" +
150 "\t</treeModel>\n");
151
152 w.flush();
153 // w.writeText("\n<!-- Generate a coalescent likelihood -->\n" +
154 // "\t<coalescentLikelihood id=\"coalescent\">\n" +
155 // "\t\t<model>\n" +
156 // "\t\t\t<constantSize idref=\"constant\"/>\n" +
157 // "\t\t</model>\n" +
158 // "\t\t<populationTree>\n" +
159 // "\t\t\t<treeModel idref=\"treeModel\"/>\n" +
160 // "\t\t</populationTree>\n" +
161 // "\t</coalescentLikelihood>\n");
162
163 w.writeText("\n" +
164 "\t<!-- Generate a speciation likelihood for Yule or Birth Death -->\n" +
165 "\t<speciationLikelihood id=\"speciation\">\n" +
166 "\t\t<model>\n" +
167 "\t\t\t<birthDeathModel idref=\"birthDeath\"/>\n" +
168 "\t\t</model>\n" +
169 "\t\t<speciesTree>\n" +
170 "\t\t\t<treeModel idref=\"treeModel\"/>\n" +
171 "\t\t</speciesTree>\n" +
172 "\t</speciationLikelihood>\n");
173
174 w.writeText("\n<!-- The uncorrelated relaxed clock (Drummond, Ho, Phillips & Rambaut (2006) PLoS Biology 4, e88 )-->\n" +
175 "\t<discretizedBranchRates id=\"branchRates\">\n" +
176 "\t\t<treeModel idref=\"treeModel\"/>\n" +
177 "\t\t<distribution>\n" +
178 "\t\t\t<logNormalDistributionModel meanInRealSpace=\"true\">\n" +
179 "\t\t\t\t<mean>\n" +
180 "\t\t\t\t\t<parameter id=\"ucld.mean\" value=\"1.0\" lower=\"0.0\" upper=\"Infinity\"/>\n" +
181 "\t\t\t\t</mean>\n" +
182 "\t\t\t\t<stdev>\n" +
183 "\t\t\t\t\t<parameter id=\"ucld.stdev\" value=\"0.3333333333333333\" lower=\"0.0\" upper=\"Infinity\"/>\n" +
184 "\t\t\t\t</stdev>\n" +
185 "\t\t\t</logNormalDistributionModel>\n" +
186 "\t\t</distribution>\n" +
187 "\t\t<rateCategories>\n" +
188 "\t\t\t<parameter id=\"branchRates.categories\" dimension=\"10\"/>\n" +
189 "\t\t</rateCategories>\n" +
190 "\t</discretizedBranchRates>\n");
191
192 // w.writeText("\n\t<rateStatistic id=\"meanRate\" name=\"meanRate\" mode=\"mean\" internal=\"true\" external=\"true\">\n" +
193 // "\t\t<treeModel idref=\"treeModel\"/>\n" +
194 // "\t\t<discretizedBranchRates idref=\"branchRates\"/>\n" +
195 // "\t</rateStatistic>\n" +
196 // "\t<rateStatistic id=\"coefficientOfVariation\" name=\"coefficientOfVariation\" mode=\"coefficientOfVariation\" internal=\"true\" external=\"true\">\n" +
197 // "\t\t<treeModel idref=\"treeModel\"/>\n" +
198 // "\t\t<discretizedBranchRates idref=\"branchRates\"/>\n" +
199 // "\t</rateStatistic>\n" +
200 // "\t<rateCovarianceStatistic id=\"covariance\" name=\"covariance\">\n" +
201 // "\t\t<treeModel idref=\"treeModel\"/>\n" +
202 // "\t\t<discretizedBranchRates idref=\"branchRates\"/>\n" +
203 // "\t</rateCovarianceStatistic>\n");
204
205
206 w.writeText("\t<!-- The HKY substitution model (Hasegawa, Kishino & Yano, 1985) -->\n" +
207 "\t<HKYModel id=\"hky\">\n" +
208 "\t\t<frequencies>\n" +
209 "\t\t\t<frequencyModel dataType=\"nucleotide\">\n" +
210 "\t\t\t\t<frequencies>\n" +
211 "\t\t\t\t\t<parameter id=\"hky.frequencies\" value=\"0.25 0.25 0.25 0.25\"/>\n" +
212 "\t\t\t\t</frequencies>\n" +
213 "\t\t\t</frequencyModel>\n" +
214 "\t\t</frequencies>\n" +
215 "\t\t<kappa>\n" +
216 "\t\t\t<parameter id=\"hky.kappa\" value=\"2.0\" lower=\"0.0\" upper=\"Infinity\"/>\n" +
217 "\t\t</kappa>\n" +
218 "\t</HKYModel>\n" +
219 "\n" +
220 "\t<!-- site model -->\n" +
221 "\t<siteModel id=\"siteModel\">\n" +
222 "\t\t<substitutionModel>\n" +
223 "\t\t\t<HKYModel idref=\"hky\"/>\n" +
224 "\t\t</substitutionModel>\n" +
225 "\t</siteModel>\n");
226
227 w.writeText("\t<!-- Likelihood for tree given sequence data -->\n" +
228 "\t<treeLikelihood id=\"treeLikelihood\" useAmbiguities=\"false\">\n" +
229 "\t\t<patterns idref=\"patterns\"/>\n" +
230 "\t\t<treeModel idref=\"treeModel\"/>\n" +
231 "\t\t<siteModel idref=\"siteModel\"/>\n" +
232 "\t\t<discretizedBranchRates idref=\"branchRates\"/> \n" +
233 "\t</treeLikelihood>\n");
234
235 w.flush();
236
237 w.writeText("\n" + "\t<!-- Define operators -->\n" +
238 "\t<operators id=\"operators\">\n");
239
240 w.writeText("\t\t<scaleOperator scaleFactor=\"0.75\" weight=\"0.1\">\n" +
241 "\t\t\t<parameter idref=\"hky.kappa\"/>\n" +
242 "\t\t</scaleOperator>\n" +
243 "\t\t<deltaExchange delta=\"0.01\" weight=\"0.4\">\n" +
244 "\t\t\t<parameter idref=\"hky.frequencies\"/>\n" +
245 "\t\t</deltaExchange>\n" +
246 // "\t\t<subtreeSlide size=\"0.0077\" gaussian=\"true\" weight=\"15\">\n" +
247 // "\t\t\t<treeModel idref=\"treeModel\"/>\n" +
248 // "\t\t</subtreeSlide>\n" +
249 // "\t\t<narrowExchange weight=\"15\">\n" +
250 // "\t\t\t<treeModel idref=\"treeModel\"/>\n" +
251 // "\t\t</narrowExchange>\n" +
252 // "\t\t<wideExchange weight=\"3\">\n" +
253 // "\t\t\t<treeModel idref=\"treeModel\"/>\n" +
254 // "\t\t</wideExchange>\n" +
255 // "\t\t<wilsonBalding weight=\"3\">\n" +
256 // "\t\t\t<treeModel idref=\"treeModel\"/>\n" +
257 // "\t\t</wilsonBalding>\n" +
258 "\t\t<scaleOperator scaleFactor=\"0.75\" weight=\"1\">\n" +
259 "\t\t\t<parameter idref=\"treeModel.rootHeight\"/>\n" +
260 "\t\t</scaleOperator>\n" +
261 "\t\t<uniformOperator weight=\"12\">\n" +
262 "\t\t\t<parameter idref=\"treeModel.internalNodeHeights\"/>\n" +
263 "\t\t</uniformOperator>\n" +
264 // "\t\t<scaleOperator scaleFactor=\"0.75\" weight=\"3\">\n" +
265 // "\t\t\t<parameter idref=\"popSize\"/>\n" +
266 // "\t\t</scaleOperator>\n" +
267 "\t\t<scaleOperator scaleFactor=\"0.75\" weight=\"0.1\">\n" +
268 "\t\t\t<parameter idref=\"birthDiffRate\"/>\n" +
269 "\t\t</scaleOperator>\n" +
270 "\t\t<scaleOperator scaleFactor=\"0.75\" weight=\"0.1\">\n" +
271 "\t\t\t<parameter idref=\"relativeDeathRate\"/>\n" +
272 "\t\t</scaleOperator>\n" +
273
274 "\t\t<upDownOperator scaleFactor=\"0.75\" weight=\"13\">\n" +
275 "\t\t\t<up>\n" +
276 "\t\t\t</up>\n" +
277 "\t\t\t<down>\n" +
278 "\t\t\t\t<parameter idref=\"treeModel.allInternalNodeHeights\"/>\n" +
279 "\t\t\t</down>\n" +
280 "\t\t</upDownOperator>\n" +
281 "\t\t<scaleOperator scaleFactor=\"0.75\" weight=\"3\">\n" +
282 "\t\t\t<parameter idref=\"ucld.stdev\"/>\n" +
283 "\t\t</scaleOperator> \n" +
284 "\t\t<uniformIntegerOperator weight=\"13\">\n" +
285 "\t\t\t<parameter idref=\"branchRates.categories\"/>\n" +
286 "\t\t</uniformIntegerOperator>\n" +
287 "\t\t<swapOperator size=\"1\" weight=\"13\" autoOptimize=\"false\">\n" +
288 "\t\t\t<parameter idref=\"branchRates.categories\"/>\n" +
289 "\t\t</swapOperator>\n");
290
291 w.writeText("\t</operators>");
292
293 w.flush();
294 w.writeText("\n" +
295 "\t<!-- Define MCMC -->\n" +
296 "\t<mcmc id=\"mcmc\" chainLength=\"1000000\" autoOptimize=\"true\" " +
297 "operatorAnalysis=\"" + outputFileName + ".ops\">\n" +
298 "\t\t<posterior id=\"posterior\">\n" +
299 "\t\t\t<prior id=\"prior\">\n" +
300 "\t\t\t\t<logNormalPrior mean=\"1.0\" stdev=\"1.25\" offset=\"0.0\" meanInRealSpace=\"false\">\n" +
301 "\t\t\t\t\t<parameter idref=\"hky.kappa\"/>\n" +
302 "\t\t\t\t</logNormalPrior>\n" +
303 "\t\t\t\t<uniformPrior lower=\"0.0\" upper=\"1.0\">\n" +
304 "\t\t\t\t\t<parameter idref=\"hky.frequencies\"/>\n" +
305 "\t\t\t\t</uniformPrior>\n" +
306 "\t\t\t\t<exponentialPrior mean=\"0.3333333333333333\" offset=\"0.0\">\n" +
307 "\t\t\t\t\t<parameter idref=\"ucld.stdev\"/>\n" +
308 "\t\t\t\t</exponentialPrior> \n" +
309 // "\t\t\t\t<oneOnXPrior>\n" +
310 // "\t\t\t\t\t<parameter idref=\"popSize\"/>\n" +
311 // "\t\t\t\t</oneOnXPrior> \n" +
312 // "\t\t\t\t<coalescentLikelihood idref=\"coalescent\"/>\n");
313 "\t\t\t<speciationLikelihood idref=\"speciation\"/>\n");
314
315 w.writeText("\n" +
316 "\t\t\t</prior>\n" +
317 "\t\t\t<likelihood id=\"likelihood\">\n" +
318 "\t\t\t\t<treeLikelihood idref=\"treeLikelihood\"/>\n" +
319 "\t\t\t</likelihood>\n" +
320 "\t\t</posterior>\n" +
321 "\t\t<operators idref=\"operators\"/>\n");
322
323 w.flush();
324
325 w.writeText("\n" +
326 "\t\t<!-- write log to screen -->\n" +
327 "\t\t<log id=\"screenLog\" logEvery=\"100000\">\n" +
328 "\t\t\t<column label=\"Posterior\" dp=\"4\" width=\"12\">\n" +
329 "\t\t\t\t<posterior idref=\"posterior\"/>\n" +
330 "\t\t\t</column>\n" +
331 // "\t\t\t<column label=\"Prior\" dp=\"4\" width=\"12\">\n" +
332 // "\t\t\t\t<prior idref=\"prior\"/>\n" +
333 // "\t\t\t</column>\n" +
334 "\t\t\t<column label=\"Likelihood\" dp=\"4\" width=\"12\">\n" +
335 "\t\t\t\t<likelihood idref=\"likelihood\"/>\n" +
336 "\t\t\t</column>\n" +
337 "\t\t\t<column label=\"rootHeight\" sf=\"6\" width=\"12\">\n" +
338 "\t\t\t\t<parameter idref=\"treeModel.rootHeight\"/>\n" +
339 "\t\t\t</column>\n" +
340 // "\t\t\t<parameter idref=\"ucld.mean\"/>\n" +
341 "\t\t</log>\n"
342 );
343
344
345 w.writeText("\t\t<!-- write log to file -->\n" +
346 "\t\t<log id=\"fileLog\" logEvery=\"100\" fileName=\"" + outputFileName + ".log\">\n" +
347 "\t\t\t<posterior idref=\"posterior\"/>\n" +
348 "\t\t\t<prior idref=\"prior\"/>\n" +
349 "\t\t\t<treeLikelihood idref=\"treeLikelihood\"/>\n" +
350 // "\t\t\t<coalescentLikelihood idref=\"coalescent\"/>\n" +
351 "\t\t\t<speciationLikelihood idref=\"speciation\"/>\n" +
352 // "\t\t\t<parameter idref=\"popSize\"/>\n" +
353 "\t\t\t<parameter idref=\"birthDiffRate\"/>\n" +
354 "\t\t\t<parameter idref=\"relativeDeathRate\"/>\n" +
355 "\t\t\t<parameter idref=\"treeModel.rootHeight\"/>\n" +
356 "\t\t\t<parameter idref=\"hky.kappa\"/>\n" +
357 "\t\t\t<parameter idref=\"hky.frequencies\"/>\n" +
358 "\t\t\t<parameter idref=\"ucld.mean\"/>\n" +
359 "\t\t\t<parameter idref=\"ucld.stdev\"/>\n" +
360 "\t\t\t<parameter idref=\"branchRates.categories\"/> \n");
361 w.writeText("\t\t</log>\n");
362
363 w.writeText("\t\t<logTree id=\"treeFileLog\" logEvery=\"10000\" nexusFormat=\"true\" " +
364 "fileName=\"" + outputFileName + ".trees\" sortTranslationTable=\"true\">\n" +
365 "\t\t\t<treeModel idref=\"treeModel\"/>\n" +
366 "\t\t\t<discretizedBranchRates idref=\"branchRates\"/> \n" +
367 "\t\t\t<posterior idref=\"posterior\"/>\n" +
368 "\t\t</logTree>\n");
369
370 w.writeText("\t</mcmc>\n" +
371 "\t<report>\n" +
372 "\t\t<property name=\"timer\">\n" +
373 "\t\t\t<mcmc idref=\"mcmc\"/>\n" +
374 "\t\t</property>\n" +
375 "\t</report>\n" +
376 "</beast>\n");
377
378 w.flush();
379 }
380
381 private static void writeData(XMLWriter w, String[][] tips) throws IOException {
382 w.writeText("<?xml version=\"1.0\" standalone=\"yes\"?>\n" + "\n" +
383 "<!-- Generated by BEAUTi v1.6.2 -->\n" +
384 "<!-- by Alexei J. Drummond and Andrew Rambaut -->\n" +
385 "<!-- Department of Computer Science, University of Auckland and -->\n" +
386 "<!-- Institute of Evolutionary Biology, University of Edinburgh -->\n" +
387 "<!-- http://beast.bio.ed.ac.uk/ -->\n" +
388 "<beast>\n" + "\n" +
389 "\t<!-- The list of taxa to be analysed (can also include dates/ages). -->\n" +
390 "\t<!-- ntax=" + tips.length + " -->\n");
391
392 w.writeText("\t<taxa id=\"taxa\">\n");
393 for (int n = 0; n < tips.length; n++) {
394 w.writeText("\t\t<taxon id=\"" + tips[n][0] + "\"/>\n");
395 }
396 w.writeText("\t</taxa>\n");
397 w.flush();
398
399 w.writeText("\t<alignment id=\"alignment\" dataType=\"nucleotide\">\n");
400 for (int n = 0; n < tips.length; n++) {
401 w.writeText("\t\t<sequence>\n");
402 w.writeText("\t\t<taxon idref=\"" + tips[n][0] + "\"/>\n");
403 w.writeText(tips[n][1]);
404 w.writeText("\t\t</sequence>\n");
405 w.flush();
406 }
407 w.writeText("\t</alignment>\n");
408
409 w.writeText("\n\t<patterns id=\"patterns\" from=\"1\">\n" +
410 "\t\t<alignment idref=\"alignment\"/>\n" +
411 "\t</patterns>");
412
413 }
414
415 }