in progress
[jalview.git] / forester / java / src / org / forester / surfacing / DomainCountsDifferenceUtil.java
1 // $Id:
2 // $
3 //
4 // FORESTER -- software libraries and applications
5 // for evolutionary biology research and applications.
6 //
7 // Copyright (C) 2008-2009 Christian M. Zmasek
8 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
9 // All rights reserved
10 //
11 // This library is free software; you can redistribute it and/or
12 // modify it under the terms of the GNU Lesser General Public
13 // License as published by the Free Software Foundation; either
14 // version 2.1 of the License, or (at your option) any later version.
15 //
16 // This library is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 // Lesser General Public License for more details.
20 //
21 // You should have received a copy of the GNU Lesser General Public
22 // License along with this library; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 //
25 // Contact: phylosoft @ gmail . com
26 // WWW: www.phylosoft.org/forester
27
28 package org.forester.surfacing;
29
30 import java.io.BufferedWriter;
31 import java.io.File;
32 import java.io.FileWriter;
33 import java.io.IOException;
34 import java.io.Writer;
35 import java.text.DecimalFormat;
36 import java.text.NumberFormat;
37 import java.util.ArrayList;
38 import java.util.HashMap;
39 import java.util.List;
40 import java.util.Map;
41 import java.util.Set;
42 import java.util.SortedMap;
43 import java.util.SortedSet;
44 import java.util.TreeMap;
45 import java.util.TreeSet;
46
47 import org.forester.application.surfacing;
48 import org.forester.go.GoId;
49 import org.forester.go.GoTerm;
50 import org.forester.util.BasicDescriptiveStatistics;
51 import org.forester.util.DescriptiveStatistics;
52 import org.forester.util.ForesterUtil;
53
54 /*
55  * Poorly designed static class which essential has one method:
56  * calculateCopyNumberDifferences.
57  */
58 public final class DomainCountsDifferenceUtil {
59
60     private final static NumberFormat          FORMATTER                                   = new DecimalFormat( "0.0E0" );
61     private static final COPY_CALCULATION_MODE COPY_CALC_MODE_FOR_HIGH_COPY_TARGET_SPECIES = COPY_CALCULATION_MODE.MIN;
62     private static final COPY_CALCULATION_MODE COPY_CALC_MODE_FOR_HIGH_COPY_BASE_SPECIES   = COPY_CALCULATION_MODE.MIN;
63     private static final COPY_CALCULATION_MODE COPY_CALC_MODE_FOR_LOW_COPY_SPECIES         = COPY_CALCULATION_MODE.MAX;
64     private static final String                PLUS_MINUS_PROTEINS_FILE_DOM_SUFFIX         = ".prot";
65
66     //FIXME really needs to be tested! 
67     private static void addCounts( final SortedMap<BinaryDomainCombination, List<Integer>> copy_counts,
68                                    final BinaryDomainCombination dc,
69                                    final GenomeWideCombinableDomains genome,
70                                    final Set<BinaryDomainCombination> bdc ) {
71         if ( !copy_counts.containsKey( dc ) ) {
72             copy_counts.put( dc, new ArrayList<Integer>() );
73         }
74         if ( bdc.contains( dc )
75                 && ( ( ( BasicCombinableDomains ) genome.get( dc.getId0() ) ).getCombiningDomains().get( dc.getId1() ) != null ) ) {
76             final int count = ( ( BasicCombinableDomains ) genome.get( dc.getId0() ) ).getCombiningDomains()
77                     .get( dc.getId1() );
78             copy_counts.get( dc ).add( count );
79         }
80         else {
81             copy_counts.get( dc ).add( 0 );
82         }
83     }
84
85     private static void addCounts( final SortedMap<DomainId, List<Integer>> copy_counts,
86                                    final DomainId domain,
87                                    final GenomeWideCombinableDomains genome ) {
88         if ( !copy_counts.containsKey( domain ) ) {
89             copy_counts.put( domain, new ArrayList<Integer>() );
90         }
91         if ( genome.contains( domain ) ) {
92             copy_counts.get( domain ).add( genome.get( domain ).getKeyDomainProteinsCount() );
93         }
94         else {
95             copy_counts.get( domain ).add( 0 );
96         }
97     }
98
99     private static StringBuilder addGoInformation( final DomainId d,
100                                                    final Map<DomainId, List<GoId>> domain_id_to_go_ids_map,
101                                                    final Map<GoId, GoTerm> go_id_to_term_map ) {
102         final StringBuilder sb = new StringBuilder();
103         if ( ( domain_id_to_go_ids_map == null ) || domain_id_to_go_ids_map.isEmpty()
104                 || !domain_id_to_go_ids_map.containsKey( d ) ) {
105             return sb;
106         }
107         final List<GoId> go_ids = domain_id_to_go_ids_map.get( d );
108         for( int i = 0; i < go_ids.size(); ++i ) {
109             final GoId go_id = go_ids.get( i );
110             if ( go_id_to_term_map.containsKey( go_id ) ) {
111                 appendGoTerm( sb, go_id_to_term_map.get( go_id ) );
112                 sb.append( "<br>" );
113             }
114             else {
115                 sb.append( "go id \"" + go_id + "\" not found [" + d.getId() + "]" );
116             }
117         }
118         return sb;
119     }
120
121     private static void appendGoTerm( final StringBuilder sb, final GoTerm go_term ) {
122         final GoId go_id = go_term.getGoId();
123         sb.append( "<a href=\"" + SurfacingConstants.AMIGO_LINK + go_id + "\" target=\"amigo_window\">" + go_id
124                 + "</a>" );
125         sb.append( ":" );
126         sb.append( go_term.getName() );
127         sb.append( " [" );
128         sb.append( go_term.getGoNameSpace().toShortString() );
129         sb.append( "]" );
130     }
131
132     public static void calculateCopyNumberDifferences( final List<GenomeWideCombinableDomains> genomes,
133                                                        final SortedMap<Species, List<Protein>> protein_lists_per_species,
134                                                        final List<String> high_copy_base_species,
135                                                        final List<String> high_copy_target_species,
136                                                        final List<String> low_copy_species,
137                                                        final int min_diff,
138                                                        final Double factor,
139                                                        final File plain_output_dom,
140                                                        final File html_output_dom,
141                                                        final File html_output_dc,
142                                                        final Map<DomainId, List<GoId>> domain_id_to_go_ids_map,
143                                                        final Map<GoId, GoTerm> go_id_to_term_map,
144                                                        final File all_domains_go_ids_out_dom,
145                                                        final File passing_domains_go_ids_out_dom,
146                                                        final File proteins_file_base ) throws IOException {
147         if ( genomes.size() < 1 ) {
148             throw new IllegalArgumentException( "attempt to use empty list of genomes for domain difference calculation" );
149         }
150         if ( ( high_copy_base_species.size() < 1 ) || ( low_copy_species.size() < 1 ) ) {
151             throw new IllegalArgumentException( "attempt to use empty list of species for domain difference calculation" );
152         }
153         if ( high_copy_base_species.contains( high_copy_target_species )
154                 || low_copy_species.contains( high_copy_target_species ) ) {
155             throw new IllegalArgumentException( "species [" + high_copy_target_species
156                     + "] appears in other list as well" );
157         }
158         if ( min_diff < 0 ) {
159             throw new IllegalArgumentException( "attempt to use negative addition [" + min_diff + "]" );
160         }
161         if ( factor <= 0.0 ) {
162             throw new IllegalArgumentException( "attempt to use factor equal or smaller than 0.0 [" + factor + "]" );
163         }
164         SurfacingUtil.checkForOutputFileWriteability( plain_output_dom );
165         SurfacingUtil.checkForOutputFileWriteability( html_output_dom );
166         SurfacingUtil.checkForOutputFileWriteability( html_output_dc );
167         SurfacingUtil.checkForOutputFileWriteability( all_domains_go_ids_out_dom );
168         SurfacingUtil.checkForOutputFileWriteability( passing_domains_go_ids_out_dom );
169         final Writer plain_writer = new BufferedWriter( new FileWriter( plain_output_dom ) );
170         final Writer html_writer = new BufferedWriter( new FileWriter( html_output_dom ) );
171         final Writer html_writer_dc = new BufferedWriter( new FileWriter( html_output_dc ) );
172         final Writer all_gos_writer = new BufferedWriter( new FileWriter( all_domains_go_ids_out_dom ) );
173         final Writer passing_gos_writer = new BufferedWriter( new FileWriter( passing_domains_go_ids_out_dom ) );
174         final SortedMap<DomainId, Double> high_copy_base_values = new TreeMap<DomainId, Double>();
175         final SortedMap<DomainId, Double> high_copy_target_values = new TreeMap<DomainId, Double>();
176         final SortedMap<DomainId, Double> low_copy_values = new TreeMap<DomainId, Double>();
177         final SortedMap<DomainId, List<Integer>> high_copy_base_copy_counts = new TreeMap<DomainId, List<Integer>>();
178         final SortedMap<DomainId, List<Integer>> high_copy_target_copy_counts = new TreeMap<DomainId, List<Integer>>();
179         final SortedMap<DomainId, List<Integer>> low_copy_copy_counts = new TreeMap<DomainId, List<Integer>>();
180         final SortedSet<DomainId> all_domains = new TreeSet<DomainId>();
181         final SortedMap<BinaryDomainCombination, Double> high_copy_base_values_dc = new TreeMap<BinaryDomainCombination, Double>();
182         final SortedMap<BinaryDomainCombination, Double> high_copy_target_values_dc = new TreeMap<BinaryDomainCombination, Double>();
183         final SortedMap<BinaryDomainCombination, Double> low_copy_values_dc = new TreeMap<BinaryDomainCombination, Double>();
184         final SortedMap<BinaryDomainCombination, List<Integer>> high_copy_base_copy_counts_dc = new TreeMap<BinaryDomainCombination, List<Integer>>();
185         final SortedMap<BinaryDomainCombination, List<Integer>> high_copy_target_copy_counts_dc = new TreeMap<BinaryDomainCombination, List<Integer>>();
186         final SortedMap<BinaryDomainCombination, List<Integer>> low_copy_copy_counts_dc = new TreeMap<BinaryDomainCombination, List<Integer>>();
187         final SortedSet<BinaryDomainCombination> all_dcs = new TreeSet<BinaryDomainCombination>();
188         final Map<String, Set<BinaryDomainCombination>> bdcs_per_genome = new HashMap<String, Set<BinaryDomainCombination>>();
189         final SortedSet<GoId> go_ids_of_passing_domains = new TreeSet<GoId>();
190         final SortedSet<GoId> go_ids_all = new TreeSet<GoId>();
191         for( final GenomeWideCombinableDomains genome : genomes ) {
192             final SortedSet<DomainId> domains = genome.getAllDomainIds();
193             final SortedSet<BinaryDomainCombination> dcs = genome.toBinaryDomainCombinations();
194             final String species = genome.getSpecies().getSpeciesId();
195             bdcs_per_genome.put( species, genome.toBinaryDomainCombinations() );
196             for( final DomainId d : domains ) {
197                 all_domains.add( d );
198                 if ( domain_id_to_go_ids_map.containsKey( d ) ) {
199                     go_ids_all.addAll( domain_id_to_go_ids_map.get( d ) );
200                 }
201             }
202             for( final BinaryDomainCombination dc : dcs ) {
203                 all_dcs.add( dc );
204             }
205         }
206         for( final DomainId domain : all_domains ) {
207             for( final GenomeWideCombinableDomains genome : genomes ) {
208                 final String species = genome.getSpecies().getSpeciesId();
209                 if ( high_copy_base_species.contains( species ) ) {
210                     DomainCountsDifferenceUtil.addCounts( high_copy_base_copy_counts, domain, genome );
211                 }
212                 if ( high_copy_target_species.contains( species ) ) {
213                     DomainCountsDifferenceUtil.addCounts( high_copy_target_copy_counts, domain, genome );
214                 }
215                 if ( low_copy_species.contains( species ) ) {
216                     DomainCountsDifferenceUtil.addCounts( low_copy_copy_counts, domain, genome );
217                 }
218             }
219         }
220         for( final BinaryDomainCombination dc : all_dcs ) {
221             for( final GenomeWideCombinableDomains genome : genomes ) {
222                 final String species = genome.getSpecies().getSpeciesId();
223                 if ( high_copy_base_species.contains( species ) ) {
224                     DomainCountsDifferenceUtil.addCounts( high_copy_base_copy_counts_dc,
225                                                           dc,
226                                                           genome,
227                                                           bdcs_per_genome.get( species ) );
228                 }
229                 if ( high_copy_target_species.contains( species ) ) {
230                     DomainCountsDifferenceUtil.addCounts( high_copy_target_copy_counts_dc,
231                                                           dc,
232                                                           genome,
233                                                           bdcs_per_genome.get( species ) );
234                 }
235                 if ( low_copy_species.contains( species ) ) {
236                     DomainCountsDifferenceUtil.addCounts( low_copy_copy_counts_dc,
237                                                           dc,
238                                                           genome,
239                                                           bdcs_per_genome.get( species ) );
240                 }
241             }
242         }
243         for( final DomainId domain : all_domains ) {
244             calculateDomainCountsBasedValue( high_copy_target_values,
245                                              high_copy_target_copy_counts,
246                                              domain,
247                                              COPY_CALC_MODE_FOR_HIGH_COPY_TARGET_SPECIES );
248             calculateDomainCountsBasedValue( high_copy_base_values,
249                                              high_copy_base_copy_counts,
250                                              domain,
251                                              COPY_CALC_MODE_FOR_HIGH_COPY_BASE_SPECIES );
252             calculateDomainCountsBasedValue( low_copy_values,
253                                              low_copy_copy_counts,
254                                              domain,
255                                              COPY_CALC_MODE_FOR_LOW_COPY_SPECIES );
256         }
257         for( final BinaryDomainCombination dc : all_dcs ) {
258             calculateDomainCountsBasedValue( high_copy_target_values_dc,
259                                              high_copy_target_copy_counts_dc,
260                                              dc,
261                                              COPY_CALC_MODE_FOR_HIGH_COPY_TARGET_SPECIES );
262             calculateDomainCountsBasedValue( high_copy_base_values_dc,
263                                              high_copy_base_copy_counts_dc,
264                                              dc,
265                                              COPY_CALC_MODE_FOR_HIGH_COPY_BASE_SPECIES );
266             calculateDomainCountsBasedValue( low_copy_values_dc,
267                                              low_copy_copy_counts_dc,
268                                              dc,
269                                              COPY_CALC_MODE_FOR_LOW_COPY_SPECIES );
270         }
271         writeDomainValuesToFiles( genomes,
272                                   high_copy_base_species,
273                                   high_copy_target_species,
274                                   low_copy_species,
275                                   min_diff,
276                                   factor,
277                                   domain_id_to_go_ids_map,
278                                   go_id_to_term_map,
279                                   plain_writer,
280                                   html_writer,
281                                   proteins_file_base,
282                                   high_copy_base_values,
283                                   high_copy_target_values,
284                                   low_copy_values,
285                                   all_domains,
286                                   go_ids_of_passing_domains,
287                                   protein_lists_per_species );
288         writeDomainCombinationValuesToFiles( genomes,
289                                              high_copy_base_species,
290                                              high_copy_target_species,
291                                              low_copy_species,
292                                              min_diff,
293                                              factor,
294                                              html_writer_dc,
295                                              high_copy_base_values_dc,
296                                              high_copy_target_values_dc,
297                                              low_copy_values_dc,
298                                              all_dcs,
299                                              bdcs_per_genome );
300         writeGoIdsToFile( all_gos_writer, go_ids_all );
301         writeGoIdsToFile( passing_gos_writer, go_ids_of_passing_domains );
302     }
303
304     private static void calculateDomainCountsBasedValue( final SortedMap<BinaryDomainCombination, Double> copy_values,
305                                                          final SortedMap<BinaryDomainCombination, List<Integer>> copy_counts,
306                                                          final BinaryDomainCombination bdc,
307                                                          final COPY_CALCULATION_MODE copy_calc_mode ) {
308         if ( copy_counts.containsKey( bdc ) ) {
309             switch ( copy_calc_mode ) {
310                 case MAX:
311                     DomainCountsDifferenceUtil.calculateMaxCount( copy_values, copy_counts, bdc );
312                     break;
313                 case MIN:
314                     DomainCountsDifferenceUtil.calculateMinCount( copy_values, copy_counts, bdc );
315                     break;
316                 case MEAN:
317                     DomainCountsDifferenceUtil.calculateMeanCount( copy_values, copy_counts, bdc );
318                     break;
319                 case MEDIAN:
320                     DomainCountsDifferenceUtil.calculateMedianCount( copy_values, copy_counts, bdc );
321                     break;
322                 default:
323                     throw new IllegalArgumentException();
324             }
325         }
326         else {
327             copy_values.put( bdc, Double.valueOf( 0.0 ) );
328         }
329     }
330
331     private static void calculateDomainCountsBasedValue( final SortedMap<DomainId, Double> copy_values,
332                                                          final SortedMap<DomainId, List<Integer>> copy_counts,
333                                                          final DomainId domain,
334                                                          final COPY_CALCULATION_MODE copy_calc_mode ) {
335         if ( copy_counts.containsKey( domain ) ) {
336             switch ( copy_calc_mode ) {
337                 case MAX:
338                     DomainCountsDifferenceUtil.calculateMaxCount( copy_values, copy_counts, domain );
339                     break;
340                 case MIN:
341                     DomainCountsDifferenceUtil.calculateMinCount( copy_values, copy_counts, domain );
342                     break;
343                 case MEAN:
344                     DomainCountsDifferenceUtil.calculateMeanCount( copy_values, copy_counts, domain );
345                     break;
346                 case MEDIAN:
347                     DomainCountsDifferenceUtil.calculateMedianCount( copy_values, copy_counts, domain );
348                     break;
349                 default:
350                     throw new IllegalArgumentException();
351             }
352         }
353         else {
354             copy_values.put( domain, Double.valueOf( 0.0 ) );
355         }
356     }
357
358     private static void calculateMaxCount( final SortedMap<BinaryDomainCombination, Double> results,
359                                            final SortedMap<BinaryDomainCombination, List<Integer>> copy_counts,
360                                            final BinaryDomainCombination bdc ) {
361         final List<Integer> counts = copy_counts.get( bdc );
362         int max = 0;
363         for( final Integer count : counts ) {
364             if ( count > max ) {
365                 max = count;
366             }
367         }
368         results.put( bdc, ( double ) max );
369     }
370
371     private static void calculateMaxCount( final SortedMap<DomainId, Double> results,
372                                            final SortedMap<DomainId, List<Integer>> copy_counts,
373                                            final DomainId domain ) {
374         final List<Integer> counts = copy_counts.get( domain );
375         int max = 0;
376         for( final Integer count : counts ) {
377             if ( count > max ) {
378                 max = count;
379             }
380         }
381         results.put( domain, ( double ) max );
382     }
383
384     private static void calculateMeanCount( final SortedMap<BinaryDomainCombination, Double> results,
385                                             final SortedMap<BinaryDomainCombination, List<Integer>> copy_counts,
386                                             final BinaryDomainCombination bdc ) {
387         final List<Integer> counts = copy_counts.get( bdc );
388         int sum = 0;
389         for( final Integer count : counts ) {
390             sum += count;
391         }
392         results.put( bdc, ( ( double ) sum ) / ( ( double ) counts.size() ) );
393     }
394
395     private static void calculateMeanCount( final SortedMap<DomainId, Double> results,
396                                             final SortedMap<DomainId, List<Integer>> copy_counts,
397                                             final DomainId domain ) {
398         final List<Integer> counts = copy_counts.get( domain );
399         int sum = 0;
400         for( final Integer count : counts ) {
401             sum += count;
402         }
403         results.put( domain, ( ( double ) sum ) / ( ( double ) counts.size() ) );
404     }
405
406     private static void calculateMedianCount( final SortedMap<BinaryDomainCombination, Double> results,
407                                               final SortedMap<BinaryDomainCombination, List<Integer>> copy_counts,
408                                               final BinaryDomainCombination bdc ) {
409         final List<Integer> counts = copy_counts.get( bdc );
410         final DescriptiveStatistics stats = new BasicDescriptiveStatistics();
411         for( final Integer count : counts ) {
412             stats.addValue( count );
413         }
414         results.put( bdc, stats.median() );
415     }
416
417     private static void calculateMedianCount( final SortedMap<DomainId, Double> results,
418                                               final SortedMap<DomainId, List<Integer>> copy_counts,
419                                               final DomainId domain ) {
420         final List<Integer> counts = copy_counts.get( domain );
421         final DescriptiveStatistics stats = new BasicDescriptiveStatistics();
422         for( final Integer count : counts ) {
423             stats.addValue( count );
424         }
425         results.put( domain, stats.median() );
426     }
427
428     private static void calculateMinCount( final SortedMap<BinaryDomainCombination, Double> results,
429                                            final SortedMap<BinaryDomainCombination, List<Integer>> copy_counts,
430                                            final BinaryDomainCombination bdc ) {
431         final List<Integer> counts = copy_counts.get( bdc );
432         int min = Integer.MAX_VALUE;
433         for( final Integer count : counts ) {
434             if ( count < min ) {
435                 min = count;
436             }
437         }
438         results.put( bdc, ( double ) min );
439     }
440
441     private static void calculateMinCount( final SortedMap<DomainId, Double> results,
442                                            final SortedMap<DomainId, List<Integer>> copy_counts,
443                                            final DomainId domain ) {
444         final List<Integer> counts = copy_counts.get( domain );
445         int min = Integer.MAX_VALUE;
446         for( final Integer count : counts ) {
447             if ( count < min ) {
448                 min = count;
449             }
450         }
451         results.put( domain, ( double ) min );
452     }
453
454     private static String combinableDomaindToString( final CombinableDomains cd ) {
455         final StringBuilder sb = new StringBuilder();
456         sb.append( cd.getKeyDomainProteinsCount() );
457         sb.append( "\t[" );
458         sb.append( FORMATTER.format( cd.getKeyDomainConfidenceDescriptiveStatistics().median() ) );
459         sb.append( "]" );
460         return sb.toString();
461     }
462
463     private static String combinableDomaindToStringHtml( final CombinableDomains cd ) {
464         final StringBuilder sb = new StringBuilder();
465         sb.append( "[" );
466         sb.append( cd.getKeyDomainCount() );
467         sb.append( ", <b>" );
468         sb.append( cd.getKeyDomainProteinsCount() );
469         sb.append( "</b>, " );
470         sb.append( cd.getNumberOfCombinableDomains() );
471         sb.append( "]</td><td>[" );
472         sb.append( FORMATTER.format( cd.getKeyDomainConfidenceDescriptiveStatistics().median() ) );
473         sb.append( "]</td><td>" );
474         sb.append( cd.getCombiningDomainIdsAsStringBuilder() );
475         return sb.toString();
476     }
477
478     private static void writeCopyNumberValues( final SortedMap<BinaryDomainCombination, Double> copy_means,
479                                                final BinaryDomainCombination bdc,
480                                                final GenomeWideCombinableDomains genome,
481                                                final Map<String, Set<BinaryDomainCombination>> bdcs_per_genome,
482                                                final String species,
483                                                final Writer html_writer,
484                                                final String color ) throws IOException {
485         html_writer.write( "<td> " );
486         if ( !ForesterUtil.isEmpty( color ) ) {
487             html_writer.write( "<font color=\"" + color + "\">" );
488         }
489         html_writer.write( "<b>" + species + ":</b> " );
490         if ( !ForesterUtil.isEmpty( color ) ) {
491             html_writer.write( "</font>" );
492         }
493         html_writer.write( "</td><td>" );
494         if ( bdcs_per_genome.get( species ).contains( bdc ) && ( copy_means.get( bdc ) > 0 ) ) {
495             final int count = ( ( BasicCombinableDomains ) genome.get( bdc.getId0() ) ).getCombiningDomains()
496                     .get( bdc.getId1() );
497             html_writer.write( count + "" );
498         }
499         else {
500             html_writer.write( "0" );
501         }
502         html_writer.write( "</td>" );
503     }
504
505     private static void writeCopyNumberValues( final SortedMap<DomainId, Double> copy_means,
506                                                final DomainId domain,
507                                                final GenomeWideCombinableDomains genome,
508                                                final String species,
509                                                final Writer plain_writer,
510                                                final Writer html_writer,
511                                                final String color ) throws IOException {
512         plain_writer.write( "  " + species + "\t" );
513         html_writer.write( "<td> " );
514         if ( !ForesterUtil.isEmpty( color ) ) {
515             html_writer.write( "<font color=\"" + color + "\">" );
516         }
517         html_writer.write( "<b>" + species + ":</b> " );
518         if ( !ForesterUtil.isEmpty( color ) ) {
519             html_writer.write( "</font>" );
520         }
521         html_writer.write( "</td><td>" );
522         if ( genome.contains( domain ) && ( copy_means.get( domain ) > 0 ) ) {
523             plain_writer.write( DomainCountsDifferenceUtil.combinableDomaindToString( genome.get( domain ) ) );
524             html_writer.write( DomainCountsDifferenceUtil.combinableDomaindToStringHtml( genome.get( domain ) ) );
525         }
526         else {
527             plain_writer.write( "0" );
528             html_writer.write( "0" );
529         }
530         html_writer.write( "</td>" );
531         plain_writer.write( SurfacingConstants.NL );
532     }
533
534     private static void writeDomainCombinationValuesToFiles( final List<GenomeWideCombinableDomains> genomes,
535                                                              final List<String> high_copy_base_species,
536                                                              final List<String> high_copy_target_species,
537                                                              final List<String> low_copy_species,
538                                                              final int min_diff,
539                                                              final Double factor,
540                                                              final Writer html_writer,
541                                                              final SortedMap<BinaryDomainCombination, Double> high_copy_base_values,
542                                                              final SortedMap<BinaryDomainCombination, Double> high_copy_target_values,
543                                                              final SortedMap<BinaryDomainCombination, Double> low_copy_values,
544                                                              final SortedSet<BinaryDomainCombination> all_bdcs,
545                                                              final Map<String, Set<BinaryDomainCombination>> bdcs_per_genome )
546             throws IOException {
547         int counter = 0;
548         int total_absense_counter = 0;
549         int not_total_absense_counter = 0;
550         SurfacingUtil.addHtmlHead( html_writer, "Binary Domain Combination Copy Differences" );
551         html_writer.write( "<body><table>" );
552         for( final BinaryDomainCombination bdc : all_bdcs ) {
553             if ( ( high_copy_base_values.get( bdc ) > 0 ) && ( high_copy_target_values.get( bdc ) > 0 )
554                     && ( high_copy_base_values.get( bdc ) >= low_copy_values.get( bdc ) ) ) {
555                 if ( high_copy_target_values.get( bdc ) >= min_diff + ( factor * low_copy_values.get( bdc ) ) ) {
556                     if ( low_copy_values.get( bdc ) <= 0.0 ) {
557                         ++total_absense_counter;
558                     }
559                     else {
560                         ++not_total_absense_counter;
561                     }
562                     ++counter;
563                     html_writer.write( "<tr><td><a href=\"" + SurfacingConstants.PFAM_FAMILY_ID_LINK + bdc.getId0()
564                             + "\">" + bdc.getId0() + "</a> = <a href=\"" + SurfacingConstants.PFAM_FAMILY_ID_LINK
565                             + bdc.getId1() + "\">" + bdc.getId1() + "</a>" );
566                     html_writer.write( "</td><td>" );
567                     html_writer.write( "<table>" );
568                     for( final GenomeWideCombinableDomains genome : genomes ) {
569                         final String species = genome.getSpecies().getSpeciesId();
570                         if ( high_copy_target_species.contains( species ) ) {
571                             html_writer.write( "<tr>" );
572                             writeCopyNumberValues( high_copy_target_values,
573                                                    bdc,
574                                                    genome,
575                                                    bdcs_per_genome,
576                                                    species,
577                                                    html_writer,
578                                                    "#0000FF" );
579                             html_writer.write( "</tr>" );
580                         }
581                         else if ( low_copy_species.contains( species ) ) {
582                             html_writer.write( "<tr>" );
583                             writeCopyNumberValues( low_copy_values,
584                                                    bdc,
585                                                    genome,
586                                                    bdcs_per_genome,
587                                                    species,
588                                                    html_writer,
589                                                    "#A0A0A0" );
590                             html_writer.write( "</tr>" );
591                         }
592                         else if ( high_copy_base_species.contains( species ) ) {
593                             html_writer.write( "<tr>" );
594                             writeCopyNumberValues( high_copy_base_values,
595                                                    bdc,
596                                                    genome,
597                                                    bdcs_per_genome,
598                                                    species,
599                                                    html_writer,
600                                                    "#404040" );
601                             html_writer.write( "</tr>" );
602                         }
603                     }
604                     html_writer.write( "</table>" );
605                     html_writer.write( "</td></tr>" );
606                     html_writer.write( SurfacingConstants.NL );
607                 }
608             }
609         }
610         html_writer.write( "</table>" );
611         html_writer.write( SurfacingConstants.NL );
612         html_writer.write( "<hr>" );
613         html_writer.write( SurfacingConstants.NL );
614         html_writer.write( "Rule 1: high-copy-base > 0 && high-copy-target > 0 && high-copy-base >= low-copy" );
615         html_writer.write( "<br>" );
616         html_writer.write( SurfacingConstants.NL );
617         html_writer.write( "Rule 2: high-copy-target >= minimal-difference + ( factor * low-copy )" );
618         html_writer.write( "<br>" );
619         html_writer.write( SurfacingConstants.NL );
620         html_writer.write( "Calculation mode for high copy target : " + COPY_CALC_MODE_FOR_HIGH_COPY_TARGET_SPECIES );
621         html_writer.write( SurfacingConstants.NL );
622         html_writer.write( "<br>" );
623         html_writer.write( "Calculation mode for high copy base : " + COPY_CALC_MODE_FOR_HIGH_COPY_BASE_SPECIES );
624         html_writer.write( SurfacingConstants.NL );
625         html_writer.write( "<br>" );
626         html_writer.write( "Calculation mode for low copy : " + COPY_CALC_MODE_FOR_LOW_COPY_SPECIES );
627         html_writer.write( SurfacingConstants.NL );
628         html_writer.write( "<br>" );
629         html_writer.write( "Minimal difference : " + min_diff );
630         html_writer.write( SurfacingConstants.NL );
631         html_writer.write( "<br>" );
632         html_writer.write( "Factor : " + factor );
633         html_writer.write( SurfacingConstants.NL );
634         html_writer.write( "<br>" );
635         html_writer.write( "Lower copy binary domain combinations : " + counter );
636         html_writer.write( SurfacingConstants.NL );
637         html_writer.write( "<br>" );
638         html_writer.write( "Total absence : " + total_absense_counter );
639         html_writer.write( SurfacingConstants.NL );
640         html_writer.write( "<br>" );
641         html_writer.write( "Not total absence : " + not_total_absense_counter );
642         html_writer.write( SurfacingConstants.NL );
643         html_writer.write( "<br>" );
644         html_writer.write( "Total binary domain combinations : " + all_bdcs.size() );
645         html_writer.write( SurfacingConstants.NL );
646         html_writer.write( "<hr>" );
647         html_writer.write( SurfacingConstants.NL );
648         html_writer.write( "</body></html>" );
649         html_writer.write( SurfacingConstants.NL );
650         html_writer.close();
651     }
652
653     private static void writeDomainValuesToFiles( final List<GenomeWideCombinableDomains> genomes,
654                                                   final List<String> high_copy_base_species,
655                                                   final List<String> high_copy_target_species,
656                                                   final List<String> low_copy_species,
657                                                   final int min_diff,
658                                                   final Double factor,
659                                                   final Map<DomainId, List<GoId>> domain_id_to_go_ids_map,
660                                                   final Map<GoId, GoTerm> go_id_to_term_map,
661                                                   final Writer plain_writer,
662                                                   final Writer html_writer,
663                                                   final File proteins_file_base,
664                                                   final SortedMap<DomainId, Double> high_copy_base_values,
665                                                   final SortedMap<DomainId, Double> high_copy_target_values,
666                                                   final SortedMap<DomainId, Double> low_copy_values,
667                                                   final SortedSet<DomainId> all_domains,
668                                                   final SortedSet<GoId> go_ids_of_passing_domains,
669                                                   final SortedMap<Species, List<Protein>> protein_lists_per_species )
670             throws IOException {
671         int counter = 0;
672         int total_absense_counter = 0;
673         int not_total_absense_counter = 0;
674         SurfacingUtil.addHtmlHead( html_writer, "Domain Copy Differences" );
675         html_writer.write( "<body><table>" );
676         for( final DomainId domain_id : all_domains ) {
677             if ( ( high_copy_base_values.get( domain_id ) > 0 ) && ( high_copy_target_values.get( domain_id ) > 0 )
678                     && ( high_copy_base_values.get( domain_id ) >= low_copy_values.get( domain_id ) ) ) {
679                 if ( high_copy_target_values.get( domain_id ) >= min_diff
680                         + ( factor * low_copy_values.get( domain_id ) ) ) {
681                     if ( low_copy_values.get( domain_id ) <= 0.0 ) {
682                         ++total_absense_counter;
683                     }
684                     else {
685                         ++not_total_absense_counter;
686                     }
687                     ++counter;
688                     writeProteinsToFile( proteins_file_base, protein_lists_per_species, domain_id );
689                     if ( domain_id_to_go_ids_map.containsKey( domain_id ) ) {
690                         go_ids_of_passing_domains.addAll( domain_id_to_go_ids_map.get( domain_id ) );
691                     }
692                     plain_writer.write( domain_id.getId() );
693                     plain_writer.write( SurfacingConstants.NL );
694                     html_writer.write( "<tr><td><a href=\"" + SurfacingConstants.PFAM_FAMILY_ID_LINK
695                             + domain_id.getId() + "\">" + domain_id.getId() + "</a></td><td>" );
696                     html_writer.write( addGoInformation( domain_id, domain_id_to_go_ids_map, go_id_to_term_map )
697                             .toString() );
698                     html_writer.write( "</td><td>" );
699                     html_writer.write( "<table>" );
700                     for( final GenomeWideCombinableDomains genome : genomes ) {
701                         final String species = genome.getSpecies().getSpeciesId();
702                         if ( high_copy_target_species.contains( species ) ) {
703                             html_writer.write( "<tr>" );
704                             writeCopyNumberValues( high_copy_target_values,
705                                                    domain_id,
706                                                    genome,
707                                                    species,
708                                                    plain_writer,
709                                                    html_writer,
710                                                    "#0000FF" );
711                             html_writer.write( "</tr>" );
712                         }
713                         else if ( low_copy_species.contains( species ) ) {
714                             html_writer.write( "<tr>" );
715                             writeCopyNumberValues( low_copy_values,
716                                                    domain_id,
717                                                    genome,
718                                                    species,
719                                                    plain_writer,
720                                                    html_writer,
721                                                    "#A0A0A0" );
722                             html_writer.write( "</tr>" );
723                         }
724                         else if ( high_copy_base_species.contains( species ) ) {
725                             html_writer.write( "<tr>" );
726                             writeCopyNumberValues( high_copy_base_values,
727                                                    domain_id,
728                                                    genome,
729                                                    species,
730                                                    plain_writer,
731                                                    html_writer,
732                                                    "#404040" );
733                             html_writer.write( "</tr>" );
734                         }
735                     }
736                     html_writer.write( "</table>" );
737                     html_writer.write( "</td></tr>" );
738                     html_writer.write( SurfacingConstants.NL );
739                     plain_writer.write( SurfacingConstants.NL );
740                 }
741             }
742         }
743         html_writer.write( "</table>" );
744         html_writer.write( SurfacingConstants.NL );
745         html_writer.write( "<hr>" );
746         html_writer.write( SurfacingConstants.NL );
747         html_writer.write( "Rule 1: high-copy-base > 0 && high-copy-target > 0 && high-copy-base >= low-copy" );
748         html_writer.write( "<br>" );
749         html_writer.write( SurfacingConstants.NL );
750         html_writer.write( "Rule 2: high-copy-target >= minimal-difference + ( factor * low-copy )" );
751         html_writer.write( "<br>" );
752         html_writer.write( SurfacingConstants.NL );
753         html_writer.write( "Calculation mode for high copy target : " + COPY_CALC_MODE_FOR_HIGH_COPY_TARGET_SPECIES );
754         html_writer.write( SurfacingConstants.NL );
755         html_writer.write( "<br>" );
756         html_writer.write( "Calculation mode for high copy base : " + COPY_CALC_MODE_FOR_HIGH_COPY_BASE_SPECIES );
757         html_writer.write( SurfacingConstants.NL );
758         html_writer.write( "<br>" );
759         html_writer.write( "Calculation mode for low copy : " + COPY_CALC_MODE_FOR_LOW_COPY_SPECIES );
760         html_writer.write( SurfacingConstants.NL );
761         html_writer.write( "<br>" );
762         html_writer.write( "Minimal difference : " + min_diff );
763         html_writer.write( SurfacingConstants.NL );
764         html_writer.write( "<br>" );
765         html_writer.write( "Factor : " + factor );
766         html_writer.write( SurfacingConstants.NL );
767         html_writer.write( "<br>" );
768         html_writer.write( "Lower copy domains : " + counter );
769         html_writer.write( SurfacingConstants.NL );
770         html_writer.write( "<br>" );
771         html_writer.write( "Total absence : " + total_absense_counter );
772         html_writer.write( SurfacingConstants.NL );
773         html_writer.write( "<br>" );
774         html_writer.write( "Not total absence : " + not_total_absense_counter );
775         html_writer.write( SurfacingConstants.NL );
776         html_writer.write( "<br>" );
777         html_writer.write( "Total domains : " + all_domains.size() );
778         html_writer.write( SurfacingConstants.NL );
779         html_writer.write( "<hr>" );
780         html_writer.write( SurfacingConstants.NL );
781         html_writer.write( "</body></html>" );
782         html_writer.write( SurfacingConstants.NL );
783         html_writer.close();
784         plain_writer.write( "# Rule 1: high-copy-base > 0 && high-copy-target > 0 && high-copy-base >= low-copy" );
785         plain_writer.write( SurfacingConstants.NL );
786         plain_writer.write( "# Rule 2: high-copy-target >= minimal-difference + ( factor * low-copy )" );
787         plain_writer.write( SurfacingConstants.NL );
788         plain_writer.write( "# Calculation mode for high copy target: " + COPY_CALC_MODE_FOR_HIGH_COPY_TARGET_SPECIES );
789         plain_writer.write( SurfacingConstants.NL );
790         plain_writer.write( "# Calculation mode for high copy base  : " + COPY_CALC_MODE_FOR_HIGH_COPY_BASE_SPECIES );
791         plain_writer.write( SurfacingConstants.NL );
792         plain_writer.write( "# Calculation mode for low copy        : " + COPY_CALC_MODE_FOR_LOW_COPY_SPECIES );
793         plain_writer.write( SurfacingConstants.NL );
794         plain_writer.write( "# Minimal difference: " + min_diff );
795         plain_writer.write( SurfacingConstants.NL );
796         plain_writer.write( "# Factor            : " + factor );
797         plain_writer.write( SurfacingConstants.NL );
798         plain_writer.write( "# Lower copy domains: " + counter );
799         plain_writer.write( SurfacingConstants.NL );
800         plain_writer.write( "# Total absence     : " + total_absense_counter );
801         plain_writer.write( SurfacingConstants.NL );
802         plain_writer.write( "# Not total absence : " + not_total_absense_counter );
803         plain_writer.write( SurfacingConstants.NL );
804         plain_writer.write( "# Total domains     : " + all_domains.size() );
805         plain_writer.write( SurfacingConstants.NL );
806         plain_writer.close();
807     }
808
809     private static void writeGoIdsToFile( final Writer writer, final SortedSet<GoId> gos ) throws IOException {
810         for( final GoId go_id : gos ) {
811             writer.write( go_id.toString() );
812             writer.write( SurfacingConstants.NL );
813         }
814         writer.close();
815     }
816
817     private static void writeProteinsToFile( final File proteins_file_base,
818                                              final SortedMap<Species, List<Protein>> protein_lists_per_species,
819                                              final DomainId domain_id ) throws IOException {
820         final File my_proteins_file = new File( proteins_file_base.getParentFile() + ForesterUtil.FILE_SEPARATOR
821                 + domain_id + PLUS_MINUS_PROTEINS_FILE_DOM_SUFFIX );
822         SurfacingUtil.checkForOutputFileWriteability( my_proteins_file );
823         final Writer proteins_file_writer = new BufferedWriter( new FileWriter( my_proteins_file ) );
824         SurfacingUtil.extractProteinNames( protein_lists_per_species,
825                                            domain_id,
826                                            proteins_file_writer,
827                                            "\t",
828                                            surfacing.LIMIT_SPEC_FOR_PROT_EX );
829         proteins_file_writer.close();
830         System.out.println( "Wrote proteins list to \"" + my_proteins_file + "\"" );
831     }
832
833     public static enum COPY_CALCULATION_MODE {
834         MEAN, MEDIAN, MAX, MIN
835     }
836 }