2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
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.
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.
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
23 // Contact: phylosoft @ gmail . com
24 // WWW: www.phylosoft.org
26 package org.forester.go.etc;
28 import java.io.BufferedReader;
30 import java.io.FileReader;
31 import java.io.IOException;
32 import java.util.ArrayList;
33 import java.util.List;
35 import org.forester.go.GoId;
36 import org.forester.util.ForesterUtil;
40 * Note: this class has a natural ordering that is inconsistent with equals.
42 public class OntologizerResult implements Comparable<OntologizerResult> {
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;
58 private OntologizerResult( final String s ) {
59 if ( ForesterUtil.isEmpty( s ) ) {
60 throw new IllegalArgumentException( "result string is null or empty" );
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" );
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 ) {
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 ] );
83 else if ( tokens.length == 9 ) {
85 // ID Pop.total Pop.term Study.total Study.term p p.adjusted p.min name
86 _type = TYPE.TERM_FOR_TERM;
91 _p = Double.parseDouble( tokens[ 5 ] );
92 _p_adjusted = Double.parseDouble( tokens[ 6 ] );
93 _p_min = Double.parseDouble( tokens[ 7 ] );
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 ] );
110 public int compareTo( final OntologizerResult o ) {
114 else if ( getPAdjusted() < o.getPAdjusted() ) {
117 else if ( getPAdjusted() > o.getPAdjusted() ) {
125 public GoId getGoId() {
129 public int getNParents() {
133 public double getP() {
137 public double getPAdjusted() {
141 public double getPMin() {
145 public int getPopFamily() {
149 public int getPopTerm() {
153 public int getPopTotal() {
157 public int getStudyFamily() {
158 return _study_family;
161 public int getStudyTerm() {
165 public int getStudyTotal() {
169 public TYPE getType() {
173 public boolean isTrivial() {
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 );
182 final BufferedReader br = new BufferedReader( new FileReader( input ) );
184 final List<OntologizerResult> results = new ArrayList<OntologizerResult>();
187 while ( ( line = br.readLine() ) != null ) {
190 if ( line.startsWith( "GO:" ) ) {
191 results.add( new OntologizerResult( line ) );
195 catch ( final Exception e ) {
196 throw new IOException( "parsing problem [at line " + line_number + "] in [" + input + "]: "
202 public static enum TYPE {
203 TOPOLOGY, TERM_FOR_TERM, PARENT_CHILD;