View Javadoc

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   * A collection of <i>alternative</i> reactions catalysed by one enzyme entry.
18   * Each reaction can have a view set independently, and a IUBMB flag (main
19   * reaction assigned by the organization to an EC number). Note that only
20   * Rhea reactions can have this flag set (i.e. textual descriptions will never
21   * have it).
22   * <br>
23   * Note that sequential reactions (steps in an overall reaction) and coupled
24   * reactions (elementary reactions in undefined order) should be handled in
25   * Rhea.
26   * @author rafalcan
27   *
28   */
29  public class EnzymaticReactions implements Collection<VisibleReaction> {
30  
31      /**
32       * Set of reactions catalysed (controlled) by the enzyme.
33       */
34      private Set<VisibleReaction> reactions;
35  
36      /**
37       * Utility class to wrap a Reaction with the views where it is available
38       * and the IUBMB flag.
39       * @author rafalcan
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       * @return the number of reactions held by this object.
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       * @param view
89       * @return a List containing the Reactions whose view match the parameter.
90       *      For the IntEnz view, if there is at least one public Rhea reaction,
91       *      only public Rhea reactions are returned. Obsolete Rhea reactions
92       *      are ignored.
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      * Filters this collection of reactions for a given view.
104      * @param view the view used as filter.
105      * @return a subset of this collection, containing only reactions in the
106      * 		given view. For the IntEnz view, if there is at least one public
107      * 		Rhea reaction, only public Rhea reactions are returned.
108      * @since 4.3.0
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) // ignore OBs
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      * Has a reaction the IUBMB flag set?
151      * @since 4.2.7
152      * @param i the index of the reaction.
153      * @return the IUBMB flag.
154      */
155     public boolean getReactionIubmbFlag(int i){
156     	return ((VisibleReaction) reactions.toArray()[i]).iubmb;
157     }
158 
159     /**
160      * Adds a reaction to the list. If it is already listed, even with a
161      * different web view, nothing is done.
162      * @param reaction
163      * @param view
164      * @param iubmb
165      * @return <code>true</code> if the set of reactions is changed.
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      * Adds several reactions to the list in one go.
174      * @param er
175      * @return <code>true</code> if the set of reactions is changed.
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      * Removes a reaction from the list, no matter its view.
189      * @param reaction
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 }