1 |
package proteinstructure; |
2 |
import java.lang.Comparable; |
3 |
import java.util.TreeSet; |
4 |
import java.util.regex.Matcher; |
5 |
import java.util.regex.Pattern; |
6 |
|
7 |
/** |
8 |
* Class representing an integer interval with a beginning |
9 |
* and an end integers |
10 |
* |
11 |
*/ |
12 |
public class Interval implements Comparable { |
13 |
|
14 |
public int beg; |
15 |
public int end; |
16 |
|
17 |
public Interval(int beg,int end){ |
18 |
this.beg=beg; |
19 |
this.end=end; |
20 |
} |
21 |
|
22 |
public int compareTo(Object o) { |
23 |
Interval other = (Interval) o; |
24 |
if (this.beg>other.beg){ |
25 |
return 1; |
26 |
} |
27 |
else if (this.beg<other.beg){ |
28 |
return -1; |
29 |
} |
30 |
else if (this.beg==other.beg){ |
31 |
if (this.end>other.end){ |
32 |
return 1; |
33 |
} |
34 |
else if (this.end<other.end){ |
35 |
return -1; |
36 |
} |
37 |
} |
38 |
return 0; // if none of the conditions before returned, then both beg and end are equal |
39 |
} |
40 |
|
41 |
public int getLength(){ |
42 |
return (end - beg); |
43 |
} |
44 |
|
45 |
public boolean equals(Object o){ |
46 |
Interval other = (Interval) o; |
47 |
if (this.beg==other.beg && this.end==other.end){ |
48 |
return true; |
49 |
} |
50 |
return false; |
51 |
} |
52 |
|
53 |
public String toString() { |
54 |
return this.beg+" "+this.end; |
55 |
} |
56 |
|
57 |
|
58 |
/*----------------------------- static methods --------------------------------*/ |
59 |
|
60 |
/** |
61 |
* Returns true if selStr is a valid selection string in 'comma-hyphen' syntax, e.g. 1-3,5,7-8. |
62 |
* @return true if selStr is a syntactically correct selection string, false otherwise |
63 |
*/ |
64 |
public static boolean isValidSelectionString(String selStr) { |
65 |
Pattern p = Pattern.compile("\\d+(-\\d+)?(,\\d+(-\\d+)?)*"); |
66 |
Matcher m = p.matcher(selStr); |
67 |
return m.matches(); |
68 |
} |
69 |
|
70 |
/** |
71 |
* Create a new TreeSet of integers from a selection string in 'comma-hyphen' nomenclature, e.g. 1-3,5,7-8. |
72 |
* The validity of a selection string can be checked by isValidSelectionString(). |
73 |
* @return A TreeSet of integers corresponding to the given selection string or null of string is invalid. |
74 |
*/ |
75 |
public static TreeSet<Integer> parseSelectionString(String selStr) { |
76 |
if(!isValidSelectionString(selStr)) return null; |
77 |
TreeSet<Integer> newSet = new TreeSet<Integer>(); |
78 |
String[] tokens = selStr.split(","); |
79 |
for(String t:tokens) { |
80 |
if(t.contains("-")) { |
81 |
String[] range = t.split("-"); |
82 |
int from = Integer.parseInt(range[0]); |
83 |
int to = Integer.parseInt(range[1]); |
84 |
for(int i=from; i <= to; i++) { |
85 |
newSet.add(i); |
86 |
} |
87 |
|
88 |
} else { |
89 |
int num = Integer.parseInt(t); |
90 |
newSet.add(num); |
91 |
} |
92 |
} |
93 |
return newSet; |
94 |
} |
95 |
|
96 |
/** |
97 |
* Returns the set as a vector of intervals of consecutive elements. |
98 |
* @return |
99 |
*/ |
100 |
public static IntervalSet getIntervals(TreeSet<Integer> intSet) { |
101 |
IntervalSet intervals = new IntervalSet(); |
102 |
if(intSet.size() == 0) return intervals; |
103 |
// intSet is sorted becaus it's a TreeSet |
104 |
int last = intSet.first(); // previous element |
105 |
int start = last; // start if current interval |
106 |
for(int n:intSet) { |
107 |
int i = n; |
108 |
if(i > last+1) { |
109 |
// output interval and start new one |
110 |
intervals.add(new Interval(start, last)); |
111 |
start = i; |
112 |
last = i; |
113 |
} else |
114 |
if(i == last) { |
115 |
// can that even happen? |
116 |
} else |
117 |
if(i == last+1) { |
118 |
last = i; |
119 |
} |
120 |
} |
121 |
// output last interval |
122 |
intervals.add(new Interval(start, last)); |
123 |
return intervals; |
124 |
} |
125 |
} |