ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/owl/trunk/proteinstructure/NodeSet.java
Revision: 334
Committed: Tue Oct 2 07:35:26 2007 UTC (17 years ago) by stehr
File size: 2272 byte(s)
Log Message:
New class NodesAndEdges (simple graph class), some added javadocs
Line User Rev File contents
1 stehr 230 package proteinstructure;
2    
3     import java.util.TreeSet;
4     import java.util.Vector;
5 stehr 325 import java.util.regex.Matcher;
6     import java.util.regex.Pattern;
7 stehr 230
8 stehr 334 // TODO: Create a Node class (which can contain node coloring, class, residue type, etc.)
9 stehr 230 public class NodeSet extends TreeSet<Integer> {
10    
11     private static final long serialVersionUID = 1L;
12 stehr 325
13 stehr 230 /**
14     * Returns the set as a vector of intervals of consecutive elements.
15     * @return
16     */
17     public Vector<Interval> getIntervals() {
18     Vector<Interval> intervals = new Vector<Interval>();
19     if(this.size() == 0) return intervals;
20     // assuming that this set is sorted, otherwise sort it
21     int last = this.first(); // previous element
22     int start = last; // start if current interval
23     for(int i:this) {
24     if(i > last+1) {
25     // output interval and start new one
26     intervals.add(new Interval(start, last));
27     start = i;
28     last = i;
29     } else
30     if(i == last) {
31     // can that even happen?
32     } else
33     if(i == last+1) {
34     last = i;
35     }
36     }
37     // output last interval
38     intervals.add(new Interval(start, last));
39     return intervals;
40     }
41    
42 stehr 325 /**
43     * Returns true if selStr is a valid selection string in 'comma-hyphen' syntax, e.g. 1-3,5,7-8.
44     * @return true if selStr is a syntactically correct selection string, false otherwise
45     */
46     public static boolean isValidSelectionString(String selStr) {
47     Pattern p = Pattern.compile("\\d+(-\\d+)?(,\\d+(-\\d+)?)*");
48     Matcher m = p.matcher(selStr);
49     return m.matches();
50     }
51    
52     /**
53     * Create a new NodeSet from a selection string in 'comma-hyphen' nomenclature, e.g. 1-3,5,7-8.
54     * The validity of a selection string can be checked by isValidSelectionString().
55     * @return A node set corresponding to the given selection string or null of string is invalid.
56     */
57     public static NodeSet parseSelectionString(String selStr) {
58     if(!isValidSelectionString(selStr)) return null;
59     NodeSet newSet = new NodeSet();
60     String[] tokens = selStr.split(",");
61     for(String t:tokens) {
62     if(t.contains("-")) {
63     String[] range = t.split("-");
64     int from = Integer.parseInt(range[0]);
65     int to = Integer.parseInt(range[1]);
66     for(int i=from; i <= to; i++) {
67     newSet.add(i);
68     }
69    
70     } else {
71     int num = Integer.parseInt(t);
72     newSet.add(num);
73     }
74     }
75     return newSet;
76     }
77    
78 stehr 230 }