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