Debian Codegraph @

src/dr/app/beauti/options/DateGuesser.java

raw symbols SCIP
1 /*
2 * DateGuesser.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.beauti.options;
27
28 import dr.evolution.util.Date;
29 import dr.evolution.util.Taxon;
30 import dr.evolution.util.TaxonList;
31 import dr.evolution.util.Units;
32
33 import java.io.*;
34 import java.text.*;
35 import java.util.*;
36 import java.util.regex.Matcher;
37 import java.util.regex.Pattern;
38
39 /**
40 * @author Andrew Rambaut
41 * @author Tommy Lam
42 */
43 public class DateGuesser implements Serializable {
44 private static final long serialVersionUID = -9106689400887615213L;
45
46 public enum GuessType {
47 ORDER,
48 PREFIX,
49 REGEX
50 }
51
52 public boolean guessDates = false;
53 public GuessType guessType = GuessType.ORDER;
54 public boolean fromLast = false;
55 public int order = 0;
56 public String prefix;
57 public String regex;
58 public File loadFile;
59 public HashMap<String, String> load;
60 public double offset = 0.0;
61 public double unlessLessThan = 0.0;
62 public double offset2 = 0.0;
63 public boolean parseCalendarDates = false;
64 public boolean parseCalendarDatesAndPrecision = false;
65 public String calendarDateFormat = "yyyy-MM-dd";
66
67 private DateFormat dateFormat;
68
69 public void guessDates(TaxonList taxonList) {
70 // To avoid duplicating code, add all the taxa into a list and
71 // pass it to guessDates(List<Taxon> taxonList)
72 List<Taxon> taxa = new ArrayList<Taxon>();
73 for (Taxon taxon : taxonList) {
74 taxa.add(taxon);
75 }
76
77 guessDates(taxa);
78 }
79
80 public void guessDates(TaxonList taxonList, Map<Taxon, String> taxonDateMap) {
81 // To avoid duplicating code, add all the taxa into a list and
82 // pass it to guessDates(List<Taxon> taxonList)
83 List<Taxon> taxa = new ArrayList<Taxon>();
84 for (Taxon taxon : taxonList) {
85 taxa.add(taxon);
86 }
87
88 guessDates(taxa, taxonDateMap);
89 }
90
91 public void guessDates(List<Taxon> taxonList) {
92 guessDates(taxonList, null);
93 }
94
95 public void guessDates(List<Taxon> taxonList, Map<Taxon, String> taxonDateMap) {
96
97 dateFormat = new SimpleDateFormat(calendarDateFormat);
98 dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
99
100 for (int i = 0; i < taxonList.size(); i++) {
101 Taxon taxon = taxonList.get(i);
102
103 // Allocates a Date object and initializes it to represent the specified number of milliseconds since the
104 // standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT
105 java.util.Date origin = new java.util.Date(0);
106
107 double[] values = new double[2];
108
109 try {
110
111 if (taxonDateMap != null) {
112 String dateString = taxonDateMap.get(taxon);
113 if (dateString == null) {
114 continue;
115 }
116
117 parseDate(taxon.getId(), dateString, values);
118
119 } else {
120 switch (guessType) {
121 case ORDER:
122 guessDateFromOrder(taxonList.get(i).getId(), order, fromLast, values);
123 break;
124 case PREFIX:
125 guessDateFromPrefix(taxonList.get(i).getId(), prefix, order, fromLast, values);
126 break;
127 case REGEX:
128 guessDateFromRegex(taxonList.get(i).getId(), regex, values);
129 break;
130 default:
131 throw new IllegalArgumentException("unknown GuessType");
132 }
133 }
134
135 } catch (GuessDatesException gfe) {
136 // @todo catch errors and give to user
137 }
138
139 double d = values[0];
140
141 if (!parseCalendarDates && !parseCalendarDatesAndPrecision) {
142 if (offset > 0) {
143 if (unlessLessThan > 0) {
144 if (d < unlessLessThan) {
145 d += offset2;
146 } else {
147 d += offset;
148 }
149 } else {
150 d += offset;
151 }
152 }
153 }
154
155 // @todo if any taxa aren't set then return warning
156
157 Date date = Date.createTimeSinceOrigin(d, Units.Type.YEARS, origin);
158 date.setUncertainty(values[1]);
159 taxon.setAttribute("date", date);
160 }
161 }
162
163 public Date parseDate(String value) throws GuessDatesException {
164 double[] values = new double[2];
165 parseDate("", value, values);
166
167 // Allocates a Date object and initializes it to represent the specified number of milliseconds since the
168 // standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT
169 java.util.Date origin = new java.util.Date(0);
170
171 return Date.createTimeSinceOrigin(values[0], Units.Type.YEARS, origin);
172
173 }
174
175 private void guessDateFromOrder(String label, int order, boolean fromLast, double[] values) throws GuessDatesException {
176
177 String field;
178
179 if (fromLast) {
180 int count = 0;
181 int i = label.length() - 1;
182
183 char c = label.charAt(i);
184
185 do {
186 // first find a part of a number
187 while (!Character.isDigit(c) && c != '.') {
188 i--;
189 if (i < 0) break;
190 c = label.charAt(i);
191 }
192
193 if (i < 0) throw new GuessDatesException("Missing number field in taxon label, " + label);
194
195 int j = i + 1;
196
197 // now find the beginning of the number
198 while (Character.isDigit(c) || c == '.') {
199 i--;
200 if (i < 0) break;
201 c = label.charAt(i);
202 }
203
204 field = label.substring(i + 1, j);
205
206 count++;
207
208 } while (count <= order);
209
210 } else {
211 int count = 0;
212 int i = 0;
213
214 char c = label.charAt(i);
215
216 do {
217 // first find a part of a number
218 while (!Character.isDigit(c)) {
219 i++;
220 if (i == label.length()) break;
221 c = label.charAt(i);
222 }
223 int j = i;
224
225 if (i == label.length()) throw new GuessDatesException("Missing number field in taxon label, " + label);
226
227 // now find the beginning of the number
228 while (Character.isDigit(c) || c == '.') {
229 i++;
230 if (i == label.length()) break;
231 c = label.charAt(i);
232 }
233
234 field = label.substring(j, i);
235
236 count++;
237
238 } while (count <= order);
239 }
240
241 parseDate(label, field, values);
242 }
243
244 private static final String REGEX_CHARACTERS = "|[].*()-^$";
245
246 private void guessDateFromPrefix(String label, String prefix, int order, boolean fromLast, double[] values) throws GuessDatesException {
247
248 StringBuilder sb = new StringBuilder();
249 for (int i = 0; i < prefix.length(); i++) {
250 if (REGEX_CHARACTERS.contains("" + prefix.charAt(i))) {
251 sb.append("\\").append(prefix.charAt(i));
252 } else {
253 sb.append(prefix.charAt(i));
254 }
255 }
256
257 String[] fields = label.split(sb.toString());
258 int index;
259 if (fromLast) {
260 index = fields.length - order - 1;
261 } else {
262 index = order;
263 }
264 if (index < 0) {
265 index = 0;
266 }
267 if (index >= fields.length) {
268 index = fields.length - 1;
269 }
270
271 parseDate(label, fields[index], values);
272 }
273
274 private void guessDateFromPrefix(String label, String prefix, double[] values) throws GuessDatesException {
275
276 int i = label.indexOf(prefix);
277
278 if (i == -1) throw new GuessDatesException("Missing prefix in taxon label, " + label);
279
280 i += prefix.length();
281 int j = i;
282
283 // now find the beginning of the number
284 char c = label.charAt(i);
285 while (i < label.length() - 1 && (Character.isDigit(c) || c == '.')) {
286 i++;
287 c = label.charAt(i);
288 }
289
290 if (i == j) throw new GuessDatesException("Missing field after prefix in taxon label, " + label);
291
292 String field = label.substring(j, i + 1);
293
294 parseDate(label, field, values);
295 }
296
297 private void guessDateFromRegex(String label, String regex, double[] values) throws GuessDatesException {
298
299 if (!regex.contains("(")) {
300 // if user hasn't specified a replace element, assume the whole regex should match
301 regex = "(" + regex + ")";
302 }
303
304 Pattern pattern = Pattern.compile(regex);
305 Matcher matcher = pattern.matcher(label);
306 if (!matcher.find()) {
307 throw new GuessDatesException("Regular expression doesn't find a match in taxon label, " + label);
308 }
309
310 if (matcher.groupCount() < 1) {
311 throw new GuessDatesException("Date group not defined in regular expression");
312 }
313
314 parseDate(label, matcher.group(0), values);
315 }
316
317
318 private void parseDateFromValue(String label, HashMap<String, String> myload, double[] values) throws GuessDatesException {
319 String dateStr = "";
320 if (myload.containsKey(label)) {
321 dateStr = (String)(myload.get(label));
322 } else {
323 throw new GuessDatesException("The imported table doesn't contain the taxon label, " + label);
324 }
325
326 parseDate(label, dateStr, values);
327 }
328
329
330 private DateFormat dateFormat1 = null;
331 private DateFormat dateFormat2 = null;
332 private DateFormat dateFormat3 = null;
333
334 private void parseDate(String label, String value, double[] values) throws GuessDatesException {
335 double d;
336 double p = 0.0;
337
338 if (dateFormat1 == null) {
339 // set the timezones to GMT so they match the origin date...
340 dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
341 dateFormat1.setTimeZone(TimeZone.getTimeZone("GMT"));
342 dateFormat2 = new SimpleDateFormat("yyyy-MM");
343 dateFormat2.setTimeZone(TimeZone.getTimeZone("GMT"));
344 dateFormat3 = new SimpleDateFormat("yyyy");
345 dateFormat3.setTimeZone(TimeZone.getTimeZone("GMT"));
346 }
347
348 if (parseCalendarDatesAndPrecision) {
349 try {
350 Date date = new Date(dateFormat1.parse(value));
351
352 d = date.getTimeValue();
353 p = 0.0;
354
355 } catch (ParseException pe) {
356 try {
357 Date date = new Date(dateFormat2.parse(value));
358
359 d = date.getTimeValue();
360 p = 1.0 / 12.0;
361
362 } catch (ParseException pe2) {
363 try {
364 Date date = new Date(dateFormat3.parse(value));
365
366 d = date.getTimeValue();
367 p = 1.0;
368
369 } catch (ParseException pe3) {
370 throw new GuessDatesException("Badly formatted date for taxon, " + label);
371 }
372 }
373 }
374
375 } else if (parseCalendarDates) {
376 try {
377
378 Date date = new Date(dateFormat.parse(value));
379
380 d = date.getTimeValue();
381 } catch (ParseException pe) {
382 throw new GuessDatesException("Badly formatted date for taxon, " + label);
383 }
384
385 } else {
386 try {
387 d = Double.parseDouble(value);
388 } catch (NumberFormatException nfe) {
389 throw new GuessDatesException("Badly formatted date for taxon, " + label);
390 }
391 }
392
393 values[0] = d;
394 values[1] = p;
395 }
396 }