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