in progress...
[jalview.git] / forester / java / src / org / forester / application / rio.java
1 // $Id:
2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
4 //
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
7 // Copyright (C) 2000-2001 Washington University School of Medicine
8 // and Howard Hughes Medical Institute
9 // All rights reserved
10 //
11 // This library is free software; you can redistribute it and/or
12 // modify it under the terms of the GNU Lesser General Public
13 // License as published by the Free Software Foundation; either
14 // version 2.1 of the License, or (at your option) any later version.
15 //
16 // This library is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 // Lesser General Public License for more details.
20 //
21 // You should have received a copy of the GNU Lesser General Public
22 // License along with this library; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 //
25 // Contact: phylosoft @ gmail . com
26 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
27
28 package org.forester.application;
29
30 import java.io.File;
31 import java.io.IOException;
32 import java.math.RoundingMode;
33 import java.util.ArrayList;
34 import java.util.List;
35
36 import org.forester.datastructures.IntMatrix;
37 import org.forester.io.parsers.IteratingPhylogenyParser;
38 import org.forester.io.parsers.PhylogenyParser;
39 import org.forester.io.parsers.nexus.NexusPhylogeniesParser;
40 import org.forester.io.parsers.nhx.NHXParser;
41 import org.forester.io.parsers.nhx.NHXParser.TAXONOMY_EXTRACTION;
42 import org.forester.io.parsers.phyloxml.PhyloXmlParser;
43 import org.forester.io.parsers.util.ParserUtils;
44 import org.forester.io.writers.PhylogenyWriter;
45 import org.forester.phylogeny.Phylogeny;
46 import org.forester.rio.RIO;
47 import org.forester.rio.RIO.REROOTING;
48 import org.forester.rio.RIOException;
49 import org.forester.sdi.SDIException;
50 import org.forester.sdi.SDIutil.ALGORITHM;
51 import org.forester.util.BasicDescriptiveStatistics;
52 import org.forester.util.CommandLineArguments;
53 import org.forester.util.EasyWriter;
54 import org.forester.util.ForesterUtil;
55
56 public class rio {
57
58     final static private String PRG_NAME                 = "rio";
59     final static private String PRG_VERSION              = "4.000 beta 11";
60     final static private String PRG_DATE                 = "170417";
61     final static private String E_MAIL                   = "phyloxml@gmail.com";
62     final static private String WWW                      = "https://sites.google.com/site/cmzmasek/home/software/forester";
63     final static private String HELP_OPTION_1            = "help";
64     final static private String HELP_OPTION_2            = "h";
65     final static private String GT_FIRST                 = "f";
66     final static private String GT_LAST                  = "l";
67     final static private String REROOTING_OPT            = "r";
68     final static private String OUTGROUP                 = "o";
69     final static private String RETURN_SPECIES_TREE      = "s";
70     final static private String RETURN_BEST_GENE_TREE    = "g";
71     final static private String USE_SDIR                 = "b";
72     final static private String TRANSFER_TAXONOMY_OPTION = "t";
73
74     public static void main( final String[] args ) {
75         ForesterUtil.printProgramInformation( PRG_NAME,
76                                               "resampled inference of orthologs",
77                                               PRG_VERSION,
78                                               PRG_DATE,
79                                               E_MAIL,
80                                               WWW,
81                                               ForesterUtil.getForesterLibraryInformation() );
82         CommandLineArguments cla = null;
83         try {
84             cla = new CommandLineArguments( args );
85         }
86         catch ( final Exception e ) {
87             ForesterUtil.fatalError( e.getMessage() );
88         }
89         if ( cla.isOptionSet( HELP_OPTION_1 ) || cla.isOptionSet( HELP_OPTION_2 ) || ( args.length == 0 ) ) {
90             printHelp();
91         }
92         if ( ( args.length < 3 ) || ( args.length > 11 ) || ( cla.getNumberOfNames() < 3 ) ) {
93             System.out.println();
94             System.out.println( "error: incorrect number of arguments" );
95             System.out.println();
96             printHelp();
97         }
98         final List<String> allowed_options = new ArrayList<String>();
99         allowed_options.add( GT_FIRST );
100         allowed_options.add( GT_LAST );
101         allowed_options.add( REROOTING_OPT );
102         allowed_options.add( OUTGROUP );
103         allowed_options.add( USE_SDIR );
104         allowed_options.add( RETURN_SPECIES_TREE );
105         allowed_options.add( RETURN_BEST_GENE_TREE );
106         allowed_options.add( TRANSFER_TAXONOMY_OPTION );
107         final String dissallowed_options = cla.validateAllowedOptionsAsString( allowed_options );
108         if ( dissallowed_options.length() > 0 ) {
109             ForesterUtil.fatalError( "unknown option(s): " + dissallowed_options );
110         }
111         final File gene_trees_file = cla.getFile( 0 );
112         final File species_tree_file = cla.getFile( 1 );
113         final File orthology_outtable = cla.getFile( 2 );
114         final File logfile;
115         if ( cla.getNumberOfNames() > 3 ) {
116             logfile = cla.getFile( 3 );
117             if ( logfile.exists() ) {
118                 ForesterUtil.fatalError( "\"" + logfile + "\" already exists" );
119             }
120         }
121         else {
122             logfile = null;
123         }
124         boolean sdir = false;
125         if ( cla.isOptionSet( USE_SDIR ) ) {
126             if ( cla.isOptionHasAValue( USE_SDIR ) ) {
127                 ForesterUtil.fatalError( "no value allowed for -" + USE_SDIR );
128             }
129             sdir = true;
130             if ( logfile != null ) {
131                 ForesterUtil.fatalError( "no logfile output for SDIR algorithm" );
132             }
133         }
134         String outgroup = null;
135         if ( cla.isOptionSet( OUTGROUP ) ) {
136             if ( !cla.isOptionHasAValue( OUTGROUP ) ) {
137                 ForesterUtil.fatalError( "no value for -" + OUTGROUP );
138             }
139             if ( sdir ) {
140                 ForesterUtil.fatalError( "no outgroup option for SDIR algorithm" );
141             }
142             outgroup = cla.getOptionValueAsCleanString( OUTGROUP );
143         }
144         REROOTING rerooting = REROOTING.BY_ALGORITHM;
145         if ( cla.isOptionSet( REROOTING_OPT ) ) {
146             if ( !cla.isOptionHasAValue( REROOTING_OPT ) ) {
147                 ForesterUtil.fatalError( "no value for -" + REROOTING_OPT );
148             }
149             if ( sdir ) {
150                 ForesterUtil.fatalError( "no re-rooting option for SDIR algorithm" );
151             }
152             final String rerooting_str = cla.getOptionValueAsCleanString( REROOTING_OPT ).toLowerCase();
153             if ( rerooting_str.equals( "none" ) ) {
154                 rerooting = REROOTING.NONE;
155             }
156             else if ( rerooting_str.equals( "midpoint" ) ) {
157                 rerooting = REROOTING.MIDPOINT;
158             }
159             else if ( rerooting_str.equals( "outgroup" ) ) {
160                 rerooting = REROOTING.OUTGROUP;
161             }
162             else {
163                 ForesterUtil
164                         .fatalError( "values for re-rooting are: 'none', 'midpoint', or 'outgroup' (minizming duplications is default)" );
165             }
166         }
167         if ( ForesterUtil.isEmpty( outgroup ) && ( rerooting == REROOTING.OUTGROUP ) ) {
168             ForesterUtil.fatalError( "selected re-rooting by outgroup, but outgroup not set" );
169         }
170         if ( !ForesterUtil.isEmpty( outgroup ) && ( rerooting != REROOTING.OUTGROUP ) ) {
171             ForesterUtil.fatalError( "outgroup set, but selected re-rooting by other approach" );
172         }
173         int gt_first = RIO.DEFAULT_RANGE;
174         int gt_last = RIO.DEFAULT_RANGE;
175         if ( cla.isOptionSet( GT_FIRST ) ) {
176             if ( !cla.isOptionHasAValue( GT_FIRST ) ) {
177                 ForesterUtil.fatalError( "no value for -" + GT_FIRST );
178             }
179             if ( sdir ) {
180                 ForesterUtil.fatalError( "no gene tree range option for SDIR algorithm" );
181             }
182             try {
183                 gt_first = cla.getOptionValueAsInt( GT_FIRST );
184             }
185             catch ( final IOException e ) {
186                 ForesterUtil.fatalError( "could not parse integer for -" + GT_FIRST + " option" );
187             }
188             if ( gt_first < 0 ) {
189                 ForesterUtil.fatalError( "attempt to set index of first tree to analyze to: " + gt_first );
190             }
191         }
192         if ( cla.isOptionSet( GT_LAST ) ) {
193             if ( !cla.isOptionHasAValue( GT_LAST ) ) {
194                 ForesterUtil.fatalError( "no value for -" + GT_LAST );
195             }
196             if ( sdir ) {
197                 ForesterUtil.fatalError( "no gene tree range option for SDIR algorithm" );
198             }
199             try {
200                 gt_last = cla.getOptionValueAsInt( GT_LAST );
201             }
202             catch ( final IOException e ) {
203                 ForesterUtil.fatalError( "could not parse integer for -" + GT_LAST + " option" );
204             }
205             if ( gt_last < 0 ) {
206                 ForesterUtil.fatalError( "attempt to set index of last tree to analyze to: " + gt_last );
207             }
208         }
209         if ( ( ( gt_last != RIO.DEFAULT_RANGE ) && ( gt_first != RIO.DEFAULT_RANGE ) ) && ( ( gt_last < gt_first ) ) ) {
210             ForesterUtil.fatalError( "attempt to set range (0-based) of gene to analyze to: from " + gt_first + " to "
211                     + gt_last );
212         }
213         File return_species_tree = null;
214         if ( !sdir && cla.isOptionSet( RETURN_SPECIES_TREE ) ) {
215             if ( !cla.isOptionHasAValue( RETURN_SPECIES_TREE ) ) {
216                 ForesterUtil.fatalError( "no value for -" + RETURN_SPECIES_TREE );
217             }
218             final String s = cla.getOptionValueAsCleanString( RETURN_SPECIES_TREE );
219             return_species_tree = new File( s );
220             if ( return_species_tree.exists() ) {
221                 ForesterUtil.fatalError( "\"" + return_species_tree + "\" already exists" );
222             }
223         }
224         File return_gene_tree = null;
225         if ( !sdir && cla.isOptionSet( RETURN_BEST_GENE_TREE ) ) {
226             if ( !cla.isOptionHasAValue( RETURN_BEST_GENE_TREE ) ) {
227                 ForesterUtil.fatalError( "no value for -" + RETURN_BEST_GENE_TREE );
228             }
229             final String s = cla.getOptionValueAsCleanString( RETURN_BEST_GENE_TREE );
230             return_gene_tree = new File( s );
231             if ( return_gene_tree.exists() ) {
232                 ForesterUtil.fatalError( "\"" + return_gene_tree + "\" already exists" );
233             }
234         }
235         boolean transfer_taxonomy = false;
236         if ( !sdir && cla.isOptionSet( TRANSFER_TAXONOMY_OPTION ) ) {
237             if ( return_gene_tree == null ) {
238                 ForesterUtil.fatalError( "no point in transferring taxonomy data without returning best gene tree" );
239             }
240             transfer_taxonomy = true;
241         }
242         ForesterUtil.fatalErrorIfFileNotReadable( gene_trees_file );
243         ForesterUtil.fatalErrorIfFileNotReadable( species_tree_file );
244         if ( orthology_outtable.exists() ) {
245             ForesterUtil.fatalError( "\"" + orthology_outtable + "\" already exists" );
246         }
247         long time = 0;
248         try {
249             System.out.println( "Gene trees                          :\t" + gene_trees_file.getCanonicalPath() );
250             System.out.println( "Species tree                        :\t" + species_tree_file.getCanonicalPath() );
251         }
252         catch ( final IOException e ) {
253             ForesterUtil.fatalError( e.getLocalizedMessage() );
254         }
255         System.out.println( "All vs all orthology results table  :\t" + orthology_outtable );
256         if ( logfile != null ) {
257             System.out.println( "Logfile                             :\t" + logfile );
258         }
259         if ( gt_first != RIO.DEFAULT_RANGE ) {
260             System.out.println( "First gene tree to analyze          :\t" + gt_first );
261         }
262         if ( gt_last != RIO.DEFAULT_RANGE ) {
263             System.out.println( "Last gene tree to analyze           :\t" + gt_last );
264         }
265         String rerooting_str = "";
266         switch ( rerooting ) {
267             case BY_ALGORITHM: {
268                 rerooting_str = "by minimizing duplications";
269                 break;
270             }
271             case MIDPOINT: {
272                 rerooting_str = "by midpoint method";
273                 break;
274             }
275             case OUTGROUP: {
276                 rerooting_str = "by outgroup: " + outgroup;
277                 break;
278             }
279             case NONE: {
280                 rerooting_str = "none";
281                 break;
282             }
283         }
284         System.out.println( "Re-rooting                          : \t" + rerooting_str );
285         if ( !sdir ) {
286             System.out.println( "Non binary species tree             :\tallowed" );
287         }
288         else {
289             System.out.println( "Non binary species tree             :\tdisallowed" );
290         }
291         if ( return_species_tree != null ) {
292             System.out.println( "Write used species tree to          :\t" + return_species_tree );
293         }
294         if ( return_gene_tree != null ) {
295             System.out.println( "Write best gene tree to             :\t" + return_gene_tree );
296             System.out.println( "Transfer taxonomic data             :\t" + transfer_taxonomy );
297         }
298         time = System.currentTimeMillis();
299         final ALGORITHM algorithm;
300         if ( sdir ) {
301             algorithm = ALGORITHM.SDIR;
302         }
303         else {
304             algorithm = ALGORITHM.GSDIR;
305         }
306         try {
307             final RIO rio;
308             boolean iterating = false;
309             final PhylogenyParser p = ParserUtils.createParserDependingOnFileType( gene_trees_file, true );
310             if ( p instanceof PhyloXmlParser ) {
311                 rio = RIO.executeAnalysis( gene_trees_file,
312                                            species_tree_file,
313                                            algorithm,
314                                            rerooting,
315                                            outgroup,
316                                            gt_first,
317                                            gt_last,
318                                            logfile != null,
319                                            true,
320                                            transfer_taxonomy );
321             }
322             else {
323                 iterating = true;
324                 if ( p instanceof NHXParser ) {
325                     final NHXParser nhx = ( NHXParser ) p;
326                     nhx.setReplaceUnderscores( false );
327                     nhx.setIgnoreQuotes( true );
328                     nhx.setTaxonomyExtraction( TAXONOMY_EXTRACTION.AGGRESSIVE );
329                 }
330                 else if ( p instanceof NexusPhylogeniesParser ) {
331                     final NexusPhylogeniesParser nex = ( NexusPhylogeniesParser ) p;
332                     nex.setReplaceUnderscores( false );
333                     nex.setIgnoreQuotes( true );
334                     nex.setTaxonomyExtraction( TAXONOMY_EXTRACTION.AGGRESSIVE );
335                 }
336                 else {
337                     throw new RuntimeException( "unknown parser type: " + p );
338                 }
339                 final IteratingPhylogenyParser ip = ( IteratingPhylogenyParser ) p;
340                 ip.setSource( gene_trees_file );
341                 rio = RIO.executeAnalysis( ip,
342                                            species_tree_file,
343                                            algorithm,
344                                            rerooting,
345                                            outgroup,
346                                            gt_first,
347                                            gt_last,
348                                            logfile != null,
349                                            true,
350                                            transfer_taxonomy );
351             }
352             if ( algorithm == ALGORITHM.GSDIR ) {
353                 System.out.println( "Taxonomy linking based on           :\t" + rio.getGSDIRtaxCompBase() );
354             }
355             final IntMatrix m;
356             if ( iterating ) {
357                 m = rio.getOrthologTable();
358             }
359             else {
360                 m = RIO.calculateOrthologTable( rio.getAnalyzedGeneTrees(), true );
361             }
362             final BasicDescriptiveStatistics stats = rio.getDuplicationsStatistics();
363             writeTable( orthology_outtable, stats.getN(), m );
364             if ( ( algorithm != ALGORITHM.SDIR ) && ( logfile != null ) ) {
365                 writeLogFile( logfile,
366                               rio,
367                               species_tree_file,
368                               gene_trees_file,
369                               orthology_outtable,
370                               PRG_NAME,
371                               PRG_VERSION,
372                               PRG_DATE,
373                               ForesterUtil.getForesterLibraryInformation() );
374             }
375             if ( return_species_tree != null ) {
376                 writeTree( rio.getSpeciesTree(), return_species_tree, "Wrote (stripped) species tree to    :\t" );
377             }
378             if ( return_gene_tree != null ) {
379                 writeTree( rio.getMinDuplicationsGeneTree(),
380                            return_gene_tree,
381                            "Wrote one min duplication gene tree :\t" );
382             }
383             final java.text.DecimalFormat df = new java.text.DecimalFormat( "0.#" );
384             final int min = ( int ) stats.getMin();
385             final int max = ( int ) stats.getMax();
386             final int median = ( int ) stats.median();
387             int min_count = 0;
388             int max_count = 0;
389             int median_count = 0;
390             for( double d : stats.getData() ) {
391                 if ( ( ( int ) d ) == min ) {
392                     ++min_count;
393                 }
394                 if ( ( ( int ) d ) == max ) {
395                     ++max_count;
396                 }
397                 if ( ( ( int ) d ) == median ) {
398                     ++median_count;
399                 }
400             }
401             final double min_count_percentage = ( 100.0 * min_count ) / stats.getN();
402             final double max_count_percentage = ( 100.0 * max_count ) / stats.getN();
403             final double median_count_percentage = ( 100.0 * median_count ) / stats.getN();
404             System.out.println( "Gene tree internal nodes            :\t" + rio.getIntNodesOfAnalyzedGeneTrees() );
405             System.out.println( "Gene tree external nodes            :\t" + rio.getExtNodesOfAnalyzedGeneTrees() );
406             System.out.println( "Mean number of duplications         :\t" + df.format( stats.arithmeticMean() ) + "\t"
407                     + df.format( ( 100.0 * stats.arithmeticMean() ) / rio.getIntNodesOfAnalyzedGeneTrees() )
408                     + "%\t(sd: " + df.format( stats.sampleStandardDeviation() ) + ")" );
409             if ( stats.getN() > 3 ) {
410                 System.out.println( "Median number of duplications       :\t" + df.format( median ) + "\t"
411                         + df.format( ( 100.0 * median ) / rio.getIntNodesOfAnalyzedGeneTrees() ) + "%" );
412             }
413             System.out.println( "Minimum duplications                :\t" + min + "\t"
414                     + df.format( ( 100.0 * min ) / rio.getIntNodesOfAnalyzedGeneTrees() ) + "%" );
415             System.out.println( "Maximum duplications                :\t" + ( int ) max + "\t"
416                     + df.format( ( 100.0 * max ) / rio.getIntNodesOfAnalyzedGeneTrees() ) + "%" );
417             System.out.println( "Gene trees with median duplications :\t" + median_count + "\t"
418                     + df.format( median_count_percentage ) + "%" );
419             System.out.println( "Gene trees with minimum duplications:\t" + min_count + "\t"
420                     + df.format( min_count_percentage ) + "%" );
421             System.out.println( "Gene trees with maximum duplications:\t" + max_count + "\t"
422                     + df.format( max_count_percentage ) + "%" );
423         }
424         catch ( final RIOException e ) {
425             ForesterUtil.fatalError( e.getLocalizedMessage() );
426         }
427         catch ( final SDIException e ) {
428             ForesterUtil.fatalError( e.getLocalizedMessage() );
429         }
430         catch ( final IOException e ) {
431             ForesterUtil.fatalError( e.getLocalizedMessage() );
432         }
433         catch ( final OutOfMemoryError e ) {
434             ForesterUtil.outOfMemoryError( e );
435         }
436         catch ( final Exception e ) {
437             ForesterUtil.unexpectedFatalError( e );
438         }
439         catch ( final Error e ) {
440             ForesterUtil.unexpectedFatalError( e );
441         }
442         time = System.currentTimeMillis() - time;
443         System.out.println( "Time                                :\t" + time + "ms" );
444         System.exit( 0 );
445     }
446
447     private final static void printHelp() {
448         System.out.println( "Usage" );
449         System.out.println();
450         System.out.println( PRG_NAME
451                 + " [options] <gene trees infile> <species tree infile> <all vs all orthology table outfile> [logfile]" );
452         System.out.println();
453         System.out.println( " Options" );
454         System.out.println( "  -" + GT_FIRST + "=<first>     : first gene tree to analyze (0-based index)" );
455         System.out.println( "  -" + GT_LAST + "=<last>      : last gene tree to analyze (0-based index)" );
456         System.out.println( "  -" + REROOTING_OPT
457                 + "=<re-rooting>: re-rooting method for gene trees, possible values or 'none', 'midpoint'," );
458         System.out.println( "                   or 'outgroup' (default: by minizming duplications)" );
459         System.out.println( "  -" + OUTGROUP
460                 + "=<outgroup>  : for rooting by outgroup, name of outgroup (external gene tree node)" );
461         System.out
462                 .println( "  -" + RETURN_SPECIES_TREE + "=<outfile>   : to write the (stripped) species tree to file" );
463         System.out.println( "  -" + RETURN_BEST_GENE_TREE
464                 + "=<outfile>   : to write (one) minimal duplication gene tree to file" );
465         System.out.println( "  -" + TRANSFER_TAXONOMY_OPTION
466                 + "             : to transfer taxonomic data from species tree to returned minimal duplication gene tree\n"
467                 + "                   (if -" + RETURN_BEST_GENE_TREE + " option is used)" );
468         System.out.println( "  -" + USE_SDIR
469                 + "             : to use SDIR instead of GSDIR (faster, but non-binary species trees are" );
470         System.out.println( "                   disallowed, as are most options)" );
471         System.out.println();
472         System.out.println( " Formats" );
473         System.out
474                 .println( "  The gene trees, as well as the species tree, ideally are in phyloXML (www.phyloxml.org) format," );
475         System.out
476                 .println( "  but can also be in New Hamphshire (Newick) or Nexus format as long as species information can be" );
477         System.out
478                 .println( "  extracted from the gene names (e.g. \"HUMAN\" from \"BCL2_HUMAN\") and matched to a single species" );
479         System.out.println( "  in the species tree." );
480         System.out.println();
481         System.out.println( " Examples" );
482         System.out.println( "  rio gene_trees.nh species.xml outtable.tsv log.txt" );
483         System.out
484                 .println( "  rio -t -f=10 -l=100 -r=none -g=out_gene_tree.xml -s=stripped_species.xml gene_trees.xml species.xml outtable.tsv log.txt" );
485         System.out.println();
486         System.exit( -1 );
487     }
488
489     private static void writeLogFile( final File logfile,
490                                       final RIO rio,
491                                       final File species_tree_file,
492                                       final File gene_trees_file,
493                                       final File outtable,
494                                       final String prg_name,
495                                       final String prg_v,
496                                       final String prg_date,
497                                       final String f )
498             throws IOException {
499         final EasyWriter out = ForesterUtil.createEasyWriter( logfile );
500         out.println( prg_name );
501         out.println( "version : " + prg_v );
502         out.println( "date    : " + prg_date );
503         out.println( "based on: " + f );
504         out.println( "----------------------------------" );
505         out.println( "Gene trees                                      : " + gene_trees_file.getCanonicalPath() );
506         out.println( "Species tree                                    : " + species_tree_file.getCanonicalPath() );
507         out.println( "All vs all orthology table                      : " + outtable.getCanonicalPath() );
508         out.flush();
509         out.println( rio.getLog().toString() );
510         out.close();
511         System.out.println( "Wrote log to                        :\t" + logfile.getCanonicalPath() );
512     }
513
514     private static void writeTable( final File table_outfile, final int gene_trees_analyzed, final IntMatrix m )
515             throws IOException {
516         final EasyWriter w = ForesterUtil.createEasyWriter( table_outfile );
517         final java.text.DecimalFormat df = new java.text.DecimalFormat( "0.####" );
518         df.setDecimalSeparatorAlwaysShown( false );
519         df.setRoundingMode( RoundingMode.HALF_UP );
520         for( int i = 0; i < m.size(); ++i ) {
521             w.print( "\t" );
522             w.print( m.getLabel( i ) );
523         }
524         w.println();
525         for( int x = 0; x < m.size(); ++x ) {
526             w.print( m.getLabel( x ) );
527             for( int y = 0; y < m.size(); ++y ) {
528                 w.print( "\t" );
529                 if ( x == y ) {
530                     if ( m.get( x, y ) != gene_trees_analyzed ) {
531                         ForesterUtil.unexpectedFatalError( "diagonal value is off" );
532                     }
533                     w.print( "-" );
534                 }
535                 else {
536                     w.print( df.format( ( ( double ) m.get( x, y ) ) / gene_trees_analyzed ) );
537                 }
538             }
539             w.println();
540         }
541         w.close();
542         System.out.println( "Wrote table to                      :\t" + table_outfile.getCanonicalPath() );
543     }
544
545     private static void writeTree( final Phylogeny p, final File f, final String comment ) throws IOException {
546         final PhylogenyWriter writer = new PhylogenyWriter();
547         writer.toPhyloXML( f, p, 0 );
548         System.out.println( comment + f.getCanonicalPath() );
549     }
550 }