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