ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/owl/trunk/proteinstructure/RIGNbhood.java
Revision: 515
Committed: Mon Jan 14 10:02:28 2008 UTC (16 years, 9 months ago) by stehr
File size: 8997 byte(s)
Log Message:
Interval: allow spaces in residue selection strings
RIGNbhood: added method getNeighbors
Line User Rev File contents
1 duarte 420 package proteinstructure;
2    
3 duarte 452 import java.util.ArrayList;
4     import java.util.Collection;
5 stehr 515 import java.util.LinkedList;
6 duarte 420 import java.util.TreeMap;
7    
8    
9 stehr 515 /**
10     * A neighbourhood in a RIGraph, i.e. a collection of nodes connected to a 'central' node.
11     * Note that the collection of neighbours now also contains the central residue.
12     * To get strictly the neighbours, use the getNeighbors() method or grab them directly
13     * from the original graph using getNeighbors().
14     */
15 duarte 420 public class RIGNbhood extends TreeMap<Integer,RIGNode> {
16    
17    
18     private static final long serialVersionUID = 1L;
19    
20     public static final String centralLetter="x";
21 duarte 452 public static final String gapLetter="_";
22 duarte 420
23     // central residue
24     private RIGNode centralResidue;
25    
26     /**
27     * Construct a RIGNbhood by passing the central residue as a RIGNode object
28 duarte 452 * and all neighbors as a Collection<RIGNode>
29 duarte 420 * @param centralResidue
30 duarte 452 * @param neighbors
31 duarte 420 */
32 duarte 452 public RIGNbhood(RIGNode centralResidue, Collection<RIGNode> neighbors){
33 duarte 420 super();
34     this.centralResidue = centralResidue;
35 duarte 452 this.put(centralResidue.getResidueSerial(), centralResidue);
36     for (RIGNode node:neighbors) {
37     this.put(node.getResidueSerial(),node);
38     }
39 duarte 420 }
40 duarte 452
41     /**
42     * Constructs a RIGNbhood with just a central residue and no neighbors
43     * @param centralResidue
44     */
45     public RIGNbhood(RIGNode centralResidue) {
46     super();
47     this.centralResidue = centralResidue;
48     this.put(centralResidue.getResidueSerial(), centralResidue);
49     }
50 duarte 420
51     public RIGNode getCentralResidue() {
52     return centralResidue;
53     }
54    
55 duarte 452 /**
56     * Returns true if one of the neighbors or the central residue is of the given residue type
57     * @param resType
58     * @return
59     */
60     public boolean containsResType(String resType) {
61     for (RIGNode node:this.values()) {
62     if (node.getResidueType().equals(resType)) {
63     return true;
64     }
65     }
66     return false;
67     }
68    
69     /**
70     * Returns whether this RIGNbhood is equal to the given one
71     * Equality defined as: same residue types in the same order, both to the left and right of central residue
72     * @param other
73     * @return
74     */
75     public boolean equals(Object other) {
76    
77    
78     RIGNbhood otherNbhood = (RIGNbhood) other;
79     if (this.size()!=otherNbhood.size()) {
80     return false;
81     }
82     if (!this.centralResidue.getResidueType().equals(otherNbhood.centralResidue.getResidueType())) {
83     return false;
84     }
85    
86     // we order the RIGNodes in 2 groups: left and right of the central residues (both for this and other)
87     ArrayList<RIGNode> thisLeft = new ArrayList<RIGNode>();
88     ArrayList<RIGNode> thisRight = new ArrayList<RIGNode>();
89     ArrayList<RIGNode> otherLeft = new ArrayList<RIGNode>();
90     ArrayList<RIGNode> otherRight = new ArrayList<RIGNode>();
91     for (RIGNode node:this.values()) {
92     if (node.getResidueSerial()<this.centralResidue.getResidueSerial()) {
93     thisLeft.add(node);
94     } else if (node.getResidueSerial()!=this.centralResidue.getResidueSerial()){
95     thisRight.add(node);
96     }
97     }
98     for (RIGNode node:otherNbhood.values()) {
99     if (node.getResidueSerial()<otherNbhood.centralResidue.getResidueSerial()) {
100     otherLeft.add(node);
101     } else if (node.getResidueSerial()!=otherNbhood.centralResidue.getResidueSerial()){
102     otherRight.add(node);
103     }
104     }
105    
106     // if sizes don't match they are different
107     if (thisLeft.size()!=otherLeft.size()) {
108     return false;
109     }
110     if (thisRight.size()!=otherRight.size()) {
111     return false;
112     }
113    
114     // as the RIGNodes are ordered as originally (sequence order) we simply compare if types match (left and then right)
115     for (int i=0;i<thisLeft.size();i++) {
116     if (!thisLeft.get(i).getResidueType().equals(otherLeft.get(i).getResidueType())) {
117     return false;
118     }
119     }
120     for (int i=0;i<thisRight.size();i++) {
121     if (!thisRight.get(i).getResidueType().equals(otherRight.get(i).getResidueType())) {
122     return false;
123     }
124     }
125    
126     // if we are here, we passed all tests: they are equal!
127     return true;
128     }
129    
130    
131     /**
132     * Returns true if this neighborhood matches the given one.
133     * A match is defined as: both left and right sides of the this central
134     * residue are substrings of other's left and right sides.
135     * e.g.:
136     * this: ABxEF matches:
137     * other: --A-----B---xE---F--- (with '-' being other residues in between)
138     * @param other
139     * @return
140     */
141     public boolean match(RIGNbhood other) {
142     // we order the RIGNodes in 2 groups: left and right of the central residues (both for this and other)
143     ArrayList<RIGNode> thisLeft = new ArrayList<RIGNode>();
144     ArrayList<RIGNode> thisRight = new ArrayList<RIGNode>();
145     ArrayList<RIGNode> otherLeft = new ArrayList<RIGNode>();
146     ArrayList<RIGNode> otherRight = new ArrayList<RIGNode>();
147     for (RIGNode node:this.values()) {
148     if (node.getResidueSerial()<this.centralResidue.getResidueSerial()) {
149     thisLeft.add(node);
150     } else if (node.getResidueSerial()!=this.centralResidue.getResidueSerial()){
151     thisRight.add(node);
152     }
153     }
154     for (RIGNode node:other.values()) {
155     if (node.getResidueSerial()<other.centralResidue.getResidueSerial()) {
156     otherLeft.add(node);
157     } else if (node.getResidueSerial()!=other.centralResidue.getResidueSerial()){
158     otherRight.add(node);
159     }
160     }
161    
162     // as the RIGNodes are ordered as originally (sequence order) we simply compare if types match (left and then right)
163     int i = 0;
164     int j = 0;
165     while (i<thisLeft.size()) {
166     if (j==otherLeft.size()) {
167     return false;
168     }
169     if (thisLeft.get(i).getResidueType().equals(otherLeft.get(j).getResidueType())) {
170     i++;
171     j++;
172     } else {
173     j++;
174     }
175     }
176    
177     i = 0;
178     j = 0;
179     while (i<thisRight.size()) {
180     if (j==otherRight.size()) {
181     return false;
182     }
183     if (thisRight.get(i).getResidueType().equals(otherRight.get(j).getResidueType())) {
184     i++;
185     j++;
186     } else {
187     j++;
188     }
189     }
190    
191     // if we are here, we passed all tests: they match!
192     return true;
193     }
194    
195 duarte 420 public String getMotifFullGaps(){
196     String motif="";
197 duarte 452 for (int i=this.firstKey();i<=this.lastKey();i++) {
198     if (this.containsKey(i)){
199     if (i!=centralResidue.getResidueSerial()){
200 duarte 420 motif+=AAinfo.threeletter2oneletter(this.get(i).getResidueType());
201 duarte 452 } else {
202 duarte 420 motif+=centralLetter;
203     }
204 duarte 452 } else {
205     motif+=gapLetter;
206 duarte 420 }
207     }
208     return motif;
209     }
210    
211     public String getMotif(){
212     String motif="";
213 duarte 452 int gapSize = 0;
214     String gap = "";
215     for (int i=this.firstKey();i<=this.lastKey();i++) {
216     if (this.containsKey(i)){
217     if (i!=centralResidue.getResidueSerial()){
218 duarte 420 motif+=gap;
219     motif+=AAinfo.threeletter2oneletter(this.get(i).getResidueType());
220     gapSize=0;
221     gap="";
222 duarte 452 } else {
223 duarte 420 motif+=gap;
224     motif+=centralLetter;
225     gapSize=0;
226     gap="";
227     }
228 duarte 452 } else {
229     gapSize++;
230     gap=gapLetter+"{"+gapSize+"}";
231 duarte 420 }
232     }
233     return motif;
234     }
235    
236     public String getMotifNoGaps(){
237     String motif="";
238 duarte 452 for (int i:this.keySet()) {
239     if (i!=centralResidue.getResidueSerial()){
240     motif+=AAinfo.threeletter2oneletter(this.get(i).getResidueType());
241     } else {
242     motif+=centralLetter;
243 duarte 420 }
244     }
245     return motif;
246     }
247    
248     public String getMotifReducedAlphabet(RIGraph graph) {
249     String motif="";
250 duarte 452 for (int i:this.keySet()) {
251     if (i!=centralResidue.getResidueSerial()){
252     // as long as the graph is undirected, JUNG finds the edge correctly independently of the order in which we give the nodes
253     RIGEdge edge = graph.findEdge(centralResidue,this.get(i));
254     double weight = edge.getWeight();
255     if (weight>0) { // SC dominated
256     motif+=AAinfo.threeletter2oneletter(this.get(i).getResidueType());
257     } else if (weight<0) { //BB dominated
258     // for the sec structure we take the "seen" node (so not the central but the other), following Michael's convention
259     char ssType = this.get(i).getSecStrucElement().getType();
260     char ssLetter = 'o';
261     if (ssType == 0 || ssType == 'O') ssLetter='o';
262     if (ssType=='S') ssLetter = 'b';
263     else if (ssType=='H') ssLetter= 'z';
264     motif+=ssLetter;
265 duarte 420 }
266 duarte 452 } else {
267     motif+=centralLetter;
268 duarte 420 }
269     }
270 duarte 452 return motif;
271 duarte 420 }
272    
273     public String getCommaSeparatedResSerials(){
274     String ressers="";
275     for (int resser:this.keySet()){
276 duarte 452 if (resser!=centralResidue.getResidueSerial()) {
277 duarte 454 ressers += resser+",";
278 duarte 452 }
279 duarte 420 }
280 duarte 454 // we chop off the last comma
281     if (ressers.length()!=0) {
282     ressers = ressers.substring(0, ressers.length()-1);
283     }
284 duarte 420 return ressers;
285     }
286    
287     public String toString(){
288     if (this.isEmpty()) return "";
289     else return this.getMotif();
290     }
291    
292     /**
293 stehr 515 * Returns a collection of the neighbours in this RIGNbh.
294     * @return A (possibly empty) collection of RIGNodes.
295     */
296     public Collection<RIGNode> getNeighbors() {
297     LinkedList<RIGNode> nbs = new LinkedList<RIGNode>();
298     for(RIGNode n:this.values()) {
299     if(n != centralResidue) {
300     nbs.add(n);
301     }
302     }
303     return nbs;
304     }
305    
306     /**
307 duarte 452 * Returns a copy (deep) of this RIGNbhood
308 duarte 420 * @return
309     */
310     public RIGNbhood copy(){
311 duarte 452 RIGNbhood copy = new RIGNbhood(centralResidue.copy());
312     for (int residueSerial:this.keySet()) {
313     if (residueSerial!=centralResidue.getResidueSerial()) {
314     copy.put(residueSerial, this.get(residueSerial).copy());
315     }
316 duarte 420 }
317     return copy;
318     }
319     }