1 package uk.ac.ebi.intenz.domain.enzyme;
2
3 import java.util.StringTokenizer;
4
5 import uk.ac.ebi.biobabel.validator.DbIdentifierValidator;
6 import uk.ac.ebi.intenz.domain.DomainObject;
7 import uk.ac.ebi.intenz.domain.exceptions.EcException;
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 public class EnzymeCommissionNumber extends DomainObject
30 implements Comparable<EnzymeCommissionNumber> {
31
32
33
34 public final static EnzymeCommissionNumber UNDEF =
35 new EnzymeCommissionNumber(-1, -1, -1, -1, false);
36
37
38
39
40 public static final char PRELIMINARY_PREFIX = 'n';
41
42
43
44
45
46 public static enum Type {
47
48 CLASS,
49
50 SUBCLASS,
51
52 SUBSUBCLASS,
53
54 ENZYME,
55
56 PRELIMINARY,
57
58 UNDEF
59 }
60
61
62
63
64 private int ec1;
65
66
67
68
69 private int ec2;
70
71
72
73
74 private int ec3;
75
76
77
78
79 private int ec4;
80
81
82
83
84 private Type type;
85
86
87
88
89
90
91
92
93
94
95
96 private EnzymeCommissionNumber(int ec1, int ec2, int ec3, int ec4, boolean preliminary) {
97 super();
98 this.ec1 = ec1;
99 this.ec2 = ec2;
100 this.ec3 = ec3;
101 this.ec4 = ec4;
102 this.type = (ec1 == -1)? Type.UNDEF:
103 (ec2 == -1)? Type.CLASS:
104 (ec3 == -1)? Type.SUBCLASS:
105 (ec4 == -1)? Type.SUBSUBCLASS:
106 preliminary? Type.PRELIMINARY : Type.ENZYME;
107 }
108
109
110
111
112
113
114
115
116
117
118
119 public static EnzymeCommissionNumber valueOf(int ec1) throws EcException {
120 if (ec1 < 1) throw new EcException("The class number is invalid.");
121 return new EnzymeCommissionNumber(ec1, -1, -1, -1, false);
122 }
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140 public static EnzymeCommissionNumber valueOf(int ec1, int ec2) throws EcException {
141 if (ec1 < 1) throw new EcException("The class number is invalid.");
142 if (ec2 < 0) throw new EcException("The subclass number is invalid.");
143
144 return new EnzymeCommissionNumber(ec1, ec2, -1, -1, false);
145 }
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164 public static EnzymeCommissionNumber valueOf(int ec1, int ec2, int ec3) throws EcException {
165 if (ec1 < 1) throw new EcException("The class number is invalid.");
166 if (ec2 < 0) throw new EcException("The subclass number is invalid.");
167 if (ec3 < 0) throw new EcException("The sub-subclass number is invalid.");
168 if (ec1 == 0) {
169 if (ec2 > 0 || ec3 > 0) throw new EcException("Invalid EC.");
170 }
171 if (ec2 == 0) {
172 if (ec3 > 0) throw new EcException("Invalid EC.");
173 }
174
175 return new EnzymeCommissionNumber(ec1, ec2, ec3, -1, false);
176 }
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195 public static EnzymeCommissionNumber valueOf(int ec1, int ec2, int ec3, int ec4)
196 throws EcException {
197 return valueOf(ec1, ec2, ec3, ec4, false);
198 }
199
200
201
202
203
204
205
206
207
208
209
210 public static EnzymeCommissionNumber valueOf(int ec1, int ec2, int ec3, int ec4,
211 boolean preliminary) throws EcException{
212 if (ec1 < 1) throw new EcException("The class number is invalid.");
213 if (ec2 == 0) {
214 if (ec3 > 0 || ec4 > 0) throw new EcException("Invalid EC.");
215 }
216 if (ec2 < 0) {
217 if (ec3 > -1 || ec4 > -1) throw new EcException("Invalid EC.");
218 }
219 if (ec3 == 0) {
220 if (ec4 > 0) throw new EcException("Invalid EC.");
221 }
222 if (ec3 < 0) {
223 if (ec4 > -1) throw new EcException("Invalid EC.");
224 }
225 if (preliminary && (ec1 < 1 || ec2 < 1 || ec3 < 1 || ec4 < 1))
226 throw new EcException("Invalid preliminary EC");
227 return new EnzymeCommissionNumber(ec1, ec2, ec3, ec4, preliminary);
228 }
229
230
231
232
233
234
235
236
237
238
239
240 public static EnzymeCommissionNumber valueOf(String ecString)
241 throws EcException, NumberFormatException {
242 if (ecString == null)
243 throw new NullPointerException("Parameter 'ecString' must not be null.");
244 int ec1 = -1, ec2 = -1, ec3 = -1, ec4 = -1;
245 boolean preliminary = false;
246 int iii = 1;
247 for (StringTokenizer st = new StringTokenizer(ecString.trim(), "."); st.hasMoreTokens();) {
248 String ecPart = st.nextToken();
249 switch (iii) {
250 case 1:
251 ec1 = Integer.parseInt(ecPart);
252 break;
253 case 2:
254 ec2 = Integer.parseInt(ecPart);
255 break;
256 case 3:
257 ec3 = Integer.parseInt(ecPart);
258 break;
259 case 4:
260 if (ecPart.matches("\\*|-")){
261
262
263
264
265
266
267
268
269
270 } else if (ecPart.matches("\\d+")){
271 ec4 = Integer.parseInt(ecPart);
272 } else if (ecPart.matches("n\\d+")){
273 preliminary = true;
274 ec4 = Integer.parseInt(ecPart.substring(1));
275 } else {
276 throw new EcException("Invalid EC: " + ecString);
277 }
278 break;
279 }
280 iii++;
281 }
282 return EnzymeCommissionNumber.valueOf(ec1, ec2, ec3, ec4, preliminary);
283 }
284
285
286
287
288
289
290
291
292 public static EnzymeCommissionNumber copy(EnzymeCommissionNumber ecToCopy) {
293 if (ecToCopy == null)
294 throw new NullPointerException("Parameter 'ecToCopy' must not be null.");
295 return new EnzymeCommissionNumber(ecToCopy.ec1, ecToCopy.ec2, ecToCopy.ec3,
296 ecToCopy.ec4, Type.PRELIMINARY.equals(ecToCopy.getType()));
297 }
298
299
300
301
302
303
304
305
306 public int compareTo(EnzymeCommissionNumber ec) {
307 int ec1Diff = ec1 - ec.ec1;
308 if (ec1Diff != 0) return ec1Diff;
309
310 int ec2Diff = ec2 - ec.ec2;
311 if (ec2Diff != 0) return ec2Diff;
312
313 int ec3Diff = ec3 - ec.ec3;
314 if (ec3Diff != 0) return ec3Diff;
315
316 int typeDiff = type.ordinal() - ec.type.ordinal();
317 if (typeDiff != 0) return typeDiff;
318
319 int ec4Diff = ec4 - ec.ec4;
320 if (ec4Diff != 0) return ec4Diff;
321
322 return type.ordinal() - ec.type.ordinal();
323 }
324
325
326
327
328
329 public Type getType() {
330 return type;
331 }
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352 public static boolean isValid(String ecString) {
353 return DbIdentifierValidator.getInstance()
354 .validate(ecString, DbIdentifierValidator.EC_NUMBER);
355 }
356
357
358
359
360
361
362
363
364 public static boolean isPreliminary(String ecString){
365 return ecString.indexOf(PRELIMINARY_PREFIX) > -1;
366 }
367
368
369
370
371
372
373 public String toString() {
374 StringBuffer ecString = new StringBuffer();
375
376 if (ec1 == 0) {
377 ecString.append(ec1);
378 ecString.append(".");
379 ecString.append(ec2);
380 ecString.append(".");
381 ecString.append(ec3);
382 ecString.append(".");
383 ecString.append(ec4);
384 } else {
385 ecString.append(ec1);
386 if (ec2 > 0) {
387 ecString.append(".");
388 ecString.append(ec2);
389 if (ec3 > 0) {
390 ecString.append(".");
391 ecString.append(ec3);
392 if (ec4 > 0) {
393 ecString.append(".");
394 if (Type.PRELIMINARY.equals(type))
395 ecString.append('n');
396 ecString.append(ec4);
397 }
398 }
399 }
400 }
401
402 return ecString.toString();
403 }
404
405 @Override
406 public int hashCode() {
407 final int prime = 31;
408 int result = super.hashCode();
409 result = prime * result + ec1;
410 result = prime * result + ec2;
411 result = prime * result + ec3;
412 result = prime * result + ec4;
413 result = prime * result + ((type == null) ? 0 : type.hashCode());
414 return result;
415 }
416
417 @Override
418 public boolean equals(Object obj) {
419 if (this == obj)
420 return true;
421 if (!super.equals(obj))
422 return false;
423 if (getClass() != obj.getClass())
424 return false;
425 EnzymeCommissionNumber other = (EnzymeCommissionNumber) obj;
426 if (ec1 != other.ec1)
427 return false;
428 if (ec2 != other.ec2)
429 return false;
430 if (ec3 != other.ec3)
431 return false;
432 if (ec4 != other.ec4)
433 return false;
434 if (type == null) {
435 if (other.type != null)
436 return false;
437 } else if (!type.equals(other.type))
438 return false;
439 return true;
440 }
441
442
443
444
445
446
447 public int getEc1() {
448 return ec1;
449 }
450
451
452
453
454
455
456 public int getEc2() {
457 return ec2;
458 }
459
460
461
462
463
464
465 public int getEc3() {
466 return ec3;
467 }
468
469
470
471
472
473
474 public int getEc4() {
475 return ec4;
476 }
477
478
479
480 }