| 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.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 |
|
| 41 |
|
| 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 |
|
| 71 |
|
| 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 |
|
| 82 |
|
| 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 |
|
| 104 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 168 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
} |