d539947115d910cc54d9fd45d59289edc39e4623
[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 {
59             throw new IllegalArgumentException( "unknown GO relationship type: " + type );
60         }
61         _go_id = new GoId( go_id );
62     }
63
64     public BasicGoRelationship( final String type, final String go_id ) {
65         if ( type.toLowerCase().equals( PART_OF_STR ) ) {
66             _type = Type.PART_OF;
67         }
68         else {
69             throw new IllegalArgumentException( "unknown GO relationship type: " + type );
70         }
71         _go_id = new GoId( go_id );
72     }
73
74     public BasicGoRelationship( final Type type, final GoId go_id ) {
75         _type = type;
76         _go_id = go_id;
77     }
78
79     @Override
80     public int compareTo( final GoRelationship rel ) {
81         return getGoId().compareTo( rel.getGoId() );
82     }
83
84     /**
85      * Based on value and type.
86      *
87      *
88      */
89     @Override
90     public boolean equals( final Object o ) {
91         if ( this == o ) {
92             return true;
93         }
94         else if ( o == null ) {
95             throw new IllegalArgumentException( "attempt to check go relationship equality to null" );
96         }
97         else if ( o.getClass() != this.getClass() ) {
98             throw new IllegalArgumentException( "attempt to check go relationship equality to " + o + " ["
99                     + o.getClass() + "]" );
100         }
101         else {
102             return getType().equals( ( ( GoRelationship ) o ).getType() )
103                     && getGoId().equals( ( ( GoRelationship ) o ).getGoId() );
104         }
105     }
106
107     @Override
108     public GoId getGoId() {
109         return _go_id;
110     }
111
112     @Override
113     public Type getType() {
114         return _type;
115     }
116
117     @Override
118     public String toString() {
119         final StringBuffer sb = new StringBuffer();
120         switch ( getType() ) {
121             case PART_OF:
122                 sb.append( PART_OF_STR );
123                 break;
124             case NEGATIVELY_REGULATES:
125                 sb.append( NEGATIVELY_REGULATES_STR );
126                 break;
127             case POSITIVELY_REGULATES:
128                 sb.append( POSITIVELY_REGULATES_STR );
129                 break;
130             case REGULATES:
131                 sb.append( REGULATES_STR );
132                 break;
133             case HAS_PART:
134                 sb.append( HAS_PART_STR );
135                 break;
136             case OCCURS_IN:
137                 sb.append( OCCURS_IN_STR );
138                 break;
139             default:
140                 new IllegalStateException( "unknown type: " + getType() );
141         }
142         sb.append( ": " );
143         sb.append( getGoId().toString() );
144         return sb.toString();
145     }
146 }