Debian Codegraph @

src/dr/app/beastgen/BEASTGen.java

raw symbols SCIP
1 /*
2 * BEASTGen.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.beastgen;
27
28 import dr.app.beauti.options.DateGuesser;
29 import dr.app.util.Arguments;
30 import dr.evolution.io.Importer;
31 import freemarker.template.*;
32
33 import java.io.*;
34 import java.util.*;
35 import java.util.regex.Matcher;
36 import java.util.regex.Pattern;
37
38 public class BEASTGen {
39
40 public BEASTGen(DateGuesser guesser, Map argumentMap, String treeFileName, String templateFileName, String inputFileName, String outputFileName) throws IOException {
41
42 Configuration cfg = new Configuration();
43 cfg.setObjectWrapper(new DefaultObjectWrapper());
44 cfg.setDefaultEncoding("UTF-8");
45 cfg.setTemplateExceptionHandler(TemplateExceptionHandler.DEBUG_HANDLER);
46 cfg.setIncompatibleImprovements(new Version(2, 3, 20)); // FreeMarker 2.3.20
47
48 Map root = null;
49 try {
50 root = constructDataModel(inputFileName, treeFileName, guesser);
51 } catch (Importer.ImportException ie) {
52 System.err.println("Error importing file: " + ie.getMessage());
53 System.exit(1);
54 }
55
56 root.putAll(argumentMap);
57
58 Template template = cfg.getTemplate(templateFileName);
59
60 try {
61 Writer out = (outputFileName != null ?
62 new FileWriter(new File(outputFileName)) :
63 new OutputStreamWriter(System.out));
64
65 template.process(root, out);
66 } catch (TemplateException te) {
67 System.err.println("Error processing template, " + templateFileName + ": " + te.getMessage());
68 System.exit(1);
69 }
70 }
71
72 private Map constructDataModel(String inputFileName, String treeFileName, DateGuesser guesser) throws IOException, Importer.ImportException {
73 DataModelImporter importer = new DataModelImporter(guesser);
74
75 Map root = importer.importFromFile(new File(inputFileName));
76
77 if (treeFileName != null) {
78 importer.importFromTreeFile(treeFileName, root);
79 }
80
81 return root;
82 }
83
84 public static void centreLine(String line, int pageWidth) {
85 int n = pageWidth - line.length();
86 int n1 = n / 2;
87 for (int i = 0; i < n1; i++) {
88 System.out.print(" ");
89 }
90 System.out.println(line);
91 }
92
93 public static void printTitle() {
94 System.out.println();
95 centreLine("BEASTGen v1.0.1, 2013-2014", 60);
96 centreLine("BEAST input file generator", 60);
97 centreLine("Andrew Rambaut, University of Edinburgh", 60);
98 System.out.println();
99 }
100
101 public static void printUsage(Arguments arguments) {
102
103 arguments.printUsage("beastgen", "<template-file-name> <input-file-name> [<output-file-name>]");
104 System.out.println();
105 System.out.println(" Example: beastgen template.beast test.nex test.xml");
106 System.out.println(" Example: beastgen -help");
107 System.out.println();
108 }
109
110 public static void main(String[] args) {
111
112 // There is a major issue with languages that use the comma as a decimal separator.
113 // To ensure compatibility between programs in the package, enforce the US locale.
114 Locale.setDefault(Locale.US);
115
116 Arguments arguments = new Arguments(
117 new Arguments.Option[]{
118 new Arguments.IntegerOption("date_order", "The order of the date field (negative numbers from last)"),
119 new Arguments.StringOption("date_prefix", "prefix", "A string that is the prefix to the date field"),
120 new Arguments.StringOption("date_regex", "regex", "A string that gives the regular expression to match the date"),
121 new Arguments.StringOption("date_format", "format", "A string that gives the date format for parsing"),
122 new Arguments.Option("date_precision", "Specifies the date is a variable precision yyyy-MM-dd format"),
123 new Arguments.StringOption("tree", "tree-file-name", "Read a tree from a file"),
124 new Arguments.StringOption("D", "\"key=value,key=value...\"", "Properties for exchange in templates"),
125 new Arguments.Option("version", "Print the version and credits and stop"),
126 new Arguments.Option("help", "Print this information and stop"),
127 });
128
129
130 try {
131 arguments.parseArguments(args);
132 } catch (Arguments.ArgumentException ae) {
133 printTitle();
134 System.out.println();
135 System.out.println(ae.getMessage());
136 System.out.println();
137 printUsage(arguments);
138 System.exit(1);
139 }
140
141 DateGuesser guesser = new DateGuesser();
142 // guesser.guessDates = false;
143 // guesser.guessType = DateGuesser.GuessType.ORDER;
144 // guesser.fromLast = false;
145 // guesser.order = 0;
146 // guesser.prefix = null;
147 // guesser.regex = null;
148 // guesser.offset = 0.0;
149 // guesser.unlessLessThan = 0.0;
150 // guesser.offset2 = 0.0;
151 // guesser.parseCalendarDates = false;
152 // guesser.parseCalendarDatesAndPrecision = false;
153 // guesser.calendarDateFormat = "yyyy-MM-dd";
154
155 if (arguments.hasOption("date_order")) {
156 guesser.guessDates = true;
157 int order = arguments.getIntegerOption("date_order");
158 if (order < 0) {
159 guesser.order = 1 + order;
160 guesser.fromLast = true;
161 } else if (order > 0) {
162 guesser.order = order - 1;
163 guesser.fromLast = false;
164 } else {
165 guesser.order = 0;
166 guesser.fromLast = false;
167 }
168 }
169
170 if (arguments.hasOption("date_prefix")) {
171 guesser.guessDates = true;
172 guesser.prefix = arguments.getStringOption("date_prefix");
173 guesser.guessType = DateGuesser.GuessType.PREFIX;
174 }
175
176 if (arguments.hasOption("date_regex")) {
177 guesser.guessDates = true;
178 guesser.regex = arguments.getStringOption("date_regex");
179 guesser.guessType = DateGuesser.GuessType.REGEX;
180 }
181
182 if (arguments.hasOption("date_format")) {
183 guesser.guessDates = true;
184 guesser.calendarDateFormat = arguments.getStringOption("date_format");
185 guesser.parseCalendarDates = true;
186 }
187
188 if (arguments.hasOption("date_precision")) {
189 guesser.guessDates = true;
190 guesser.parseCalendarDatesAndPrecision = true;
191 }
192
193 String treeFileName = null;
194 if (arguments.hasOption("tree")) {
195 treeFileName = arguments.getStringOption("tree");
196 }
197
198
199 Map argumentMap = new HashMap();
200
201 if (arguments.hasOption("D")) {
202 String properties = arguments.getStringOption("D");
203
204 for (String property : properties.split("\\s*,\\s*")) {
205 String[] keyValue = property.split("=");
206 if (keyValue.length != 2) {
207 System.err.println("Properties should take the form: key=value");
208 System.exit(1);
209 }
210 String key = keyValue[0].trim();
211 String value = keyValue[1].trim();
212 if (key.isEmpty()) {
213 System.err.println("Properties should take the form: key=value");
214 System.exit(1);
215 }
216 argumentMap.put(key, value);
217 }
218 }
219
220 if (arguments.hasOption("help")) {
221 printTitle();
222 printUsage(arguments);
223 System.exit(0);
224 }
225
226 if (arguments.hasOption("version")) {
227 printTitle();
228 }
229
230
231 String[] args2 = arguments.getLeftoverArguments();
232
233 if (args2.length < 1) {
234 printTitle();
235 printUsage(arguments);
236 System.exit(0);
237 }
238
239 if (args2.length < 2 || args2.length > 3) {
240 System.err.println("Unknown option: " + args2[0]);
241 System.err.println();
242 printUsage(arguments);
243 System.exit(1);
244 }
245
246 try {
247 new BEASTGen(guesser, argumentMap, treeFileName, args2[0], args2[1], (args2.length == 3 ? args2[2] : null));
248 } catch (IOException e) {
249 e.printStackTrace();
250 System.exit(1);
251 }
252
253 }
254 }