b7590aa8f4da61b64ad9c3103347e8228e0321c9
[jalview.git] / forester / java / src / org / forester / surfacing / SurfacingUtil.java
1 // $Id:
2 //
3 // FORESTER -- software libraries and applications
4 // for evolutionary biology research and applications.
5 //
6 // Copyright (C) 2008-2009 Christian M. Zmasek
7 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
8 // All rights reserved
9 //
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Lesser General Public
12 // License as published by the Free Software Foundation; either
13 // version 2.1 of the License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 //
24 // Contact: phylosoft @ gmail . com
25 // WWW: www.phylosoft.org/forester
26
27 package org.forester.surfacing;
28
29 import java.io.BufferedWriter;
30 import java.io.File;
31 import java.io.FileWriter;
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.Arrays;
38 import java.util.Collections;
39 import java.util.Comparator;
40 import java.util.HashMap;
41 import java.util.HashSet;
42 import java.util.List;
43 import java.util.Map;
44 import java.util.Map.Entry;
45 import java.util.PriorityQueue;
46 import java.util.Set;
47 import java.util.SortedMap;
48 import java.util.SortedSet;
49 import java.util.TreeMap;
50 import java.util.TreeSet;
51 import java.util.regex.Matcher;
52 import java.util.regex.Pattern;
53
54 import org.forester.application.surfacing;
55 import org.forester.evoinference.distance.NeighborJoining;
56 import org.forester.evoinference.matrix.character.BasicCharacterStateMatrix;
57 import org.forester.evoinference.matrix.character.CharacterStateMatrix;
58 import org.forester.evoinference.matrix.character.CharacterStateMatrix.BinaryStates;
59 import org.forester.evoinference.matrix.character.CharacterStateMatrix.Format;
60 import org.forester.evoinference.matrix.character.CharacterStateMatrix.GainLossStates;
61 import org.forester.evoinference.matrix.distance.BasicSymmetricalDistanceMatrix;
62 import org.forester.evoinference.matrix.distance.DistanceMatrix;
63 import org.forester.go.GoId;
64 import org.forester.go.GoNameSpace;
65 import org.forester.go.GoTerm;
66 import org.forester.go.PfamToGoMapping;
67 import org.forester.io.parsers.nexus.NexusConstants;
68 import org.forester.io.writers.PhylogenyWriter;
69 import org.forester.phylogeny.Phylogeny;
70 import org.forester.phylogeny.PhylogenyMethods;
71 import org.forester.phylogeny.PhylogenyNode;
72 import org.forester.phylogeny.PhylogenyNodeI.NH_CONVERSION_SUPPORT_VALUE_STYLE;
73 import org.forester.phylogeny.data.BinaryCharacters;
74 import org.forester.phylogeny.data.Confidence;
75 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
76 import org.forester.protein.BasicDomain;
77 import org.forester.protein.BasicProtein;
78 import org.forester.protein.BinaryDomainCombination;
79 import org.forester.protein.Domain;
80 import org.forester.protein.DomainId;
81 import org.forester.protein.Protein;
82 import org.forester.species.Species;
83 import org.forester.surfacing.DomainSimilarityCalculator.Detailedness;
84 import org.forester.surfacing.DomainSimilarityCalculator.GoAnnotationOutput;
85 import org.forester.surfacing.GenomeWideCombinableDomains.GenomeWideCombinableDomainsSortOrder;
86 import org.forester.util.AsciiHistogram;
87 import org.forester.util.BasicDescriptiveStatistics;
88 import org.forester.util.BasicTable;
89 import org.forester.util.BasicTableParser;
90 import org.forester.util.DescriptiveStatistics;
91 import org.forester.util.ForesterUtil;
92
93 public final class SurfacingUtil {
94
95     private final static NumberFormat       FORMATTER                        = new DecimalFormat( "0.0E0" );
96     private final static NumberFormat       FORMATTER_3                      = new DecimalFormat( "0.000" );
97     private static final Comparator<Domain> ASCENDING_CONFIDENCE_VALUE_ORDER = new Comparator<Domain>() {
98
99                                                                                  @Override
100                                                                                  public int compare( final Domain d1,
101                                                                                                      final Domain d2 ) {
102                                                                                      if ( d1.getPerSequenceEvalue() < d2
103                                                                                              .getPerSequenceEvalue() ) {
104                                                                                          return -1;
105                                                                                      }
106                                                                                      else if ( d1
107                                                                                              .getPerSequenceEvalue() > d2
108                                                                                              .getPerSequenceEvalue() ) {
109                                                                                          return 1;
110                                                                                      }
111                                                                                      else {
112                                                                                          return d1.compareTo( d2 );
113                                                                                      }
114                                                                                  }
115                                                                              };
116     public final static Pattern             PATTERN_SP_STYLE_TAXONOMY        = Pattern.compile( "^[A-Z0-9]{3,5}$" );
117     private static final boolean            USE_LAST                         = true;
118
119     private SurfacingUtil() {
120         // Hidden constructor.
121     }
122
123     public static void addAllBinaryDomainCombinationToSet( final GenomeWideCombinableDomains genome,
124                                                            final SortedSet<BinaryDomainCombination> binary_domain_combinations ) {
125         final SortedMap<DomainId, CombinableDomains> all_cd = genome.getAllCombinableDomainsIds();
126         for( final DomainId domain_id : all_cd.keySet() ) {
127             binary_domain_combinations.addAll( all_cd.get( domain_id ).toBinaryDomainCombinations() );
128         }
129     }
130
131     public static void addAllDomainIdsToSet( final GenomeWideCombinableDomains genome,
132                                              final SortedSet<DomainId> domain_ids ) {
133         final SortedSet<DomainId> domains = genome.getAllDomainIds();
134         for( final DomainId domain : domains ) {
135             domain_ids.add( domain );
136         }
137     }
138
139     public static void addHtmlHead( final Writer w, final String title ) throws IOException {
140         w.write( SurfacingConstants.NL );
141         w.write( "<head>" );
142         w.write( "<title>" );
143         w.write( title );
144         w.write( "</title>" );
145         w.write( SurfacingConstants.NL );
146         w.write( "<style>" );
147         w.write( SurfacingConstants.NL );
148         w.write( "a:visited { color : #6633FF; text-decoration : none; }" );
149         w.write( SurfacingConstants.NL );
150         w.write( "a:link { color : #6633FF; text-decoration : none; }" );
151         w.write( SurfacingConstants.NL );
152         w.write( "a:active { color : #99FF00; text-decoration : none; }" );
153         w.write( SurfacingConstants.NL );
154         w.write( "a:hover { color : #FFFFFF; background-color : #99FF00; text-decoration : none; }" );
155         w.write( SurfacingConstants.NL );
156         w.write( "td { text-align: left; vertical-align: top; font-family: Verdana, Arial, Helvetica; font-size: 8pt}" );
157         w.write( SurfacingConstants.NL );
158         w.write( "h1 { color : #0000FF; font-family: Verdana, Arial, Helvetica; font-size: 18pt; font-weight: bold }" );
159         w.write( SurfacingConstants.NL );
160         w.write( "h2 { color : #0000FF; font-family: Verdana, Arial, Helvetica; font-size: 16pt; font-weight: bold }" );
161         w.write( SurfacingConstants.NL );
162         w.write( "</style>" );
163         w.write( SurfacingConstants.NL );
164         w.write( "</head>" );
165         w.write( SurfacingConstants.NL );
166     }
167
168     public static DescriptiveStatistics calculateDescriptiveStatisticsForMeanValues( final Set<DomainSimilarity> similarities ) {
169         final DescriptiveStatistics stats = new BasicDescriptiveStatistics();
170         for( final DomainSimilarity similarity : similarities ) {
171             stats.addValue( similarity.getMeanSimilarityScore() );
172         }
173         return stats;
174     }
175
176     private static void calculateIndependentDomainCombinationGains( final Phylogeny local_phylogeny_l,
177                                                                     final String outfilename_for_counts,
178                                                                     final String outfilename_for_dc,
179                                                                     final String outfilename_for_dc_for_go_mapping,
180                                                                     final String outfilename_for_dc_for_go_mapping_unique,
181                                                                     final String outfilename_for_rank_counts,
182                                                                     final String outfilename_for_ancestor_species_counts,
183                                                                     final String outfilename_for_protein_stats,
184                                                                     final Map<String, DescriptiveStatistics> protein_length_stats_by_dc,
185                                                                     final Map<String, DescriptiveStatistics> domain_number_stats_by_dc,
186                                                                     final Map<String, DescriptiveStatistics> domain_length_stats_by_domain ) {
187         try {
188             //
189             //            if ( protein_length_stats_by_dc != null ) {
190             //                for( final Entry<?, DescriptiveStatistics> entry : protein_length_stats_by_dc.entrySet() ) {
191             //                    System.out.print( entry.getKey().toString() );
192             //                    System.out.print( ": " );
193             //                    double[] a = entry.getValue().getDataAsDoubleArray();
194             //                    for( int i = 0; i < a.length; i++ ) {
195             //                        System.out.print( a[ i ] + " " );
196             //                    }
197             //                    System.out.println();
198             //                }
199             //            }
200             //            if ( domain_number_stats_by_dc != null ) {
201             //                for( final Entry<?, DescriptiveStatistics> entry : domain_number_stats_by_dc.entrySet() ) {
202             //                    System.out.print( entry.getKey().toString() );
203             //                    System.out.print( ": " );
204             //                    double[] a = entry.getValue().getDataAsDoubleArray();
205             //                    for( int i = 0; i < a.length; i++ ) {
206             //                        System.out.print( a[ i ] + " " );
207             //                    }
208             //                    System.out.println();
209             //                }
210             //            }
211             //
212             final BufferedWriter out_counts = new BufferedWriter( new FileWriter( outfilename_for_counts ) );
213             final BufferedWriter out_dc = new BufferedWriter( new FileWriter( outfilename_for_dc ) );
214             final BufferedWriter out_dc_for_go_mapping = new BufferedWriter( new FileWriter( outfilename_for_dc_for_go_mapping ) );
215             final BufferedWriter out_dc_for_go_mapping_unique = new BufferedWriter( new FileWriter( outfilename_for_dc_for_go_mapping_unique ) );
216             final SortedMap<String, Integer> dc_gain_counts = new TreeMap<String, Integer>();
217             for( final PhylogenyNodeIterator it = local_phylogeny_l.iteratorPostorder(); it.hasNext(); ) {
218                 final PhylogenyNode n = it.next();
219                 final Set<String> gained_dc = n.getNodeData().getBinaryCharacters().getGainedCharacters();
220                 for( final String dc : gained_dc ) {
221                     if ( dc_gain_counts.containsKey( dc ) ) {
222                         dc_gain_counts.put( dc, dc_gain_counts.get( dc ) + 1 );
223                     }
224                     else {
225                         dc_gain_counts.put( dc, 1 );
226                     }
227                 }
228             }
229             final SortedMap<Integer, Integer> histogram = new TreeMap<Integer, Integer>();
230             final SortedMap<Integer, StringBuilder> domain_lists = new TreeMap<Integer, StringBuilder>();
231             final SortedMap<Integer, DescriptiveStatistics> dc_reapp_counts_to_protein_length_stats = new TreeMap<Integer, DescriptiveStatistics>();
232             final SortedMap<Integer, DescriptiveStatistics> dc_reapp_counts_to_domain_number_stats = new TreeMap<Integer, DescriptiveStatistics>();
233             final SortedMap<Integer, DescriptiveStatistics> dc_reapp_counts_to_domain_lengths_stats = new TreeMap<Integer, DescriptiveStatistics>();
234             final SortedMap<Integer, PriorityQueue<String>> domain_lists_go = new TreeMap<Integer, PriorityQueue<String>>();
235             final SortedMap<Integer, SortedSet<String>> domain_lists_go_unique = new TreeMap<Integer, SortedSet<String>>();
236             final Set<String> dcs = dc_gain_counts.keySet();
237             final SortedSet<String> more_than_once = new TreeSet<String>();
238             final DescriptiveStatistics gained_once_lengths_stats = new BasicDescriptiveStatistics();
239             final DescriptiveStatistics gained_once_domain_count_stats = new BasicDescriptiveStatistics();
240             final DescriptiveStatistics gained_multiple_times_lengths_stats = new BasicDescriptiveStatistics();
241             final DescriptiveStatistics gained_multiple_times_domain_count_stats = new BasicDescriptiveStatistics();
242             long gained_multiple_times_domain_length_sum = 0;
243             long gained_once_domain_length_sum = 0;
244             long gained_multiple_times_domain_length_count = 0;
245             long gained_once_domain_length_count = 0;
246             for( final String dc : dcs ) {
247                 final int count = dc_gain_counts.get( dc );
248                 if ( histogram.containsKey( count ) ) {
249                     histogram.put( count, histogram.get( count ) + 1 );
250                     domain_lists.get( count ).append( ", " + dc );
251                     domain_lists_go.get( count ).addAll( splitDomainCombination( dc ) );
252                     domain_lists_go_unique.get( count ).addAll( splitDomainCombination( dc ) );
253                 }
254                 else {
255                     histogram.put( count, 1 );
256                     domain_lists.put( count, new StringBuilder( dc ) );
257                     final PriorityQueue<String> q = new PriorityQueue<String>();
258                     q.addAll( splitDomainCombination( dc ) );
259                     domain_lists_go.put( count, q );
260                     final SortedSet<String> set = new TreeSet<String>();
261                     set.addAll( splitDomainCombination( dc ) );
262                     domain_lists_go_unique.put( count, set );
263                 }
264                 if ( protein_length_stats_by_dc != null ) {
265                     if ( !dc_reapp_counts_to_protein_length_stats.containsKey( count ) ) {
266                         dc_reapp_counts_to_protein_length_stats.put( count, new BasicDescriptiveStatistics() );
267                     }
268                     dc_reapp_counts_to_protein_length_stats.get( count ).addValue( protein_length_stats_by_dc.get( dc )
269                             .arithmeticMean() );
270                 }
271                 if ( domain_number_stats_by_dc != null ) {
272                     if ( !dc_reapp_counts_to_domain_number_stats.containsKey( count ) ) {
273                         dc_reapp_counts_to_domain_number_stats.put( count, new BasicDescriptiveStatistics() );
274                     }
275                     dc_reapp_counts_to_domain_number_stats.get( count ).addValue( domain_number_stats_by_dc.get( dc )
276                             .arithmeticMean() );
277                 }
278                 if ( domain_length_stats_by_domain != null ) {
279                     if ( !dc_reapp_counts_to_domain_lengths_stats.containsKey( count ) ) {
280                         dc_reapp_counts_to_domain_lengths_stats.put( count, new BasicDescriptiveStatistics() );
281                     }
282                     final String[] ds = dc.split( "=" );
283                     dc_reapp_counts_to_domain_lengths_stats.get( count ).addValue( domain_length_stats_by_domain
284                             .get( ds[ 0 ] ).arithmeticMean() );
285                     dc_reapp_counts_to_domain_lengths_stats.get( count ).addValue( domain_length_stats_by_domain
286                             .get( ds[ 1 ] ).arithmeticMean() );
287                 }
288                 if ( count > 1 ) {
289                     more_than_once.add( dc );
290                     if ( protein_length_stats_by_dc != null ) {
291                         final DescriptiveStatistics s = protein_length_stats_by_dc.get( dc );
292                         for( final double element : s.getData() ) {
293                             gained_multiple_times_lengths_stats.addValue( element );
294                         }
295                     }
296                     if ( domain_number_stats_by_dc != null ) {
297                         final DescriptiveStatistics s = domain_number_stats_by_dc.get( dc );
298                         for( final double element : s.getData() ) {
299                             gained_multiple_times_domain_count_stats.addValue( element );
300                         }
301                     }
302                     if ( domain_length_stats_by_domain != null ) {
303                         final String[] ds = dc.split( "=" );
304                         final DescriptiveStatistics s0 = domain_length_stats_by_domain.get( ds[ 0 ] );
305                         final DescriptiveStatistics s1 = domain_length_stats_by_domain.get( ds[ 1 ] );
306                         for( final double element : s0.getData() ) {
307                             gained_multiple_times_domain_length_sum += element;
308                             ++gained_multiple_times_domain_length_count;
309                         }
310                         for( final double element : s1.getData() ) {
311                             gained_multiple_times_domain_length_sum += element;
312                             ++gained_multiple_times_domain_length_count;
313                         }
314                     }
315                 }
316                 else {
317                     if ( protein_length_stats_by_dc != null ) {
318                         final DescriptiveStatistics s = protein_length_stats_by_dc.get( dc );
319                         for( final double element : s.getData() ) {
320                             gained_once_lengths_stats.addValue( element );
321                         }
322                     }
323                     if ( domain_number_stats_by_dc != null ) {
324                         final DescriptiveStatistics s = domain_number_stats_by_dc.get( dc );
325                         for( final double element : s.getData() ) {
326                             gained_once_domain_count_stats.addValue( element );
327                         }
328                     }
329                     if ( domain_length_stats_by_domain != null ) {
330                         final String[] ds = dc.split( "=" );
331                         final DescriptiveStatistics s0 = domain_length_stats_by_domain.get( ds[ 0 ] );
332                         final DescriptiveStatistics s1 = domain_length_stats_by_domain.get( ds[ 1 ] );
333                         for( final double element : s0.getData() ) {
334                             gained_once_domain_length_sum += element;
335                             ++gained_once_domain_length_count;
336                         }
337                         for( final double element : s1.getData() ) {
338                             gained_once_domain_length_sum += element;
339                             ++gained_once_domain_length_count;
340                         }
341                     }
342                 }
343             }
344             final Set<Integer> histogram_keys = histogram.keySet();
345             for( final Integer histogram_key : histogram_keys ) {
346                 final int count = histogram.get( histogram_key );
347                 final StringBuilder dc = domain_lists.get( histogram_key );
348                 out_counts.write( histogram_key + "\t" + count + ForesterUtil.LINE_SEPARATOR );
349                 out_dc.write( histogram_key + "\t" + dc + ForesterUtil.LINE_SEPARATOR );
350                 out_dc_for_go_mapping.write( "#" + histogram_key + ForesterUtil.LINE_SEPARATOR );
351                 final Object[] sorted = domain_lists_go.get( histogram_key ).toArray();
352                 Arrays.sort( sorted );
353                 for( final Object domain : sorted ) {
354                     out_dc_for_go_mapping.write( domain + ForesterUtil.LINE_SEPARATOR );
355                 }
356                 out_dc_for_go_mapping_unique.write( "#" + histogram_key + ForesterUtil.LINE_SEPARATOR );
357                 for( final String domain : domain_lists_go_unique.get( histogram_key ) ) {
358                     out_dc_for_go_mapping_unique.write( domain + ForesterUtil.LINE_SEPARATOR );
359                 }
360             }
361             out_counts.close();
362             out_dc.close();
363             out_dc_for_go_mapping.close();
364             out_dc_for_go_mapping_unique.close();
365             final SortedMap<String, Integer> lca_rank_counts = new TreeMap<String, Integer>();
366             final SortedMap<String, Integer> lca_ancestor_species_counts = new TreeMap<String, Integer>();
367             for( final String dc : more_than_once ) {
368                 final List<PhylogenyNode> nodes = new ArrayList<PhylogenyNode>();
369                 for( final PhylogenyNodeIterator it = local_phylogeny_l.iteratorExternalForward(); it.hasNext(); ) {
370                     final PhylogenyNode n = it.next();
371                     if ( n.getNodeData().getBinaryCharacters().getGainedCharacters().contains( dc ) ) {
372                         nodes.add( n );
373                     }
374                 }
375                 for( int i = 0; i < nodes.size() - 1; ++i ) {
376                     for( int j = i + 1; j < nodes.size(); ++j ) {
377                         final PhylogenyNode lca = PhylogenyMethods.calculateLCA( nodes.get( i ), nodes.get( j ) );
378                         String rank = "unknown";
379                         if ( lca.getNodeData().isHasTaxonomy()
380                                 && !ForesterUtil.isEmpty( lca.getNodeData().getTaxonomy().getRank() ) ) {
381                             rank = lca.getNodeData().getTaxonomy().getRank();
382                         }
383                         addToCountMap( lca_rank_counts, rank );
384                         String lca_species;
385                         if ( lca.getNodeData().isHasTaxonomy()
386                                 && !ForesterUtil.isEmpty( lca.getNodeData().getTaxonomy().getScientificName() ) ) {
387                             lca_species = lca.getNodeData().getTaxonomy().getScientificName();
388                         }
389                         else if ( lca.getNodeData().isHasTaxonomy()
390                                 && !ForesterUtil.isEmpty( lca.getNodeData().getTaxonomy().getCommonName() ) ) {
391                             lca_species = lca.getNodeData().getTaxonomy().getCommonName();
392                         }
393                         else {
394                             lca_species = lca.getName();
395                         }
396                         addToCountMap( lca_ancestor_species_counts, lca_species );
397                     }
398                 }
399             }
400             final BufferedWriter out_for_rank_counts = new BufferedWriter( new FileWriter( outfilename_for_rank_counts ) );
401             final BufferedWriter out_for_ancestor_species_counts = new BufferedWriter( new FileWriter( outfilename_for_ancestor_species_counts ) );
402             ForesterUtil.map2writer( out_for_rank_counts, lca_rank_counts, "\t", ForesterUtil.LINE_SEPARATOR );
403             ForesterUtil.map2writer( out_for_ancestor_species_counts,
404                                      lca_ancestor_species_counts,
405                                      "\t",
406                                      ForesterUtil.LINE_SEPARATOR );
407             out_for_rank_counts.close();
408             out_for_ancestor_species_counts.close();
409             if ( !ForesterUtil.isEmpty( outfilename_for_protein_stats )
410                     && ( ( domain_length_stats_by_domain != null ) || ( protein_length_stats_by_dc != null ) || ( domain_number_stats_by_dc != null ) ) ) {
411                 final BufferedWriter w = new BufferedWriter( new FileWriter( outfilename_for_protein_stats ) );
412                 w.write( "Domain Lengths: " );
413                 w.write( "\n" );
414                 if ( domain_length_stats_by_domain != null ) {
415                     for( final Entry<Integer, DescriptiveStatistics> entry : dc_reapp_counts_to_domain_lengths_stats
416                             .entrySet() ) {
417                         w.write( entry.getKey().toString() );
418                         w.write( "\t" + entry.getValue().arithmeticMean() );
419                         w.write( "\t" + entry.getValue().median() );
420                         w.write( "\n" );
421                     }
422                 }
423                 w.flush();
424                 w.write( "\n" );
425                 w.write( "\n" );
426                 w.write( "Protein Lengths: " );
427                 w.write( "\n" );
428                 if ( protein_length_stats_by_dc != null ) {
429                     for( final Entry<Integer, DescriptiveStatistics> entry : dc_reapp_counts_to_protein_length_stats
430                             .entrySet() ) {
431                         w.write( entry.getKey().toString() );
432                         w.write( "\t" + entry.getValue().arithmeticMean() );
433                         w.write( "\t" + entry.getValue().median() );
434                         w.write( "\n" );
435                     }
436                 }
437                 w.flush();
438                 w.write( "\n" );
439                 w.write( "\n" );
440                 w.write( "Number of domains: " );
441                 w.write( "\n" );
442                 if ( domain_number_stats_by_dc != null ) {
443                     for( final Entry<Integer, DescriptiveStatistics> entry : dc_reapp_counts_to_domain_number_stats
444                             .entrySet() ) {
445                         w.write( entry.getKey().toString() );
446                         w.write( "\t" + entry.getValue().arithmeticMean() );
447                         w.write( "\t" + entry.getValue().median() );
448                         w.write( "\n" );
449                     }
450                 }
451                 w.flush();
452                 w.write( "\n" );
453                 w.write( "\n" );
454                 w.write( "Gained once, domain lengths:" );
455                 w.write( "\n" );
456                 w.write( "N: " + gained_once_domain_length_count );
457                 w.write( "\n" );
458                 w.write( "Avg: " + ( ( double ) gained_once_domain_length_sum / gained_once_domain_length_count ) );
459                 w.write( "\n" );
460                 w.write( "\n" );
461                 w.write( "Gained multiple times, domain lengths:" );
462                 w.write( "\n" );
463                 w.write( "N: " + gained_multiple_times_domain_length_count );
464                 w.write( "\n" );
465                 w.write( "Avg: "
466                         + ( ( double ) gained_multiple_times_domain_length_sum / gained_multiple_times_domain_length_count ) );
467                 w.write( "\n" );
468                 w.write( "\n" );
469                 w.write( "\n" );
470                 w.write( "\n" );
471                 w.write( "Gained once, protein lengths:" );
472                 w.write( "\n" );
473                 w.write( gained_once_lengths_stats.toString() );
474                 w.write( "\n" );
475                 w.write( "\n" );
476                 w.write( "Gained once, domain counts:" );
477                 w.write( "\n" );
478                 w.write( gained_once_domain_count_stats.toString() );
479                 w.write( "\n" );
480                 w.write( "\n" );
481                 w.write( "Gained multiple times, protein lengths:" );
482                 w.write( "\n" );
483                 w.write( gained_multiple_times_lengths_stats.toString() );
484                 w.write( "\n" );
485                 w.write( "\n" );
486                 w.write( "Gained multiple times, domain counts:" );
487                 w.write( "\n" );
488                 w.write( gained_multiple_times_domain_count_stats.toString() );
489                 w.flush();
490                 w.close();
491             }
492         }
493         catch ( final IOException e ) {
494             ForesterUtil.printWarningMessage( surfacing.PRG_NAME, "Failure to write: " + e );
495         }
496         ForesterUtil.programMessage( surfacing.PRG_NAME, "Wrote independent domain combination gains fitch counts to ["
497                 + outfilename_for_counts + "]" );
498         ForesterUtil.programMessage( surfacing.PRG_NAME, "Wrote independent domain combination gains fitch lists to ["
499                 + outfilename_for_dc + "]" );
500         ForesterUtil.programMessage( surfacing.PRG_NAME,
501                                      "Wrote independent domain combination gains fitch lists to (for GO mapping) ["
502                                              + outfilename_for_dc_for_go_mapping + "]" );
503         ForesterUtil.programMessage( surfacing.PRG_NAME,
504                                      "Wrote independent domain combination gains fitch lists to (for GO mapping, unique) ["
505                                              + outfilename_for_dc_for_go_mapping_unique + "]" );
506     }
507
508     private final static void addToCountMap( final Map<String, Integer> map, final String s ) {
509         if ( map.containsKey( s ) ) {
510             map.put( s, map.get( s ) + 1 );
511         }
512         else {
513             map.put( s, 1 );
514         }
515     }
516
517     public static int calculateOverlap( final Domain domain, final List<Boolean> covered_positions ) {
518         int overlap_count = 0;
519         for( int i = domain.getFrom(); i <= domain.getTo(); ++i ) {
520             if ( ( i < covered_positions.size() ) && ( covered_positions.get( i ) == true ) ) {
521                 ++overlap_count;
522             }
523         }
524         return overlap_count;
525     }
526
527     public static void checkForOutputFileWriteability( final File outfile ) {
528         final String error = ForesterUtil.isWritableFile( outfile );
529         if ( !ForesterUtil.isEmpty( error ) ) {
530             ForesterUtil.fatalError( surfacing.PRG_NAME, error );
531         }
532     }
533
534     private static SortedSet<String> collectAllDomainsChangedOnSubtree( final PhylogenyNode subtree_root,
535                                                                         final boolean get_gains ) {
536         final SortedSet<String> domains = new TreeSet<String>();
537         for( final PhylogenyNode descendant : PhylogenyMethods.getAllDescendants( subtree_root ) ) {
538             final BinaryCharacters chars = descendant.getNodeData().getBinaryCharacters();
539             if ( get_gains ) {
540                 domains.addAll( chars.getGainedCharacters() );
541             }
542             else {
543                 domains.addAll( chars.getLostCharacters() );
544             }
545         }
546         return domains;
547     }
548
549     public static void collectChangedDomainCombinationsFromBinaryStatesMatrixAsListToFile( final CharacterStateMatrix<CharacterStateMatrix.GainLossStates> matrix,
550                                                                                            final BinaryDomainCombination.DomainCombinationType dc_type,
551                                                                                            final List<BinaryDomainCombination> all_binary_domains_combination_gained,
552                                                                                            final boolean get_gains ) {
553         final SortedSet<String> sorted_ids = new TreeSet<String>();
554         for( int i = 0; i < matrix.getNumberOfIdentifiers(); ++i ) {
555             sorted_ids.add( matrix.getIdentifier( i ) );
556         }
557         for( final String id : sorted_ids ) {
558             for( int c = 0; c < matrix.getNumberOfCharacters(); ++c ) {
559                 if ( ( get_gains && ( matrix.getState( id, c ) == CharacterStateMatrix.GainLossStates.GAIN ) )
560                         || ( !get_gains && ( matrix.getState( id, c ) == CharacterStateMatrix.GainLossStates.LOSS ) ) ) {
561                     if ( dc_type == BinaryDomainCombination.DomainCombinationType.DIRECTED_ADJACTANT ) {
562                         all_binary_domains_combination_gained.add( AdjactantDirectedBinaryDomainCombination
563                                 .createInstance( matrix.getCharacter( c ) ) );
564                     }
565                     else if ( dc_type == BinaryDomainCombination.DomainCombinationType.DIRECTED ) {
566                         all_binary_domains_combination_gained.add( DirectedBinaryDomainCombination
567                                 .createInstance( matrix.getCharacter( c ) ) );
568                     }
569                     else {
570                         all_binary_domains_combination_gained.add( BasicBinaryDomainCombination.createInstance( matrix
571                                 .getCharacter( c ) ) );
572                     }
573                 }
574             }
575         }
576     }
577
578     private static File createBaseDirForPerNodeDomainFiles( final String base_dir,
579                                                             final boolean domain_combinations,
580                                                             final CharacterStateMatrix.GainLossStates state,
581                                                             final String outfile ) {
582         File per_node_go_mapped_domain_gain_loss_files_base_dir = new File( new File( outfile ).getParent()
583                 + ForesterUtil.FILE_SEPARATOR + base_dir );
584         if ( !per_node_go_mapped_domain_gain_loss_files_base_dir.exists() ) {
585             per_node_go_mapped_domain_gain_loss_files_base_dir.mkdir();
586         }
587         if ( domain_combinations ) {
588             per_node_go_mapped_domain_gain_loss_files_base_dir = new File( per_node_go_mapped_domain_gain_loss_files_base_dir
589                     + ForesterUtil.FILE_SEPARATOR + "DC" );
590         }
591         else {
592             per_node_go_mapped_domain_gain_loss_files_base_dir = new File( per_node_go_mapped_domain_gain_loss_files_base_dir
593                     + ForesterUtil.FILE_SEPARATOR + "DOMAINS" );
594         }
595         if ( !per_node_go_mapped_domain_gain_loss_files_base_dir.exists() ) {
596             per_node_go_mapped_domain_gain_loss_files_base_dir.mkdir();
597         }
598         if ( state == GainLossStates.GAIN ) {
599             per_node_go_mapped_domain_gain_loss_files_base_dir = new File( per_node_go_mapped_domain_gain_loss_files_base_dir
600                     + ForesterUtil.FILE_SEPARATOR + "GAINS" );
601         }
602         else if ( state == GainLossStates.LOSS ) {
603             per_node_go_mapped_domain_gain_loss_files_base_dir = new File( per_node_go_mapped_domain_gain_loss_files_base_dir
604                     + ForesterUtil.FILE_SEPARATOR + "LOSSES" );
605         }
606         else {
607             per_node_go_mapped_domain_gain_loss_files_base_dir = new File( per_node_go_mapped_domain_gain_loss_files_base_dir
608                     + ForesterUtil.FILE_SEPARATOR + "PRESENT" );
609         }
610         if ( !per_node_go_mapped_domain_gain_loss_files_base_dir.exists() ) {
611             per_node_go_mapped_domain_gain_loss_files_base_dir.mkdir();
612         }
613         return per_node_go_mapped_domain_gain_loss_files_base_dir;
614     }
615
616     public static Map<DomainId, List<GoId>> createDomainIdToGoIdMap( final List<PfamToGoMapping> pfam_to_go_mappings ) {
617         final Map<DomainId, List<GoId>> domain_id_to_go_ids_map = new HashMap<DomainId, List<GoId>>( pfam_to_go_mappings
618                 .size() );
619         for( final PfamToGoMapping pfam_to_go : pfam_to_go_mappings ) {
620             if ( !domain_id_to_go_ids_map.containsKey( pfam_to_go.getKey() ) ) {
621                 domain_id_to_go_ids_map.put( pfam_to_go.getKey(), new ArrayList<GoId>() );
622             }
623             domain_id_to_go_ids_map.get( pfam_to_go.getKey() ).add( pfam_to_go.getValue() );
624         }
625         return domain_id_to_go_ids_map;
626     }
627
628     public static Map<DomainId, Set<String>> createDomainIdToSecondaryFeaturesMap( final File secondary_features_map_file )
629             throws IOException {
630         final BasicTable<String> primary_table = BasicTableParser.parse( secondary_features_map_file, "\t" );
631         final Map<DomainId, Set<String>> map = new TreeMap<DomainId, Set<String>>();
632         for( int r = 0; r < primary_table.getNumberOfRows(); ++r ) {
633             final DomainId domain_id = new DomainId( primary_table.getValue( 0, r ) );
634             if ( !map.containsKey( domain_id ) ) {
635                 map.put( domain_id, new HashSet<String>() );
636             }
637             map.get( domain_id ).add( primary_table.getValue( 1, r ) );
638         }
639         return map;
640     }
641
642     public static Phylogeny createNjTreeBasedOnMatrixToFile( final File nj_tree_outfile, final DistanceMatrix distance ) {
643         checkForOutputFileWriteability( nj_tree_outfile );
644         final NeighborJoining nj = NeighborJoining.createInstance();
645         final Phylogeny phylogeny = nj.execute( ( BasicSymmetricalDistanceMatrix ) distance );
646         phylogeny.setName( nj_tree_outfile.getName() );
647         writePhylogenyToFile( phylogeny, nj_tree_outfile.toString() );
648         return phylogeny;
649     }
650
651     private static SortedSet<BinaryDomainCombination> createSetOfAllBinaryDomainCombinationsPerGenome( final GenomeWideCombinableDomains gwcd ) {
652         final SortedMap<DomainId, CombinableDomains> cds = gwcd.getAllCombinableDomainsIds();
653         final SortedSet<BinaryDomainCombination> binary_combinations = new TreeSet<BinaryDomainCombination>();
654         for( final DomainId domain_id : cds.keySet() ) {
655             final CombinableDomains cd = cds.get( domain_id );
656             binary_combinations.addAll( cd.toBinaryDomainCombinations() );
657         }
658         return binary_combinations;
659     }
660
661     public static void decoratePrintableDomainSimilarities( final SortedSet<DomainSimilarity> domain_similarities,
662                                                             final Detailedness detailedness,
663                                                             final GoAnnotationOutput go_annotation_output,
664                                                             final Map<GoId, GoTerm> go_id_to_term_map,
665                                                             final GoNameSpace go_namespace_limit ) {
666         if ( ( go_namespace_limit != null ) && ( ( go_id_to_term_map == null ) || go_id_to_term_map.isEmpty() ) ) {
667             throw new IllegalArgumentException( "attempt to use a GO namespace limit without a GO id to term map" );
668         }
669         for( final DomainSimilarity domain_similarity : domain_similarities ) {
670             if ( domain_similarity instanceof PrintableDomainSimilarity ) {
671                 final PrintableDomainSimilarity printable_domain_similarity = ( PrintableDomainSimilarity ) domain_similarity;
672                 printable_domain_similarity.setDetailedness( detailedness );
673                 printable_domain_similarity.setGoAnnotationOutput( go_annotation_output );
674                 printable_domain_similarity.setGoIdToTermMap( go_id_to_term_map );
675                 printable_domain_similarity.setGoNamespaceLimit( go_namespace_limit );
676             }
677         }
678     }
679
680     public static void executeDomainLengthAnalysis( final String[][] input_file_properties,
681                                                     final int number_of_genomes,
682                                                     final DomainLengthsTable domain_lengths_table,
683                                                     final File outfile ) throws IOException {
684         final DecimalFormat df = new DecimalFormat( "#.00" );
685         checkForOutputFileWriteability( outfile );
686         final BufferedWriter out = new BufferedWriter( new FileWriter( outfile ) );
687         out.write( "MEAN BASED STATISTICS PER SPECIES" );
688         out.write( ForesterUtil.LINE_SEPARATOR );
689         out.write( domain_lengths_table.createMeanBasedStatisticsPerSpeciesTable().toString() );
690         out.write( ForesterUtil.LINE_SEPARATOR );
691         out.write( ForesterUtil.LINE_SEPARATOR );
692         final List<DomainLengths> domain_lengths_list = domain_lengths_table.getDomainLengthsList();
693         out.write( "OUTLIER SPECIES PER DOMAIN (Z>=1.5)" );
694         out.write( ForesterUtil.LINE_SEPARATOR );
695         for( final DomainLengths domain_lengths : domain_lengths_list ) {
696             final List<Species> species_list = domain_lengths.getMeanBasedOutlierSpecies( 1.5 );
697             if ( species_list.size() > 0 ) {
698                 out.write( domain_lengths.getDomainId() + "\t" );
699                 for( final Species species : species_list ) {
700                     out.write( species + "\t" );
701                 }
702                 out.write( ForesterUtil.LINE_SEPARATOR );
703                 // DescriptiveStatistics stats_for_domain = domain_lengths
704                 //         .calculateMeanBasedStatistics();
705                 //AsciiHistogram histo = new AsciiHistogram( stats_for_domain );
706                 //System.out.println( histo.toStringBuffer( 40, '=', 60, 4 ).toString() );
707             }
708         }
709         out.write( ForesterUtil.LINE_SEPARATOR );
710         out.write( ForesterUtil.LINE_SEPARATOR );
711         out.write( "OUTLIER SPECIES (Z 1.0)" );
712         out.write( ForesterUtil.LINE_SEPARATOR );
713         final DescriptiveStatistics stats_for_all_species = domain_lengths_table
714                 .calculateMeanBasedStatisticsForAllSpecies();
715         out.write( stats_for_all_species.asSummary() );
716         out.write( ForesterUtil.LINE_SEPARATOR );
717         final AsciiHistogram histo = new AsciiHistogram( stats_for_all_species );
718         out.write( histo.toStringBuffer( 40, '=', 60, 4 ).toString() );
719         out.write( ForesterUtil.LINE_SEPARATOR );
720         final double population_sd = stats_for_all_species.sampleStandardDeviation();
721         final double population_mean = stats_for_all_species.arithmeticMean();
722         for( final Species species : domain_lengths_table.getSpecies() ) {
723             final double x = domain_lengths_table.calculateMeanBasedStatisticsForSpecies( species ).arithmeticMean();
724             final double z = ( x - population_mean ) / population_sd;
725             out.write( species + "\t" + z );
726             out.write( ForesterUtil.LINE_SEPARATOR );
727         }
728         out.write( ForesterUtil.LINE_SEPARATOR );
729         for( final Species species : domain_lengths_table.getSpecies() ) {
730             final DescriptiveStatistics stats_for_species = domain_lengths_table
731                     .calculateMeanBasedStatisticsForSpecies( species );
732             final double x = stats_for_species.arithmeticMean();
733             final double z = ( x - population_mean ) / population_sd;
734             if ( ( z <= -1.0 ) || ( z >= 1.0 ) ) {
735                 out.write( species + "\t" + df.format( z ) + "\t" + stats_for_species.asSummary() );
736                 out.write( ForesterUtil.LINE_SEPARATOR );
737             }
738         }
739         out.close();
740         //        final List<HistogramData> histogram_datas = new ArrayList<HistogramData>();
741         //        for( int i = 0; i < number_of_genomes; ++i ) {
742         //            final Species species = new BasicSpecies( input_file_properties[ i ][ 0 ] );
743         //            histogram_datas
744         //                    .add( new HistogramData( species.toString(), domain_lengths_table
745         //                            .calculateMeanBasedStatisticsForSpecies( species )
746         //                            .getDataAsDoubleArray(), 5, 600, null, 60 ) );
747         //        }
748         //        final HistogramsFrame hf = new HistogramsFrame( histogram_datas );
749         //        hf.setVisible( true );
750         System.gc();
751     }
752
753     /**
754      * 
755      * @param all_binary_domains_combination_lost_fitch 
756      * @param consider_directedness_and_adjacency_for_bin_combinations 
757      * @param all_binary_domains_combination_gained if null ignored, otherwise this is to list all binary domain combinations
758      * which were gained under unweighted (Fitch) parsimony.
759      */
760     public static void executeParsimonyAnalysis( final long random_number_seed_for_fitch_parsimony,
761                                                  final boolean radomize_fitch_parsimony,
762                                                  final String outfile_name,
763                                                  final DomainParsimonyCalculator domain_parsimony,
764                                                  final Phylogeny phylogeny,
765                                                  final Map<DomainId, List<GoId>> domain_id_to_go_ids_map,
766                                                  final Map<GoId, GoTerm> go_id_to_term_map,
767                                                  final GoNameSpace go_namespace_limit,
768                                                  final String parameters_str,
769                                                  final Map<DomainId, Set<String>>[] domain_id_to_secondary_features_maps,
770                                                  final SortedSet<DomainId> positive_filter,
771                                                  final boolean output_binary_domain_combinations_for_graphs,
772                                                  final List<BinaryDomainCombination> all_binary_domains_combination_gained_fitch,
773                                                  final List<BinaryDomainCombination> all_binary_domains_combination_lost_fitch,
774                                                  final BinaryDomainCombination.DomainCombinationType dc_type,
775                                                  final Map<String, DescriptiveStatistics> protein_length_stats_by_dc,
776                                                  final Map<String, DescriptiveStatistics> domain_number_stats_by_dc,
777                                                  final Map<String, DescriptiveStatistics> domain_length_stats_by_domain ) {
778         final String sep = ForesterUtil.LINE_SEPARATOR + "###################" + ForesterUtil.LINE_SEPARATOR;
779         final String date_time = ForesterUtil.getCurrentDateTime();
780         final SortedSet<String> all_pfams_encountered = new TreeSet<String>();
781         final SortedSet<String> all_pfams_gained_as_domains = new TreeSet<String>();
782         final SortedSet<String> all_pfams_lost_as_domains = new TreeSet<String>();
783         final SortedSet<String> all_pfams_gained_as_dom_combinations = new TreeSet<String>();
784         final SortedSet<String> all_pfams_lost_as_dom_combinations = new TreeSet<String>();
785         writeToNexus( outfile_name, domain_parsimony, phylogeny );
786         // DOLLO DOMAINS
787         // -------------
788         Phylogeny local_phylogeny_l = phylogeny.copy();
789         if ( ( positive_filter != null ) && ( positive_filter.size() > 0 ) ) {
790             domain_parsimony.executeDolloParsimonyOnDomainPresence( positive_filter );
791         }
792         else {
793             domain_parsimony.executeDolloParsimonyOnDomainPresence();
794         }
795         SurfacingUtil.writeMatrixToFile( domain_parsimony.getGainLossMatrix(), outfile_name
796                 + surfacing.PARSIMONY_OUTPUT_GL_SUFFIX_DOLLO_DOMAINS, Format.FORESTER );
797         SurfacingUtil.writeMatrixToFile( domain_parsimony.getGainLossCountsMatrix(), outfile_name
798                 + surfacing.PARSIMONY_OUTPUT_GL_COUNTS_SUFFIX_DOLLO_DOMAINS, Format.FORESTER );
799         SurfacingUtil.writeBinaryStatesMatrixAsListToFile( domain_parsimony.getGainLossMatrix(),
800                                                            CharacterStateMatrix.GainLossStates.GAIN,
801                                                            outfile_name + surfacing.PARSIMONY_OUTPUT_DOLLO_GAINS_D,
802                                                            sep,
803                                                            ForesterUtil.LINE_SEPARATOR,
804                                                            null );
805         SurfacingUtil.writeBinaryStatesMatrixAsListToFile( domain_parsimony.getGainLossMatrix(),
806                                                            CharacterStateMatrix.GainLossStates.LOSS,
807                                                            outfile_name + surfacing.PARSIMONY_OUTPUT_DOLLO_LOSSES_D,
808                                                            sep,
809                                                            ForesterUtil.LINE_SEPARATOR,
810                                                            null );
811         SurfacingUtil.writeBinaryStatesMatrixAsListToFile( domain_parsimony.getGainLossMatrix(), null, outfile_name
812                 + surfacing.PARSIMONY_OUTPUT_DOLLO_PRESENT_D, sep, ForesterUtil.LINE_SEPARATOR, null );
813         //HTML:
814         writeBinaryStatesMatrixToList( domain_id_to_go_ids_map,
815                                        go_id_to_term_map,
816                                        go_namespace_limit,
817                                        false,
818                                        domain_parsimony.getGainLossMatrix(),
819                                        CharacterStateMatrix.GainLossStates.GAIN,
820                                        outfile_name + surfacing.PARSIMONY_OUTPUT_DOLLO_GAINS_HTML_D,
821                                        sep,
822                                        ForesterUtil.LINE_SEPARATOR,
823                                        "Dollo Parsimony | Gains | Domains",
824                                        "+",
825                                        domain_id_to_secondary_features_maps,
826                                        all_pfams_encountered,
827                                        all_pfams_gained_as_domains,
828                                        "_dollo_gains_d" );
829         writeBinaryStatesMatrixToList( domain_id_to_go_ids_map,
830                                        go_id_to_term_map,
831                                        go_namespace_limit,
832                                        false,
833                                        domain_parsimony.getGainLossMatrix(),
834                                        CharacterStateMatrix.GainLossStates.LOSS,
835                                        outfile_name + surfacing.PARSIMONY_OUTPUT_DOLLO_LOSSES_HTML_D,
836                                        sep,
837                                        ForesterUtil.LINE_SEPARATOR,
838                                        "Dollo Parsimony | Losses | Domains",
839                                        "-",
840                                        domain_id_to_secondary_features_maps,
841                                        all_pfams_encountered,
842                                        all_pfams_lost_as_domains,
843                                        "_dollo_losses_d" );
844         writeBinaryStatesMatrixToList( domain_id_to_go_ids_map,
845                                        go_id_to_term_map,
846                                        go_namespace_limit,
847                                        false,
848                                        domain_parsimony.getGainLossMatrix(),
849                                        null,
850                                        outfile_name + surfacing.PARSIMONY_OUTPUT_DOLLO_PRESENT_HTML_D,
851                                        sep,
852                                        ForesterUtil.LINE_SEPARATOR,
853                                        "Dollo Parsimony | Present | Domains",
854                                        "",
855                                        domain_id_to_secondary_features_maps,
856                                        all_pfams_encountered,
857                                        null,
858                                        "_dollo_present_d" );
859         preparePhylogeny( local_phylogeny_l,
860                           domain_parsimony,
861                           date_time,
862                           "Dollo parsimony on domain presence/absence",
863                           "dollo_on_domains_" + outfile_name,
864                           parameters_str );
865         SurfacingUtil.writePhylogenyToFile( local_phylogeny_l, outfile_name
866                 + surfacing.DOMAINS_PARSIMONY_TREE_OUTPUT_SUFFIX_DOLLO );
867         try {
868             writeAllDomainsChangedOnAllSubtrees( local_phylogeny_l, true, outfile_name, "_dollo_all_gains_d" );
869             writeAllDomainsChangedOnAllSubtrees( local_phylogeny_l, false, outfile_name, "_dollo_all_losses_d" );
870         }
871         catch ( final IOException e ) {
872             e.printStackTrace();
873             ForesterUtil.fatalError( surfacing.PRG_NAME, e.getLocalizedMessage() );
874         }
875         if ( domain_parsimony.calculateNumberOfBinaryDomainCombination() > 0 ) {
876             // FITCH DOMAIN COMBINATIONS
877             // -------------------------
878             local_phylogeny_l = phylogeny.copy();
879             String randomization = "no";
880             if ( radomize_fitch_parsimony ) {
881                 domain_parsimony.executeFitchParsimonyOnBinaryDomainCombintion( random_number_seed_for_fitch_parsimony );
882                 randomization = "yes, seed = " + random_number_seed_for_fitch_parsimony;
883             }
884             else {
885                 domain_parsimony.executeFitchParsimonyOnBinaryDomainCombintion( USE_LAST );
886             }
887             SurfacingUtil.writeMatrixToFile( domain_parsimony.getGainLossMatrix(), outfile_name
888                     + surfacing.PARSIMONY_OUTPUT_GL_SUFFIX_FITCH_BINARY_COMBINATIONS, Format.FORESTER );
889             SurfacingUtil.writeMatrixToFile( domain_parsimony.getGainLossCountsMatrix(), outfile_name
890                     + surfacing.PARSIMONY_OUTPUT_GL_COUNTS_SUFFIX_FITCH_BINARY_COMBINATIONS, Format.FORESTER );
891             SurfacingUtil
892                     .writeBinaryStatesMatrixAsListToFile( domain_parsimony.getGainLossMatrix(),
893                                                           CharacterStateMatrix.GainLossStates.GAIN,
894                                                           outfile_name + surfacing.PARSIMONY_OUTPUT_FITCH_GAINS_BC,
895                                                           sep,
896                                                           ForesterUtil.LINE_SEPARATOR,
897                                                           null );
898             SurfacingUtil.writeBinaryStatesMatrixAsListToFile( domain_parsimony.getGainLossMatrix(),
899                                                                CharacterStateMatrix.GainLossStates.LOSS,
900                                                                outfile_name
901                                                                        + surfacing.PARSIMONY_OUTPUT_FITCH_LOSSES_BC,
902                                                                sep,
903                                                                ForesterUtil.LINE_SEPARATOR,
904                                                                null );
905             SurfacingUtil.writeBinaryStatesMatrixAsListToFile( domain_parsimony.getGainLossMatrix(), null, outfile_name
906                     + surfacing.PARSIMONY_OUTPUT_FITCH_PRESENT_BC, sep, ForesterUtil.LINE_SEPARATOR, null );
907             if ( all_binary_domains_combination_gained_fitch != null ) {
908                 collectChangedDomainCombinationsFromBinaryStatesMatrixAsListToFile( domain_parsimony.getGainLossMatrix(),
909                                                                                     dc_type,
910                                                                                     all_binary_domains_combination_gained_fitch,
911                                                                                     true );
912             }
913             if ( all_binary_domains_combination_lost_fitch != null ) {
914                 collectChangedDomainCombinationsFromBinaryStatesMatrixAsListToFile( domain_parsimony.getGainLossMatrix(),
915                                                                                     dc_type,
916                                                                                     all_binary_domains_combination_lost_fitch,
917                                                                                     false );
918             }
919             if ( output_binary_domain_combinations_for_graphs ) {
920                 SurfacingUtil
921                         .writeBinaryStatesMatrixAsListToFileForBinaryCombinationsForGraphAnalysis( domain_parsimony
922                                                                                                            .getGainLossMatrix(),
923                                                                                                    null,
924                                                                                                    outfile_name
925                                                                                                            + surfacing.PARSIMONY_OUTPUT_FITCH_PRESENT_BC_OUTPUTFILE_SUFFIX_FOR_GRAPH_ANALYSIS,
926                                                                                                    sep,
927                                                                                                    ForesterUtil.LINE_SEPARATOR,
928                                                                                                    BinaryDomainCombination.OutputFormat.DOT );
929             }
930             // HTML:
931             writeBinaryStatesMatrixToList( domain_id_to_go_ids_map,
932                                            go_id_to_term_map,
933                                            go_namespace_limit,
934                                            true,
935                                            domain_parsimony.getGainLossMatrix(),
936                                            CharacterStateMatrix.GainLossStates.GAIN,
937                                            outfile_name + surfacing.PARSIMONY_OUTPUT_FITCH_GAINS_HTML_BC,
938                                            sep,
939                                            ForesterUtil.LINE_SEPARATOR,
940                                            "Fitch Parsimony | Gains | Domain Combinations",
941                                            "+",
942                                            null,
943                                            all_pfams_encountered,
944                                            all_pfams_gained_as_dom_combinations,
945                                            "_fitch_gains_dc" );
946             writeBinaryStatesMatrixToList( domain_id_to_go_ids_map,
947                                            go_id_to_term_map,
948                                            go_namespace_limit,
949                                            true,
950                                            domain_parsimony.getGainLossMatrix(),
951                                            CharacterStateMatrix.GainLossStates.LOSS,
952                                            outfile_name + surfacing.PARSIMONY_OUTPUT_FITCH_LOSSES_HTML_BC,
953                                            sep,
954                                            ForesterUtil.LINE_SEPARATOR,
955                                            "Fitch Parsimony | Losses | Domain Combinations",
956                                            "-",
957                                            null,
958                                            all_pfams_encountered,
959                                            all_pfams_lost_as_dom_combinations,
960                                            "_fitch_losses_dc" );
961             writeBinaryStatesMatrixToList( domain_id_to_go_ids_map,
962                                            go_id_to_term_map,
963                                            go_namespace_limit,
964                                            true,
965                                            domain_parsimony.getGainLossMatrix(),
966                                            null,
967                                            outfile_name + surfacing.PARSIMONY_OUTPUT_FITCH_PRESENT_HTML_BC,
968                                            sep,
969                                            ForesterUtil.LINE_SEPARATOR,
970                                            "Fitch Parsimony | Present | Domain Combinations",
971                                            "",
972                                            null,
973                                            all_pfams_encountered,
974                                            null,
975                                            "_fitch_present_dc" );
976             writeAllEncounteredPfamsToFile( domain_id_to_go_ids_map,
977                                             go_id_to_term_map,
978                                             outfile_name,
979                                             all_pfams_encountered );
980             writePfamsToFile( outfile_name + surfacing.ALL_PFAMS_GAINED_AS_DOMAINS_SUFFIX, all_pfams_gained_as_domains );
981             writePfamsToFile( outfile_name + surfacing.ALL_PFAMS_LOST_AS_DOMAINS_SUFFIX, all_pfams_lost_as_domains );
982             writePfamsToFile( outfile_name + surfacing.ALL_PFAMS_GAINED_AS_DC_SUFFIX,
983                               all_pfams_gained_as_dom_combinations );
984             writePfamsToFile( outfile_name + surfacing.ALL_PFAMS_LOST_AS_DC_SUFFIX, all_pfams_lost_as_dom_combinations );
985             preparePhylogeny( local_phylogeny_l,
986                               domain_parsimony,
987                               date_time,
988                               "Fitch parsimony on binary domain combination presence/absence randomization: "
989                                       + randomization,
990                               "fitch_on_binary_domain_combinations_" + outfile_name,
991                               parameters_str );
992             SurfacingUtil.writePhylogenyToFile( local_phylogeny_l, outfile_name
993                     + surfacing.BINARY_DOMAIN_COMBINATIONS_PARSIMONY_TREE_OUTPUT_SUFFIX_FITCH );
994             calculateIndependentDomainCombinationGains( local_phylogeny_l,
995                                                         outfile_name
996                                                                 + surfacing.INDEPENDENT_DC_GAINS_FITCH_PARS_COUNTS_OUTPUT_SUFFIX,
997                                                         outfile_name
998                                                                 + surfacing.INDEPENDENT_DC_GAINS_FITCH_PARS_DC_OUTPUT_SUFFIX,
999                                                         outfile_name
1000                                                                 + surfacing.INDEPENDENT_DC_GAINS_FITCH_PARS_DC_FOR_GO_MAPPING_OUTPUT_SUFFIX,
1001                                                         outfile_name
1002                                                                 + surfacing.INDEPENDENT_DC_GAINS_FITCH_PARS_DC_FOR_GO_MAPPING_OUTPUT_UNIQUE_SUFFIX,
1003                                                         outfile_name + "_indep_dc_gains_fitch_lca_ranks.txt",
1004                                                         outfile_name + "_indep_dc_gains_fitch_lca_taxonomies.txt",
1005                                                         outfile_name + "_indep_dc_gains_fitch_protein_statistics.txt",
1006                                                         protein_length_stats_by_dc,
1007                                                         domain_number_stats_by_dc,
1008                                                         domain_length_stats_by_domain );
1009         }
1010     }
1011
1012     public static void executeParsimonyAnalysisForSecondaryFeatures( final String outfile_name,
1013                                                                      final DomainParsimonyCalculator secondary_features_parsimony,
1014                                                                      final Phylogeny phylogeny,
1015                                                                      final String parameters_str,
1016                                                                      final Map<Species, MappingResults> mapping_results_map ) {
1017         final String sep = ForesterUtil.LINE_SEPARATOR + "###################" + ForesterUtil.LINE_SEPARATOR;
1018         final String date_time = ForesterUtil.getCurrentDateTime();
1019         System.out.println();
1020         writeToNexus( outfile_name + surfacing.NEXUS_SECONDARY_FEATURES,
1021                       secondary_features_parsimony.createMatrixOfSecondaryFeaturePresenceOrAbsence( null ),
1022                       phylogeny );
1023         Phylogeny local_phylogeny_copy = phylogeny.copy();
1024         secondary_features_parsimony.executeDolloParsimonyOnSecondaryFeatures( mapping_results_map );
1025         SurfacingUtil.writeMatrixToFile( secondary_features_parsimony.getGainLossMatrix(), outfile_name
1026                 + surfacing.PARSIMONY_OUTPUT_GL_SUFFIX_DOLLO_SECONDARY_FEATURES, Format.FORESTER );
1027         SurfacingUtil.writeMatrixToFile( secondary_features_parsimony.getGainLossCountsMatrix(), outfile_name
1028                 + surfacing.PARSIMONY_OUTPUT_GL_COUNTS_SUFFIX_DOLLO_SECONDARY_FEATURES, Format.FORESTER );
1029         SurfacingUtil
1030                 .writeBinaryStatesMatrixAsListToFile( secondary_features_parsimony.getGainLossMatrix(),
1031                                                       CharacterStateMatrix.GainLossStates.GAIN,
1032                                                       outfile_name
1033                                                               + surfacing.PARSIMONY_OUTPUT_DOLLO_GAINS_SECONDARY_FEATURES,
1034                                                       sep,
1035                                                       ForesterUtil.LINE_SEPARATOR,
1036                                                       null );
1037         SurfacingUtil
1038                 .writeBinaryStatesMatrixAsListToFile( secondary_features_parsimony.getGainLossMatrix(),
1039                                                       CharacterStateMatrix.GainLossStates.LOSS,
1040                                                       outfile_name
1041                                                               + surfacing.PARSIMONY_OUTPUT_DOLLO_LOSSES_SECONDARY_FEATURES,
1042                                                       sep,
1043                                                       ForesterUtil.LINE_SEPARATOR,
1044                                                       null );
1045         SurfacingUtil
1046                 .writeBinaryStatesMatrixAsListToFile( secondary_features_parsimony.getGainLossMatrix(),
1047                                                       null,
1048                                                       outfile_name
1049                                                               + surfacing.PARSIMONY_OUTPUT_DOLLO_PRESENT_SECONDARY_FEATURES,
1050                                                       sep,
1051                                                       ForesterUtil.LINE_SEPARATOR,
1052                                                       null );
1053         preparePhylogeny( local_phylogeny_copy,
1054                           secondary_features_parsimony,
1055                           date_time,
1056                           "Dollo parsimony on secondary feature presence/absence",
1057                           "dollo_on_secondary_features_" + outfile_name,
1058                           parameters_str );
1059         SurfacingUtil.writePhylogenyToFile( local_phylogeny_copy, outfile_name
1060                 + surfacing.SECONDARY_FEATURES_PARSIMONY_TREE_OUTPUT_SUFFIX_DOLLO );
1061         // FITCH DOMAIN COMBINATIONS
1062         // -------------------------
1063         local_phylogeny_copy = phylogeny.copy();
1064         final String randomization = "no";
1065         secondary_features_parsimony.executeFitchParsimonyOnBinaryDomainCombintionOnSecondaryFeatures( USE_LAST );
1066         preparePhylogeny( local_phylogeny_copy,
1067                           secondary_features_parsimony,
1068                           date_time,
1069                           "Fitch parsimony on secondary binary domain combination presence/absence randomization: "
1070                                   + randomization,
1071                           "fitch_on_binary_domain_combinations_" + outfile_name,
1072                           parameters_str );
1073         SurfacingUtil.writePhylogenyToFile( local_phylogeny_copy, outfile_name
1074                 + surfacing.BINARY_DOMAIN_COMBINATIONS_PARSIMONY_TREE_OUTPUT_SUFFIX_FITCH_MAPPED );
1075         calculateIndependentDomainCombinationGains( local_phylogeny_copy, outfile_name
1076                 + surfacing.INDEPENDENT_DC_GAINS_FITCH_PARS_COUNTS_MAPPED_OUTPUT_SUFFIX, outfile_name
1077                 + surfacing.INDEPENDENT_DC_GAINS_FITCH_PARS_DC_MAPPED_OUTPUT_SUFFIX, outfile_name
1078                 + surfacing.INDEPENDENT_DC_GAINS_FITCH_PARS_DC_FOR_GO_MAPPING_MAPPED_OUTPUT_SUFFIX, outfile_name
1079                 + surfacing.INDEPENDENT_DC_GAINS_FITCH_PARS_DC_FOR_GO_MAPPING_MAPPED_OUTPUT_UNIQUE_SUFFIX, outfile_name
1080                 + "_MAPPED_indep_dc_gains_fitch_lca_ranks.txt", outfile_name
1081                 + "_MAPPED_indep_dc_gains_fitch_lca_taxonomies.txt", null, null, null, null );
1082     }
1083
1084     public static void doit( final List<Protein> proteins,
1085                              final List<DomainId> query_domain_ids_nc_order,
1086                              final Writer out,
1087                              final String separator,
1088                              final String limit_to_species,
1089                              final Map<String, List<Integer>> average_protein_lengths_by_dc ) throws IOException {
1090         for( final Protein protein : proteins ) {
1091             if ( ForesterUtil.isEmpty( limit_to_species )
1092                     || protein.getSpecies().getSpeciesId().equalsIgnoreCase( limit_to_species ) ) {
1093                 if ( protein.contains( query_domain_ids_nc_order, true ) ) {
1094                     out.write( protein.getSpecies().getSpeciesId() );
1095                     out.write( separator );
1096                     out.write( protein.getProteinId().getId() );
1097                     out.write( separator );
1098                     out.write( "[" );
1099                     final Set<DomainId> visited_domain_ids = new HashSet<DomainId>();
1100                     boolean first = true;
1101                     for( final Domain domain : protein.getProteinDomains() ) {
1102                         if ( !visited_domain_ids.contains( domain.getDomainId() ) ) {
1103                             visited_domain_ids.add( domain.getDomainId() );
1104                             if ( first ) {
1105                                 first = false;
1106                             }
1107                             else {
1108                                 out.write( " " );
1109                             }
1110                             out.write( domain.getDomainId().getId() );
1111                             out.write( " {" );
1112                             out.write( "" + domain.getTotalCount() );
1113                             out.write( "}" );
1114                         }
1115                     }
1116                     out.write( "]" );
1117                     out.write( separator );
1118                     if ( !( ForesterUtil.isEmpty( protein.getDescription() ) || protein.getDescription()
1119                             .equals( SurfacingConstants.NONE ) ) ) {
1120                         out.write( protein.getDescription() );
1121                     }
1122                     out.write( separator );
1123                     if ( !( ForesterUtil.isEmpty( protein.getAccession() ) || protein.getAccession()
1124                             .equals( SurfacingConstants.NONE ) ) ) {
1125                         out.write( protein.getAccession() );
1126                     }
1127                     out.write( SurfacingConstants.NL );
1128                 }
1129             }
1130         }
1131         out.flush();
1132     }
1133
1134     public static void extractProteinNames( final List<Protein> proteins,
1135                                             final List<DomainId> query_domain_ids_nc_order,
1136                                             final Writer out,
1137                                             final String separator,
1138                                             final String limit_to_species ) throws IOException {
1139         for( final Protein protein : proteins ) {
1140             if ( ForesterUtil.isEmpty( limit_to_species )
1141                     || protein.getSpecies().getSpeciesId().equalsIgnoreCase( limit_to_species ) ) {
1142                 if ( protein.contains( query_domain_ids_nc_order, true ) ) {
1143                     out.write( protein.getSpecies().getSpeciesId() );
1144                     out.write( separator );
1145                     out.write( protein.getProteinId().getId() );
1146                     out.write( separator );
1147                     out.write( "[" );
1148                     final Set<DomainId> visited_domain_ids = new HashSet<DomainId>();
1149                     boolean first = true;
1150                     for( final Domain domain : protein.getProteinDomains() ) {
1151                         if ( !visited_domain_ids.contains( domain.getDomainId() ) ) {
1152                             visited_domain_ids.add( domain.getDomainId() );
1153                             if ( first ) {
1154                                 first = false;
1155                             }
1156                             else {
1157                                 out.write( " " );
1158                             }
1159                             out.write( domain.getDomainId().getId() );
1160                             out.write( " {" );
1161                             out.write( "" + domain.getTotalCount() );
1162                             out.write( "}" );
1163                         }
1164                     }
1165                     out.write( "]" );
1166                     out.write( separator );
1167                     if ( !( ForesterUtil.isEmpty( protein.getDescription() ) || protein.getDescription()
1168                             .equals( SurfacingConstants.NONE ) ) ) {
1169                         out.write( protein.getDescription() );
1170                     }
1171                     out.write( separator );
1172                     if ( !( ForesterUtil.isEmpty( protein.getAccession() ) || protein.getAccession()
1173                             .equals( SurfacingConstants.NONE ) ) ) {
1174                         out.write( protein.getAccession() );
1175                     }
1176                     out.write( SurfacingConstants.NL );
1177                 }
1178             }
1179         }
1180         out.flush();
1181     }
1182
1183     public static void extractProteinNames( final SortedMap<Species, List<Protein>> protein_lists_per_species,
1184                                             final DomainId domain_id,
1185                                             final Writer out,
1186                                             final String separator,
1187                                             final String limit_to_species,
1188                                             final double domain_e_cutoff ) throws IOException {
1189         System.out.println( "Per domain E-value: " + domain_e_cutoff );
1190         for( final Species species : protein_lists_per_species.keySet() ) {
1191             System.out.println( species + ":" );
1192             for( final Protein protein : protein_lists_per_species.get( species ) ) {
1193                 if ( ForesterUtil.isEmpty( limit_to_species )
1194                         || protein.getSpecies().getSpeciesId().equalsIgnoreCase( limit_to_species ) ) {
1195                     final List<Domain> domains = protein.getProteinDomains( domain_id );
1196                     if ( domains.size() > 0 ) {
1197                         out.write( protein.getSpecies().getSpeciesId() );
1198                         out.write( separator );
1199                         out.write( protein.getProteinId().getId() );
1200                         out.write( separator );
1201                         out.write( domain_id.toString() );
1202                         out.write( separator );
1203                         int prev_to = -1;
1204                         for( final Domain domain : domains ) {
1205                             if ( ( domain_e_cutoff < 0 ) || ( domain.getPerDomainEvalue() <= domain_e_cutoff ) ) {
1206                                 out.write( "/" );
1207                                 out.write( domain.getFrom() + "-" + domain.getTo() );
1208                                 if ( prev_to >= 0 ) {
1209                                     final int l = domain.getFrom() - prev_to;
1210                                     System.out.println( l );
1211                                 }
1212                                 prev_to = domain.getTo();
1213                             }
1214                         }
1215                         out.write( "/" );
1216                         out.write( separator );
1217                         final List<Domain> domain_list = new ArrayList<Domain>();
1218                         for( final Domain domain : protein.getProteinDomains() ) {
1219                             if ( ( domain_e_cutoff < 0 ) || ( domain.getPerDomainEvalue() <= domain_e_cutoff ) ) {
1220                                 domain_list.add( domain );
1221                             }
1222                         }
1223                         final Domain domain_ary[] = new Domain[ domain_list.size() ];
1224                         for( int i = 0; i < domain_list.size(); ++i ) {
1225                             domain_ary[ i ] = domain_list.get( i );
1226                         }
1227                         Arrays.sort( domain_ary, new DomainComparator( true ) );
1228                         out.write( "{" );
1229                         boolean first = true;
1230                         for( final Domain domain : domain_ary ) {
1231                             if ( first ) {
1232                                 first = false;
1233                             }
1234                             else {
1235                                 out.write( "," );
1236                             }
1237                             out.write( domain.getDomainId().toString() );
1238                             out.write( ":" + domain.getFrom() + "-" + domain.getTo() );
1239                             out.write( ":" + domain.getPerDomainEvalue() );
1240                         }
1241                         out.write( "}" );
1242                         if ( !( ForesterUtil.isEmpty( protein.getDescription() ) || protein.getDescription()
1243                                 .equals( SurfacingConstants.NONE ) ) ) {
1244                             out.write( protein.getDescription() );
1245                         }
1246                         out.write( separator );
1247                         if ( !( ForesterUtil.isEmpty( protein.getAccession() ) || protein.getAccession()
1248                                 .equals( SurfacingConstants.NONE ) ) ) {
1249                             out.write( protein.getAccession() );
1250                         }
1251                         out.write( SurfacingConstants.NL );
1252                     }
1253                 }
1254             }
1255         }
1256         out.flush();
1257     }
1258
1259     public static SortedSet<DomainId> getAllDomainIds( final List<GenomeWideCombinableDomains> gwcd_list ) {
1260         final SortedSet<DomainId> all_domains_ids = new TreeSet<DomainId>();
1261         for( final GenomeWideCombinableDomains gwcd : gwcd_list ) {
1262             final Set<DomainId> all_domains = gwcd.getAllDomainIds();
1263             //    for( final Domain domain : all_domains ) {
1264             all_domains_ids.addAll( all_domains );
1265             //    }
1266         }
1267         return all_domains_ids;
1268     }
1269
1270     public static SortedMap<String, Integer> getDomainCounts( final List<Protein> protein_domain_collections ) {
1271         final SortedMap<String, Integer> map = new TreeMap<String, Integer>();
1272         for( final Protein protein_domain_collection : protein_domain_collections ) {
1273             for( final Object name : protein_domain_collection.getProteinDomains() ) {
1274                 final BasicDomain protein_domain = ( BasicDomain ) name;
1275                 final String id = protein_domain.getDomainId().getId();
1276                 if ( map.containsKey( id ) ) {
1277                     map.put( id, map.get( id ) + 1 );
1278                 }
1279                 else {
1280                     map.put( id, 1 );
1281                 }
1282             }
1283         }
1284         return map;
1285     }
1286
1287     public static int getNumberOfNodesLackingName( final Phylogeny p, final StringBuilder names ) {
1288         final PhylogenyNodeIterator it = p.iteratorPostorder();
1289         int c = 0;
1290         while ( it.hasNext() ) {
1291             final PhylogenyNode n = it.next();
1292             if ( ForesterUtil.isEmpty( n.getName() )
1293                     && ( !n.getNodeData().isHasTaxonomy() || ForesterUtil.isEmpty( n.getNodeData().getTaxonomy()
1294                             .getScientificName() ) )
1295                     && ( !n.getNodeData().isHasTaxonomy() || ForesterUtil.isEmpty( n.getNodeData().getTaxonomy()
1296                             .getCommonName() ) ) ) {
1297                 if ( n.getParent() != null ) {
1298                     names.append( " " );
1299                     names.append( n.getParent().getName() );
1300                 }
1301                 final List l = n.getAllExternalDescendants();
1302                 for( final Object object : l ) {
1303                     System.out.println( l.toString() );
1304                 }
1305                 ++c;
1306             }
1307         }
1308         return c;
1309     }
1310
1311     /**
1312      * Returns true is Domain domain falls in an uninterrupted stretch of
1313      * covered positions.
1314      * 
1315      * @param domain
1316      * @param covered_positions
1317      * @return
1318      */
1319     public static boolean isEngulfed( final Domain domain, final List<Boolean> covered_positions ) {
1320         for( int i = domain.getFrom(); i <= domain.getTo(); ++i ) {
1321             if ( ( i >= covered_positions.size() ) || ( covered_positions.get( i ) != true ) ) {
1322                 return false;
1323             }
1324         }
1325         return true;
1326     }
1327
1328     public static void preparePhylogeny( final Phylogeny p,
1329                                          final DomainParsimonyCalculator domain_parsimony,
1330                                          final String date_time,
1331                                          final String method,
1332                                          final String name,
1333                                          final String parameters_str ) {
1334         domain_parsimony.decoratePhylogenyWithDomains( p );
1335         final StringBuilder desc = new StringBuilder();
1336         desc.append( "[Method: " + method + "] [Date: " + date_time + "] " );
1337         desc.append( "[Cost: " + domain_parsimony.getCost() + "] " );
1338         desc.append( "[Gains: " + domain_parsimony.getTotalGains() + "] " );
1339         desc.append( "[Losses: " + domain_parsimony.getTotalLosses() + "] " );
1340         desc.append( "[Unchanged: " + domain_parsimony.getTotalUnchanged() + "] " );
1341         desc.append( "[Parameters: " + parameters_str + "]" );
1342         p.setName( name );
1343         p.setDescription( desc.toString() );
1344         p.setConfidence( new Confidence( domain_parsimony.getCost(), "parsimony" ) );
1345         p.setRerootable( false );
1346         p.setRooted( true );
1347     }
1348
1349     /*
1350      * species | protein id | n-terminal domain | c-terminal domain | n-terminal domain per domain E-value | c-terminal domain per domain E-value
1351      * 
1352      * 
1353      */
1354     static public StringBuffer proteinToDomainCombinations( final Protein protein,
1355                                                             final String protein_id,
1356                                                             final String separator ) {
1357         final StringBuffer sb = new StringBuffer();
1358         if ( protein.getSpecies() == null ) {
1359             throw new IllegalArgumentException( "species must not be null" );
1360         }
1361         if ( ForesterUtil.isEmpty( protein.getSpecies().getSpeciesId() ) ) {
1362             throw new IllegalArgumentException( "species id must not be empty" );
1363         }
1364         final List<Domain> domains = protein.getProteinDomains();
1365         if ( domains.size() > 1 ) {
1366             final Map<String, Integer> counts = new HashMap<String, Integer>();
1367             for( final Domain domain : domains ) {
1368                 final String id = domain.getDomainId().getId();
1369                 if ( counts.containsKey( id ) ) {
1370                     counts.put( id, counts.get( id ) + 1 );
1371                 }
1372                 else {
1373                     counts.put( id, 1 );
1374                 }
1375             }
1376             final Set<String> dcs = new HashSet<String>();
1377             for( int i = 1; i < domains.size(); ++i ) {
1378                 for( int j = 0; j < i; ++j ) {
1379                     Domain domain_n = domains.get( i );
1380                     Domain domain_c = domains.get( j );
1381                     if ( domain_n.getFrom() > domain_c.getFrom() ) {
1382                         domain_n = domains.get( j );
1383                         domain_c = domains.get( i );
1384                     }
1385                     final String dc = domain_n.getDomainId().getId() + domain_c.getDomainId().getId();
1386                     if ( !dcs.contains( dc ) ) {
1387                         dcs.add( dc );
1388                         sb.append( protein.getSpecies() );
1389                         sb.append( separator );
1390                         sb.append( protein_id );
1391                         sb.append( separator );
1392                         sb.append( domain_n.getDomainId().getId() );
1393                         sb.append( separator );
1394                         sb.append( domain_c.getDomainId().getId() );
1395                         sb.append( separator );
1396                         sb.append( domain_n.getPerDomainEvalue() );
1397                         sb.append( separator );
1398                         sb.append( domain_c.getPerDomainEvalue() );
1399                         sb.append( separator );
1400                         sb.append( counts.get( domain_n.getDomainId().getId() ) );
1401                         sb.append( separator );
1402                         sb.append( counts.get( domain_c.getDomainId().getId() ) );
1403                         sb.append( ForesterUtil.LINE_SEPARATOR );
1404                     }
1405                 }
1406             }
1407         }
1408         else if ( domains.size() == 1 ) {
1409             sb.append( protein.getSpecies() );
1410             sb.append( separator );
1411             sb.append( protein_id );
1412             sb.append( separator );
1413             sb.append( domains.get( 0 ).getDomainId().getId() );
1414             sb.append( separator );
1415             sb.append( separator );
1416             sb.append( domains.get( 0 ).getPerDomainEvalue() );
1417             sb.append( separator );
1418             sb.append( separator );
1419             sb.append( 1 );
1420             sb.append( separator );
1421             sb.append( ForesterUtil.LINE_SEPARATOR );
1422         }
1423         else {
1424             sb.append( protein.getSpecies() );
1425             sb.append( separator );
1426             sb.append( protein_id );
1427             sb.append( separator );
1428             sb.append( separator );
1429             sb.append( separator );
1430             sb.append( separator );
1431             sb.append( separator );
1432             sb.append( separator );
1433             sb.append( ForesterUtil.LINE_SEPARATOR );
1434         }
1435         return sb;
1436     }
1437
1438     /**
1439      * 
1440      * Example regarding engulfment: ------------0.1 ----------0.2 --0.3 =>
1441      * domain with 0.3 is ignored
1442      * 
1443      * -----------0.1 ----------0.2 --0.3 => domain with 0.3 is ignored
1444      * 
1445      * 
1446      * ------------0.1 ----------0.3 --0.2 => domains with 0.3 and 0.2 are _not_
1447      * ignored
1448      * 
1449      * @param max_allowed_overlap
1450      *            maximal allowed overlap (inclusive) to be still considered not
1451      *            overlapping (zero or negative value to allow any overlap)
1452      * @param remove_engulfed_domains
1453      *            to remove domains which are completely engulfed by coverage of
1454      *            domains with better support
1455      * @param protein
1456      * @return
1457      */
1458     public static Protein removeOverlappingDomains( final int max_allowed_overlap,
1459                                                     final boolean remove_engulfed_domains,
1460                                                     final Protein protein ) {
1461         final Protein pruned_protein = new BasicProtein( protein.getProteinId().getId(), protein.getSpecies()
1462                 .getSpeciesId(), protein.getLength() );
1463         final List<Domain> sorted = SurfacingUtil.sortDomainsWithAscendingConfidenceValues( protein );
1464         final List<Boolean> covered_positions = new ArrayList<Boolean>();
1465         for( final Domain domain : sorted ) {
1466             if ( ( ( max_allowed_overlap < 0 ) || ( SurfacingUtil.calculateOverlap( domain, covered_positions ) <= max_allowed_overlap ) )
1467                     && ( !remove_engulfed_domains || !isEngulfed( domain, covered_positions ) ) ) {
1468                 final int covered_positions_size = covered_positions.size();
1469                 for( int i = covered_positions_size; i < domain.getFrom(); ++i ) {
1470                     covered_positions.add( false );
1471                 }
1472                 final int new_covered_positions_size = covered_positions.size();
1473                 for( int i = domain.getFrom(); i <= domain.getTo(); ++i ) {
1474                     if ( i < new_covered_positions_size ) {
1475                         covered_positions.set( i, true );
1476                     }
1477                     else {
1478                         covered_positions.add( true );
1479                     }
1480                 }
1481                 pruned_protein.addProteinDomain( domain );
1482             }
1483         }
1484         return pruned_protein;
1485     }
1486
1487     public static List<Domain> sortDomainsWithAscendingConfidenceValues( final Protein protein ) {
1488         final List<Domain> domains = new ArrayList<Domain>();
1489         for( final Domain d : protein.getProteinDomains() ) {
1490             domains.add( d );
1491         }
1492         Collections.sort( domains, SurfacingUtil.ASCENDING_CONFIDENCE_VALUE_ORDER );
1493         return domains;
1494     }
1495
1496     private static List<String> splitDomainCombination( final String dc ) {
1497         final String[] s = dc.split( "=" );
1498         if ( s.length != 2 ) {
1499             ForesterUtil.printErrorMessage( surfacing.PRG_NAME, "Stringyfied domain combination has illegal format: "
1500                     + dc );
1501             System.exit( -1 );
1502         }
1503         final List<String> l = new ArrayList<String>( 2 );
1504         l.add( s[ 0 ] );
1505         l.add( s[ 1 ] );
1506         return l;
1507     }
1508
1509     public static void writeAllDomainsChangedOnAllSubtrees( final Phylogeny p,
1510                                                             final boolean get_gains,
1511                                                             final String outdir,
1512                                                             final String suffix_for_filename ) throws IOException {
1513         CharacterStateMatrix.GainLossStates state = CharacterStateMatrix.GainLossStates.GAIN;
1514         if ( !get_gains ) {
1515             state = CharacterStateMatrix.GainLossStates.LOSS;
1516         }
1517         final File base_dir = createBaseDirForPerNodeDomainFiles( surfacing.BASE_DIRECTORY_PER_SUBTREE_DOMAIN_GAIN_LOSS_FILES,
1518                                                                   false,
1519                                                                   state,
1520                                                                   outdir );
1521         for( final PhylogenyNodeIterator it = p.iteratorPostorder(); it.hasNext(); ) {
1522             final PhylogenyNode node = it.next();
1523             if ( !node.isExternal() ) {
1524                 final SortedSet<String> domains = collectAllDomainsChangedOnSubtree( node, get_gains );
1525                 if ( domains.size() > 0 ) {
1526                     final Writer writer = ForesterUtil.createBufferedWriter( base_dir + ForesterUtil.FILE_SEPARATOR
1527                             + node.getName() + suffix_for_filename );
1528                     for( final String domain : domains ) {
1529                         writer.write( domain );
1530                         writer.write( ForesterUtil.LINE_SEPARATOR );
1531                     }
1532                     writer.close();
1533                 }
1534             }
1535         }
1536     }
1537
1538     private static void writeAllEncounteredPfamsToFile( final Map<DomainId, List<GoId>> domain_id_to_go_ids_map,
1539                                                         final Map<GoId, GoTerm> go_id_to_term_map,
1540                                                         final String outfile_name,
1541                                                         final SortedSet<String> all_pfams_encountered ) {
1542         final File all_pfams_encountered_file = new File( outfile_name + surfacing.ALL_PFAMS_ENCOUNTERED_SUFFIX );
1543         final File all_pfams_encountered_with_go_annotation_file = new File( outfile_name
1544                 + surfacing.ALL_PFAMS_ENCOUNTERED_WITH_GO_ANNOTATION_SUFFIX );
1545         final File encountered_pfams_summary_file = new File( outfile_name + surfacing.ENCOUNTERED_PFAMS_SUMMARY_SUFFIX );
1546         int biological_process_counter = 0;
1547         int cellular_component_counter = 0;
1548         int molecular_function_counter = 0;
1549         int pfams_with_mappings_counter = 0;
1550         int pfams_without_mappings_counter = 0;
1551         int pfams_without_mappings_to_bp_or_mf_counter = 0;
1552         int pfams_with_mappings_to_bp_or_mf_counter = 0;
1553         try {
1554             final Writer all_pfams_encountered_writer = new BufferedWriter( new FileWriter( all_pfams_encountered_file ) );
1555             final Writer all_pfams_encountered_with_go_annotation_writer = new BufferedWriter( new FileWriter( all_pfams_encountered_with_go_annotation_file ) );
1556             final Writer summary_writer = new BufferedWriter( new FileWriter( encountered_pfams_summary_file ) );
1557             summary_writer.write( "# Pfam to GO mapping summary" );
1558             summary_writer.write( ForesterUtil.LINE_SEPARATOR );
1559             summary_writer.write( "# Actual summary is at the end of this file." );
1560             summary_writer.write( ForesterUtil.LINE_SEPARATOR );
1561             summary_writer.write( "# Encountered Pfams without a GO mapping:" );
1562             summary_writer.write( ForesterUtil.LINE_SEPARATOR );
1563             for( final String pfam : all_pfams_encountered ) {
1564                 all_pfams_encountered_writer.write( pfam );
1565                 all_pfams_encountered_writer.write( ForesterUtil.LINE_SEPARATOR );
1566                 final DomainId domain_id = new DomainId( pfam );
1567                 if ( domain_id_to_go_ids_map.containsKey( domain_id ) ) {
1568                     ++pfams_with_mappings_counter;
1569                     all_pfams_encountered_with_go_annotation_writer.write( pfam );
1570                     all_pfams_encountered_with_go_annotation_writer.write( ForesterUtil.LINE_SEPARATOR );
1571                     final List<GoId> go_ids = domain_id_to_go_ids_map.get( domain_id );
1572                     boolean maps_to_bp = false;
1573                     boolean maps_to_cc = false;
1574                     boolean maps_to_mf = false;
1575                     for( final GoId go_id : go_ids ) {
1576                         final GoTerm go_term = go_id_to_term_map.get( go_id );
1577                         if ( go_term.getGoNameSpace().isBiologicalProcess() ) {
1578                             maps_to_bp = true;
1579                         }
1580                         else if ( go_term.getGoNameSpace().isCellularComponent() ) {
1581                             maps_to_cc = true;
1582                         }
1583                         else if ( go_term.getGoNameSpace().isMolecularFunction() ) {
1584                             maps_to_mf = true;
1585                         }
1586                     }
1587                     if ( maps_to_bp ) {
1588                         ++biological_process_counter;
1589                     }
1590                     if ( maps_to_cc ) {
1591                         ++cellular_component_counter;
1592                     }
1593                     if ( maps_to_mf ) {
1594                         ++molecular_function_counter;
1595                     }
1596                     if ( maps_to_bp || maps_to_mf ) {
1597                         ++pfams_with_mappings_to_bp_or_mf_counter;
1598                     }
1599                     else {
1600                         ++pfams_without_mappings_to_bp_or_mf_counter;
1601                     }
1602                 }
1603                 else {
1604                     ++pfams_without_mappings_to_bp_or_mf_counter;
1605                     ++pfams_without_mappings_counter;
1606                     summary_writer.write( pfam );
1607                     summary_writer.write( ForesterUtil.LINE_SEPARATOR );
1608                 }
1609             }
1610             all_pfams_encountered_writer.close();
1611             all_pfams_encountered_with_go_annotation_writer.close();
1612             ForesterUtil.programMessage( surfacing.PRG_NAME, "Wrote all [" + all_pfams_encountered.size()
1613                     + "] encountered Pfams to: \"" + all_pfams_encountered_file + "\"" );
1614             ForesterUtil.programMessage( surfacing.PRG_NAME, "Wrote all [" + pfams_with_mappings_counter
1615                     + "] encountered Pfams with GO mappings to: \"" + all_pfams_encountered_with_go_annotation_file
1616                     + "\"" );
1617             ForesterUtil.programMessage( surfacing.PRG_NAME, "Wrote summary (including all ["
1618                     + pfams_without_mappings_counter + "] encountered Pfams without GO mappings) to: \""
1619                     + encountered_pfams_summary_file + "\"" );
1620             ForesterUtil.programMessage( surfacing.PRG_NAME, "Sum of Pfams encountered                : "
1621                     + all_pfams_encountered.size() );
1622             ForesterUtil.programMessage( surfacing.PRG_NAME, "Pfams without a mapping                 : "
1623                     + pfams_without_mappings_counter + " ["
1624                     + ( 100 * pfams_without_mappings_counter / all_pfams_encountered.size() ) + "%]" );
1625             ForesterUtil.programMessage( surfacing.PRG_NAME, "Pfams without mapping to proc. or func. : "
1626                     + pfams_without_mappings_to_bp_or_mf_counter + " ["
1627                     + ( 100 * pfams_without_mappings_to_bp_or_mf_counter / all_pfams_encountered.size() ) + "%]" );
1628             ForesterUtil.programMessage( surfacing.PRG_NAME,
1629                                          "Pfams with a mapping                    : " + pfams_with_mappings_counter
1630                                                  + " ["
1631                                                  + ( 100 * pfams_with_mappings_counter / all_pfams_encountered.size() )
1632                                                  + "%]" );
1633             ForesterUtil.programMessage( surfacing.PRG_NAME, "Pfams with a mapping to proc. or func.  : "
1634                     + pfams_with_mappings_to_bp_or_mf_counter + " ["
1635                     + ( 100 * pfams_with_mappings_to_bp_or_mf_counter / all_pfams_encountered.size() ) + "%]" );
1636             ForesterUtil.programMessage( surfacing.PRG_NAME,
1637                                          "Pfams with mapping to biological process: " + biological_process_counter
1638                                                  + " ["
1639                                                  + ( 100 * biological_process_counter / all_pfams_encountered.size() )
1640                                                  + "%]" );
1641             ForesterUtil.programMessage( surfacing.PRG_NAME,
1642                                          "Pfams with mapping to molecular function: " + molecular_function_counter
1643                                                  + " ["
1644                                                  + ( 100 * molecular_function_counter / all_pfams_encountered.size() )
1645                                                  + "%]" );
1646             ForesterUtil.programMessage( surfacing.PRG_NAME,
1647                                          "Pfams with mapping to cellular component: " + cellular_component_counter
1648                                                  + " ["
1649                                                  + ( 100 * cellular_component_counter / all_pfams_encountered.size() )
1650                                                  + "%]" );
1651             summary_writer.write( ForesterUtil.LINE_SEPARATOR );
1652             summary_writer.write( "# Sum of Pfams encountered                : " + all_pfams_encountered.size() );
1653             summary_writer.write( ForesterUtil.LINE_SEPARATOR );
1654             summary_writer.write( "# Pfams without a mapping                 : " + pfams_without_mappings_counter
1655                     + " [" + ( 100 * pfams_without_mappings_counter / all_pfams_encountered.size() ) + "%]" );
1656             summary_writer.write( ForesterUtil.LINE_SEPARATOR );
1657             summary_writer.write( "# Pfams without mapping to proc. or func. : "
1658                     + pfams_without_mappings_to_bp_or_mf_counter + " ["
1659                     + ( 100 * pfams_without_mappings_to_bp_or_mf_counter / all_pfams_encountered.size() ) + "%]" );
1660             summary_writer.write( ForesterUtil.LINE_SEPARATOR );
1661             summary_writer.write( "# Pfams with a mapping                    : " + pfams_with_mappings_counter + " ["
1662                     + ( 100 * pfams_with_mappings_counter / all_pfams_encountered.size() ) + "%]" );
1663             summary_writer.write( ForesterUtil.LINE_SEPARATOR );
1664             summary_writer.write( "# Pfams with a mapping to proc. or func.  : "
1665                     + pfams_with_mappings_to_bp_or_mf_counter + " ["
1666                     + ( 100 * pfams_with_mappings_to_bp_or_mf_counter / all_pfams_encountered.size() ) + "%]" );
1667             summary_writer.write( ForesterUtil.LINE_SEPARATOR );
1668             summary_writer.write( "# Pfams with mapping to biological process: " + biological_process_counter + " ["
1669                     + ( 100 * biological_process_counter / all_pfams_encountered.size() ) + "%]" );
1670             summary_writer.write( ForesterUtil.LINE_SEPARATOR );
1671             summary_writer.write( "# Pfams with mapping to molecular function: " + molecular_function_counter + " ["
1672                     + ( 100 * molecular_function_counter / all_pfams_encountered.size() ) + "%]" );
1673             summary_writer.write( ForesterUtil.LINE_SEPARATOR );
1674             summary_writer.write( "# Pfams with mapping to cellular component: " + cellular_component_counter + " ["
1675                     + ( 100 * cellular_component_counter / all_pfams_encountered.size() ) + "%]" );
1676             summary_writer.write( ForesterUtil.LINE_SEPARATOR );
1677             summary_writer.close();
1678         }
1679         catch ( final IOException e ) {
1680             ForesterUtil.printWarningMessage( surfacing.PRG_NAME, "Failure to write: " + e );
1681         }
1682     }
1683
1684     public static void writeBinaryDomainCombinationsFileForGraphAnalysis( final String[][] input_file_properties,
1685                                                                           final File output_dir,
1686                                                                           final GenomeWideCombinableDomains gwcd,
1687                                                                           final int i,
1688                                                                           final GenomeWideCombinableDomainsSortOrder dc_sort_order ) {
1689         File dc_outfile_dot = new File( input_file_properties[ i ][ 0 ]
1690                 + surfacing.DOMAIN_COMBINITONS_OUTPUTFILE_SUFFIX_FOR_GRAPH_ANALYSIS );
1691         if ( output_dir != null ) {
1692             dc_outfile_dot = new File( output_dir + ForesterUtil.FILE_SEPARATOR + dc_outfile_dot );
1693         }
1694         checkForOutputFileWriteability( dc_outfile_dot );
1695         final SortedSet<BinaryDomainCombination> binary_combinations = createSetOfAllBinaryDomainCombinationsPerGenome( gwcd );
1696         try {
1697             final BufferedWriter out_dot = new BufferedWriter( new FileWriter( dc_outfile_dot ) );
1698             for( final BinaryDomainCombination bdc : binary_combinations ) {
1699                 out_dot.write( bdc.toGraphDescribingLanguage( BinaryDomainCombination.OutputFormat.DOT, null, null )
1700                         .toString() );
1701                 out_dot.write( SurfacingConstants.NL );
1702             }
1703             out_dot.close();
1704         }
1705         catch ( final IOException e ) {
1706             ForesterUtil.fatalError( surfacing.PRG_NAME, e.getMessage() );
1707         }
1708         ForesterUtil.programMessage( surfacing.PRG_NAME, "Wrote binary domain combination for \""
1709                 + input_file_properties[ i ][ 0 ] + "\" (" + input_file_properties[ i ][ 1 ] + ", "
1710                 + input_file_properties[ i ][ 2 ] + ") to: \"" + dc_outfile_dot + "\"" );
1711     }
1712
1713     public static void writeBinaryStatesMatrixAsListToFile( final CharacterStateMatrix<CharacterStateMatrix.GainLossStates> matrix,
1714                                                             final CharacterStateMatrix.GainLossStates state,
1715                                                             final String filename,
1716                                                             final String indentifier_characters_separator,
1717                                                             final String character_separator,
1718                                                             final Map<String, String> descriptions ) {
1719         final File outfile = new File( filename );
1720         checkForOutputFileWriteability( outfile );
1721         final SortedSet<String> sorted_ids = new TreeSet<String>();
1722         for( int i = 0; i < matrix.getNumberOfIdentifiers(); ++i ) {
1723             sorted_ids.add( matrix.getIdentifier( i ) );
1724         }
1725         try {
1726             final BufferedWriter out = new BufferedWriter( new FileWriter( outfile ) );
1727             for( final String id : sorted_ids ) {
1728                 out.write( indentifier_characters_separator );
1729                 out.write( "#" + id );
1730                 out.write( indentifier_characters_separator );
1731                 for( int c = 0; c < matrix.getNumberOfCharacters(); ++c ) {
1732                     // Not nice:
1733                     // using null to indicate either UNCHANGED_PRESENT or GAIN.
1734                     if ( ( matrix.getState( id, c ) == state )
1735                             || ( ( state == null ) && ( ( matrix.getState( id, c ) == CharacterStateMatrix.GainLossStates.GAIN ) || ( matrix
1736                                     .getState( id, c ) == CharacterStateMatrix.GainLossStates.UNCHANGED_PRESENT ) ) ) ) {
1737                         out.write( matrix.getCharacter( c ) );
1738                         if ( ( descriptions != null ) && !descriptions.isEmpty()
1739                                 && descriptions.containsKey( matrix.getCharacter( c ) ) ) {
1740                             out.write( "\t" );
1741                             out.write( descriptions.get( matrix.getCharacter( c ) ) );
1742                         }
1743                         out.write( character_separator );
1744                     }
1745                 }
1746             }
1747             out.flush();
1748             out.close();
1749         }
1750         catch ( final IOException e ) {
1751             ForesterUtil.fatalError( surfacing.PRG_NAME, e.getMessage() );
1752         }
1753         ForesterUtil.programMessage( surfacing.PRG_NAME, "Wrote characters list: \"" + filename + "\"" );
1754     }
1755
1756     public static void writeBinaryStatesMatrixAsListToFileForBinaryCombinationsForGraphAnalysis( final CharacterStateMatrix<CharacterStateMatrix.GainLossStates> matrix,
1757                                                                                                  final CharacterStateMatrix.GainLossStates state,
1758                                                                                                  final String filename,
1759                                                                                                  final String indentifier_characters_separator,
1760                                                                                                  final String character_separator,
1761                                                                                                  final BinaryDomainCombination.OutputFormat bc_output_format ) {
1762         final File outfile = new File( filename );
1763         checkForOutputFileWriteability( outfile );
1764         final SortedSet<String> sorted_ids = new TreeSet<String>();
1765         for( int i = 0; i < matrix.getNumberOfIdentifiers(); ++i ) {
1766             sorted_ids.add( matrix.getIdentifier( i ) );
1767         }
1768         try {
1769             final BufferedWriter out = new BufferedWriter( new FileWriter( outfile ) );
1770             for( final String id : sorted_ids ) {
1771                 out.write( indentifier_characters_separator );
1772                 out.write( "#" + id );
1773                 out.write( indentifier_characters_separator );
1774                 for( int c = 0; c < matrix.getNumberOfCharacters(); ++c ) {
1775                     // Not nice:
1776                     // using null to indicate either UNCHANGED_PRESENT or GAIN.
1777                     if ( ( matrix.getState( id, c ) == state )
1778                             || ( ( state == null ) && ( ( matrix.getState( id, c ) == CharacterStateMatrix.GainLossStates.GAIN ) || ( matrix
1779                                     .getState( id, c ) == CharacterStateMatrix.GainLossStates.UNCHANGED_PRESENT ) ) ) ) {
1780                         BinaryDomainCombination bdc = null;
1781                         try {
1782                             bdc = BasicBinaryDomainCombination.createInstance( matrix.getCharacter( c ) );
1783                         }
1784                         catch ( final Exception e ) {
1785                             ForesterUtil.fatalError( surfacing.PRG_NAME, e.getLocalizedMessage() );
1786                         }
1787                         out.write( bdc.toGraphDescribingLanguage( bc_output_format, null, null ).toString() );
1788                         out.write( character_separator );
1789                     }
1790                 }
1791             }
1792             out.flush();
1793             out.close();
1794         }
1795         catch ( final IOException e ) {
1796             ForesterUtil.fatalError( surfacing.PRG_NAME, e.getMessage() );
1797         }
1798         ForesterUtil.programMessage( surfacing.PRG_NAME, "Wrote characters list: \"" + filename + "\"" );
1799     }
1800
1801     public static void writeBinaryStatesMatrixToList( final Map<DomainId, List<GoId>> domain_id_to_go_ids_map,
1802                                                       final Map<GoId, GoTerm> go_id_to_term_map,
1803                                                       final GoNameSpace go_namespace_limit,
1804                                                       final boolean domain_combinations,
1805                                                       final CharacterStateMatrix<CharacterStateMatrix.GainLossStates> matrix,
1806                                                       final CharacterStateMatrix.GainLossStates state,
1807                                                       final String filename,
1808                                                       final String indentifier_characters_separator,
1809                                                       final String character_separator,
1810                                                       final String title_for_html,
1811                                                       final String prefix_for_html,
1812                                                       final Map<DomainId, Set<String>>[] domain_id_to_secondary_features_maps,
1813                                                       final SortedSet<String> all_pfams_encountered,
1814                                                       final SortedSet<String> pfams_gained_or_lost,
1815                                                       final String suffix_for_per_node_events_file ) {
1816         if ( ( go_namespace_limit != null ) && ( ( go_id_to_term_map == null ) || ( go_id_to_term_map.size() < 1 ) ) ) {
1817             throw new IllegalArgumentException( "attempt to use GO namespace limit without a GO-id to term map" );
1818         }
1819         else if ( ( ( domain_id_to_go_ids_map == null ) || ( domain_id_to_go_ids_map.size() < 1 ) ) ) {
1820             throw new IllegalArgumentException( "attempt to output detailed HTML without a Pfam to GO map" );
1821         }
1822         else if ( ( ( go_id_to_term_map == null ) || ( go_id_to_term_map.size() < 1 ) ) ) {
1823             throw new IllegalArgumentException( "attempt to output detailed HTML without a GO-id to term map" );
1824         }
1825         final File outfile = new File( filename );
1826         checkForOutputFileWriteability( outfile );
1827         final SortedSet<String> sorted_ids = new TreeSet<String>();
1828         for( int i = 0; i < matrix.getNumberOfIdentifiers(); ++i ) {
1829             sorted_ids.add( matrix.getIdentifier( i ) );
1830         }
1831         try {
1832             final Writer out = new BufferedWriter( new FileWriter( outfile ) );
1833             final File per_node_go_mapped_domain_gain_loss_files_base_dir = createBaseDirForPerNodeDomainFiles( surfacing.BASE_DIRECTORY_PER_NODE_DOMAIN_GAIN_LOSS_FILES,
1834                                                                                                                 domain_combinations,
1835                                                                                                                 state,
1836                                                                                                                 filename );
1837             Writer per_node_go_mapped_domain_gain_loss_outfile_writer = null;
1838             File per_node_go_mapped_domain_gain_loss_outfile = null;
1839             int per_node_counter = 0;
1840             out.write( "<html>" );
1841             out.write( SurfacingConstants.NL );
1842             addHtmlHead( out, title_for_html );
1843             out.write( SurfacingConstants.NL );
1844             out.write( "<body>" );
1845             out.write( SurfacingConstants.NL );
1846             out.write( "<h1>" );
1847             out.write( SurfacingConstants.NL );
1848             out.write( title_for_html );
1849             out.write( SurfacingConstants.NL );
1850             out.write( "</h1>" );
1851             out.write( SurfacingConstants.NL );
1852             out.write( "<table>" );
1853             out.write( SurfacingConstants.NL );
1854             for( final String id : sorted_ids ) {
1855                 final Matcher matcher = PATTERN_SP_STYLE_TAXONOMY.matcher( id );
1856                 if ( matcher.matches() ) {
1857                     continue;
1858                 }
1859                 out.write( "<tr>" );
1860                 out.write( "<td>" );
1861                 out.write( "<a href=\"#" + id + "\">" + id + "</a>" );
1862                 out.write( "</td>" );
1863                 out.write( "</tr>" );
1864                 out.write( SurfacingConstants.NL );
1865             }
1866             out.write( "</table>" );
1867             out.write( SurfacingConstants.NL );
1868             for( final String id : sorted_ids ) {
1869                 final Matcher matcher = PATTERN_SP_STYLE_TAXONOMY.matcher( id );
1870                 if ( matcher.matches() ) {
1871                     continue;
1872                 }
1873                 out.write( SurfacingConstants.NL );
1874                 out.write( "<h2>" );
1875                 out.write( "<a name=\"" + id + "\">" + id + "</a>" );
1876                 writeTaxonomyLinks( out, id );
1877                 out.write( "</h2>" );
1878                 out.write( SurfacingConstants.NL );
1879                 out.write( "<table>" );
1880                 out.write( SurfacingConstants.NL );
1881                 out.write( "<tr>" );
1882                 out.write( "<td><b>" );
1883                 out.write( "Pfam domain(s)" );
1884                 out.write( "</b></td><td><b>" );
1885                 out.write( "GO term acc" );
1886                 out.write( "</b></td><td><b>" );
1887                 out.write( "GO term" );
1888                 out.write( "</b></td><td><b>" );
1889                 out.write( "GO namespace" );
1890                 out.write( "</b></td>" );
1891                 out.write( "</tr>" );
1892                 out.write( SurfacingConstants.NL );
1893                 out.write( "</tr>" );
1894                 out.write( SurfacingConstants.NL );
1895                 per_node_counter = 0;
1896                 if ( matrix.getNumberOfCharacters() > 0 ) {
1897                     per_node_go_mapped_domain_gain_loss_outfile = new File( per_node_go_mapped_domain_gain_loss_files_base_dir
1898                             + ForesterUtil.FILE_SEPARATOR + id + suffix_for_per_node_events_file );
1899                     SurfacingUtil.checkForOutputFileWriteability( per_node_go_mapped_domain_gain_loss_outfile );
1900                     per_node_go_mapped_domain_gain_loss_outfile_writer = ForesterUtil
1901                             .createBufferedWriter( per_node_go_mapped_domain_gain_loss_outfile );
1902                 }
1903                 else {
1904                     per_node_go_mapped_domain_gain_loss_outfile = null;
1905                     per_node_go_mapped_domain_gain_loss_outfile_writer = null;
1906                 }
1907                 for( int c = 0; c < matrix.getNumberOfCharacters(); ++c ) {
1908                     // Not nice:
1909                     // using null to indicate either UNCHANGED_PRESENT or GAIN.
1910                     if ( ( matrix.getState( id, c ) == state )
1911                             || ( ( state == null ) && ( ( matrix.getState( id, c ) == CharacterStateMatrix.GainLossStates.UNCHANGED_PRESENT ) || ( matrix
1912                                     .getState( id, c ) == CharacterStateMatrix.GainLossStates.GAIN ) ) ) ) {
1913                         final String character = matrix.getCharacter( c );
1914                         String domain_0 = "";
1915                         String domain_1 = "";
1916                         if ( character.indexOf( BinaryDomainCombination.SEPARATOR ) > 0 ) {
1917                             final String[] s = character.split( BinaryDomainCombination.SEPARATOR );
1918                             if ( s.length != 2 ) {
1919                                 throw new AssertionError( "this should not have happened: unexpected format for domain combination: ["
1920                                         + character + "]" );
1921                             }
1922                             domain_0 = s[ 0 ];
1923                             domain_1 = s[ 1 ];
1924                         }
1925                         else {
1926                             domain_0 = character;
1927                         }
1928                         writeDomainData( domain_id_to_go_ids_map,
1929                                          go_id_to_term_map,
1930                                          go_namespace_limit,
1931                                          out,
1932                                          domain_0,
1933                                          domain_1,
1934                                          prefix_for_html,
1935                                          character_separator,
1936                                          domain_id_to_secondary_features_maps,
1937                                          null );
1938                         all_pfams_encountered.add( domain_0 );
1939                         if ( pfams_gained_or_lost != null ) {
1940                             pfams_gained_or_lost.add( domain_0 );
1941                         }
1942                         if ( !ForesterUtil.isEmpty( domain_1 ) ) {
1943                             all_pfams_encountered.add( domain_1 );
1944                             if ( pfams_gained_or_lost != null ) {
1945                                 pfams_gained_or_lost.add( domain_1 );
1946                             }
1947                         }
1948                         if ( per_node_go_mapped_domain_gain_loss_outfile_writer != null ) {
1949                             writeDomainsToIndividualFilePerTreeNode( per_node_go_mapped_domain_gain_loss_outfile_writer,
1950                                                                      domain_0,
1951                                                                      domain_1 );
1952                             per_node_counter++;
1953                         }
1954                     }
1955                 }
1956                 if ( per_node_go_mapped_domain_gain_loss_outfile_writer != null ) {
1957                     per_node_go_mapped_domain_gain_loss_outfile_writer.close();
1958                     if ( per_node_counter < 1 ) {
1959                         per_node_go_mapped_domain_gain_loss_outfile.delete();
1960                     }
1961                     per_node_counter = 0;
1962                 }
1963                 out.write( "</table>" );
1964                 out.write( SurfacingConstants.NL );
1965                 out.write( "<hr>" );
1966                 out.write( SurfacingConstants.NL );
1967             } // for( final String id : sorted_ids ) {  
1968             out.write( "</body>" );
1969             out.write( SurfacingConstants.NL );
1970             out.write( "</html>" );
1971             out.write( SurfacingConstants.NL );
1972             out.flush();
1973             out.close();
1974         }
1975         catch ( final IOException e ) {
1976             ForesterUtil.fatalError( surfacing.PRG_NAME, e.getMessage() );
1977         }
1978         ForesterUtil.programMessage( surfacing.PRG_NAME, "Wrote characters detailed HTML list: \"" + filename + "\"" );
1979     }
1980
1981     public static void writeDomainCombinationsCountsFile( final String[][] input_file_properties,
1982                                                           final File output_dir,
1983                                                           final Writer per_genome_domain_promiscuity_statistics_writer,
1984                                                           final GenomeWideCombinableDomains gwcd,
1985                                                           final int i,
1986                                                           final GenomeWideCombinableDomains.GenomeWideCombinableDomainsSortOrder dc_sort_order ) {
1987         File dc_outfile = new File( input_file_properties[ i ][ 0 ]
1988                 + surfacing.DOMAIN_COMBINITON_COUNTS_OUTPUTFILE_SUFFIX );
1989         if ( output_dir != null ) {
1990             dc_outfile = new File( output_dir + ForesterUtil.FILE_SEPARATOR + dc_outfile );
1991         }
1992         checkForOutputFileWriteability( dc_outfile );
1993         try {
1994             final BufferedWriter out = new BufferedWriter( new FileWriter( dc_outfile ) );
1995             out.write( gwcd.toStringBuilder( dc_sort_order ).toString() );
1996             out.close();
1997         }
1998         catch ( final IOException e ) {
1999             ForesterUtil.fatalError( surfacing.PRG_NAME, e.getMessage() );
2000         }
2001         final DescriptiveStatistics stats = gwcd.getPerGenomeDomainPromiscuityStatistics();
2002         try {
2003             per_genome_domain_promiscuity_statistics_writer.write( input_file_properties[ i ][ 0 ] + "\t" );
2004             per_genome_domain_promiscuity_statistics_writer.write( FORMATTER_3.format( stats.arithmeticMean() ) + "\t" );
2005             if ( stats.getN() < 2 ) {
2006                 per_genome_domain_promiscuity_statistics_writer.write( "n/a" + "\t" );
2007             }
2008             else {
2009                 per_genome_domain_promiscuity_statistics_writer.write( FORMATTER_3.format( stats
2010                         .sampleStandardDeviation() ) + "\t" );
2011             }
2012             per_genome_domain_promiscuity_statistics_writer.write( FORMATTER_3.format( stats.median() ) + "\t" );
2013             per_genome_domain_promiscuity_statistics_writer.write( ( int ) stats.getMin() + "\t" );
2014             per_genome_domain_promiscuity_statistics_writer.write( ( int ) stats.getMax() + "\t" );
2015             per_genome_domain_promiscuity_statistics_writer.write( stats.getN() + "\t" );
2016             final SortedSet<DomainId> mpds = gwcd.getMostPromiscuosDomain();
2017             for( final DomainId mpd : mpds ) {
2018                 per_genome_domain_promiscuity_statistics_writer.write( mpd.getId() + " " );
2019             }
2020             per_genome_domain_promiscuity_statistics_writer.write( ForesterUtil.LINE_SEPARATOR );
2021         }
2022         catch ( final IOException e ) {
2023             ForesterUtil.fatalError( surfacing.PRG_NAME, e.getMessage() );
2024         }
2025         if ( input_file_properties[ i ].length == 3 ) {
2026             ForesterUtil.programMessage( surfacing.PRG_NAME, "Wrote domain combination counts for \""
2027                     + input_file_properties[ i ][ 0 ] + "\" (" + input_file_properties[ i ][ 1 ] + ", "
2028                     + input_file_properties[ i ][ 2 ] + ") to: \"" + dc_outfile + "\"" );
2029         }
2030         else {
2031             ForesterUtil.programMessage( surfacing.PRG_NAME, "Wrote domain combination counts for \""
2032                     + input_file_properties[ i ][ 0 ] + "\" (" + input_file_properties[ i ][ 1 ] + ") to: \""
2033                     + dc_outfile + "\"" );
2034         }
2035     }
2036
2037     private static void writeDomainData( final Map<DomainId, List<GoId>> domain_id_to_go_ids_map,
2038                                          final Map<GoId, GoTerm> go_id_to_term_map,
2039                                          final GoNameSpace go_namespace_limit,
2040                                          final Writer out,
2041                                          final String domain_0,
2042                                          final String domain_1,
2043                                          final String prefix_for_html,
2044                                          final String character_separator_for_non_html_output,
2045                                          final Map<DomainId, Set<String>>[] domain_id_to_secondary_features_maps,
2046                                          final Set<GoId> all_go_ids ) throws IOException {
2047         boolean any_go_annotation_present = false;
2048         boolean first_has_no_go = false;
2049         int domain_count = 2; // To distinguish between domains and binary domain combinations.
2050         if ( ForesterUtil.isEmpty( domain_1 ) ) {
2051             domain_count = 1;
2052         }
2053         // The following has a difficult to understand logic.  
2054         for( int d = 0; d < domain_count; ++d ) {
2055             List<GoId> go_ids = null;
2056             boolean go_annotation_present = false;
2057             if ( d == 0 ) {
2058                 final DomainId domain_id = new DomainId( domain_0 );
2059                 if ( domain_id_to_go_ids_map.containsKey( domain_id ) ) {
2060                     go_annotation_present = true;
2061                     any_go_annotation_present = true;
2062                     go_ids = domain_id_to_go_ids_map.get( domain_id );
2063                 }
2064                 else {
2065                     first_has_no_go = true;
2066                 }
2067             }
2068             else {
2069                 final DomainId domain_id = new DomainId( domain_1 );
2070                 if ( domain_id_to_go_ids_map.containsKey( domain_id ) ) {
2071                     go_annotation_present = true;
2072                     any_go_annotation_present = true;
2073                     go_ids = domain_id_to_go_ids_map.get( domain_id );
2074                 }
2075             }
2076             if ( go_annotation_present ) {
2077                 boolean first = ( ( d == 0 ) || ( ( d == 1 ) && first_has_no_go ) );
2078                 for( final GoId go_id : go_ids ) {
2079                     out.write( "<tr>" );
2080                     if ( first ) {
2081                         first = false;
2082                         writeDomainIdsToHtml( out,
2083                                               domain_0,
2084                                               domain_1,
2085                                               prefix_for_html,
2086                                               domain_id_to_secondary_features_maps );
2087                     }
2088                     else {
2089                         out.write( "<td></td>" );
2090                     }
2091                     if ( !go_id_to_term_map.containsKey( go_id ) ) {
2092                         throw new IllegalArgumentException( "GO-id [" + go_id + "] not found in GO-id to GO-term map" );
2093                     }
2094                     final GoTerm go_term = go_id_to_term_map.get( go_id );
2095                     if ( ( go_namespace_limit == null ) || go_namespace_limit.equals( go_term.getGoNameSpace() ) ) {
2096                         // final String top = GoUtils.getPenultimateGoTerm( go_term, go_id_to_term_map ).getName();
2097                         final String go_id_str = go_id.getId();
2098                         out.write( "<td>" );
2099                         out.write( "<a href=\"" + SurfacingConstants.AMIGO_LINK + go_id_str
2100                                 + "\" target=\"amigo_window\">" + go_id_str + "</a>" );
2101                         out.write( "</td><td>" );
2102                         out.write( go_term.getName() );
2103                         if ( domain_count == 2 ) {
2104                             out.write( " (" + d + ")" );
2105                         }
2106                         out.write( "</td><td>" );
2107                         // out.write( top );
2108                         // out.write( "</td><td>" );
2109                         out.write( "[" );
2110                         out.write( go_term.getGoNameSpace().toShortString() );
2111                         out.write( "]" );
2112                         out.write( "</td>" );
2113                         if ( all_go_ids != null ) {
2114                             all_go_ids.add( go_id );
2115                         }
2116                     }
2117                     else {
2118                         out.write( "<td>" );
2119                         out.write( "</td><td>" );
2120                         out.write( "</td><td>" );
2121                         out.write( "</td><td>" );
2122                         out.write( "</td>" );
2123                     }
2124                     out.write( "</tr>" );
2125                     out.write( SurfacingConstants.NL );
2126                 }
2127             }
2128         } //  for( int d = 0; d < domain_count; ++d ) 
2129         if ( !any_go_annotation_present ) {
2130             out.write( "<tr>" );
2131             writeDomainIdsToHtml( out, domain_0, domain_1, prefix_for_html, domain_id_to_secondary_features_maps );
2132             out.write( "<td>" );
2133             out.write( "</td><td>" );
2134             out.write( "</td><td>" );
2135             out.write( "</td><td>" );
2136             out.write( "</td>" );
2137             out.write( "</tr>" );
2138             out.write( SurfacingConstants.NL );
2139         }
2140     }
2141
2142     private static void writeDomainIdsToHtml( final Writer out,
2143                                               final String domain_0,
2144                                               final String domain_1,
2145                                               final String prefix_for_detailed_html,
2146                                               final Map<DomainId, Set<String>>[] domain_id_to_secondary_features_maps )
2147             throws IOException {
2148         out.write( "<td>" );
2149         if ( !ForesterUtil.isEmpty( prefix_for_detailed_html ) ) {
2150             out.write( prefix_for_detailed_html );
2151             out.write( " " );
2152         }
2153         out.write( "<a href=\"" + SurfacingConstants.PFAM_FAMILY_ID_LINK + domain_0 + "\">" + domain_0 + "</a>" );
2154         out.write( "</td>" );
2155     }
2156
2157     public static DescriptiveStatistics writeDomainSimilaritiesToFile( final StringBuilder html_desc,
2158                                                                        final StringBuilder html_title,
2159                                                                        final Writer single_writer,
2160                                                                        Map<Character, Writer> split_writers,
2161                                                                        final SortedSet<DomainSimilarity> similarities,
2162                                                                        final boolean treat_as_binary,
2163                                                                        final List<Species> species_order,
2164                                                                        final PrintableDomainSimilarity.PRINT_OPTION print_option,
2165                                                                        final DomainSimilarity.DomainSimilaritySortField sort_field,
2166                                                                        final DomainSimilarity.DomainSimilarityScoring scoring,
2167                                                                        final boolean verbose ) throws IOException {
2168         final DescriptiveStatistics stats = new BasicDescriptiveStatistics();
2169         String histogram_title = null;
2170         switch ( sort_field ) {
2171             case ABS_MAX_COUNTS_DIFFERENCE:
2172                 if ( treat_as_binary ) {
2173                     histogram_title = "absolute counts difference:";
2174                 }
2175                 else {
2176                     histogram_title = "absolute (maximal) counts difference:";
2177                 }
2178                 break;
2179             case MAX_COUNTS_DIFFERENCE:
2180                 if ( treat_as_binary ) {
2181                     histogram_title = "counts difference:";
2182                 }
2183                 else {
2184                     histogram_title = "(maximal) counts difference:";
2185                 }
2186                 break;
2187             case DOMAIN_ID:
2188                 histogram_title = "score mean:";
2189                 break;
2190             case MIN:
2191                 histogram_title = "score minimum:";
2192                 break;
2193             case MAX:
2194                 histogram_title = "score maximum:";
2195                 break;
2196             case MAX_DIFFERENCE:
2197                 if ( treat_as_binary ) {
2198                     histogram_title = "difference:";
2199                 }
2200                 else {
2201                     histogram_title = "(maximal) difference:";
2202                 }
2203                 break;
2204             case MEAN:
2205                 histogram_title = "score mean:";
2206                 break;
2207             case SD:
2208                 histogram_title = "score standard deviation:";
2209                 break;
2210             case SPECIES_COUNT:
2211                 histogram_title = "species number:";
2212                 break;
2213             default:
2214                 throw new AssertionError( "Unknown sort field: " + sort_field );
2215         }
2216         for( final DomainSimilarity similarity : similarities ) {
2217             switch ( sort_field ) {
2218                 case ABS_MAX_COUNTS_DIFFERENCE:
2219                     stats.addValue( Math.abs( similarity.getMaximalDifferenceInCounts() ) );
2220                     break;
2221                 case MAX_COUNTS_DIFFERENCE:
2222                     stats.addValue( similarity.getMaximalDifferenceInCounts() );
2223                     break;
2224                 case DOMAIN_ID:
2225                     stats.addValue( similarity.getMeanSimilarityScore() );
2226                     break;
2227                 case MIN:
2228                     stats.addValue( similarity.getMinimalSimilarityScore() );
2229                     break;
2230                 case MAX:
2231                     stats.addValue( similarity.getMaximalSimilarityScore() );
2232                     break;
2233                 case MAX_DIFFERENCE:
2234                     stats.addValue( similarity.getMaximalDifference() );
2235                     break;
2236                 case MEAN:
2237                     stats.addValue( similarity.getMeanSimilarityScore() );
2238                     break;
2239                 case SD:
2240                     stats.addValue( similarity.getStandardDeviationOfSimilarityScore() );
2241                     break;
2242                 case SPECIES_COUNT:
2243                     stats.addValue( similarity.getSpecies().size() );
2244                     break;
2245                 default:
2246                     throw new AssertionError( "Unknown sort field: " + sort_field );
2247             }
2248         }
2249         //
2250         // final HistogramData[] hists = new HistogramData[ 1 ];
2251         //      
2252         //        
2253         // List<HistogramDataItem> data_items = new
2254         // ArrayList<HistogramDataItem>();
2255         // double[] values = stats.getDataAsDoubleArray();
2256         // for( int i = 0; i < values.length; i++ ) {
2257         // HistogramDataItem data_item = new BasicHistogramDataItem( "", values[
2258         // i ] );
2259         // data_items.add( data_item );
2260         // }
2261         //        
2262         //        
2263         // HistogramData hd0 = new HistogramData( "name",
2264         // data_items,
2265         // null, 20,
2266         // 40 );
2267         //        
2268         //        
2269         //        
2270         //        
2271         // hists[ 0 ] = hd0;
2272         //       
2273         // final HistogramsFrame hf = new HistogramsFrame( hists );
2274         // hf.setVisible( true );
2275         //
2276         AsciiHistogram histo = null;
2277         if ( stats.getMin() < stats.getMin() ) {
2278             histo = new AsciiHistogram( stats, histogram_title );
2279         }
2280         if ( verbose ) {
2281             if ( histo != null ) {
2282                 System.out.println( histo.toStringBuffer( 20, '|', 40, 5 ) );
2283             }
2284             System.out.println();
2285             System.out.println( "N                   : " + stats.getN() );
2286             System.out.println( "Min                 : " + stats.getMin() );
2287             System.out.println( "Max                 : " + stats.getMax() );
2288             System.out.println( "Mean                : " + stats.arithmeticMean() );
2289             if ( stats.getN() > 1 ) {
2290                 System.out.println( "SD                  : " + stats.sampleStandardDeviation() );
2291             }
2292             else {
2293                 System.out.println( "SD                  : n/a" );
2294             }
2295             System.out.println( "Median              : " + stats.median() );
2296             if ( stats.getN() > 1 ) {
2297                 System.out.println( "Pearsonian skewness : " + stats.pearsonianSkewness() );
2298             }
2299             else {
2300                 System.out.println( "Pearsonian skewness : n/a" );
2301             }
2302         }
2303         if ( ( single_writer != null ) && ( ( split_writers == null ) || split_writers.isEmpty() ) ) {
2304             split_writers = new HashMap<Character, Writer>();
2305             split_writers.put( '_', single_writer );
2306         }
2307         switch ( print_option ) {
2308             case SIMPLE_TAB_DELIMITED:
2309                 break;
2310             case HTML:
2311                 for( final Character key : split_writers.keySet() ) {
2312                     final Writer w = split_writers.get( key );
2313                     w.write( "<html>" );
2314                     w.write( SurfacingConstants.NL );
2315                     if ( key != '_' ) {
2316                         addHtmlHead( w, "DCs (" + html_title + ") " + key.toString().toUpperCase() );
2317                     }
2318                     else {
2319                         addHtmlHead( w, "DCs (" + html_title + ")" );
2320                     }
2321                     w.write( SurfacingConstants.NL );
2322                     w.write( "<body>" );
2323                     w.write( SurfacingConstants.NL );
2324                     w.write( html_desc.toString() );
2325                     w.write( SurfacingConstants.NL );
2326                     w.write( "<hr>" );
2327                     w.write( "<br>" );
2328                     w.write( SurfacingConstants.NL );
2329                     w.write( "<tt><pre>" );
2330                     w.write( SurfacingConstants.NL );
2331                     if ( histo != null ) {
2332                         w.write( histo.toStringBuffer( 20, '|', 40, 5 ).toString() );
2333                         w.write( SurfacingConstants.NL );
2334                     }
2335                     w.write( "</pre></tt>" );
2336                     w.write( SurfacingConstants.NL );
2337                     w.write( "<table>" );
2338                     w.write( SurfacingConstants.NL );
2339                     w.write( "<tr><td>N: </td><td>" + stats.getN() + "</td></tr>" );
2340                     w.write( SurfacingConstants.NL );
2341                     w.write( "<tr><td>Min: </td><td>" + stats.getMin() + "</td></tr>" );
2342                     w.write( SurfacingConstants.NL );
2343                     w.write( "<tr><td>Max: </td><td>" + stats.getMax() + "</td></tr>" );
2344                     w.write( SurfacingConstants.NL );
2345                     w.write( "<tr><td>Mean: </td><td>" + stats.arithmeticMean() + "</td></tr>" );
2346                     w.write( SurfacingConstants.NL );
2347                     if ( stats.getN() > 1 ) {
2348                         w.write( "<tr><td>SD: </td><td>" + stats.sampleStandardDeviation() + "</td></tr>" );
2349                     }
2350                     else {
2351                         w.write( "<tr><td>SD: </td><td>n/a</td></tr>" );
2352                     }
2353                     w.write( SurfacingConstants.NL );
2354                     w.write( "<tr><td>Median: </td><td>" + stats.median() + "</td></tr>" );
2355                     w.write( SurfacingConstants.NL );
2356                     if ( stats.getN() > 1 ) {
2357                         w.write( "<tr><td>Pearsonian skewness: </td><td>" + stats.pearsonianSkewness() + "</td></tr>" );
2358                     }
2359                     else {
2360                         w.write( "<tr><td>Pearsonian skewness: </td><td>n/a</td></tr>" );
2361                     }
2362                     w.write( SurfacingConstants.NL );
2363                     w.write( "</table>" );
2364                     w.write( SurfacingConstants.NL );
2365                     w.write( "<br>" );
2366                     w.write( SurfacingConstants.NL );
2367                     w.write( "<hr>" );
2368                     w.write( SurfacingConstants.NL );
2369                     w.write( "<br>" );
2370                     w.write( SurfacingConstants.NL );
2371                     w.write( "<table>" );
2372                     w.write( SurfacingConstants.NL );
2373                 }
2374                 break;
2375         }
2376         for( final Writer w : split_writers.values() ) {
2377             w.write( SurfacingConstants.NL );
2378         }
2379         for( final DomainSimilarity similarity : similarities ) {
2380             if ( ( species_order != null ) && !species_order.isEmpty() ) {
2381                 ( ( PrintableDomainSimilarity ) similarity ).setSpeciesOrder( species_order );
2382             }
2383             if ( single_writer != null ) {
2384                 single_writer.write( similarity.toStringBuffer( print_option ).toString() );
2385             }
2386             else {
2387                 Writer local_writer = split_writers.get( ( similarity.getDomainId().getId().charAt( 0 ) + "" )
2388                         .toLowerCase().charAt( 0 ) );
2389                 if ( local_writer == null ) {
2390                     local_writer = split_writers.get( '0' );
2391                 }
2392                 local_writer.write( similarity.toStringBuffer( print_option ).toString() );
2393             }
2394             for( final Writer w : split_writers.values() ) {
2395                 w.write( SurfacingConstants.NL );
2396             }
2397         }
2398         switch ( print_option ) {
2399             case HTML:
2400                 for( final Writer w : split_writers.values() ) {
2401                     w.write( SurfacingConstants.NL );
2402                     w.write( "</table>" );
2403                     w.write( SurfacingConstants.NL );
2404                     w.write( "</font>" );
2405                     w.write( SurfacingConstants.NL );
2406                     w.write( "</body>" );
2407                     w.write( SurfacingConstants.NL );
2408                     w.write( "</html>" );
2409                     w.write( SurfacingConstants.NL );
2410                 }
2411                 break;
2412         }
2413         for( final Writer w : split_writers.values() ) {
2414             w.close();
2415         }
2416         return stats;
2417     }
2418
2419     private static void writeDomainsToIndividualFilePerTreeNode( final Writer individual_files_writer,
2420                                                                  final String domain_0,
2421                                                                  final String domain_1 ) throws IOException {
2422         individual_files_writer.write( domain_0 );
2423         individual_files_writer.write( ForesterUtil.LINE_SEPARATOR );
2424         if ( !ForesterUtil.isEmpty( domain_1 ) ) {
2425             individual_files_writer.write( domain_1 );
2426             individual_files_writer.write( ForesterUtil.LINE_SEPARATOR );
2427         }
2428     }
2429
2430     public static void writeMatrixToFile( final CharacterStateMatrix<?> matrix,
2431                                           final String filename,
2432                                           final Format format ) {
2433         final File outfile = new File( filename );
2434         checkForOutputFileWriteability( outfile );
2435         try {
2436             final BufferedWriter out = new BufferedWriter( new FileWriter( outfile ) );
2437             matrix.toWriter( out, format );
2438             out.flush();
2439             out.close();
2440         }
2441         catch ( final IOException e ) {
2442             ForesterUtil.fatalError( surfacing.PRG_NAME, e.getMessage() );
2443         }
2444         ForesterUtil.programMessage( surfacing.PRG_NAME, "Wrote matrix: \"" + filename + "\"" );
2445     }
2446
2447     public static void writeMatrixToFile( final File matrix_outfile, final List<DistanceMatrix> matrices ) {
2448         checkForOutputFileWriteability( matrix_outfile );
2449         try {
2450             final BufferedWriter out = new BufferedWriter( new FileWriter( matrix_outfile ) );
2451             for( final DistanceMatrix distance_matrix : matrices ) {
2452                 out.write( distance_matrix.toStringBuffer( DistanceMatrix.Format.PHYLIP ).toString() );
2453                 out.write( ForesterUtil.LINE_SEPARATOR );
2454                 out.flush();
2455             }
2456             out.close();
2457         }
2458         catch ( final IOException e ) {
2459             ForesterUtil.fatalError( surfacing.PRG_NAME, e.getMessage() );
2460         }
2461         ForesterUtil.programMessage( surfacing.PRG_NAME, "Wrote distance matrices to \"" + matrix_outfile + "\"" );
2462     }
2463
2464     private static void writePfamsToFile( final String outfile_name, final SortedSet<String> pfams ) {
2465         try {
2466             final Writer writer = new BufferedWriter( new FileWriter( new File( outfile_name ) ) );
2467             for( final String pfam : pfams ) {
2468                 writer.write( pfam );
2469                 writer.write( ForesterUtil.LINE_SEPARATOR );
2470             }
2471             writer.close();
2472             ForesterUtil.programMessage( surfacing.PRG_NAME, "Wrote " + pfams.size() + " pfams to [" + outfile_name
2473                     + "]" );
2474         }
2475         catch ( final IOException e ) {
2476             ForesterUtil.printWarningMessage( surfacing.PRG_NAME, "Failure to write: " + e );
2477         }
2478     }
2479
2480     public static void writePhylogenyToFile( final Phylogeny phylogeny, final String filename ) {
2481         final PhylogenyWriter writer = new PhylogenyWriter();
2482         try {
2483             writer.toPhyloXML( new File( filename ), phylogeny, 1 );
2484         }
2485         catch ( final IOException e ) {
2486             ForesterUtil.printWarningMessage( surfacing.PRG_NAME, "failed to write phylogeny to \"" + filename + "\": "
2487                     + e );
2488         }
2489         ForesterUtil.programMessage( surfacing.PRG_NAME, "Wrote phylogeny to \"" + filename + "\"" );
2490     }
2491
2492     public static void writeTaxonomyLinks( final Writer writer, final String species ) throws IOException {
2493         if ( ( species.length() > 1 ) && ( species.indexOf( '_' ) < 1 ) ) {
2494             final Matcher matcher = PATTERN_SP_STYLE_TAXONOMY.matcher( species );
2495             writer.write( " [" );
2496             if ( matcher.matches() ) {
2497                 writer.write( "<a href=\"" + SurfacingConstants.UNIPROT_LINK + species
2498                         + "\" target=\"taxonomy_window\">uniprot</a>" );
2499             }
2500             else {
2501                 writer.write( "<a href=\"" + SurfacingConstants.EOL_LINK + species
2502                         + "\" target=\"taxonomy_window\">eol</a>" );
2503                 writer.write( "|" );
2504                 writer.write( "<a href=\"" + SurfacingConstants.TOL_LINK + species
2505                         + "\" target=\"taxonomy_window\">tol</a>" );
2506             }
2507             writer.write( "]" );
2508         }
2509     }
2510
2511     private static void writeToNexus( final String outfile_name,
2512                                       final CharacterStateMatrix<BinaryStates> matrix,
2513                                       final Phylogeny phylogeny ) {
2514         if ( !( matrix instanceof BasicCharacterStateMatrix ) ) {
2515             throw new IllegalArgumentException( "can only write matrices of type [" + BasicCharacterStateMatrix.class
2516                     + "] to nexus" );
2517         }
2518         final BasicCharacterStateMatrix<BinaryStates> my_matrix = ( org.forester.evoinference.matrix.character.BasicCharacterStateMatrix<BinaryStates> ) matrix;
2519         final List<Phylogeny> phylogenies = new ArrayList<Phylogeny>( 1 );
2520         phylogenies.add( phylogeny );
2521         try {
2522             final BufferedWriter w = new BufferedWriter( new FileWriter( outfile_name ) );
2523             w.write( NexusConstants.NEXUS );
2524             w.write( ForesterUtil.LINE_SEPARATOR );
2525             my_matrix.writeNexusTaxaBlock( w );
2526             my_matrix.writeNexusBinaryChractersBlock( w );
2527             PhylogenyWriter.writeNexusTreesBlock( w, phylogenies, NH_CONVERSION_SUPPORT_VALUE_STYLE.NONE );
2528             w.flush();
2529             w.close();
2530             ForesterUtil.programMessage( surfacing.PRG_NAME, "Wrote Nexus file: \"" + outfile_name + "\"" );
2531         }
2532         catch ( final IOException e ) {
2533             ForesterUtil.fatalError( surfacing.PRG_NAME, e.getMessage() );
2534         }
2535     }
2536
2537     private static void writeToNexus( final String outfile_name,
2538                                       final DomainParsimonyCalculator domain_parsimony,
2539                                       final Phylogeny phylogeny ) {
2540         writeToNexus( outfile_name + surfacing.NEXUS_EXTERNAL_DOMAINS,
2541                       domain_parsimony.createMatrixOfDomainPresenceOrAbsence(),
2542                       phylogeny );
2543         writeToNexus( outfile_name + surfacing.NEXUS_EXTERNAL_DOMAIN_COMBINATIONS,
2544                       domain_parsimony.createMatrixOfBinaryDomainCombinationPresenceOrAbsence(),
2545                       phylogeny );
2546     }
2547
2548     public static void domainsPerProteinsStatistics( final String genome,
2549                                                      final List<Protein> protein_list,
2550                                                      final DescriptiveStatistics all_genomes_domains_per_potein_stats,
2551                                                      final SortedMap<Integer, Integer> all_genomes_domains_per_potein_histo,
2552                                                      final SortedSet<String> domains_which_are_always_single,
2553                                                      final SortedSet<String> domains_which_are_sometimes_single_sometimes_not,
2554                                                      final SortedSet<String> domains_which_never_single,
2555                                                      final Writer writer ) {
2556         final DescriptiveStatistics stats = new BasicDescriptiveStatistics();
2557         for( final Protein protein : protein_list ) {
2558             final int domains = protein.getNumberOfProteinDomains();
2559             //System.out.println( domains );
2560             stats.addValue( domains );
2561             all_genomes_domains_per_potein_stats.addValue( domains );
2562             if ( !all_genomes_domains_per_potein_histo.containsKey( domains ) ) {
2563                 all_genomes_domains_per_potein_histo.put( domains, 1 );
2564             }
2565             else {
2566                 all_genomes_domains_per_potein_histo.put( domains,
2567                                                           1 + all_genomes_domains_per_potein_histo.get( domains ) );
2568             }
2569             if ( domains == 1 ) {
2570                 final String domain = protein.getProteinDomain( 0 ).getDomainId().getId();
2571                 if ( !domains_which_are_sometimes_single_sometimes_not.contains( domain ) ) {
2572                     if ( domains_which_never_single.contains( domain ) ) {
2573                         domains_which_never_single.remove( domain );
2574                         domains_which_are_sometimes_single_sometimes_not.add( domain );
2575                     }
2576                     else {
2577                         domains_which_are_always_single.add( domain );
2578                     }
2579                 }
2580             }
2581             else if ( domains > 1 ) {
2582                 for( final Domain d : protein.getProteinDomains() ) {
2583                     final String domain = d.getDomainId().getId();
2584                     // System.out.println( domain );
2585                     if ( !domains_which_are_sometimes_single_sometimes_not.contains( domain ) ) {
2586                         if ( domains_which_are_always_single.contains( domain ) ) {
2587                             domains_which_are_always_single.remove( domain );
2588                             domains_which_are_sometimes_single_sometimes_not.add( domain );
2589                         }
2590                         else {
2591                             domains_which_never_single.add( domain );
2592                         }
2593                     }
2594                 }
2595             }
2596         }
2597         try {
2598             writer.write( genome );
2599             writer.write( "\t" );
2600             if ( stats.getN() >= 1 ) {
2601                 writer.write( stats.arithmeticMean() + "" );
2602                 writer.write( "\t" );
2603                 if ( stats.getN() >= 2 ) {
2604                     writer.write( stats.sampleStandardDeviation() + "" );
2605                 }
2606                 else {
2607                     writer.write( "" );
2608                 }
2609                 writer.write( "\t" );
2610                 writer.write( stats.median() + "" );
2611                 writer.write( "\t" );
2612                 writer.write( stats.getN() + "" );
2613                 writer.write( "\t" );
2614                 writer.write( stats.getMin() + "" );
2615                 writer.write( "\t" );
2616                 writer.write( stats.getMax() + "" );
2617             }
2618             else {
2619                 writer.write( "\t" );
2620                 writer.write( "\t" );
2621                 writer.write( "\t" );
2622                 writer.write( "0" );
2623                 writer.write( "\t" );
2624                 writer.write( "\t" );
2625             }
2626             writer.write( "\n" );
2627         }
2628         catch ( final IOException e ) {
2629             e.printStackTrace();
2630         }
2631     }
2632
2633     final static class DomainComparator implements Comparator<Domain> {
2634
2635         final private boolean _ascending;
2636
2637         public DomainComparator( final boolean ascending ) {
2638             _ascending = ascending;
2639         }
2640
2641         @Override
2642         public final int compare( final Domain d0, final Domain d1 ) {
2643             if ( d0.getFrom() < d1.getFrom() ) {
2644                 return _ascending ? -1 : 1;
2645             }
2646             else if ( d0.getFrom() > d1.getFrom() ) {
2647                 return _ascending ? 1 : -1;
2648             }
2649             return 0;
2650         }
2651     }
2652 }