47751e874649545b9b1506ffd9cf5b5887259efd
[jalview.git] / forester / java / src / org / forester / go / etc / OntologizerResult.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
25
26 package org.forester.go.etc;
27
28 import java.io.BufferedReader;
29 import java.io.File;
30 import java.io.FileReader;
31 import java.io.IOException;
32 import java.util.ArrayList;
33 import java.util.List;
34
35 import org.forester.go.GoId;
36 import org.forester.util.ForesterUtil;
37
38 /*
39  * 
40  * Note: this class has a natural ordering that is inconsistent with equals.
41  */
42 public class OntologizerResult implements Comparable<OntologizerResult> {
43
44     final private GoId    _goid;
45     final private int     _pop_total;
46     final private int     _pop_term;
47     final private int     _study_total;
48     final private int     _study_term;
49     final private int     _pop_family;
50     final private int     _study_family;
51     final private int     _nparents;
52     final private boolean _is_trivial;
53     final private double  _p;
54     final private double  _p_adjusted;
55     final private double  _p_min;
56     final private TYPE    _type;
57
58     private OntologizerResult( final String s ) {
59         if ( ForesterUtil.isEmpty( s ) ) {
60             throw new IllegalArgumentException( "result string is null or empty" );
61         }
62         final String[] tokens = s.split( "\t" );
63         if ( ( tokens.length != 9 ) && ( tokens.length != 11 ) && ( tokens.length != 12 ) ) {
64             throw new IllegalArgumentException( "result string [" + s + "] has unexpected format" );
65         }
66         _goid = new GoId( tokens[ 0 ] );
67         _pop_total = Integer.parseInt( tokens[ 1 ] );
68         _pop_term = Integer.parseInt( tokens[ 2 ] );
69         _study_total = Integer.parseInt( tokens[ 3 ] );
70         _study_term = Integer.parseInt( tokens[ 4 ] );
71         if ( tokens.length == 11 ) {
72             // Topology Elim
73             // ID Pop.total Pop.term Study.total Study.term Pop.family Study.family is.trivial p p.adjusted p.min
74             _type = TYPE.TOPOLOGY;
75             _pop_family = Integer.parseInt( tokens[ 5 ] );
76             _study_family = Integer.parseInt( tokens[ 6 ] );
77             _is_trivial = Boolean.parseBoolean( tokens[ 7 ] );
78             _p = Double.parseDouble( tokens[ 8 ] );
79             _p_adjusted = Double.parseDouble( tokens[ 9 ] );
80             _p_min = Double.parseDouble( tokens[ 10 ] );
81             _nparents = -1;
82         }
83         else if ( tokens.length == 9 ) {
84             // Term for Term
85             // ID Pop.total Pop.term Study.total Study.term p p.adjusted p.min name
86             _type = TYPE.TERM_FOR_TERM;
87             _pop_family = -1;
88             _study_family = -1;
89             _nparents = -1;
90             _is_trivial = false;
91             _p = Double.parseDouble( tokens[ 5 ] );
92             _p_adjusted = Double.parseDouble( tokens[ 6 ] );
93             _p_min = Double.parseDouble( tokens[ 7 ] );
94         }
95         else {
96             // Parent Child Union
97             // ID Pop.total Pop.term Study.total Study.term Pop.family Study.family nparents is.trivial p p.adjusted p.min
98             _type = TYPE.PARENT_CHILD;
99             _pop_family = Integer.parseInt( tokens[ 5 ] );
100             _study_family = Integer.parseInt( tokens[ 6 ] );
101             _nparents = Integer.parseInt( tokens[ 7 ] );
102             _is_trivial = Boolean.parseBoolean( tokens[ 8 ] );
103             _p = Double.parseDouble( tokens[ 9 ] );
104             _p_adjusted = Double.parseDouble( tokens[ 10 ] );
105             _p_min = Double.parseDouble( tokens[ 11 ] );
106         }
107     }
108
109     @Override
110     public int compareTo( final OntologizerResult o ) {
111         if ( this == o ) {
112             return 0;
113         }
114         else if ( getPAdjusted() < o.getPAdjusted() ) {
115             return -1;
116         }
117         else if ( getPAdjusted() > o.getPAdjusted() ) {
118             return 1;
119         }
120         else {
121             return 0;
122         }
123     }
124
125     public GoId getGoId() {
126         return _goid;
127     }
128
129     public int getNParents() {
130         return _nparents;
131     }
132
133     public double getP() {
134         return _p;
135     }
136
137     public double getPAdjusted() {
138         return _p_adjusted;
139     }
140
141     public double getPMin() {
142         return _p_min;
143     }
144
145     public int getPopFamily() {
146         return _pop_family;
147     }
148
149     public int getPopTerm() {
150         return _pop_term;
151     }
152
153     public int getPopTotal() {
154         return _pop_total;
155     }
156
157     public int getStudyFamily() {
158         return _study_family;
159     }
160
161     public int getStudyTerm() {
162         return _study_term;
163     }
164
165     public int getStudyTotal() {
166         return _study_total;
167     }
168
169     public TYPE getType() {
170         return _type;
171     }
172
173     public boolean isTrivial() {
174         return _is_trivial;
175     }
176
177     public static List<OntologizerResult> parse( final File input ) throws IOException {
178         final String error = ForesterUtil.isReadableFile( input );
179         if ( !ForesterUtil.isEmpty( error ) ) {
180             throw new IOException( error );
181         }
182         final BufferedReader br = new BufferedReader( new FileReader( input ) );
183         String line;
184         final List<OntologizerResult> results = new ArrayList<OntologizerResult>();
185         int line_number = 0;
186         try {
187             while ( ( line = br.readLine() ) != null ) {
188                 line_number++;
189                 line = line.trim();
190                 if ( line.startsWith( "GO:" ) ) {
191                     results.add( new OntologizerResult( line ) );
192                 }
193             }
194         }
195         catch ( final Exception e ) {
196             throw new IOException( "parsing problem [at line " + line_number + "] in [" + input + "]: "
197                     + e.getMessage() );
198         }
199         return results;
200     }
201
202     public static enum TYPE {
203         TOPOLOGY, TERM_FOR_TERM, PARENT_CHILD;
204     }
205 }