99a0c9ecdb09b88a3797ee48631f49711ee646ff
[jalview.git] / forester / java / src / org / forester / go / BasicGoXRef.java
1 // $Id:
2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
4 //
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
7 // All rights reserved
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 //
23 // Contact: phylosoft @ gmail . com
24 // WWW: www.phylosoft.org/forester
25
26 package org.forester.go;
27
28 public class BasicGoXRef implements GoXRef {
29
30     private final String _xref;
31     private final Type   _type;
32
33     public BasicGoXRef( final String s ) {
34         final String[] sa = s.split( ":" );
35         if ( sa.length < 2 ) {
36             throw new IllegalArgumentException( "unexpected format for GO xref: " + s );
37         }
38         final String type = sa[ 0 ].trim();
39         if ( type.equals( EC_STR ) ) {
40             _type = Type.EC;
41         }
42         else if ( type.equals( META_CYC_STR ) ) {
43             _type = Type.META_CYC;
44         }
45         else if ( type.equals( REACTOME_STR ) ) {
46             _type = Type.REACTOME;
47         }
48         else if ( type.equals( RESID_STR ) ) {
49             _type = Type.RESID;
50         }
51         else if ( type.equals( UM_BBD_ENZYME_ID_STR ) ) {
52             _type = Type.UM_BBD_ENZYME_ID;
53         }
54         else if ( type.equals( UM_BBD_PATHWAY_ID_STR ) ) {
55             _type = Type.UM_BBD_PATHWAY_ID;
56         }
57         else if ( type.equals( UM_BBD_REACTIONID_STR ) ) {
58             _type = Type.UM_BBD_REACTIONID;
59         }
60         else if ( type.equals( TC_STR ) ) {
61             _type = Type.TC;
62         }
63         else if ( type.equals( ARACYC_STR ) ) {
64             _type = Type.ARACYC;
65         }
66         else if ( type.equals( XX_STR ) ) {
67             _type = Type.XX;
68         }
69         else if ( type.equals( PMID_STR ) ) {
70             _type = Type.PMID;
71         }
72         else if ( type.equals( IMG_STR ) ) {
73             _type = Type.IMG;
74         }
75         else if ( type.equals( GOC_STR ) ) {
76             _type = Type.GOC;
77         }
78         else if ( type.equals( KEGG_STR ) ) {
79             _type = Type.KEGG;
80         }
81         else if ( type.equals( WIKIPEDIA_STR ) ) {
82             _type = Type.WIKIPEDIA;
83         }
84         else if ( type.equals( RHEA_STR ) ) {
85             _type = Type.RHEA;
86         }
87         else if ( type.equals( NIF_SUBCELLULAR_STR ) ) {
88             _type = Type.NIF_SUBCELLULAR;
89         }
90         else if ( type.equals( CORUM_STR ) ) {
91             _type = Type.CORUM;
92         }
93         else if ( type.equals( UNIPATHWAY_STR ) ) {
94             _type = Type.UNIPATHWAY;
95         }
96         else if ( type.equals( PO_STR ) ) {
97             _type = Type.PO;
98         }
99         else {
100             throw new IllegalArgumentException( "unknown GO xref type: " + type );
101         }
102         _xref = sa[ 1 ].trim();
103     }
104
105     public BasicGoXRef( final Type type, final String xref ) {
106         _type = type;
107         _xref = xref;
108     }
109
110     @Override
111     public int compareTo( final GoXRef xref ) {
112         return getXRef().compareTo( xref.getXRef() );
113     }
114
115     /**
116      * Based on value and type.
117      * 
118      * 
119      */
120     @Override
121     public boolean equals( final Object o ) {
122         if ( this == o ) {
123             return true;
124         }
125         else if ( o == null ) {
126             throw new IllegalArgumentException( "attempt to check go xref equality to null" );
127         }
128         else if ( o.getClass() != this.getClass() ) {
129             throw new IllegalArgumentException( "attempt to check go xref equality to " + o + " [" + o.getClass() + "]" );
130         }
131         else {
132             return getXRef().equals( ( ( GoXRef ) o ).getXRef() ) && getType().equals( ( ( GoXRef ) o ).getType() );
133         }
134     }
135
136     @Override
137     public Type getType() {
138         return _type;
139     }
140
141     @Override
142     public String getXRef() {
143         return _xref;
144     }
145
146     @Override
147     public String toString() {
148         final StringBuffer sb = new StringBuffer();
149         switch ( getType() ) {
150             case EC:
151                 sb.append( EC_STR );
152                 break;
153             case META_CYC:
154                 sb.append( META_CYC_STR );
155                 break;
156             case REACTOME:
157                 sb.append( REACTOME_STR );
158                 break;
159             case RESID:
160                 sb.append( RESID_STR );
161                 break;
162             case UM_BBD_ENZYME_ID:
163                 sb.append( UM_BBD_ENZYME_ID_STR );
164                 break;
165             case UM_BBD_PATHWAY_ID:
166                 sb.append( UM_BBD_PATHWAY_ID_STR );
167                 break;
168             case UM_BBD_REACTIONID:
169                 sb.append( UM_BBD_REACTIONID_STR );
170                 break;
171             case TC:
172                 sb.append( TC_STR );
173                 break;
174             case ARACYC:
175                 sb.append( ARACYC_STR );
176                 break;
177             case XX:
178                 sb.append( XX_STR );
179                 break;
180             case GOC:
181                 sb.append( GOC_STR );
182                 break;
183             case IMG:
184                 sb.append( IMG_STR );
185                 break;
186             case PMID:
187                 sb.append( PMID_STR );
188                 break;
189             case WIKIPEDIA:
190                 sb.append( WIKIPEDIA_STR );
191                 break;
192             default:
193                 new AssertionError( "unknown type: " + getType() );
194         }
195         sb.append( ":" );
196         sb.append( getXRef() );
197         return sb.toString();
198     }
199 }