in progress...
[jalview.git] / forester / java / src / org / forester / go / BasicGoRelationship.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: https://sites.google.com/site/cmzmasek/home/software/forester
25
26 package org.forester.go;
27
28 public class BasicGoRelationship implements GoRelationship {
29
30     final Type _type;
31     final GoId _go_id;
32
33     public BasicGoRelationship( final String s ) {
34         final String[] sa = s.split( " " );
35         if ( sa.length != 2 ) {
36             throw new IllegalArgumentException( "unexpected format for GO relationship: " + s );
37         }
38         final String type = sa[ 0 ].trim();
39         final String go_id = sa[ 1 ].trim();
40         if ( type.toLowerCase().equals( PART_OF_STR ) ) {
41             _type = Type.PART_OF;
42         }
43         else if ( type.toLowerCase().equals( REGULATES_STR ) ) {
44             _type = Type.REGULATES;
45         }
46         else if ( type.toLowerCase().equals( NEGATIVELY_REGULATES_STR ) ) {
47             _type = Type.NEGATIVELY_REGULATES;
48         }
49         else if ( type.toLowerCase().equals( POSITIVELY_REGULATES_STR ) ) {
50             _type = Type.POSITIVELY_REGULATES;
51         }
52         else if ( type.toLowerCase().equals( HAS_PART_STR ) ) {
53             _type = Type.HAS_PART;
54         }
55         else if ( type.toLowerCase().equals( OCCURS_IN_STR ) ) {
56             _type = Type.OCCURS_IN;
57         }
58         else if ( type.toLowerCase().equals( HAPPENS_DURING_STR ) ) {
59             _type = Type.HAPPENS_DURING;
60         }
61         else if ( type.toLowerCase().equals(ENDS_DURING_STR ) ) {
62             _type = Type.ENDS_DURING;
63         }
64         else {
65             throw new IllegalArgumentException( "unknown GO relationship type: " + type );
66         }
67         _go_id = new GoId( go_id );
68     }
69
70     public BasicGoRelationship( final String type, final String go_id ) {
71         if ( type.toLowerCase().equals( PART_OF_STR ) ) {
72             _type = Type.PART_OF;
73         }
74         else {
75             throw new IllegalArgumentException( "unknown GO relationship type: " + type );
76         }
77         _go_id = new GoId( go_id );
78     }
79
80     public BasicGoRelationship( final Type type, final GoId go_id ) {
81         _type = type;
82         _go_id = go_id;
83     }
84
85     @Override
86     public int compareTo( final GoRelationship rel ) {
87         return getGoId().compareTo( rel.getGoId() );
88     }
89
90     /**
91      * Based on value and type.
92      *
93      *
94      */
95     @Override
96     public boolean equals( final Object o ) {
97         if ( this == o ) {
98             return true;
99         }
100         else if ( o == null ) {
101             throw new IllegalArgumentException( "attempt to check go relationship equality to null" );
102         }
103         else if ( o.getClass() != this.getClass() ) {
104             throw new IllegalArgumentException( "attempt to check go relationship equality to " + o + " ["
105                     + o.getClass() + "]" );
106         }
107         else {
108             return getType().equals( ( ( GoRelationship ) o ).getType() )
109                     && getGoId().equals( ( ( GoRelationship ) o ).getGoId() );
110         }
111     }
112
113     @Override
114     public GoId getGoId() {
115         return _go_id;
116     }
117
118     @Override
119     public Type getType() {
120         return _type;
121     }
122
123     @Override
124     public String toString() {
125         final StringBuffer sb = new StringBuffer();
126         switch ( getType() ) {
127             case PART_OF:
128                 sb.append( PART_OF_STR );
129                 break;
130             case NEGATIVELY_REGULATES:
131                 sb.append( NEGATIVELY_REGULATES_STR );
132                 break;
133             case POSITIVELY_REGULATES:
134                 sb.append( POSITIVELY_REGULATES_STR );
135                 break;
136             case REGULATES:
137                 sb.append( REGULATES_STR );
138                 break;
139             case HAS_PART:
140                 sb.append( HAS_PART_STR );
141                 break;
142             case OCCURS_IN:
143                 sb.append( OCCURS_IN_STR );
144                 break;
145             default:
146                 new IllegalStateException( "unknown type: " + getType() );
147         }
148         sb.append( ": " );
149         sb.append( getGoId().toString() );
150         return sb.toString();
151     }
152 }