initial commit
[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 {
85             throw new IllegalArgumentException( "unknown GO xref type: " + type );
86         }
87         _xref = sa[ 1 ].trim();
88     }
89
90     public BasicGoXRef( final Type type, final String xref ) {
91         _type = type;
92         _xref = xref;
93     }
94
95     public int compareTo( final GoXRef xref ) {
96         return getXRef().compareTo( xref.getXRef() );
97     }
98
99     /**
100      * Based on value and type.
101      * 
102      * 
103      */
104     @Override
105     public boolean equals( final Object o ) {
106         if ( this == o ) {
107             return true;
108         }
109         else if ( o == null ) {
110             throw new IllegalArgumentException( "attempt to check go xref equality to null" );
111         }
112         else if ( o.getClass() != this.getClass() ) {
113             throw new IllegalArgumentException( "attempt to check go xref equality to " + o + " [" + o.getClass() + "]" );
114         }
115         else {
116             return getXRef().equals( ( ( GoXRef ) o ).getXRef() ) && getType().equals( ( ( GoXRef ) o ).getType() );
117         }
118     }
119
120     public Type getType() {
121         return _type;
122     }
123
124     @Override
125     public String getXRef() {
126         return _xref;
127     }
128
129     @Override
130     public String toString() {
131         final StringBuffer sb = new StringBuffer();
132         switch ( getType() ) {
133             case EC:
134                 sb.append( EC_STR );
135                 break;
136             case META_CYC:
137                 sb.append( META_CYC_STR );
138                 break;
139             case REACTOME:
140                 sb.append( REACTOME_STR );
141                 break;
142             case RESID:
143                 sb.append( RESID_STR );
144                 break;
145             case UM_BBD_ENZYME_ID:
146                 sb.append( UM_BBD_ENZYME_ID_STR );
147                 break;
148             case UM_BBD_PATHWAY_ID:
149                 sb.append( UM_BBD_PATHWAY_ID_STR );
150                 break;
151             case UM_BBD_REACTIONID:
152                 sb.append( UM_BBD_REACTIONID_STR );
153                 break;
154             case TC:
155                 sb.append( TC_STR );
156                 break;
157             case ARACYC:
158                 sb.append( ARACYC_STR );
159                 break;
160             case XX:
161                 sb.append( XX_STR );
162                 break;
163             case GOC:
164                 sb.append( GOC_STR );
165                 break;
166             case IMG:
167                 sb.append( IMG_STR );
168                 break;
169             case PMID:
170                 sb.append( PMID_STR );
171                 break;
172             case WIKIPEDIA:
173                 sb.append( WIKIPEDIA_STR );
174                 break;
175             default:
176                 new AssertionError( "unknown type: " + getType() );
177         }
178         sb.append( ":" );
179         sb.append( getXRef() );
180         return sb.toString();
181     }
182 }