Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * TraitGuesser.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.app.beauti.traitspanel.GuessTraitException;
29 import dr.evolution.util.TaxonList;
30
31 import java.io.Serializable;
32 import java.util.regex.Matcher;
33 import java.util.regex.Pattern;
34
35 /**
36 * @author Andrew Rambaut
37 * @author Walter Xie
38 */
39 public class TraitGuesser implements Serializable {
40
41 private static final long serialVersionUID = 1786080930502001515L;
42
43 private final TraitData traitData;
44
45 public TraitGuesser(TraitData traitData) {
46 this.traitData = traitData;
47 }
48
49 public enum GuessType {
50 DELIMITER,
51 REGEX
52 }
53
54 private GuessType guessType = GuessType.DELIMITER;
55
56 private int order = 0;
57 private String delimiter;
58 private String regex;
59
60 private static final String REGEX_CHARACTERS = "|[].*()-^$";
61
62 ////////////////////////////////////////////////////////////////
63
64 public TraitData getTraitData() {
65 return traitData;
66 }
67
68 public GuessType getGuessType() {
69 return guessType;
70 }
71
72 public void setGuessType(GuessType guessType) {
73 this.guessType = guessType;
74 }
75
76 public int getOrder() {
77 return order;
78 }
79
80 public void setOrder(int order) {
81 this.order = order;
82 }
83
84 public String getDelimiter() {
85 return delimiter;
86 }
87
88 public void setDelimiter(String delimiter) {
89 this.delimiter = delimiter;
90 }
91
92 public String getRegex() {
93 return regex;
94 }
95
96 public void setRegex(String regex) {
97 this.regex = regex;
98 }
99
100 ////////////////////////////////////////////////////////////////
101 public void guessTrait(TaxonList taxa) {
102
103 for (int i = 0; i < taxa.getTaxonCount(); i++) {
104
105 String value = null;
106
107 try {
108 switch (guessType) {
109 case DELIMITER:
110 value = guessTraitByDelimiter(taxa.getTaxonId(i), delimiter);
111 break;
112 case REGEX:
113 value = guessTraitFromRegex(taxa.getTaxonId(i), regex);
114 break;
115 default:
116 throw new IllegalArgumentException("unknown GuessType");
117 }
118
119 } catch (GuessTraitException gfe) {
120 //
121 }
122
123 taxa.getTaxon(i).setAttribute(traitData.getName(), value);
124 }
125 }
126
127 private String guessTraitByDelimiter(String label, String delimiter) throws GuessTraitException {
128
129 StringBuilder sb = new StringBuilder();
130 for (int i = 0; i < delimiter.length(); i++) {
131 if (REGEX_CHARACTERS.contains("" + delimiter.charAt(i))) {
132 sb.append("\\").append(delimiter.charAt(i));
133 } else {
134 sb.append(delimiter.charAt(i));
135 }
136 }
137
138 if (sb.toString().length() < 1) {
139 throw new IllegalArgumentException("No delimiter");
140 }
141
142 String[] tokens = label.split(sb.toString());
143
144 if (tokens.length < 2) {
145 throw new IllegalArgumentException("Can not find delimiter in taxon label (" + label + ")\n or invalid delimiter (" + delimiter + ").");
146 }
147
148 if (order >= 0) {
149 if (order >= tokens.length) {
150 throw new IllegalArgumentException("Insufficent delimiters in taxon lable (" + label + ")\n to find requested field.");
151 }
152
153 return tokens[order];
154 } else {
155 if (tokens.length + order < 0) {
156 throw new IllegalArgumentException("Insufficent delimiters in taxon lable (" + label + ")\n to find requested field.");
157 }
158
159 return tokens[tokens.length + order];
160 }
161 }
162
163 private String guessTraitFromRegex(String label, String regex) throws GuessTraitException {
164 String t;
165
166 if (!regex.contains("(")) {
167 // if user hasn't specified a replace element, assume the whole regex should match
168 regex = "(" + regex + ")";
169 }
170
171 try {
172 Pattern pattern = Pattern.compile(regex);
173 Matcher matcher = pattern.matcher(label);
174 if (!matcher.find()) {
175 throw new GuessTraitException("Regular expression doesn't find a match in taxon label, " + label);
176 }
177
178 if (matcher.groupCount() < 1) {
179 throw new GuessTraitException("Trait value group not defined in regular expression");
180 }
181
182 t = matcher.group(1); // TODO: not working?
183 } catch (NumberFormatException nfe) {
184 throw new GuessTraitException("Badly formatted trait value in taxon label, " + label);
185 }
186
187 return t;
188 }
189 }