clean up
[jalview.git] / forester / java / src / org / forester / go / etc / MetaOntologizer.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.awt.Color;
29 import java.io.BufferedReader;
30 import java.io.File;
31 import java.io.FileReader;
32 import java.io.IOException;
33 import java.io.Writer;
34 import java.text.DecimalFormat;
35 import java.text.NumberFormat;
36 import java.util.ArrayList;
37 import java.util.HashSet;
38 import java.util.List;
39 import java.util.Map;
40 import java.util.Set;
41 import java.util.SortedMap;
42 import java.util.SortedSet;
43 import java.util.TreeMap;
44 import java.util.TreeSet;
45 import java.util.regex.Matcher;
46 import java.util.regex.Pattern;
47
48 import org.forester.go.GoId;
49 import org.forester.go.GoNameSpace;
50 import org.forester.go.GoTerm;
51 import org.forester.go.GoUtils;
52 import org.forester.go.OBOparser;
53 import org.forester.go.PfamToGoMapping;
54 import org.forester.surfacing.BasicSpecies;
55 import org.forester.surfacing.DomainId;
56 import org.forester.surfacing.Species;
57 import org.forester.surfacing.SurfacingConstants;
58 import org.forester.surfacing.SurfacingUtil;
59 import org.forester.util.ForesterUtil;
60
61 public class MetaOntologizer {
62
63     private final static NumberFormat FORMATER                         = new DecimalFormat( "0.00E0" );
64     private final static Color        MIN_COLOR                        = new Color( 0, 200, 50 );
65     private final static Color        MAX_COLOR                        = new Color( 0, 0, 0 );
66     final static private String       PRG_NAME                         = "meta_ontologizer";
67     private static final boolean      VERBOSE                          = true;
68     //table-a_41_dollo_all_gains_d-Topology-Elim-Bonferroni.txt:
69     private final static Pattern      PATTERN_ONTOLOGIZER_TABLE_OUTPUT = Pattern.compile( ".*table-(.+)_dollo_.*",
70                                                                                           Pattern.CASE_INSENSITIVE ); //TODO this might need some work...
71
72     private static boolean hasResultsForSpecies( final Map<GoId, GoTerm> go_id_to_terms,
73                                                  final SortedMap<String, SortedSet<OntologizerResult>> species_to_results_map,
74                                                  final String species,
75                                                  final GoNameSpace.GoNamespaceType namespace ) {
76         for( final OntologizerResult ontologizer_result : species_to_results_map.get( species ) ) {
77             if ( go_id_to_terms.get( ontologizer_result.getGoId() ).getGoNameSpace().getType() == namespace ) {
78                 return true;
79             }
80         }
81         return false;
82     }
83
84     private static StringBuilder obtainDomainsForGoId( final List<PfamToGoMapping> pfam_to_go,
85                                                        final SortedSet<DomainId> domains_per_species,
86                                                        final Map<GoId, GoTerm> all_go_terms,
87                                                        final GoId query_go_id,
88                                                        final Set<DomainId> found_domain_ids ) {
89         final StringBuilder sb = new StringBuilder();
90         D: for( final DomainId domain_id : domains_per_species ) {
91             for( final PfamToGoMapping ptg : pfam_to_go ) {
92                 if ( ptg.getKey().equals( domain_id ) ) {
93                     final GoId go_id = ptg.getValue();
94                     final Set<GoId> super_ids = new HashSet<GoId>();
95                     for( final GoTerm term : GoUtils.getAllSuperGoTerms( go_id, all_go_terms ) ) {
96                         super_ids.add( term.getGoId() );
97                     }
98                     super_ids.add( go_id );
99                     if ( super_ids.contains( query_go_id ) ) {
100                         sb.append( "[<a href=\"" + SurfacingConstants.PFAM_FAMILY_ID_LINK + domain_id + "\">"
101                                 + domain_id + "</a>] " );
102                         found_domain_ids.add( domain_id );
103                         continue D;
104                     }
105                 }
106             }
107         }
108         return sb;
109     }
110
111     private static String obtainSpecies( final File ontologizer_outfile ) {
112         final Matcher matcher = PATTERN_ONTOLOGIZER_TABLE_OUTPUT.matcher( ontologizer_outfile.getName() );
113         String species = null;
114         if ( matcher.matches() ) {
115             species = matcher.group( 1 );
116             if ( VERBOSE ) {
117                 ForesterUtil
118                         .programMessage( PRG_NAME, "species for [" + ontologizer_outfile + "] is [" + species + "]" );
119             }
120         }
121         else {
122             throw new RuntimeException( "pattern [" + PATTERN_ONTOLOGIZER_TABLE_OUTPUT + "] did not match ["
123                     + ontologizer_outfile.getName() + "]" );
124         }
125         return species;
126     }
127
128     private static SortedMap<Species, SortedSet<DomainId>> parseDomainGainLossFile( final File input )
129             throws IOException {
130         final String error = ForesterUtil.isReadableFile( input );
131         if ( !ForesterUtil.isEmpty( error ) ) {
132             throw new IOException( error );
133         }
134         final SortedMap<Species, SortedSet<DomainId>> speciesto_to_domain_id = new TreeMap<Species, SortedSet<DomainId>>();
135         final BufferedReader br = new BufferedReader( new FileReader( input ) );
136         String line;
137         int line_number = 0;
138         Species current_species = null;
139         try {
140             while ( ( line = br.readLine() ) != null ) {
141                 line_number++;
142                 line = line.trim();
143                 if ( ( ForesterUtil.isEmpty( line ) ) || ( line.startsWith( "##" ) ) ) {
144                     // Ignore.
145                 }
146                 else if ( line.startsWith( "#" ) ) {
147                     current_species = new BasicSpecies( line.substring( 1 ) );
148                     speciesto_to_domain_id.put( current_species, new TreeSet<DomainId>() );
149                 }
150                 else {
151                     if ( current_species == null ) {
152                         throw new IOException( "parsing problem [at line " + line_number + "] in [" + input + "]" );
153                     }
154                     speciesto_to_domain_id.get( current_species ).add( new DomainId( line ) );
155                 }
156             }
157         }
158         catch ( final Exception e ) {
159             throw new IOException( "parsing problem [at line " + line_number + "] in [" + input + "]: "
160                     + e.getMessage() );
161         }
162         return speciesto_to_domain_id;
163     }
164
165     private static void processOneSpecies( final Map<GoId, GoTerm> go_id_to_terms,
166                                            final Writer b_html_writer,
167                                            final Writer b_tab_writer,
168                                            final Writer c_html_writer,
169                                            final Writer c_tab_writer,
170                                            final Writer m_html_writer,
171                                            final Writer m_tab_writer,
172                                            final SortedMap<String, SortedSet<OntologizerResult>> species_to_results_map,
173                                            final String species,
174                                            final double p_adjusted_upper_limit,
175                                            final SortedSet<DomainId> domains_per_species,
176                                            final List<PfamToGoMapping> pfam_to_go,
177                                            final Set<DomainId> domain_ids_with_go_annot ) throws IOException {
178         final SortedSet<OntologizerResult> ontologizer_results = species_to_results_map.get( species );
179         for( final OntologizerResult ontologizer_result : ontologizer_results ) {
180             final GoTerm go_term = go_id_to_terms.get( ontologizer_result.getGoId() );
181             Writer current_html_writer = b_html_writer;
182             Writer current_tab_writer = b_tab_writer;
183             switch ( go_term.getGoNameSpace().getType() ) {
184                 case CELLULAR_COMPONENT:
185                     current_html_writer = c_html_writer;
186                     current_tab_writer = c_tab_writer;
187                     break;
188                 case MOLECULAR_FUNCTION:
189                     current_html_writer = m_html_writer;
190                     current_tab_writer = m_tab_writer;
191                     break;
192             }
193             writeValuesToTabWriter( species, ontologizer_result, go_term, current_tab_writer );
194             writeValuesToHtmlWriter( ontologizer_result,
195                                      go_term,
196                                      current_html_writer,
197                                      p_adjusted_upper_limit,
198                                      species,
199                                      go_id_to_terms,
200                                      domains_per_species,
201                                      pfam_to_go,
202                                      domain_ids_with_go_annot );
203         }
204     }
205
206     public static void reformat( final File ontologizer_outdir,
207                                  final String result_file_prefix,
208                                  final File domain_gain_loss_file,
209                                  final String outfile_base,
210                                  final File obo_file,
211                                  final double p_adjusted_upper_limit,
212                                  final String comment,
213                                  final List<PfamToGoMapping> pfam_to_go ) throws IOException {
214         if ( !ontologizer_outdir.exists() ) {
215             throw new IllegalArgumentException( "[" + ontologizer_outdir + "] does not exist" );
216         }
217         if ( !ontologizer_outdir.isDirectory() ) {
218             throw new IllegalArgumentException( "[" + ontologizer_outdir + "] is not a directory" );
219         }
220         if ( !obo_file.exists() ) {
221             throw new IllegalArgumentException( "[" + obo_file + "] does not exist" );
222         }
223         if ( ( p_adjusted_upper_limit < 0.0 ) || ( p_adjusted_upper_limit > 1.0 ) ) {
224             throw new IllegalArgumentException( "adjusted P values limit [" + p_adjusted_upper_limit
225                     + "] is out of range" );
226         }
227         SortedMap<Species, SortedSet<DomainId>> speciesto_to_domain_id = null;
228         if ( domain_gain_loss_file != null ) {
229             if ( !domain_gain_loss_file.exists() ) {
230                 throw new IllegalArgumentException( "[" + domain_gain_loss_file + "] does not exist" );
231             }
232             speciesto_to_domain_id = parseDomainGainLossFile( domain_gain_loss_file );
233             if ( VERBOSE ) {
234                 ForesterUtil.programMessage( PRG_NAME, "parsed gain/loss domains for " + speciesto_to_domain_id.size()
235                         + " species from [" + domain_gain_loss_file + "]" );
236             }
237         }
238         final String[] children = ontologizer_outdir.list();
239         final List<File> ontologizer_outfiles = new ArrayList<File>();
240         if ( children == null ) {
241             throw new IllegalArgumentException( "problem with [" + ontologizer_outdir + "]" );
242         }
243         else {
244             for( final String filename : children ) {
245                 if ( filename.startsWith( result_file_prefix ) ) {
246                     ontologizer_outfiles.add( new File( filename ) );
247                 }
248             }
249         }
250         if ( VERBOSE ) {
251             ForesterUtil.programMessage( PRG_NAME, "need to analyze " + ontologizer_outfiles.size()
252                     + " Ontologizer outfiles from [" + ontologizer_outdir + "]" );
253         }
254         final OBOparser parser = new OBOparser( obo_file, OBOparser.ReturnType.BASIC_GO_TERM );
255         final List<GoTerm> go_terms = parser.parse();
256         if ( VERBOSE ) {
257             ForesterUtil.programMessage( PRG_NAME, "parsed " + go_terms.size() + " GO terms from [" + obo_file + "]" );
258         }
259         final Map<GoId, GoTerm> go_id_to_terms = GoUtils.createGoIdToGoTermMap( go_terms );
260         //FIXME not needed? when doe sthis error arise?
261         //   if ( go_id_to_terms.size() != go_terms.size() ) {
262         //       throw new IllegalArgumentException( "GO terms with non-unique ids found" );
263         //   }
264         final String b_file_html = outfile_base + "_B.html";
265         final String b_file_txt = outfile_base + "_B.txt";
266         final String m_file_html = outfile_base + "_C.html";
267         final String m_file_txt = outfile_base + "_C.txt";
268         final String c_file_html = outfile_base + "_M.html";
269         final String c_file_txt = outfile_base + "_M.txt";
270         final Writer b_html_writer = ForesterUtil.createBufferedWriter( b_file_html );
271         final Writer b_tab_writer = ForesterUtil.createBufferedWriter( b_file_txt );
272         final Writer c_html_writer = ForesterUtil.createBufferedWriter( m_file_html );
273         final Writer c_tab_writer = ForesterUtil.createBufferedWriter( m_file_txt );
274         final Writer m_html_writer = ForesterUtil.createBufferedWriter( c_file_html );
275         final Writer m_tab_writer = ForesterUtil.createBufferedWriter( c_file_txt );
276         final SortedMap<String, SortedSet<OntologizerResult>> species_to_results_map = new TreeMap<String, SortedSet<OntologizerResult>>();
277         for( final File ontologizer_outfile : ontologizer_outfiles ) {
278             final String species = obtainSpecies( ontologizer_outfile );
279             final List<OntologizerResult> ontologizer_results = OntologizerResult.parse( new File( ontologizer_outdir
280                     + ForesterUtil.FILE_SEPARATOR + ontologizer_outfile ) );
281             final SortedSet<OntologizerResult> filtered_ontologizer_results = new TreeSet<OntologizerResult>();
282             for( final OntologizerResult ontologizer_result : ontologizer_results ) {
283                 if ( ontologizer_result.getPAdjusted() <= p_adjusted_upper_limit ) {
284                     filtered_ontologizer_results.add( ontologizer_result );
285                 }
286             }
287             species_to_results_map.put( species, filtered_ontologizer_results );
288         }
289         writeLabelsToTabWriter( b_tab_writer );
290         writeLabelsToTabWriter( c_tab_writer );
291         writeLabelsToTabWriter( m_tab_writer );
292         String domain_gain_loss_file_full_path_str = null;
293         if ( domain_gain_loss_file != null ) {
294             domain_gain_loss_file_full_path_str = domain_gain_loss_file.getAbsolutePath();
295         }
296         writeHtmlHeader( b_html_writer,
297                          GoNameSpace.GoNamespaceType.BIOLOGICAL_PROCESS.toString() + " | Pmax = "
298                                  + p_adjusted_upper_limit + " | " + comment,
299                          ontologizer_outdir.getAbsolutePath(),
300                          domain_gain_loss_file_full_path_str );
301         writeHtmlHeader( c_html_writer,
302                          GoNameSpace.GoNamespaceType.CELLULAR_COMPONENT.toString() + " | Pmax = "
303                                  + p_adjusted_upper_limit + " | " + comment,
304                          ontologizer_outdir.getAbsolutePath(),
305                          domain_gain_loss_file_full_path_str );
306         writeHtmlHeader( m_html_writer,
307                          GoNameSpace.GoNamespaceType.MOLECULAR_FUNCTION.toString() + " | Pmax = "
308                                  + p_adjusted_upper_limit + " | " + comment,
309                          ontologizer_outdir.getAbsolutePath(),
310                          domain_gain_loss_file_full_path_str );
311         for( final String species : species_to_results_map.keySet() ) {
312             if ( hasResultsForSpecies( go_id_to_terms,
313                                        species_to_results_map,
314                                        species,
315                                        GoNameSpace.GoNamespaceType.BIOLOGICAL_PROCESS ) ) {
316                 writeHtmlSpecies( b_html_writer, species );
317             }
318             if ( hasResultsForSpecies( go_id_to_terms,
319                                        species_to_results_map,
320                                        species,
321                                        GoNameSpace.GoNamespaceType.CELLULAR_COMPONENT ) ) {
322                 writeHtmlSpecies( c_html_writer, species );
323             }
324             if ( hasResultsForSpecies( go_id_to_terms,
325                                        species_to_results_map,
326                                        species,
327                                        GoNameSpace.GoNamespaceType.MOLECULAR_FUNCTION ) ) {
328                 writeHtmlSpecies( m_html_writer, species );
329             }
330             SortedSet<DomainId> domains_per_species = null;
331             if ( ( speciesto_to_domain_id != null ) && ( speciesto_to_domain_id.size() > 0 ) ) {
332                 domains_per_species = speciesto_to_domain_id.get( new BasicSpecies( species ) );
333             }
334             final Set<DomainId> domain_ids_with_go_annot = new HashSet<DomainId>();
335             processOneSpecies( go_id_to_terms,
336                                b_html_writer,
337                                b_tab_writer,
338                                c_html_writer,
339                                c_tab_writer,
340                                m_html_writer,
341                                m_tab_writer,
342                                species_to_results_map,
343                                species,
344                                p_adjusted_upper_limit,
345                                domains_per_species,
346                                pfam_to_go,
347                                domain_ids_with_go_annot );
348             if ( ( speciesto_to_domain_id != null ) && ( speciesto_to_domain_id.size() > 0 ) ) {
349                 if ( hasResultsForSpecies( go_id_to_terms,
350                                            species_to_results_map,
351                                            species,
352                                            GoNameSpace.GoNamespaceType.BIOLOGICAL_PROCESS ) ) {
353                     writeHtmlDomains( b_html_writer, domains_per_species, domain_ids_with_go_annot );
354                 }
355                 if ( hasResultsForSpecies( go_id_to_terms,
356                                            species_to_results_map,
357                                            species,
358                                            GoNameSpace.GoNamespaceType.CELLULAR_COMPONENT ) ) {
359                     writeHtmlDomains( c_html_writer, domains_per_species, domain_ids_with_go_annot );
360                 }
361                 if ( hasResultsForSpecies( go_id_to_terms,
362                                            species_to_results_map,
363                                            species,
364                                            GoNameSpace.GoNamespaceType.MOLECULAR_FUNCTION ) ) {
365                     writeHtmlDomains( m_html_writer, domains_per_species, domain_ids_with_go_annot );
366                 }
367             }
368         }
369         writeHtmlEnd( b_html_writer );
370         writeHtmlEnd( c_html_writer );
371         writeHtmlEnd( m_html_writer );
372         b_html_writer.close();
373         b_tab_writer.close();
374         c_html_writer.close();
375         c_tab_writer.close();
376         m_html_writer.close();
377         m_tab_writer.close();
378         if ( VERBOSE ) {
379             ForesterUtil.programMessage( PRG_NAME, "successfully wrote biological process summary to [" + b_file_html
380                     + "]" );
381             ForesterUtil.programMessage( PRG_NAME, "successfully wrote biological process summary to [" + b_file_txt
382                     + "]" );
383             ForesterUtil.programMessage( PRG_NAME, "successfully wrote molecular function summary to [" + m_file_html
384                     + "]" );
385             ForesterUtil.programMessage( PRG_NAME, "successfully wrote molecular function summary to [" + m_file_txt
386                     + "]" );
387             ForesterUtil.programMessage( PRG_NAME, "successfully wrote cellular component summary to [" + c_file_html
388                     + "]" );
389             ForesterUtil.programMessage( PRG_NAME, "successfully wrote cellular component summary to [" + c_file_txt
390                     + "]" );
391         }
392     }
393
394     private static void writeHtmlDomains( final Writer writer,
395                                           final SortedSet<DomainId> domains,
396                                           final Set<DomainId> domain_ids_with_go_annot ) throws IOException {
397         writer.write( "<tr>" );
398         writer.write( "<td colspan=\"10\">" );
399         if ( domains != null ) {
400             for( final DomainId domain : domains ) {
401                 if ( !domain_ids_with_go_annot.contains( domain ) ) {
402                     writer.write( "[<a class=\"new_type\" href=\"" + SurfacingConstants.PFAM_FAMILY_ID_LINK + domain
403                             + "\">" + domain + "</a>] " );
404                 }
405             }
406         }
407         writer.write( "</td>" );
408         writer.write( "</tr>" );
409         writer.write( ForesterUtil.LINE_SEPARATOR );
410     }
411
412     private static void writeHtmlEnd( final Writer writer ) throws IOException {
413         writer.write( "</table>" );
414         writer.write( "</body>" );
415         writer.write( "</html>" );
416     }
417
418     private static void writeHtmlHeader( final Writer w,
419                                          final String desc,
420                                          final String ontologizer_outdir,
421                                          final String domain_gain_loss_file ) throws IOException {
422         w.write( "<head>" );
423         w.write( "<title>" );
424         w.write( desc );
425         w.write( "</title>" );
426         w.write( ForesterUtil.LINE_SEPARATOR );
427         w.write( "<style>" );
428         w.write( ForesterUtil.LINE_SEPARATOR );
429         w.write( "a:visited { color : #F87217; text-decoration : none; }" );
430         w.write( ForesterUtil.LINE_SEPARATOR );
431         w.write( "a:link { color : #F87217; text-decoration : none; }" );
432         w.write( ForesterUtil.LINE_SEPARATOR );
433         w.write( "a:hover { color : #FFFFFF; background-color : #00FF00; text-decoration : none; }" );
434         w.write( ForesterUtil.LINE_SEPARATOR );
435         w.write( "a:hover { color : #FFFFFF; background-color : #00FF00; text-decoration : none; }" );
436         w.write( ForesterUtil.LINE_SEPARATOR );
437         w.write( "a.new_type:visited { font-size: 7pt; color : #808080; text-decoration : none; }" );
438         w.write( ForesterUtil.LINE_SEPARATOR );
439         w.write( "a.new_type:link { font-size: 7pt; color : #505050; text-decoration : none; }" );
440         w.write( ForesterUtil.LINE_SEPARATOR );
441         w.write( "a.new_type:hover { font-size: 7pt; color : #000000; background-color : #FFFF00; text-decoration : none; }" );
442         w.write( ForesterUtil.LINE_SEPARATOR );
443         w.write( "a.new_type:hover { font-size: 7pt; color : #000000; background-color : #FFFF00; text-decoration : none; }" );
444         w.write( ForesterUtil.LINE_SEPARATOR );
445         w.write( "td { text-align: left; vertical-align: top; font-family: Verdana, Arial, Helvetica; font-size: 8pt}" );
446         w.write( ForesterUtil.LINE_SEPARATOR );
447         w.write( "th { text-align: left; vertical-align: top; font-family: Verdana, Arial, Helvetica; font-size: 10pt; font-weight: bold }" );
448         w.write( ForesterUtil.LINE_SEPARATOR );
449         w.write( "h1 { color : #000000; font-family: Verdana, Arial, Helvetica; font-size: 18pt; font-weight: bold }" );
450         w.write( ForesterUtil.LINE_SEPARATOR );
451         w.write( "h2 { color : #000000; font-family: Verdana, Arial, Helvetica; font-size: 16pt; font-weight: bold }" );
452         w.write( "h3 { margin-top: 12px;  margin-bottom: 0px; color : #000000; font-family: Verdana, Arial, Helvetica; font-size: 12pt; font-weight: bold }" );
453         w.write( ForesterUtil.LINE_SEPARATOR );
454         w.write( "</style>" );
455         w.write( ForesterUtil.LINE_SEPARATOR );
456         w.write( "</head>" );
457         w.write( ForesterUtil.LINE_SEPARATOR );
458         w.write( "<body>" );
459         w.write( ForesterUtil.LINE_SEPARATOR );
460         w.write( "<h2>" );
461         w.write( "meta ontologizer" );
462         w.write( "</h2>" );
463         w.write( ForesterUtil.LINE_SEPARATOR );
464         w.write( "<h2>" );
465         w.write( desc );
466         w.write( "</h2>" );
467         w.write( ForesterUtil.LINE_SEPARATOR );
468         w.write( "<table>" );
469         w.write( ForesterUtil.LINE_SEPARATOR );
470         w.write( "<tr><th>" );
471         w.write( "ontolgizer output directory analysed:" );
472         w.write( "</th><td>" );
473         w.write( ontologizer_outdir );
474         w.write( "</td></tr>" );
475         if ( !ForesterUtil.isEmpty( domain_gain_loss_file ) ) {
476             w.write( ForesterUtil.LINE_SEPARATOR );
477             w.write( "<tr><th>" );
478             w.write( "domain gain or loss file:" );
479             w.write( "</th><td>" );
480             w.write( domain_gain_loss_file );
481             w.write( "</td></tr>" );
482         }
483         w.write( "</table>" );
484         w.write( ForesterUtil.LINE_SEPARATOR );
485         w.write( "<table>" );
486         w.write( ForesterUtil.LINE_SEPARATOR );
487         w.write( "<tr>" );
488         w.write( "<th>" );
489         w.write( "GO term name" );
490         w.write( "</th><th>" );
491         w.write( "GO id" );
492         w.write( "</th><th>" );
493         w.write( "P adjusted" );
494         w.write( "</th><th>" );
495         w.write( "P" );
496         w.write( "</th><th>" );
497         w.write( "Pop total" );
498         w.write( "</th><th>" );
499         w.write( "Pop term" );
500         w.write( "</th><th>" );
501         w.write( "Study total" );
502         w.write( "</th><th>" );
503         w.write( "Study term" );
504         w.write( "</th><th>" );
505         w.write( "Domains" );
506         w.write( "</th><th>" );
507         w.write( "trivial?" );
508         w.write( "</th>" );
509         w.write( "</tr>" );
510         w.write( ForesterUtil.LINE_SEPARATOR );
511     }
512
513     private static void writeHtmlSpecies( final Writer writer, final String species ) throws IOException {
514         writer.write( "<tr>" );
515         writer.write( "<td><h3>" );
516         writer.write( species );
517         SurfacingUtil.writeTaxonomyLinks( writer, species );
518         writer.write( "</h3></td>" );
519         writer.write( "</tr>" );
520         writer.write( ForesterUtil.LINE_SEPARATOR );
521     }
522
523     private static void writeLabelsToTabWriter( final Writer writer ) throws IOException {
524         writer.write( "#species" );
525         writer.write( "\t" );
526         writer.write( "GO name" );
527         writer.write( "\t" );
528         writer.write( "GO id" );
529         writer.write( "\t" );
530         writer.write( "P adjusted" );
531         writer.write( "\t" );
532         writer.write( "P" );
533         writer.write( "\t" );
534         writer.write( "Pop total" );
535         writer.write( "\t" );
536         writer.write( "Pop term" );
537         writer.write( "\t" );
538         writer.write( "Study total" );
539         writer.write( "\t" );
540         writer.write( "Study term" );
541         writer.write( "\t" );
542         writer.write( "is trivial" );
543         writer.write( ForesterUtil.LINE_SEPARATOR );
544     }
545
546     private static void writeValuesToHtmlWriter( final OntologizerResult ontologizer_result,
547                                                  final GoTerm go_term,
548                                                  final Writer writer,
549                                                  final double p_adjusted_upper_limit,
550                                                  final String species,
551                                                  final Map<GoId, GoTerm> go_id_to_terms,
552                                                  final SortedSet<DomainId> domains_per_species,
553                                                  final List<PfamToGoMapping> pfam_to_go,
554                                                  final Set<DomainId> domain_ids_with_go_annot ) throws IOException {
555         final Color p_adj_color = ForesterUtil.calcColor( ontologizer_result.getPAdjusted(),
556                                                           0,
557                                                           p_adjusted_upper_limit,
558                                                           MIN_COLOR,
559                                                           MAX_COLOR );
560         final Color p_color = ForesterUtil.calcColor( ontologizer_result.getP(),
561                                                       0,
562                                                       p_adjusted_upper_limit,
563                                                       MIN_COLOR,
564                                                       MAX_COLOR );
565         writer.write( "<tr>" );
566         writer.write( "<td>" );
567         writer.write( "<font color=\"#" + ForesterUtil.colorToHex( p_adj_color ) + "\">" );
568         writer.write( go_term.getName() );
569         writer.write( "</font>" );
570         writer.write( "</td><td>" );
571         writer.write( "<a href=\"" + SurfacingConstants.GO_LINK + ontologizer_result.getGoId().getId()
572                 + "\" target=\"amigo_window\">" + ontologizer_result.getGoId().getId() + "</a>" );
573         writer.write( "</td><td>" );
574         writer.write( "<font color=\"#" + ForesterUtil.colorToHex( p_adj_color ) + "\">" );
575         writer.write( FORMATER.format( ontologizer_result.getPAdjusted() ) );
576         writer.write( "</font>" );
577         writer.write( "</td><td>" );
578         writer.write( "<font color=\"#" + ForesterUtil.colorToHex( p_color ) + "\">" );
579         writer.write( FORMATER.format( ontologizer_result.getP() ) );
580         writer.write( "</font>" );
581         writer.write( "</td><td>" );
582         writer.write( String.valueOf( ontologizer_result.getPopTotal() ) );
583         writer.write( "</td><td>" );
584         writer.write( String.valueOf( ontologizer_result.getPopTerm() ) );
585         writer.write( "</td><td>" );
586         writer.write( String.valueOf( ontologizer_result.getStudyTotal() ) );
587         writer.write( "</td><td>" );
588         writer.write( String.valueOf( ontologizer_result.getStudyTerm() ) );
589         writer.write( "</td><td>" );
590         if ( domains_per_species != null ) {
591             final StringBuilder sb = obtainDomainsForGoId( pfam_to_go,
592                                                            domains_per_species,
593                                                            go_id_to_terms,
594                                                            go_term.getGoId(),
595                                                            domain_ids_with_go_annot );
596             writer.write( sb.toString() );
597         }
598         else {
599             writer.write( " " );
600         }
601         writer.write( "</td><td>" );
602         if ( ontologizer_result.isTrivial() ) {
603             writer.write( "trivial" );
604         }
605         else {
606             writer.write( " " );
607         }
608         writer.write( "</td>" );
609         writer.write( "</tr>" );
610         writer.write( ForesterUtil.LINE_SEPARATOR );
611     }
612
613     private static void writeValuesToTabWriter( final String species,
614                                                 final OntologizerResult ontologizer_result,
615                                                 final GoTerm got_term,
616                                                 final Writer writer ) throws IOException {
617         writer.write( species );
618         writer.write( "\t" );
619         writer.write( got_term.getName() );
620         writer.write( "\t" );
621         writer.write( ontologizer_result.getGoId().getId() );
622         writer.write( "\t" );
623         writer.write( String.valueOf( ontologizer_result.getPAdjusted() ) );
624         writer.write( "\t" );
625         writer.write( String.valueOf( ontologizer_result.getP() ) );
626         writer.write( "\t" );
627         writer.write( String.valueOf( ontologizer_result.getPopTotal() ) );
628         writer.write( "\t" );
629         writer.write( String.valueOf( ontologizer_result.getPopTerm() ) );
630         writer.write( "\t" );
631         writer.write( String.valueOf( ontologizer_result.getStudyTotal() ) );
632         writer.write( "\t" );
633         writer.write( String.valueOf( ontologizer_result.getStudyTerm() ) );
634         writer.write( "\t" );
635         writer.write( String.valueOf( ontologizer_result.isTrivial() ) );
636         writer.write( ForesterUtil.LINE_SEPARATOR );
637     }
638 }