View Javadoc

1   package uk.ac.ebi.intenz.domain.enzyme;
2   
3   import java.util.Map;
4   import java.util.StringTokenizer;
5   import java.util.TreeMap;
6   
7   import uk.ac.ebi.intenz.domain.constants.EnzymeSourceConstant;
8   import uk.ac.ebi.intenz.domain.constants.EnzymeViewConstant;
9   import uk.ac.ebi.intenz.domain.constants.XrefDatabaseConstant;
10  
11  /**
12   * An enzyme link is used to store cross-reference information.
13   * <p/>
14   * Most of the links use a static URL which differs only in the EC number used to identify the enzyme.
15   * These links can be obtained by accessing the class constants.<br/>
16   * Other cross-references contain a static URL but need a different identifier. For example,
17   * a SwissProt cross-reference needs a protein accession number. These links are provided
18   * via the according factory methods. In case of SwissProt and MIM links it is also possible
19   * to provide a name for the protein or disease respectively.<br/>
20   * <p/>
21   * The terms <i>link</i> and <i>cross-reference</i> are used synonymously.
22   * <p/>
23   * Instances of this class are immutable.
24   *
25   * @author Michael Darsow
26   * @version $Revision: 1.2 $ $Date: 2008/01/28 12:33:00 $
27   */
28  public class EnzymeLink implements Comparable<EnzymeLink>, Commented, Viewable {
29    /**
30     * Static link to the BRENDA database.
31     */
32    public static final EnzymeLink BRENDA =
33  	  new EnzymeLink(XrefDatabaseConstant.BRENDA, XrefDatabaseConstant.BRENDA.getUrl(),
34  			  "", "BRENDA", EnzymeSourceConstant.INTENZ, EnzymeViewConstant.INTENZ);
35    
36    /**
37     * Link to MetaCyc.
38     */
39    public static final EnzymeLink METACYC =
40  	  new EnzymeLink(XrefDatabaseConstant.METACYC, XrefDatabaseConstant.METACYC.getUrl(),
41  			  "", "MetaCyc", EnzymeSourceConstant.INTENZ, EnzymeViewConstant.INTENZ);
42  
43    /**
44     * Static link to the KEGG database.
45     */
46    public static final EnzymeLink KEGG =
47  	  new EnzymeLink(XrefDatabaseConstant.KEGG, XrefDatabaseConstant.KEGG.getUrl(),
48  			  "", "", EnzymeSourceConstant.INTENZ, EnzymeViewConstant.INTENZ);
49  
50    /**
51     * Static link to the ENZYME database at expasy.org.
52     */
53    public static final EnzymeLink EXPASY =
54  	  new EnzymeLink(XrefDatabaseConstant.ENZYME, XrefDatabaseConstant.ENZYME.getUrl(),
55  			  "", "", EnzymeSourceConstant.INTENZ, EnzymeViewConstant.INTENZ);
56  
57    /**
58     * Static link to the GM pages (NC-IUBMB).
59     */
60    public static final EnzymeLink NC_IUBMB =
61  	  new EnzymeLink(XrefDatabaseConstant.NC_IUBMB, XrefDatabaseConstant.NC_IUBMB.getUrl(),
62  			  "", "", EnzymeSourceConstant.IUBMB, EnzymeViewConstant.INTENZ);
63  
64    /**
65     * Static link to the ERGO database.
66     */
67    public static final EnzymeLink ERGO =
68  	  new EnzymeLink(XrefDatabaseConstant.ERGO, XrefDatabaseConstant.ERGO.getUrl(),
69  			  "", "ERGO", EnzymeSourceConstant.INTENZ, EnzymeViewConstant.INTENZ);
70  
71    /**
72     * Static link to the ERGO (formerly WIT) database.
73     */
74    public static final EnzymeLink WIT =
75  	  new EnzymeLink(XrefDatabaseConstant.WIT, XrefDatabaseConstant.WIT.getUrl(),
76  			  "", "", EnzymeSourceConstant.INTENZ, EnzymeViewConstant.INTENZ);
77  
78    /**
79     * Static link to the Gene Ontology database.
80     */
81    public static final EnzymeLink GO =
82  	  new EnzymeLink(XrefDatabaseConstant.GO, XrefDatabaseConstant.GO.getUrl(),
83  			  "", "", EnzymeSourceConstant.INTENZ, EnzymeViewConstant.INTENZ);
84  
85    /**
86     * Static link to the NIST 74 database.
87     */
88    public static final EnzymeLink NIST74 =
89  	  new EnzymeLink(XrefDatabaseConstant.NIST74, XrefDatabaseConstant.NIST74.getUrl(),
90  			  "", "", EnzymeSourceConstant.INTENZ, EnzymeViewConstant.INTENZ);
91  
92    /**
93     * Static link to the UM-BBD database.
94     */
95    public static final EnzymeLink UMBBD =
96  	  new EnzymeLink(XrefDatabaseConstant.UMBBD, XrefDatabaseConstant.UMBBD.getUrl(),
97  			  "", "", EnzymeSourceConstant.INTENZ, EnzymeViewConstant.INTENZ);
98  
99    /**
100    * Static link to the PDB database.
101    */
102   public static final EnzymeLink PDB =
103 	  new EnzymeLink(XrefDatabaseConstant.PDB, XrefDatabaseConstant.PDB.getUrl(),
104 			  "", "", EnzymeSourceConstant.INTENZ, EnzymeViewConstant.INTENZ);
105 
106   /**
107    * Static link to the CSA database.
108    */
109   public static final EnzymeLink CSA =
110 	  new EnzymeLink(XrefDatabaseConstant.CSA, XrefDatabaseConstant.CSA.getUrl(),
111 			  "", "", EnzymeSourceConstant.INTENZ, EnzymeViewConstant.INTENZ);
112 
113   private static Map<XrefDatabaseConstant, EnzymeLink> STATIC_LINKS;
114 
115   static {
116     STATIC_LINKS = new TreeMap<XrefDatabaseConstant, EnzymeLink>();
117     STATIC_LINKS.put(BRENDA.getXrefDatabaseConstant(), BRENDA);
118     STATIC_LINKS.put(METACYC.getXrefDatabaseConstant(), METACYC);
119     STATIC_LINKS.put(KEGG.getXrefDatabaseConstant(), KEGG);
120     STATIC_LINKS.put(EXPASY.getXrefDatabaseConstant(), EXPASY);
121     STATIC_LINKS.put(ERGO.getXrefDatabaseConstant(), ERGO);
122 //    STATIC_LINKS.put(GO.getXrefDatabaseConstant(), GO);
123     STATIC_LINKS.put(NIST74.getXrefDatabaseConstant(), NIST74);
124     STATIC_LINKS.put(UMBBD.getXrefDatabaseConstant(), UMBBD);
125     STATIC_LINKS.put(PDB.getXrefDatabaseConstant(), PDB);
126   }
127 
128   /**
129    * All sources are controlled vocabulary.
130    */
131   private XrefDatabaseConstant xrefDatabaseConstant;
132 
133   /**
134    * Some links have specific URLs.
135    */
136   private String specificUrl;
137 
138   /**
139    * Some links need an accession number (e.g. EC number).
140    */
141   private String accession;
142 
143   /**
144    * Some links have name associated (e.g. MIM links).
145    */
146   private String name;
147 
148   private EnzymeSourceConstant source;
149 
150   /**
151    * Defines in which view this link will be displayed.
152    */
153   private EnzymeViewConstant view;
154 
155     private String dataComment;
156 
157     /**
158    * Object cannot be created outside this class.
159    *
160    * @param xrefDatabaseConstant The data xrefDatabaseConstant name.
161    * @param specificUrl          The URL of the given xrefDatabaseConstant.
162    * @param accession            The accession (if needed).
163    * @param name                 The name (if available).
164    */
165   private EnzymeLink(XrefDatabaseConstant xrefDatabaseConstant, String specificUrl, String accession, String name,
166                      EnzymeSourceConstant source, EnzymeViewConstant view) {
167     this.xrefDatabaseConstant = xrefDatabaseConstant;
168     this.specificUrl = specificUrl;
169     this.accession = accession;
170     this.name = name;
171     this.source = source;
172     this.view = view;
173   }
174 
175   /**
176    * Returns an <code>EnzymeLink</code> instance.
177    *
178    * @param xrefDatabaseConstant The link's xrefDatabaseConstant.
179    * @param specificUrl          The link's URL.
180    * @param accession            The accession of this link.
181    * @param name                 The name of this link.
182    * @return the <code>EnzymeLink</code> instance.
183    * @throws NullPointerException     if <code>xrefDatabaseConstant</code> is <code>null</code>.
184    * @throws IllegalArgumentException if the arguments are not sufficient to create a link.
185    */
186   public static EnzymeLink valueOf(XrefDatabaseConstant xrefDatabaseConstant,
187 		  String specificUrl, String accession, String name,
188           EnzymeSourceConstant source, EnzymeViewConstant view) {
189     if (xrefDatabaseConstant == null) throw new NullPointerException("Parameter 'xrefDatabaseConstant' must not be null.");
190     if (source == null) throw new NullPointerException("Parameter 'source' must not be null.");
191     if (view == null) throw new NullPointerException("Parameter 'view' must not be null.");
192 
193     // Static links.
194 //      if (view.toString().equals("") && STATIC_LINKS.containsKey(xrefDatabaseConstant))
195     // Links based on EC number, except when they are passed as accession (BRENDA):
196     if (STATIC_LINKS.containsKey(xrefDatabaseConstant)){
197     	if (accession == null){
198             return STATIC_LINKS.get(xrefDatabaseConstant);
199     	} else {
200     		// A static link to an EC number, but we have an accession? Preliminary ECs
201     		return new EnzymeLink(xrefDatabaseConstant,
202     				xrefDatabaseConstant.getUrl(),
203     				accession, accession, source, view);
204     	}
205     }
206 
207     if ((specificUrl == null || specificUrl.equals("")) && (accession == null || accession.equals("")) &&
208         (name == null || name.equals("")))
209       throw new IllegalArgumentException("Either parameter specificUrl, accession or name must contain a value.");
210 
211     return new EnzymeLink(xrefDatabaseConstant, specificUrl, accession, name, source, view);
212   }
213 
214   /**
215    * Returns a commented <code>EnzymeLink</code> instance.
216    * @param xrefDatabaseConstant
217    * @param specificUrl
218    * @param accession
219    * @param name
220    * @param source
221    * @param view
222    * @param dataComment
223    * @return
224    */
225   public static EnzymeLink valueOf(XrefDatabaseConstant xrefDatabaseConstant, String specificUrl, String accession, String name,
226                                    EnzymeSourceConstant source, EnzymeViewConstant view, String dataComment){
227       EnzymeLink el = EnzymeLink.valueOf(xrefDatabaseConstant, specificUrl, accession, name, source, view);
228       el.setDataComment(dataComment);
229       return el;
230   }
231 
232   /**
233    * Checks if the given link is a static link.
234    *
235    * @param databaseConstant The database constant to be checked.
236    * @return <code>true</code> if the link is a static link.
237    * @throws NullPointerException if <code>link</code> is <code>null</code>.
238    */
239   public static boolean isStaticLink(XrefDatabaseConstant databaseConstant) {
240     if (databaseConstant == null) throw new NullPointerException("Parameter 'databaseConstant' must not be null.");
241     return STATIC_LINKS.containsKey(databaseConstant);
242   }
243 
244   public static EnzymeLink getStaticLink(XrefDatabaseConstant databaseConstant) {
245     if (databaseConstant == null) throw new NullPointerException("Parameter 'databaseConstant' must not be null.");
246     return STATIC_LINKS.get(databaseConstant);
247   }
248 
249   /**
250    * Used for ordering links in a collection.
251    * <p/>
252    * Static and other links are ordered by their xrefDatabaseConstant name,
253    * SwissProt and MIM cross-references by their name
254    * and PROSITE links by their accession. Other links are ordered by their specific URL
255    * if the xrefDatabaseConstant name is the same.
256    * Noted: Changed so that only SwissProt compares on the basis of name and OMIM compares on the
257    * basis of accession like PROSITE.
258    *
259    * @param link The object to be compared to this.
260    * @return neg, 0 or pos. value to indicate the order of these two objects.
261    * @throws NullPointerException if <code>o</code> is <code>null</code>.
262    */
263   public int compareTo(EnzymeLink link) {
264     if (xrefDatabaseConstant == link.getXrefDatabaseConstant()) {
265       if (name.equals(link.getName())) {
266         if (accession.equals(link.getAccession())) {
267           return specificUrl.compareTo(link.getSpecificUrl());
268         }
269         return accession.compareTo(link.getAccession());
270       }
271       return name.compareTo(link.getName());
272     }
273 
274     return xrefDatabaseConstant.getDatabaseCode().compareTo(link.getXrefDatabaseConstant().getDatabaseCode());
275   }
276 
277   /**
278    * Checks for equality.
279    *
280    * @param o The object to be compared to this.
281    * @return <code>true</code> if <code>o</code> is equal this object.
282    */
283   public boolean equals(Object o) {
284     if (this == o) return true;
285     if (!(o instanceof EnzymeLink)) return false;
286 
287     final EnzymeLink enzymeLink = (EnzymeLink) o;
288 
289     if (accession != null ? !accession.equals(enzymeLink.accession) : enzymeLink.accession != null) return false;
290     if (name != null ? !name.equals(enzymeLink.name) : enzymeLink.name != null) return false;
291     if (source != null ? !source.equals(enzymeLink.source) : enzymeLink.source != null) return false;
292     if (specificUrl != null ? !specificUrl.equals(enzymeLink.specificUrl) : enzymeLink.specificUrl != null) return false;
293     if (view != null ? !view.equals(enzymeLink.view) : enzymeLink.view != null) return false;
294     if (xrefDatabaseConstant != null ? !xrefDatabaseConstant.equals(enzymeLink.xrefDatabaseConstant) : enzymeLink.xrefDatabaseConstant !=
295                                                                                                        null)
296       return false;
297 
298     return true;
299   }
300 
301   /**
302    * Generates a hash code for this object.
303    *
304    * @return the hash code.
305    */
306   public int hashCode() {
307     int result;
308     result = (xrefDatabaseConstant != null ? xrefDatabaseConstant.hashCode() : 0);
309     result = 29 * result + (specificUrl != null ? specificUrl.hashCode() : 0);
310     result = 29 * result + (accession != null ? accession.hashCode() : 0);
311     result = 29 * result + (name != null ? name.hashCode() : 0);
312     result = 29 * result + (source != null ? source.hashCode() : 0);
313     result = 29 * result + (view != null ? view.hashCode() : 0);
314     return result;
315   }
316 
317   /**
318    * Provides a string representation of an <code>EnzymeLink</code>.
319    *
320    * @return the string representation.
321    */
322   public String toString() {
323     StringBuffer output = new StringBuffer();
324     output.append(this.xrefDatabaseConstant);
325     output.append(", ");
326     output.append(this.specificUrl);
327     output.append(", ");
328     output.append(this.accession);
329     output.append(", ");
330     output.append(this.name);
331     output.append(", ");
332     output.append(this.view.toString());
333     output.append(", ");
334     output.append(this.source.toString());
335 
336     return output.toString();
337   }
338 
339 
340   // ----------------  GETTER ------------------
341 
342   /**
343    * Creates a unique name for this link.
344    * <p/>
345    * This method will be used to populate lists (esp. combo box content).
346    *
347    * @return the unique name.
348    */
349   public String getListOrderCriterion() {
350     if (xrefDatabaseConstant == XrefDatabaseConstant.SWISSPROT ||
351         xrefDatabaseConstant == XrefDatabaseConstant.PROSITE ||
352         xrefDatabaseConstant == XrefDatabaseConstant.CAS) {
353       return accession;
354     }
355 
356     if (xrefDatabaseConstant == XrefDatabaseConstant.MEROPS || xrefDatabaseConstant == XrefDatabaseConstant.UMBBD) return specificUrl;
357 
358     if (xrefDatabaseConstant == XrefDatabaseConstant.DIAGRAM) return name;
359 
360     return xrefDatabaseConstant.getDatabaseCode();
361   }
362 
363   /**
364    * Links with the EC number as identifier need to call this method to get the correct URL.
365    *
366    * @param ec The EC number.
367    * @return the full URL.
368    * @throws NullPointerException if <code>ec</code> is <code>null</code>.
369    */
370   public String getFullUrl(String ec) {
371     if (ec == null) throw new NullPointerException();
372     if (STATIC_LINKS.keySet().contains(xrefDatabaseConstant)){
373     	boolean withAccession = EnzymeCommissionNumber.isPreliminary(ec)
374     		&& accession != null && accession.length() > 0;
375         return specificUrl + (withAccession? accession : ec);
376     }
377     if (xrefDatabaseConstant == XrefDatabaseConstant.CSA){
378         StringTokenizer st = new StringTokenizer(ec, ".");
379         StringBuffer sb = new StringBuffer(xrefDatabaseConstant.getUrl());
380         sb.append("ec1=").append(st.nextToken())
381             .append("&ec2=").append(st.nextToken())
382             .append("&ec3=").append(st.nextToken())
383             .append("&ec4=").append(st.nextToken());
384         return sb.toString();
385     }
386     if (xrefDatabaseConstant == XrefDatabaseConstant.PROSITE ||
387         xrefDatabaseConstant == XrefDatabaseConstant.SWISSPROT ||
388         xrefDatabaseConstant == XrefDatabaseConstant.MIM ||
389         xrefDatabaseConstant == XrefDatabaseConstant.PDB ||
390         xrefDatabaseConstant == XrefDatabaseConstant.GO ||
391         xrefDatabaseConstant == XrefDatabaseConstant.BRENDA ||
392         xrefDatabaseConstant == XrefDatabaseConstant.METACYC){
393         StringBuffer sb = new StringBuffer(xrefDatabaseConstant.getUrl());
394         sb.append(accession);
395         return sb.toString();
396     }
397     if (xrefDatabaseConstant == XrefDatabaseConstant.NC_IUBMB){
398     	StringTokenizer st = new StringTokenizer(ec, ".");
399         StringBuffer sb = new StringBuffer(xrefDatabaseConstant.getUrl());
400     	sb.append(st.nextToken()).append('/')        // ec1
401     		.append(st.nextToken()).append('/')      // ec2
402     		.append(st.nextToken()).append('/')      // ec3
403     		.append(st.nextToken()).append(".html"); // ec4
404     	return sb.toString();
405     }
406     return specificUrl;
407   }
408 
409   public XrefDatabaseConstant getXrefDatabaseConstant() {
410     return xrefDatabaseConstant;
411   }
412 
413   public String getSpecificUrl() {
414     return specificUrl;
415   }
416 
417   public String getAccession() {
418     return accession;
419   }
420 
421   public String getName() {
422     return name;
423   }
424 
425   public EnzymeViewConstant getView() {
426     return view;
427   }
428 
429   public EnzymeSourceConstant getSource() {
430     return source;
431   }
432 
433     public String getDataComment() {
434         return dataComment;
435     }
436 
437     public void setDataComment(String comment) {
438         dataComment = comment;
439     }
440 }