|
24 | 24 |
|
25 | 25 | public class SsldUtils { |
26 | 26 |
|
| 27 | + public enum Qualifier { |
| 28 | + FREE, BOUND, TOTAL, NONE; |
| 29 | + public static Qualifier fromPrefix(String s) { |
| 30 | + for (Qualifier q : values()) { |
| 31 | + if (q != NONE && q.name().equals(s)) { |
| 32 | + return q; |
| 33 | + } |
| 34 | + } |
| 35 | + return NONE; |
| 36 | + } |
| 37 | + } |
| 38 | + |
27 | 39 | // result entry from langevin output |
28 | 40 | public static class LangevinResult { |
29 | | - public final String qualifier; |
| 41 | + public final Qualifier qualifier; |
30 | 42 | public final String molecule; |
31 | 43 | public final String site; |
32 | 44 | public final String state; |
33 | 45 |
|
34 | 46 | public static LangevinResult fromString(String str) { |
35 | | - if(str == null || str.length() == 0) { |
| 47 | + if (str == null || str.isEmpty()) { |
36 | 48 | throw new RuntimeException("Langevin generated molecules name must contain some text"); |
37 | 49 | } |
38 | | - String qualifier = ""; |
39 | | - String molecule = ""; |
| 50 | + |
| 51 | + // Step 1: split only on DOUBLE underscore |
| 52 | + String[] parts = str.split("__"); |
| 53 | + |
| 54 | + String first = parts[0]; |
| 55 | + |
| 56 | + // Step 2: find first underscore |
| 57 | + int idx = first.indexOf('_'); |
| 58 | + |
| 59 | + Qualifier qualifier = Qualifier.NONE; |
| 60 | + String molecule; |
| 61 | + |
| 62 | + if (idx > 0) { |
| 63 | + String prefix = first.substring(0, idx); |
| 64 | + qualifier = Qualifier.fromPrefix(prefix); |
| 65 | + |
| 66 | + if (qualifier != Qualifier.NONE) { |
| 67 | + // Valid qualifier found |
| 68 | + molecule = first.substring(idx + 1); |
| 69 | + } else { |
| 70 | + // Not a valid qualifier → whole thing is molecule |
| 71 | + molecule = first; |
| 72 | + } |
| 73 | + } else { |
| 74 | + // No underscore at all → no qualifier |
| 75 | + molecule = first; |
| 76 | + } |
| 77 | + |
40 | 78 | String site = ""; |
41 | 79 | String state = ""; |
42 | 80 |
|
43 | | - String[] tokens = str.split("_{1,2}"); |
44 | | - if(tokens.length <1 || tokens.length > 4) { |
45 | | - throw new RuntimeException("Langevin generated molecules name must have between one and 4 tokens"); |
| 81 | + if (parts.length >= 2) { |
| 82 | + site = parts[1]; |
46 | 83 | } |
47 | | - for (int i=0; i<tokens.length; i++) { |
48 | | - String token = tokens[i]; |
49 | | - if(i == 0) { |
50 | | - qualifier = token; |
51 | | - } else if(i == 1) { |
52 | | - molecule = token; |
53 | | - } else if(i == 2) { |
54 | | - site = token; |
55 | | - } else if(i == 3) { |
56 | | - state = token; |
57 | | - } |
| 84 | + if (parts.length >= 3) { |
| 85 | + state = parts[2]; |
58 | 86 | } |
59 | | - LangevinResult lr = new LangevinResult(qualifier, molecule, site, state); |
60 | | - return lr; |
| 87 | + if (parts.length > 3) { |
| 88 | + throw new RuntimeException("Too many '__' sections in: " + str); |
| 89 | + } |
| 90 | + |
| 91 | + return new LangevinResult(qualifier, molecule, site, state); |
61 | 92 | } |
62 | 93 |
|
63 | | - public LangevinResult(String qualifier, String molecule, String site, String state) { |
| 94 | + public LangevinResult(Qualifier qualifier, String molecule, String site, String state) { |
64 | 95 | this.qualifier = qualifier; |
65 | 96 | this.molecule = molecule; |
66 | 97 | this.site = site; |
|
0 commit comments