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