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