1 package uk.ac.ebi.intenz.domain.enzyme;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Iterator;
6 import java.util.LinkedHashSet;
7 import java.util.List;
8 import java.util.Set;
9
10 import uk.ac.ebi.intenz.domain.constants.EnzymeViewConstant;
11 import uk.ac.ebi.intenz.domain.constants.View;
12 import uk.ac.ebi.intenz.domain.enzyme.EnzymaticReactions.VisibleReaction;
13 import uk.ac.ebi.rhea.domain.Reaction;
14 import uk.ac.ebi.rhea.domain.Status;
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 public class EnzymaticReactions implements Collection<VisibleReaction> {
30
31
32
33
34 private Set<VisibleReaction> reactions;
35
36
37
38
39
40
41 public class VisibleReaction {
42 private Reaction reaction;
43 private EnzymeViewConstant view;
44 private boolean iubmb;
45 private VisibleReaction(Reaction reaction, EnzymeViewConstant view,
46 boolean iubmb){
47 this.reaction = reaction;
48 this.view = view;
49 this.iubmb = iubmb;
50 }
51 public Reaction getReaction() {
52 return reaction;
53 }
54 public EnzymeViewConstant getView() {
55 return view;
56 }
57 public boolean isIubmb() {
58 return iubmb;
59 }
60 @Override
61 public boolean equals(Object obj) {
62 if (obj == this) return true;
63 if (!(obj instanceof VisibleReaction)) return false;
64 return ((VisibleReaction) obj).reaction.equals(reaction);
65 }
66 @Override
67 public int hashCode(){
68 return reaction.hashCode();
69 }
70 }
71
72 public EnzymaticReactions(){
73 reactions = new LinkedHashSet<VisibleReaction>(4);
74 }
75
76
77
78
79 public int size(){
80 return reactions.size();
81 }
82
83 public Reaction getReaction(int i){
84 return ((VisibleReaction) reactions.toArray()[i]).reaction;
85 }
86
87
88
89
90
91
92
93
94 public List<Reaction> getReactions(View theView){
95 List<Reaction> reactionsInView = new ArrayList<Reaction>();
96 for (VisibleReaction vr : forView(theView)) {
97 reactionsInView.add(vr.reaction);
98 }
99 return reactionsInView;
100 }
101
102
103
104
105
106
107
108
109
110 public EnzymaticReactions forView(View theView){
111 boolean isInView, isRhea, isPublicRhea;
112 EnzymaticReactions reactionsInView = new EnzymaticReactions();
113 EnzymaticReactions rheaReactions = new EnzymaticReactions();
114 for (Iterator<VisibleReaction> it = reactions.iterator(); it.hasNext();) {
115 VisibleReaction wr = it.next();
116 isRhea = wr.reaction.getId() > Reaction.NO_ID_ASSIGNED;
117 isPublicRhea = isRhea
118 && wr.reaction.getStatus().isPublic()
119 && !wr.reaction.getStatus().equals(Status.OB)
120 && wr.reaction.isMapped();
121 switch (theView) {
122 case INTENZ:
123 isInView = wr.view.isInIntEnzView();
124 if (isInView){
125 if (!isRhea) {
126 reactionsInView.add(wr);
127 } else if (isPublicRhea){
128 rheaReactions.add(wr);
129 }
130 }
131 break;
132 case IUBMB:
133 isInView = wr.view.isInIUBMBView();
134 if (isInView && !isRhea) reactionsInView.add(wr);
135 break;
136 case SIB:
137 isInView = wr.view.isInSIBView();
138 if (isInView && !isRhea) reactionsInView.add(wr);
139 break;
140 }
141 }
142 return rheaReactions.isEmpty()? reactionsInView : rheaReactions;
143 }
144
145 public EnzymeViewConstant getReactionView(int i){
146 return ((VisibleReaction) reactions.toArray()[i]).view;
147 }
148
149
150
151
152
153
154
155 public boolean getReactionIubmbFlag(int i){
156 return ((VisibleReaction) reactions.toArray()[i]).iubmb;
157 }
158
159
160
161
162
163
164
165
166
167 public boolean add(Reaction reaction, String view, boolean iubmb){
168 return reactions.add(new VisibleReaction(
169 reaction, EnzymeViewConstant.valueOf(view), iubmb));
170 }
171
172
173
174
175
176
177 public boolean add(EnzymaticReactions er){
178 boolean changed = false;
179 if (er != null && er.size() > 0){
180 if (reactions == null)
181 reactions = new LinkedHashSet<VisibleReaction>(4);
182 changed = reactions.addAll(er.reactions);
183 }
184 return changed;
185 }
186
187
188
189
190
191 public void removeReaction(Reaction reaction){
192 for (Iterator<VisibleReaction> it = reactions.iterator(); it.hasNext();) {
193 VisibleReaction wr = it.next();
194 if (wr.reaction.equals(reaction)){
195 it.remove();
196 break;
197 }
198 }
199 }
200
201 public void setReactions(LinkedHashSet<VisibleReaction> reactions) {
202 this.reactions = reactions;
203 }
204
205 @Override
206 public boolean equals(Object obj) {
207 if (this == obj) return true;
208 if (!(obj instanceof EnzymaticReactions)) return false;
209 EnzymaticReactions er = (EnzymaticReactions) obj;
210 if (er.reactions.size() != this.reactions.size()) return false;
211 for (int i = 0; i < this.reactions.size(); i++) {
212 if (!this.getReaction(i).equals(er.getReaction(i)))
213 return false;
214 if (!this.getReactionView(i).equals(er.getReactionView(i)))
215 return false;
216 }
217 return true;
218 }
219
220 @Override
221 public int hashCode() {
222 int hash = 3;
223 hash = 61 * hash + (this.reactions != null ? this.reactions.hashCode() : 0);
224 return hash;
225 }
226
227 public Iterator<VisibleReaction> iterator() {
228 return reactions.iterator();
229 }
230
231 public boolean add(VisibleReaction arg0) {
232 return reactions.add(arg0);
233 }
234
235 public boolean addAll(Collection<? extends VisibleReaction> arg0) {
236 return reactions.addAll(arg0);
237 }
238
239 public void clear() {
240 reactions.clear();
241 }
242
243 public boolean contains(Object arg0) {
244 return reactions.contains(arg0);
245 }
246
247 public boolean containsAll(Collection<?> arg0) {
248 return reactions.containsAll(arg0);
249 }
250
251 public boolean isEmpty() {
252 return reactions.isEmpty();
253 }
254
255 public boolean remove(Object arg0) {
256 return reactions.remove(arg0);
257 }
258
259 public boolean removeAll(Collection<?> arg0) {
260 return reactions.removeAll(arg0);
261 }
262
263 public boolean retainAll(Collection<?> arg0) {
264 return reactions.retainAll(arg0);
265 }
266
267 public Object[] toArray() {
268 return reactions.toArray();
269 }
270
271 public <VisibleReaction> VisibleReaction[] toArray(VisibleReaction[] arg0) {
272 return null;
273 }
274
275 }