3 // FORESTER -- software libraries and applications
4 // for evolutionary biology research and applications.
6 // Copyright (C) 2008-2009 Christian M. Zmasek
7 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
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.
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.
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
24 // Contact: phylosoft @ gmail . com
25 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
27 package org.forester.surfacing;
29 import java.awt.Color;
30 import java.io.BufferedWriter;
32 import java.io.FileWriter;
33 import java.io.IOException;
34 import java.io.Writer;
35 import java.text.DecimalFormat;
36 import java.text.NumberFormat;
37 import java.util.ArrayList;
38 import java.util.Arrays;
39 import java.util.Collections;
40 import java.util.Comparator;
41 import java.util.HashMap;
42 import java.util.HashSet;
43 import java.util.Iterator;
44 import java.util.List;
46 import java.util.Map.Entry;
47 import java.util.PriorityQueue;
49 import java.util.SortedMap;
50 import java.util.SortedSet;
51 import java.util.TreeMap;
52 import java.util.TreeSet;
53 import java.util.regex.Matcher;
54 import java.util.regex.Pattern;
56 import org.forester.application.surfacing;
57 import org.forester.evoinference.distance.NeighborJoining;
58 import org.forester.evoinference.matrix.character.BasicCharacterStateMatrix;
59 import org.forester.evoinference.matrix.character.CharacterStateMatrix;
60 import org.forester.evoinference.matrix.character.CharacterStateMatrix.BinaryStates;
61 import org.forester.evoinference.matrix.character.CharacterStateMatrix.Format;
62 import org.forester.evoinference.matrix.character.CharacterStateMatrix.GainLossStates;
63 import org.forester.evoinference.matrix.distance.DistanceMatrix;
64 import org.forester.go.GoId;
65 import org.forester.go.GoNameSpace;
66 import org.forester.go.GoTerm;
67 import org.forester.go.PfamToGoMapping;
68 import org.forester.io.parsers.nexus.NexusConstants;
69 import org.forester.io.parsers.phyloxml.PhyloXmlUtil;
70 import org.forester.io.parsers.util.ParserUtils;
71 import org.forester.io.writers.PhylogenyWriter;
72 import org.forester.phylogeny.Phylogeny;
73 import org.forester.phylogeny.PhylogenyMethods;
74 import org.forester.phylogeny.PhylogenyNode;
75 import org.forester.phylogeny.PhylogenyNode.NH_CONVERSION_SUPPORT_VALUE_STYLE;
76 import org.forester.phylogeny.data.BinaryCharacters;
77 import org.forester.phylogeny.data.Confidence;
78 import org.forester.phylogeny.data.Taxonomy;
79 import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
80 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
81 import org.forester.protein.BasicDomain;
82 import org.forester.protein.BasicProtein;
83 import org.forester.protein.BinaryDomainCombination;
84 import org.forester.protein.Domain;
85 import org.forester.protein.Protein;
86 import org.forester.species.Species;
87 import org.forester.surfacing.DomainSimilarity.PRINT_OPTION;
88 import org.forester.surfacing.DomainSimilarityCalculator.Detailedness;
89 import org.forester.surfacing.GenomeWideCombinableDomains.GenomeWideCombinableDomainsSortOrder;
90 import org.forester.util.AsciiHistogram;
91 import org.forester.util.BasicDescriptiveStatistics;
92 import org.forester.util.BasicTable;
93 import org.forester.util.BasicTableParser;
94 import org.forester.util.CommandLineArguments;
95 import org.forester.util.DescriptiveStatistics;
96 import org.forester.util.ForesterUtil;
97 import org.forester.util.TaxonomyColors;
98 import org.forester.util.TaxonomyGroups;
100 public final class SurfacingUtil {
102 public final static Pattern PATTERN_SP_STYLE_TAXONOMY = Pattern.compile( "^[A-Z0-9]{3,5}$" );
103 private final static Map<String, String> _TAXCODE_HEXCOLORSTRING_MAP = new HashMap<String, String>();
104 private final static Map<String, String> _TAXCODE_TAXGROUP_MAP = new HashMap<String, String>();
105 private static final Comparator<Domain> ASCENDING_CONFIDENCE_VALUE_ORDER = new Comparator<Domain>() {
108 public int compare( final Domain d1,
110 if ( d1.getPerDomainEvalue() < d2
111 .getPerDomainEvalue() ) {
115 .getPerDomainEvalue() > d2
116 .getPerDomainEvalue() ) {
120 return d1.compareTo( d2 );
124 private final static NumberFormat FORMATTER_3 = new DecimalFormat( "0.000" );
126 private SurfacingUtil() {
127 // Hidden constructor.
130 public static void addAllBinaryDomainCombinationToSet( final GenomeWideCombinableDomains genome,
131 final SortedSet<BinaryDomainCombination> binary_domain_combinations ) {
132 final SortedMap<String, CombinableDomains> all_cd = genome.getAllCombinableDomainsIds();
133 for( final String domain_id : all_cd.keySet() ) {
134 binary_domain_combinations.addAll( all_cd.get( domain_id ).toBinaryDomainCombinations() );
138 public static void addAllDomainIdsToSet( final GenomeWideCombinableDomains genome,
139 final SortedSet<String> domain_ids ) {
140 final SortedSet<String> domains = genome.getAllDomainIds();
141 for( final String domain : domains ) {
142 domain_ids.add( domain );
146 public static DescriptiveStatistics calculateDescriptiveStatisticsForMeanValues( final Set<DomainSimilarity> similarities ) {
147 final DescriptiveStatistics stats = new BasicDescriptiveStatistics();
148 for( final DomainSimilarity similarity : similarities ) {
149 stats.addValue( similarity.getMeanSimilarityScore() );
154 public static void checkForOutputFileWriteability( final File outfile ) {
155 final String error = ForesterUtil.isWritableFile( outfile );
156 if ( !ForesterUtil.isEmpty( error ) ) {
157 ForesterUtil.fatalError( surfacing.PRG_NAME, error );
161 public static void checkWriteabilityForPairwiseComparisons( final DomainSimilarity.PRINT_OPTION domain_similarity_print_option,
162 final String[][] input_file_properties,
163 final String automated_pairwise_comparison_suffix,
164 final File outdir ) {
165 for( int i = 0; i < input_file_properties.length; ++i ) {
166 for( int j = 0; j < i; ++j ) {
167 final String species_i = input_file_properties[ i ][ 1 ];
168 final String species_j = input_file_properties[ j ][ 1 ];
169 String pairwise_similarities_output_file_str = surfacing.PAIRWISE_DOMAIN_COMPARISONS_PREFIX + species_i
170 + "_" + species_j + automated_pairwise_comparison_suffix;
171 switch ( domain_similarity_print_option ) {
173 if ( !pairwise_similarities_output_file_str.endsWith( ".html" ) ) {
174 pairwise_similarities_output_file_str += ".html";
178 final String error = ForesterUtil
179 .isWritableFile( new File( outdir == null ? pairwise_similarities_output_file_str
180 : outdir + ForesterUtil.FILE_SEPARATOR + pairwise_similarities_output_file_str ) );
181 if ( !ForesterUtil.isEmpty( error ) ) {
182 ForesterUtil.fatalError( surfacing.PRG_NAME, error );
188 public static void collectChangedDomainCombinationsFromBinaryStatesMatrixAsListToFile( final CharacterStateMatrix<CharacterStateMatrix.GainLossStates> matrix,
189 final BinaryDomainCombination.DomainCombinationType dc_type,
190 final List<BinaryDomainCombination> all_binary_domains_combination_gained,
191 final boolean get_gains ) {
192 final SortedSet<String> sorted_ids = new TreeSet<String>();
193 for( int i = 0; i < matrix.getNumberOfIdentifiers(); ++i ) {
194 sorted_ids.add( matrix.getIdentifier( i ) );
196 for( final String id : sorted_ids ) {
197 for( int c = 0; c < matrix.getNumberOfCharacters(); ++c ) {
198 if ( ( get_gains && ( matrix.getState( id, c ) == CharacterStateMatrix.GainLossStates.GAIN ) )
200 && ( matrix.getState( id, c ) == CharacterStateMatrix.GainLossStates.LOSS ) ) ) {
201 if ( dc_type == BinaryDomainCombination.DomainCombinationType.DIRECTED_ADJACTANT ) {
202 all_binary_domains_combination_gained.add( AdjactantDirectedBinaryDomainCombination
203 .obtainInstance( matrix.getCharacter( c ) ) );
205 else if ( dc_type == BinaryDomainCombination.DomainCombinationType.DIRECTED ) {
206 all_binary_domains_combination_gained
207 .add( DirectedBinaryDomainCombination.obtainInstance( matrix.getCharacter( c ) ) );
210 all_binary_domains_combination_gained
211 .add( BasicBinaryDomainCombination.obtainInstance( matrix.getCharacter( c ) ) );
218 public static Map<String, List<GoId>> createDomainIdToGoIdMap( final List<PfamToGoMapping> pfam_to_go_mappings ) {
219 final Map<String, List<GoId>> domain_id_to_go_ids_map = new HashMap<String, List<GoId>>( pfam_to_go_mappings
221 for( final PfamToGoMapping pfam_to_go : pfam_to_go_mappings ) {
222 if ( !domain_id_to_go_ids_map.containsKey( pfam_to_go.getKey() ) ) {
223 domain_id_to_go_ids_map.put( pfam_to_go.getKey(), new ArrayList<GoId>() );
225 domain_id_to_go_ids_map.get( pfam_to_go.getKey() ).add( pfam_to_go.getValue() );
227 return domain_id_to_go_ids_map;
230 public static Map<String, Set<String>> createDomainIdToSecondaryFeaturesMap( final File secondary_features_map_file )
232 final BasicTable<String> primary_table = BasicTableParser.parse( secondary_features_map_file, '\t' );
233 final Map<String, Set<String>> map = new TreeMap<String, Set<String>>();
234 for( int r = 0; r < primary_table.getNumberOfRows(); ++r ) {
235 final String domain_id = primary_table.getValue( 0, r );
236 if ( !map.containsKey( domain_id ) ) {
237 map.put( domain_id, new HashSet<String>() );
239 map.get( domain_id ).add( primary_table.getValue( 1, r ) );
244 public static Phylogeny createNjTreeBasedOnMatrixToFile( final File nj_tree_outfile,
245 final DistanceMatrix distance ) {
246 checkForOutputFileWriteability( nj_tree_outfile );
247 final NeighborJoining nj = NeighborJoining.createInstance();
248 final Phylogeny phylogeny = nj.execute( ( DistanceMatrix ) distance );
249 phylogeny.setName( nj_tree_outfile.getName() );
250 writePhylogenyToFile( phylogeny, nj_tree_outfile.toString() );
254 public static StringBuilder createParametersAsString( final boolean ignore_dufs,
255 final double ie_value_max,
256 final double fs_e_value_max,
257 final int max_allowed_overlap,
258 final boolean no_engulfing_overlaps,
259 final File cutoff_scores_file,
260 final BinaryDomainCombination.DomainCombinationType dc_type ) {
261 final StringBuilder parameters_sb = new StringBuilder();
262 parameters_sb.append( "iE-value: " + ie_value_max );
263 parameters_sb.append( ", FS E-value: " + fs_e_value_max );
264 if ( cutoff_scores_file != null ) {
265 parameters_sb.append( ", Cutoff-scores-file: " + cutoff_scores_file );
268 parameters_sb.append( ", Cutoff-scores-file: not-set" );
270 if ( max_allowed_overlap != surfacing.MAX_ALLOWED_OVERLAP_DEFAULT ) {
271 parameters_sb.append( ", Max-overlap: " + max_allowed_overlap );
274 parameters_sb.append( ", Max-overlap: not-set" );
276 if ( no_engulfing_overlaps ) {
277 parameters_sb.append( ", Engulfing-overlaps: not-allowed" );
280 parameters_sb.append( ", Engulfing-overlaps: allowed" );
283 parameters_sb.append( ", Ignore-dufs: true" );
286 parameters_sb.append( ", Ignore-dufs: false" );
288 parameters_sb.append( ", DC type (if applicable): " + dc_type );
289 return parameters_sb;
292 public static void createSplitWriters( final File out_dir,
293 final String my_outfile,
294 final Map<Character, Writer> split_writers )
296 split_writers.put( 'a',
297 new BufferedWriter( new FileWriter( out_dir + ForesterUtil.FILE_SEPARATOR + my_outfile
298 + "_domains_A.html" ) ) );
299 split_writers.put( 'b',
300 new BufferedWriter( new FileWriter( out_dir + ForesterUtil.FILE_SEPARATOR + my_outfile
301 + "_domains_B.html" ) ) );
302 split_writers.put( 'c',
303 new BufferedWriter( new FileWriter( out_dir + ForesterUtil.FILE_SEPARATOR + my_outfile
304 + "_domains_C.html" ) ) );
305 split_writers.put( 'd',
306 new BufferedWriter( new FileWriter( out_dir + ForesterUtil.FILE_SEPARATOR + my_outfile
307 + "_domains_D.html" ) ) );
308 split_writers.put( 'e',
309 new BufferedWriter( new FileWriter( out_dir + ForesterUtil.FILE_SEPARATOR + my_outfile
310 + "_domains_E.html" ) ) );
311 split_writers.put( 'f',
312 new BufferedWriter( new FileWriter( out_dir + ForesterUtil.FILE_SEPARATOR + my_outfile
313 + "_domains_F.html" ) ) );
314 split_writers.put( 'g',
315 new BufferedWriter( new FileWriter( out_dir + ForesterUtil.FILE_SEPARATOR + my_outfile
316 + "_domains_G.html" ) ) );
317 split_writers.put( 'h',
318 new BufferedWriter( new FileWriter( out_dir + ForesterUtil.FILE_SEPARATOR + my_outfile
319 + "_domains_H.html" ) ) );
320 split_writers.put( 'i',
321 new BufferedWriter( new FileWriter( out_dir + ForesterUtil.FILE_SEPARATOR + my_outfile
322 + "_domains_I.html" ) ) );
323 split_writers.put( 'j',
324 new BufferedWriter( new FileWriter( out_dir + ForesterUtil.FILE_SEPARATOR + my_outfile
325 + "_domains_J.html" ) ) );
326 split_writers.put( 'k',
327 new BufferedWriter( new FileWriter( out_dir + ForesterUtil.FILE_SEPARATOR + my_outfile
328 + "_domains_K.html" ) ) );
329 split_writers.put( 'l',
330 new BufferedWriter( new FileWriter( out_dir + ForesterUtil.FILE_SEPARATOR + my_outfile
331 + "_domains_L.html" ) ) );
332 split_writers.put( 'm',
333 new BufferedWriter( new FileWriter( out_dir + ForesterUtil.FILE_SEPARATOR + my_outfile
334 + "_domains_M.html" ) ) );
335 split_writers.put( 'n',
336 new BufferedWriter( new FileWriter( out_dir + ForesterUtil.FILE_SEPARATOR + my_outfile
337 + "_domains_N.html" ) ) );
338 split_writers.put( 'o',
339 new BufferedWriter( new FileWriter( out_dir + ForesterUtil.FILE_SEPARATOR + my_outfile
340 + "_domains_O.html" ) ) );
341 split_writers.put( 'p',
342 new BufferedWriter( new FileWriter( out_dir + ForesterUtil.FILE_SEPARATOR + my_outfile
343 + "_domains_P.html" ) ) );
344 split_writers.put( 'q',
345 new BufferedWriter( new FileWriter( out_dir + ForesterUtil.FILE_SEPARATOR + my_outfile
346 + "_domains_Q.html" ) ) );
347 split_writers.put( 'r',
348 new BufferedWriter( new FileWriter( out_dir + ForesterUtil.FILE_SEPARATOR + my_outfile
349 + "_domains_R.html" ) ) );
350 split_writers.put( 's',
351 new BufferedWriter( new FileWriter( out_dir + ForesterUtil.FILE_SEPARATOR + my_outfile
352 + "_domains_S.html" ) ) );
353 split_writers.put( 't',
354 new BufferedWriter( new FileWriter( out_dir + ForesterUtil.FILE_SEPARATOR + my_outfile
355 + "_domains_T.html" ) ) );
356 split_writers.put( 'u',
357 new BufferedWriter( new FileWriter( out_dir + ForesterUtil.FILE_SEPARATOR + my_outfile
358 + "_domains_U.html" ) ) );
359 split_writers.put( 'v',
360 new BufferedWriter( new FileWriter( out_dir + ForesterUtil.FILE_SEPARATOR + my_outfile
361 + "_domains_V.html" ) ) );
362 split_writers.put( 'w',
363 new BufferedWriter( new FileWriter( out_dir + ForesterUtil.FILE_SEPARATOR + my_outfile
364 + "_domains_W.html" ) ) );
365 split_writers.put( 'x',
366 new BufferedWriter( new FileWriter( out_dir + ForesterUtil.FILE_SEPARATOR + my_outfile
367 + "_domains_X.html" ) ) );
368 split_writers.put( 'y',
369 new BufferedWriter( new FileWriter( out_dir + ForesterUtil.FILE_SEPARATOR + my_outfile
370 + "_domains_Y.html" ) ) );
371 split_writers.put( 'z',
372 new BufferedWriter( new FileWriter( out_dir + ForesterUtil.FILE_SEPARATOR + my_outfile
373 + "_domains_Z.html" ) ) );
374 split_writers.put( '0',
375 new BufferedWriter( new FileWriter( out_dir + ForesterUtil.FILE_SEPARATOR + my_outfile
376 + "_domains_0.html" ) ) );
379 public static Map<String, Integer> createTaxCodeToIdMap( final Phylogeny phy ) {
380 final Map<String, Integer> m = new HashMap<String, Integer>();
381 for( final PhylogenyNodeIterator iter = phy.iteratorExternalForward(); iter.hasNext(); ) {
382 final PhylogenyNode n = iter.next();
383 if ( n.getNodeData().isHasTaxonomy() ) {
384 final Taxonomy t = n.getNodeData().getTaxonomy();
385 final String c = t.getTaxonomyCode();
386 if ( !ForesterUtil.isEmpty( c ) ) {
387 if ( n.getNodeData().getTaxonomy() == null ) {
388 ForesterUtil.fatalError( surfacing.PRG_NAME, "no taxonomy id for node " + n );
390 final String id = n.getNodeData().getTaxonomy().getIdentifier().getValue();
391 if ( ForesterUtil.isEmpty( id ) ) {
392 ForesterUtil.fatalError( surfacing.PRG_NAME, "no taxonomy id for node " + n );
394 if ( m.containsKey( c ) ) {
395 ForesterUtil.fatalError( surfacing.PRG_NAME, "taxonomy code " + c + " is not unique" );
397 final int iid = Integer.valueOf( id );
398 if ( m.containsValue( iid ) ) {
399 ForesterUtil.fatalError( surfacing.PRG_NAME, "taxonomy id " + iid + " is not unique" );
405 ForesterUtil.fatalError( surfacing.PRG_NAME, "no taxonomy for node " + n );
411 public static void decoratePrintableDomainSimilarities( final SortedSet<DomainSimilarity> domain_similarities,
412 final Detailedness detailedness ) {
413 for( final DomainSimilarity domain_similarity : domain_similarities ) {
414 if ( domain_similarity instanceof DomainSimilarity ) {
415 final DomainSimilarity printable_domain_similarity = domain_similarity;
416 printable_domain_similarity.setDetailedness( detailedness );
421 public static void doit( final List<Protein> proteins,
422 final List<String> query_domain_ids_nc_order,
424 final String separator,
425 final String limit_to_species,
426 final Map<String, List<Integer>> average_protein_lengths_by_dc )
428 for( final Protein protein : proteins ) {
429 if ( ForesterUtil.isEmpty( limit_to_species )
430 || protein.getSpecies().getSpeciesId().equalsIgnoreCase( limit_to_species ) ) {
431 if ( protein.contains( query_domain_ids_nc_order, true ) ) {
432 out.write( protein.getSpecies().getSpeciesId() );
433 out.write( separator );
434 out.write( protein.getProteinId().getId() );
435 out.write( separator );
437 final Set<String> visited_domain_ids = new HashSet<String>();
438 boolean first = true;
439 for( final Domain domain : protein.getProteinDomains() ) {
440 if ( !visited_domain_ids.contains( domain.getDomainId() ) ) {
441 visited_domain_ids.add( domain.getDomainId() );
448 out.write( domain.getDomainId() );
450 out.write( "" + domain.getTotalCount() );
455 out.write( separator );
456 if ( !( ForesterUtil.isEmpty( protein.getDescription() )
457 || protein.getDescription().equals( SurfacingConstants.NONE ) ) ) {
458 out.write( protein.getDescription() );
460 out.write( separator );
461 if ( !( ForesterUtil.isEmpty( protein.getAccession() )
462 || protein.getAccession().equals( SurfacingConstants.NONE ) ) ) {
463 out.write( protein.getAccession() );
465 out.write( SurfacingConstants.NL );
472 public static void domainsPerProteinsStatistics( final String genome,
473 final List<Protein> protein_list,
474 final DescriptiveStatistics all_genomes_domains_per_potein_stats,
475 final SortedMap<Integer, Integer> all_genomes_domains_per_potein_histo,
476 final SortedSet<String> domains_which_are_always_single,
477 final SortedSet<String> domains_which_are_sometimes_single_sometimes_not,
478 final SortedSet<String> domains_which_never_single,
479 final Writer writer ) {
480 final DescriptiveStatistics stats = new BasicDescriptiveStatistics();
481 for( final Protein protein : protein_list ) {
482 final int domains = protein.getNumberOfProteinDomains();
483 //System.out.println( domains );
484 stats.addValue( domains );
485 all_genomes_domains_per_potein_stats.addValue( domains );
486 if ( !all_genomes_domains_per_potein_histo.containsKey( domains ) ) {
487 all_genomes_domains_per_potein_histo.put( domains, 1 );
490 all_genomes_domains_per_potein_histo.put( domains,
491 1 + all_genomes_domains_per_potein_histo.get( domains ) );
493 if ( domains == 1 ) {
494 final String domain = protein.getProteinDomain( 0 ).getDomainId();
495 if ( !domains_which_are_sometimes_single_sometimes_not.contains( domain ) ) {
496 if ( domains_which_never_single.contains( domain ) ) {
497 domains_which_never_single.remove( domain );
498 domains_which_are_sometimes_single_sometimes_not.add( domain );
501 domains_which_are_always_single.add( domain );
505 else if ( domains > 1 ) {
506 for( final Domain d : protein.getProteinDomains() ) {
507 final String domain = d.getDomainId();
508 // System.out.println( domain );
509 if ( !domains_which_are_sometimes_single_sometimes_not.contains( domain ) ) {
510 if ( domains_which_are_always_single.contains( domain ) ) {
511 domains_which_are_always_single.remove( domain );
512 domains_which_are_sometimes_single_sometimes_not.add( domain );
515 domains_which_never_single.add( domain );
522 writer.write( genome );
523 writer.write( "\t" );
524 if ( stats.getN() >= 1 ) {
525 writer.write( stats.arithmeticMean() + "" );
526 writer.write( "\t" );
527 if ( stats.getN() >= 2 ) {
528 writer.write( stats.sampleStandardDeviation() + "" );
533 writer.write( "\t" );
534 writer.write( stats.median() + "" );
535 writer.write( "\t" );
536 writer.write( stats.getN() + "" );
537 writer.write( "\t" );
538 writer.write( stats.getMin() + "" );
539 writer.write( "\t" );
540 writer.write( stats.getMax() + "" );
543 writer.write( "\t" );
544 writer.write( "\t" );
545 writer.write( "\t" );
547 writer.write( "\t" );
548 writer.write( "\t" );
550 writer.write( "\n" );
552 catch ( final IOException e ) {
557 public static void executeDomainLengthAnalysis( final String[][] input_file_properties,
558 final int number_of_genomes,
559 final DomainLengthsTable domain_lengths_table,
562 final DecimalFormat df = new DecimalFormat( "#.00" );
563 checkForOutputFileWriteability( outfile );
564 final BufferedWriter out = new BufferedWriter( new FileWriter( outfile ) );
565 out.write( "MEAN BASED STATISTICS PER SPECIES" );
566 out.write( ForesterUtil.LINE_SEPARATOR );
567 out.write( domain_lengths_table.createMeanBasedStatisticsPerSpeciesTable().toString() );
568 out.write( ForesterUtil.LINE_SEPARATOR );
569 out.write( ForesterUtil.LINE_SEPARATOR );
570 final List<DomainLengths> domain_lengths_list = domain_lengths_table.getDomainLengthsList();
571 out.write( "OUTLIER SPECIES PER DOMAIN (Z>=1.5)" );
572 out.write( ForesterUtil.LINE_SEPARATOR );
573 for( final DomainLengths domain_lengths : domain_lengths_list ) {
574 final List<Species> species_list = domain_lengths.getMeanBasedOutlierSpecies( 1.5 );
575 if ( species_list.size() > 0 ) {
576 out.write( domain_lengths.getDomainId() + "\t" );
577 for( final Species species : species_list ) {
578 out.write( species + "\t" );
580 out.write( ForesterUtil.LINE_SEPARATOR );
583 out.write( ForesterUtil.LINE_SEPARATOR );
584 out.write( ForesterUtil.LINE_SEPARATOR );
585 out.write( "OUTLIER SPECIES (Z 1.0)" );
586 out.write( ForesterUtil.LINE_SEPARATOR );
587 final DescriptiveStatistics stats_for_all_species = domain_lengths_table
588 .calculateMeanBasedStatisticsForAllSpecies();
589 out.write( stats_for_all_species.asSummary() );
590 out.write( ForesterUtil.LINE_SEPARATOR );
591 final AsciiHistogram histo = new AsciiHistogram( stats_for_all_species );
592 out.write( histo.toStringBuffer( 40, '=', 60, 4 ).toString() );
593 out.write( ForesterUtil.LINE_SEPARATOR );
594 final double population_sd = stats_for_all_species.sampleStandardDeviation();
595 final double population_mean = stats_for_all_species.arithmeticMean();
596 for( final Species species : domain_lengths_table.getSpecies() ) {
597 final double x = domain_lengths_table.calculateMeanBasedStatisticsForSpecies( species ).arithmeticMean();
598 final double z = ( x - population_mean ) / population_sd;
599 out.write( species + "\t" + z );
600 out.write( ForesterUtil.LINE_SEPARATOR );
602 out.write( ForesterUtil.LINE_SEPARATOR );
603 for( final Species species : domain_lengths_table.getSpecies() ) {
604 final DescriptiveStatistics stats_for_species = domain_lengths_table
605 .calculateMeanBasedStatisticsForSpecies( species );
606 final double x = stats_for_species.arithmeticMean();
607 final double z = ( x - population_mean ) / population_sd;
608 if ( ( z <= -1.0 ) || ( z >= 1.0 ) ) {
609 out.write( species + "\t" + df.format( z ) + "\t" + stats_for_species.asSummary() );
610 out.write( ForesterUtil.LINE_SEPARATOR );
618 * Warning: This side-effects 'all_bin_domain_combinations_encountered'!
622 * @param all_bin_domain_combinations_changed
623 * @param sum_of_all_domains_encountered
624 * @param all_bin_domain_combinations_encountered
625 * @param is_gains_analysis
626 * @param protein_length_stats_by_dc
627 * @throws IOException
629 public static void executeFitchGainsAnalysis( final File output_file,
630 final List<BinaryDomainCombination> all_bin_domain_combinations_changed,
631 final int sum_of_all_domains_encountered,
632 final SortedSet<BinaryDomainCombination> all_bin_domain_combinations_encountered,
633 final boolean is_gains_analysis )
635 checkForOutputFileWriteability( output_file );
636 final Writer out = ForesterUtil.createBufferedWriter( output_file );
637 final SortedMap<Object, Integer> bdc_to_counts = ForesterUtil
638 .listToSortedCountsMap( all_bin_domain_combinations_changed );
639 final SortedSet<String> all_domains_in_combination_changed_more_than_once = new TreeSet<String>();
640 final SortedSet<String> all_domains_in_combination_changed_only_once = new TreeSet<String>();
643 for( final Object bdc_object : bdc_to_counts.keySet() ) {
644 final BinaryDomainCombination bdc = ( BinaryDomainCombination ) bdc_object;
645 final int count = bdc_to_counts.get( bdc_object );
647 ForesterUtil.unexpectedFatalError( surfacing.PRG_NAME, "count < 1 " );
649 out.write( bdc + "\t" + count + ForesterUtil.LINE_SEPARATOR );
651 all_domains_in_combination_changed_more_than_once.add( bdc.getId0() );
652 all_domains_in_combination_changed_more_than_once.add( bdc.getId1() );
655 else if ( count == 1 ) {
656 all_domains_in_combination_changed_only_once.add( bdc.getId0() );
657 all_domains_in_combination_changed_only_once.add( bdc.getId1() );
661 final int all = all_bin_domain_combinations_encountered.size();
663 if ( !is_gains_analysis ) {
664 all_bin_domain_combinations_encountered.removeAll( all_bin_domain_combinations_changed );
665 never_lost = all_bin_domain_combinations_encountered.size();
666 for( final BinaryDomainCombination bdc : all_bin_domain_combinations_encountered ) {
667 out.write( bdc + "\t" + "0" + ForesterUtil.LINE_SEPARATOR );
670 if ( is_gains_analysis ) {
671 out.write( "Sum of all distinct domain combinations appearing once : " + one
672 + ForesterUtil.LINE_SEPARATOR );
673 out.write( "Sum of all distinct domain combinations appearing more than once : " + above_one
674 + ForesterUtil.LINE_SEPARATOR );
675 out.write( "Sum of all distinct domains in combinations apppearing only once : "
676 + all_domains_in_combination_changed_only_once.size() + ForesterUtil.LINE_SEPARATOR );
677 out.write( "Sum of all distinct domains in combinations apppearing more than once: "
678 + all_domains_in_combination_changed_more_than_once.size() + ForesterUtil.LINE_SEPARATOR );
681 out.write( "Sum of all distinct domain combinations never lost : " + never_lost
682 + ForesterUtil.LINE_SEPARATOR );
683 out.write( "Sum of all distinct domain combinations lost once : " + one
684 + ForesterUtil.LINE_SEPARATOR );
685 out.write( "Sum of all distinct domain combinations lost more than once : " + above_one
686 + ForesterUtil.LINE_SEPARATOR );
687 out.write( "Sum of all distinct domains in combinations lost only once : "
688 + all_domains_in_combination_changed_only_once.size() + ForesterUtil.LINE_SEPARATOR );
689 out.write( "Sum of all distinct domains in combinations lost more than once: "
690 + all_domains_in_combination_changed_more_than_once.size() + ForesterUtil.LINE_SEPARATOR );
692 out.write( "All binary combinations : " + all
693 + ForesterUtil.LINE_SEPARATOR );
694 out.write( "All domains : "
695 + sum_of_all_domains_encountered );
698 .programMessage( surfacing.PRG_NAME,
699 "Wrote fitch domain combination dynamics counts analysis to \"" + output_file + "\"" );
704 * @param all_binary_domains_combination_lost_fitch
705 * @param use_last_in_fitch_parsimony
706 * @param perform_dc_fich
707 * @param consider_directedness_and_adjacency_for_bin_combinations
708 * @param all_binary_domains_combination_gained if null ignored, otherwise this is to list all binary domain combinations
709 * which were gained under unweighted (Fitch) parsimony.
711 public static void executeParsimonyAnalysis( final long random_number_seed_for_fitch_parsimony,
712 final boolean radomize_fitch_parsimony,
713 final String outfile_name,
714 final DomainParsimonyCalculator domain_parsimony,
715 final Phylogeny phylogeny,
716 final Map<String, List<GoId>> domain_id_to_go_ids_map,
717 final Map<GoId, GoTerm> go_id_to_term_map,
718 final GoNameSpace go_namespace_limit,
719 final String parameters_str,
720 final Map<String, Set<String>>[] domain_id_to_secondary_features_maps,
721 final SortedSet<String> positive_filter,
722 final boolean output_binary_domain_combinations_for_graphs,
723 final List<BinaryDomainCombination> all_binary_domains_combination_gained_fitch,
724 final List<BinaryDomainCombination> all_binary_domains_combination_lost_fitch,
725 final BinaryDomainCombination.DomainCombinationType dc_type,
726 final Map<String, DescriptiveStatistics> protein_length_stats_by_dc,
727 final Map<String, DescriptiveStatistics> domain_number_stats_by_dc,
728 final Map<String, DescriptiveStatistics> domain_length_stats_by_domain,
729 final Map<String, Integer> tax_code_to_id_map,
730 final boolean write_to_nexus,
731 final boolean use_last_in_fitch_parsimony,
732 final boolean perform_dc_fich ) {
733 final String sep = ForesterUtil.LINE_SEPARATOR + "###################" + ForesterUtil.LINE_SEPARATOR;
734 final String date_time = ForesterUtil.getCurrentDateTime();
735 final SortedSet<String> all_pfams_encountered = new TreeSet<String>();
736 final SortedSet<String> all_pfams_gained_as_domains = new TreeSet<String>();
737 final SortedSet<String> all_pfams_lost_as_domains = new TreeSet<String>();
738 final SortedSet<String> all_pfams_gained_as_dom_combinations = new TreeSet<String>();
739 final SortedSet<String> all_pfams_lost_as_dom_combinations = new TreeSet<String>();
740 if ( write_to_nexus ) {
741 writeToNexus( outfile_name, domain_parsimony, phylogeny );
745 Phylogeny local_phylogeny_l = phylogeny.copy();
746 if ( ( positive_filter != null ) && ( positive_filter.size() > 0 ) ) {
747 domain_parsimony.executeDolloParsimonyOnDomainPresence( positive_filter );
750 domain_parsimony.executeDolloParsimonyOnDomainPresence();
752 SurfacingUtil.writeMatrixToFile( domain_parsimony.getGainLossMatrix(),
753 outfile_name + surfacing.PARSIMONY_OUTPUT_GL_SUFFIX_DOLLO_DOMAINS,
755 SurfacingUtil.writeMatrixToFile( domain_parsimony.getGainLossCountsMatrix(),
756 outfile_name + surfacing.PARSIMONY_OUTPUT_GL_COUNTS_SUFFIX_DOLLO_DOMAINS,
758 SurfacingUtil.writeBinaryStatesMatrixAsListToFile( domain_parsimony.getGainLossMatrix(),
759 CharacterStateMatrix.GainLossStates.GAIN,
760 outfile_name + surfacing.PARSIMONY_OUTPUT_DOLLO_GAINS_D,
762 ForesterUtil.LINE_SEPARATOR,
764 SurfacingUtil.writeBinaryStatesMatrixAsListToFile( domain_parsimony.getGainLossMatrix(),
765 CharacterStateMatrix.GainLossStates.LOSS,
766 outfile_name + surfacing.PARSIMONY_OUTPUT_DOLLO_LOSSES_D,
768 ForesterUtil.LINE_SEPARATOR,
770 SurfacingUtil.writeBinaryStatesMatrixAsListToFile( domain_parsimony.getGainLossMatrix(),
772 outfile_name + surfacing.PARSIMONY_OUTPUT_DOLLO_PRESENT_D,
774 ForesterUtil.LINE_SEPARATOR,
777 writeBinaryStatesMatrixToList( domain_id_to_go_ids_map,
781 domain_parsimony.getGainLossMatrix(),
782 CharacterStateMatrix.GainLossStates.GAIN,
783 outfile_name + surfacing.PARSIMONY_OUTPUT_DOLLO_GAINS_HTML_D,
785 ForesterUtil.LINE_SEPARATOR,
786 "Dollo Parsimony | Gains | Domains",
788 domain_id_to_secondary_features_maps,
789 all_pfams_encountered,
790 all_pfams_gained_as_domains,
792 tax_code_to_id_map );
793 writeBinaryStatesMatrixToList( domain_id_to_go_ids_map,
797 domain_parsimony.getGainLossMatrix(),
798 CharacterStateMatrix.GainLossStates.LOSS,
799 outfile_name + surfacing.PARSIMONY_OUTPUT_DOLLO_LOSSES_HTML_D,
801 ForesterUtil.LINE_SEPARATOR,
802 "Dollo Parsimony | Losses | Domains",
804 domain_id_to_secondary_features_maps,
805 all_pfams_encountered,
806 all_pfams_lost_as_domains,
808 tax_code_to_id_map );
809 writeBinaryStatesMatrixToList( domain_id_to_go_ids_map,
813 domain_parsimony.getGainLossMatrix(),
815 outfile_name + surfacing.PARSIMONY_OUTPUT_DOLLO_PRESENT_HTML_D,
817 ForesterUtil.LINE_SEPARATOR,
818 "Dollo Parsimony | Present | Domains",
820 domain_id_to_secondary_features_maps,
821 all_pfams_encountered,
824 tax_code_to_id_map );
825 preparePhylogeny( local_phylogeny_l,
828 "Dollo parsimony on domain presence/absence",
829 "dollo_on_domains_" + outfile_name,
831 SurfacingUtil.writePhylogenyToFile( local_phylogeny_l,
832 outfile_name + surfacing.DOMAINS_PARSIMONY_TREE_OUTPUT_SUFFIX_DOLLO );
834 writeAllDomainsChangedOnAllSubtrees( local_phylogeny_l, true, outfile_name, "_dollo_all_gains_d" );
835 writeAllDomainsChangedOnAllSubtrees( local_phylogeny_l, false, outfile_name, "_dollo_all_losses_d" );
837 catch ( final IOException e ) {
839 ForesterUtil.fatalError( surfacing.PRG_NAME, e.getLocalizedMessage() );
841 if ( perform_dc_fich && ( domain_parsimony.calculateNumberOfBinaryDomainCombination() > 0 ) ) {
842 // FITCH DOMAIN COMBINATIONS
843 // -------------------------
844 local_phylogeny_l = phylogeny.copy();
845 String randomization = "no";
846 if ( radomize_fitch_parsimony ) {
848 .executeFitchParsimonyOnBinaryDomainCombintion( random_number_seed_for_fitch_parsimony );
849 randomization = "yes, seed = " + random_number_seed_for_fitch_parsimony;
852 domain_parsimony.executeFitchParsimonyOnBinaryDomainCombintion( use_last_in_fitch_parsimony );
854 SurfacingUtil.writeMatrixToFile( domain_parsimony.getGainLossMatrix(),
856 + surfacing.PARSIMONY_OUTPUT_GL_SUFFIX_FITCH_BINARY_COMBINATIONS,
858 SurfacingUtil.writeMatrixToFile( domain_parsimony.getGainLossCountsMatrix(),
860 + surfacing.PARSIMONY_OUTPUT_GL_COUNTS_SUFFIX_FITCH_BINARY_COMBINATIONS,
862 SurfacingUtil.writeBinaryStatesMatrixAsListToFile( domain_parsimony.getGainLossMatrix(),
863 CharacterStateMatrix.GainLossStates.GAIN,
864 outfile_name + surfacing.PARSIMONY_OUTPUT_FITCH_GAINS_BC,
866 ForesterUtil.LINE_SEPARATOR,
868 SurfacingUtil.writeBinaryStatesMatrixAsListToFile( domain_parsimony.getGainLossMatrix(),
869 CharacterStateMatrix.GainLossStates.LOSS,
871 + surfacing.PARSIMONY_OUTPUT_FITCH_LOSSES_BC,
873 ForesterUtil.LINE_SEPARATOR,
875 SurfacingUtil.writeBinaryStatesMatrixAsListToFile( domain_parsimony.getGainLossMatrix(),
878 + surfacing.PARSIMONY_OUTPUT_FITCH_PRESENT_BC,
880 ForesterUtil.LINE_SEPARATOR,
882 if ( all_binary_domains_combination_gained_fitch != null ) {
883 collectChangedDomainCombinationsFromBinaryStatesMatrixAsListToFile( domain_parsimony
884 .getGainLossMatrix(), dc_type, all_binary_domains_combination_gained_fitch, true );
886 if ( all_binary_domains_combination_lost_fitch != null ) {
887 collectChangedDomainCombinationsFromBinaryStatesMatrixAsListToFile( domain_parsimony
888 .getGainLossMatrix(), dc_type, all_binary_domains_combination_lost_fitch, false );
890 if ( output_binary_domain_combinations_for_graphs ) {
891 SurfacingUtil.writeBinaryStatesMatrixAsListToFileForBinaryCombinationsForGraphAnalysis( domain_parsimony
892 .getGainLossMatrix(),
895 + surfacing.PARSIMONY_OUTPUT_FITCH_PRESENT_BC_OUTPUTFILE_SUFFIX_FOR_GRAPH_ANALYSIS,
897 ForesterUtil.LINE_SEPARATOR,
898 BinaryDomainCombination.OutputFormat.DOT );
901 writeBinaryStatesMatrixToList( domain_id_to_go_ids_map,
905 domain_parsimony.getGainLossMatrix(),
906 CharacterStateMatrix.GainLossStates.GAIN,
907 outfile_name + surfacing.PARSIMONY_OUTPUT_FITCH_GAINS_HTML_BC,
909 ForesterUtil.LINE_SEPARATOR,
910 "Fitch Parsimony | Gains | Domain Combinations",
913 all_pfams_encountered,
914 all_pfams_gained_as_dom_combinations,
916 tax_code_to_id_map );
917 writeBinaryStatesMatrixToList( domain_id_to_go_ids_map,
921 domain_parsimony.getGainLossMatrix(),
922 CharacterStateMatrix.GainLossStates.LOSS,
923 outfile_name + surfacing.PARSIMONY_OUTPUT_FITCH_LOSSES_HTML_BC,
925 ForesterUtil.LINE_SEPARATOR,
926 "Fitch Parsimony | Losses | Domain Combinations",
929 all_pfams_encountered,
930 all_pfams_lost_as_dom_combinations,
932 tax_code_to_id_map );
933 // writeBinaryStatesMatrixToList( domain_id_to_go_ids_map,
934 // go_id_to_term_map,
935 // go_namespace_limit,
937 // domain_parsimony.getGainLossMatrix(),
939 // outfile_name + surfacing.PARSIMONY_OUTPUT_FITCH_PRESENT_HTML_BC,
941 // ForesterUtil.LINE_SEPARATOR,
942 // "Fitch Parsimony | Present | Domain Combinations",
945 // all_pfams_encountered,
947 // "_fitch_present_dc",
948 // tax_code_to_id_map );
949 writeAllEncounteredPfamsToFile( domain_id_to_go_ids_map,
952 all_pfams_encountered );
953 writePfamsToFile( outfile_name + surfacing.ALL_PFAMS_GAINED_AS_DOMAINS_SUFFIX,
954 all_pfams_gained_as_domains );
955 writePfamsToFile( outfile_name + surfacing.ALL_PFAMS_LOST_AS_DOMAINS_SUFFIX, all_pfams_lost_as_domains );
956 writePfamsToFile( outfile_name + surfacing.ALL_PFAMS_GAINED_AS_DC_SUFFIX,
957 all_pfams_gained_as_dom_combinations );
958 writePfamsToFile( outfile_name + surfacing.ALL_PFAMS_LOST_AS_DC_SUFFIX,
959 all_pfams_lost_as_dom_combinations );
960 preparePhylogeny( local_phylogeny_l,
963 "Fitch parsimony on binary domain combination presence/absence randomization: "
965 "fitch_on_binary_domain_combinations_" + outfile_name,
968 .writePhylogenyToFile( local_phylogeny_l,
970 + surfacing.BINARY_DOMAIN_COMBINATIONS_PARSIMONY_TREE_OUTPUT_SUFFIX_FITCH );
971 calculateIndependentDomainCombinationGains( local_phylogeny_l,
973 + surfacing.INDEPENDENT_DC_GAINS_FITCH_PARS_COUNTS_OUTPUT_SUFFIX,
975 + surfacing.INDEPENDENT_DC_GAINS_FITCH_PARS_DC_OUTPUT_SUFFIX,
977 + surfacing.INDEPENDENT_DC_GAINS_FITCH_PARS_DC_FOR_GO_MAPPING_OUTPUT_SUFFIX,
979 + surfacing.INDEPENDENT_DC_GAINS_FITCH_PARS_DC_FOR_GO_MAPPING_OUTPUT_UNIQUE_SUFFIX,
980 outfile_name + "_indep_dc_gains_fitch_lca_ranks.txt",
981 outfile_name + "_indep_dc_gains_fitch_lca_taxonomies.txt",
982 outfile_name + "_indep_dc_gains_fitch_protein_statistics.txt",
983 protein_length_stats_by_dc,
984 domain_number_stats_by_dc,
985 domain_length_stats_by_domain );
989 public static void executeParsimonyAnalysisForSecondaryFeatures( final String outfile_name,
990 final DomainParsimonyCalculator secondary_features_parsimony,
991 final Phylogeny phylogeny,
992 final String parameters_str,
993 final Map<Species, MappingResults> mapping_results_map,
994 final boolean use_last_in_fitch_parsimony ) {
995 final String sep = ForesterUtil.LINE_SEPARATOR + "###################" + ForesterUtil.LINE_SEPARATOR;
996 final String date_time = ForesterUtil.getCurrentDateTime();
997 System.out.println();
998 writeToNexus( outfile_name + surfacing.NEXUS_SECONDARY_FEATURES,
999 secondary_features_parsimony.createMatrixOfSecondaryFeaturePresenceOrAbsence( null ),
1001 Phylogeny local_phylogeny_copy = phylogeny.copy();
1002 secondary_features_parsimony.executeDolloParsimonyOnSecondaryFeatures( mapping_results_map );
1003 SurfacingUtil.writeMatrixToFile( secondary_features_parsimony.getGainLossMatrix(),
1004 outfile_name + surfacing.PARSIMONY_OUTPUT_GL_SUFFIX_DOLLO_SECONDARY_FEATURES,
1006 SurfacingUtil.writeMatrixToFile( secondary_features_parsimony.getGainLossCountsMatrix(),
1008 + surfacing.PARSIMONY_OUTPUT_GL_COUNTS_SUFFIX_DOLLO_SECONDARY_FEATURES,
1010 SurfacingUtil.writeBinaryStatesMatrixAsListToFile( secondary_features_parsimony.getGainLossMatrix(),
1011 CharacterStateMatrix.GainLossStates.GAIN,
1013 + surfacing.PARSIMONY_OUTPUT_DOLLO_GAINS_SECONDARY_FEATURES,
1015 ForesterUtil.LINE_SEPARATOR,
1017 SurfacingUtil.writeBinaryStatesMatrixAsListToFile( secondary_features_parsimony.getGainLossMatrix(),
1018 CharacterStateMatrix.GainLossStates.LOSS,
1020 + surfacing.PARSIMONY_OUTPUT_DOLLO_LOSSES_SECONDARY_FEATURES,
1022 ForesterUtil.LINE_SEPARATOR,
1024 SurfacingUtil.writeBinaryStatesMatrixAsListToFile( secondary_features_parsimony.getGainLossMatrix(),
1027 + surfacing.PARSIMONY_OUTPUT_DOLLO_PRESENT_SECONDARY_FEATURES,
1029 ForesterUtil.LINE_SEPARATOR,
1031 preparePhylogeny( local_phylogeny_copy,
1032 secondary_features_parsimony,
1034 "Dollo parsimony on secondary feature presence/absence",
1035 "dollo_on_secondary_features_" + outfile_name,
1038 .writePhylogenyToFile( local_phylogeny_copy,
1039 outfile_name + surfacing.SECONDARY_FEATURES_PARSIMONY_TREE_OUTPUT_SUFFIX_DOLLO );
1040 // FITCH DOMAIN COMBINATIONS
1041 // -------------------------
1042 local_phylogeny_copy = phylogeny.copy();
1043 final String randomization = "no";
1044 secondary_features_parsimony
1045 .executeFitchParsimonyOnBinaryDomainCombintionOnSecondaryFeatures( use_last_in_fitch_parsimony );
1046 preparePhylogeny( local_phylogeny_copy,
1047 secondary_features_parsimony,
1049 "Fitch parsimony on secondary binary domain combination presence/absence randomization: "
1051 "fitch_on_binary_domain_combinations_" + outfile_name,
1054 .writePhylogenyToFile( local_phylogeny_copy,
1056 + surfacing.BINARY_DOMAIN_COMBINATIONS_PARSIMONY_TREE_OUTPUT_SUFFIX_FITCH_MAPPED );
1057 calculateIndependentDomainCombinationGains( local_phylogeny_copy,
1059 + surfacing.INDEPENDENT_DC_GAINS_FITCH_PARS_COUNTS_MAPPED_OUTPUT_SUFFIX,
1061 + surfacing.INDEPENDENT_DC_GAINS_FITCH_PARS_DC_MAPPED_OUTPUT_SUFFIX,
1063 + surfacing.INDEPENDENT_DC_GAINS_FITCH_PARS_DC_FOR_GO_MAPPING_MAPPED_OUTPUT_SUFFIX,
1065 + surfacing.INDEPENDENT_DC_GAINS_FITCH_PARS_DC_FOR_GO_MAPPING_MAPPED_OUTPUT_UNIQUE_SUFFIX,
1066 outfile_name + "_MAPPED_indep_dc_gains_fitch_lca_ranks.txt",
1067 outfile_name + "_MAPPED_indep_dc_gains_fitch_lca_taxonomies.txt",
1074 public static void executePlusMinusAnalysis( final File output_file,
1075 final List<String> plus_minus_analysis_high_copy_base,
1076 final List<String> plus_minus_analysis_high_copy_target,
1077 final List<String> plus_minus_analysis_low_copy,
1078 final List<GenomeWideCombinableDomains> gwcd_list,
1079 final SortedMap<Species, List<Protein>> protein_lists_per_species,
1080 final Map<String, List<GoId>> domain_id_to_go_ids_map,
1081 final Map<GoId, GoTerm> go_id_to_term_map,
1082 final List<Object> plus_minus_analysis_numbers ) {
1083 final Set<String> all_spec = new HashSet<String>();
1084 for( final GenomeWideCombinableDomains gwcd : gwcd_list ) {
1085 all_spec.add( gwcd.getSpecies().getSpeciesId() );
1087 final File html_out_dom = new File( output_file + surfacing.PLUS_MINUS_DOM_SUFFIX_HTML );
1088 final File plain_out_dom = new File( output_file + surfacing.PLUS_MINUS_DOM_SUFFIX );
1089 final File html_out_dc = new File( output_file + surfacing.PLUS_MINUS_DC_SUFFIX_HTML );
1090 final File all_domains_go_ids_out_dom = new File( output_file + surfacing.PLUS_MINUS_ALL_GO_IDS_DOM_SUFFIX );
1091 final File passing_domains_go_ids_out_dom = new File( output_file
1092 + surfacing.PLUS_MINUS_PASSING_GO_IDS_DOM_SUFFIX );
1093 final File proteins_file_base = new File( output_file + "" );
1094 final int min_diff = ( ( Integer ) plus_minus_analysis_numbers.get( 0 ) ).intValue();
1095 final double factor = ( ( Double ) plus_minus_analysis_numbers.get( 1 ) ).doubleValue();
1097 DomainCountsDifferenceUtil.calculateCopyNumberDifferences( gwcd_list,
1098 protein_lists_per_species,
1099 plus_minus_analysis_high_copy_base,
1100 plus_minus_analysis_high_copy_target,
1101 plus_minus_analysis_low_copy,
1107 domain_id_to_go_ids_map,
1109 all_domains_go_ids_out_dom,
1110 passing_domains_go_ids_out_dom,
1111 proteins_file_base );
1113 catch ( final IOException e ) {
1114 ForesterUtil.fatalError( surfacing.PRG_NAME, e.getLocalizedMessage() );
1116 ForesterUtil.programMessage( surfacing.PRG_NAME,
1117 "Wrote plus minus domain analysis results to \"" + html_out_dom + "\"" );
1118 ForesterUtil.programMessage( surfacing.PRG_NAME,
1119 "Wrote plus minus domain analysis results to \"" + plain_out_dom + "\"" );
1120 ForesterUtil.programMessage( surfacing.PRG_NAME,
1121 "Wrote plus minus domain analysis results to \"" + html_out_dc + "\"" );
1122 ForesterUtil.programMessage( surfacing.PRG_NAME,
1123 "Wrote plus minus domain analysis based passing GO ids to \""
1124 + passing_domains_go_ids_out_dom + "\"" );
1125 ForesterUtil.programMessage( surfacing.PRG_NAME,
1126 "Wrote plus minus domain analysis based all GO ids to \""
1127 + all_domains_go_ids_out_dom + "\"" );
1130 public static void extractProteinNames( final List<Protein> proteins,
1131 final List<String> query_domain_ids_nc_order,
1133 final String separator,
1134 final String limit_to_species )
1135 throws IOException {
1136 for( final Protein protein : proteins ) {
1137 if ( ForesterUtil.isEmpty( limit_to_species )
1138 || protein.getSpecies().getSpeciesId().equalsIgnoreCase( limit_to_species ) ) {
1139 if ( protein.contains( query_domain_ids_nc_order, true ) ) {
1140 out.write( protein.getSpecies().getSpeciesId() );
1141 out.write( separator );
1142 out.write( protein.getProteinId().getId() );
1143 out.write( separator );
1145 final Set<String> visited_domain_ids = new HashSet<String>();
1146 boolean first = true;
1147 for( final Domain domain : protein.getProteinDomains() ) {
1148 if ( !visited_domain_ids.contains( domain.getDomainId() ) ) {
1149 visited_domain_ids.add( domain.getDomainId() );
1156 out.write( domain.getDomainId() );
1158 out.write( "" + domain.getTotalCount() );
1163 out.write( separator );
1164 if ( !( ForesterUtil.isEmpty( protein.getDescription() )
1165 || protein.getDescription().equals( SurfacingConstants.NONE ) ) ) {
1166 out.write( protein.getDescription() );
1168 out.write( separator );
1169 if ( !( ForesterUtil.isEmpty( protein.getAccession() )
1170 || protein.getAccession().equals( SurfacingConstants.NONE ) ) ) {
1171 out.write( protein.getAccession() );
1173 out.write( SurfacingConstants.NL );
1180 public static void extractProteinNames( final SortedMap<Species, List<Protein>> protein_lists_per_species,
1181 final String domain_id,
1183 final String separator,
1184 final String limit_to_species,
1185 final double domain_e_cutoff )
1186 throws IOException {
1187 //System.out.println( "Per domain E-value: " + domain_e_cutoff );
1188 for( final Species species : protein_lists_per_species.keySet() ) {
1189 //System.out.println( species + ":" );
1190 for( final Protein protein : protein_lists_per_species.get( species ) ) {
1191 if ( ForesterUtil.isEmpty( limit_to_species )
1192 || protein.getSpecies().getSpeciesId().equalsIgnoreCase( limit_to_species ) ) {
1193 final List<Domain> domains = protein.getProteinDomains( domain_id );
1194 if ( domains.size() > 0 ) {
1195 out.write( protein.getSpecies().getSpeciesId() );
1196 out.write( separator );
1197 out.write( protein.getProteinId().getId() );
1198 out.write( separator );
1199 out.write( domain_id.toString() );
1200 out.write( separator );
1202 for( final Domain domain : domains ) {
1203 if ( ( domain_e_cutoff < 0 ) || ( domain.getPerDomainEvalue() <= domain_e_cutoff ) ) {
1205 out.write( domain.getFrom() + "-" + domain.getTo() );
1206 if ( prev_to >= 0 ) {
1207 final int l = domain.getFrom() - prev_to;
1208 // System.out.println( l );
1210 prev_to = domain.getTo();
1214 out.write( separator );
1215 final List<Domain> domain_list = new ArrayList<Domain>();
1216 for( final Domain domain : protein.getProteinDomains() ) {
1217 if ( ( domain_e_cutoff < 0 ) || ( domain.getPerDomainEvalue() <= domain_e_cutoff ) ) {
1218 domain_list.add( domain );
1221 final Domain domain_ary[] = new Domain[ domain_list.size() ];
1222 for( int i = 0; i < domain_list.size(); ++i ) {
1223 domain_ary[ i ] = domain_list.get( i );
1225 Arrays.sort( domain_ary, new DomainComparator( true ) );
1227 boolean first = true;
1228 for( final Domain domain : domain_ary ) {
1235 out.write( domain.getDomainId().toString() );
1236 out.write( ":" + domain.getFrom() + "-" + domain.getTo() );
1237 out.write( ":" + domain.getPerDomainEvalue() );
1240 if ( !( ForesterUtil.isEmpty( protein.getDescription() )
1241 || protein.getDescription().equals( SurfacingConstants.NONE ) ) ) {
1242 out.write( protein.getDescription() );
1244 out.write( separator );
1245 if ( !( ForesterUtil.isEmpty( protein.getAccession() )
1246 || protein.getAccession().equals( SurfacingConstants.NONE ) ) ) {
1247 out.write( protein.getAccession() );
1249 out.write( SurfacingConstants.NL );
1257 public static SortedSet<String> getAllDomainIds( final List<GenomeWideCombinableDomains> gwcd_list ) {
1258 final SortedSet<String> all_domains_ids = new TreeSet<String>();
1259 for( final GenomeWideCombinableDomains gwcd : gwcd_list ) {
1260 final Set<String> all_domains = gwcd.getAllDomainIds();
1261 // for( final Domain domain : all_domains ) {
1262 all_domains_ids.addAll( all_domains );
1265 return all_domains_ids;
1268 public static SortedMap<String, Integer> getDomainCounts( final List<Protein> protein_domain_collections ) {
1269 final SortedMap<String, Integer> map = new TreeMap<String, Integer>();
1270 for( final Protein protein_domain_collection : protein_domain_collections ) {
1271 for( final Object name : protein_domain_collection.getProteinDomains() ) {
1272 final BasicDomain protein_domain = ( BasicDomain ) name;
1273 final String id = protein_domain.getDomainId();
1274 if ( map.containsKey( id ) ) {
1275 map.put( id, map.get( id ) + 1 );
1285 public static int getNumberOfNodesLackingName( final Phylogeny p, final StringBuilder names ) {
1286 final PhylogenyNodeIterator it = p.iteratorPostorder();
1288 while ( it.hasNext() ) {
1289 final PhylogenyNode n = it.next();
1290 if ( ForesterUtil.isEmpty( n.getName() )
1291 && ( !n.getNodeData().isHasTaxonomy()
1292 || ForesterUtil.isEmpty( n.getNodeData().getTaxonomy().getScientificName() ) )
1293 && ( !n.getNodeData().isHasTaxonomy()
1294 || ForesterUtil.isEmpty( n.getNodeData().getTaxonomy().getCommonName() ) ) ) {
1295 if ( n.getParent() != null ) {
1296 names.append( " " );
1297 names.append( n.getParent().getName() );
1299 final List l = n.getAllExternalDescendants();
1300 for( final Object object : l ) {
1301 System.out.println( l.toString() );
1309 public static void log( final String msg, final Writer w ) {
1312 w.write( ForesterUtil.LINE_SEPARATOR );
1314 catch ( final IOException e ) {
1315 ForesterUtil.fatalError( surfacing.PRG_NAME, e.getLocalizedMessage() );
1319 public static Phylogeny[] obtainAndPreProcessIntrees( final File[] intree_files,
1320 final int number_of_genomes,
1321 final String[][] input_file_properties ) {
1322 final Phylogeny[] intrees = new Phylogeny[ intree_files.length ];
1324 for( final File intree_file : intree_files ) {
1325 Phylogeny intree = null;
1326 final String error = ForesterUtil.isReadableFile( intree_file );
1327 if ( !ForesterUtil.isEmpty( error ) ) {
1328 ForesterUtil.fatalError( surfacing.PRG_NAME,
1329 "cannot read input tree file [" + intree_file + "]: " + error );
1332 final Phylogeny[] p_array = ParserBasedPhylogenyFactory.getInstance()
1333 .create( intree_file, ParserUtils.createParserDependingOnFileType( intree_file, true ) );
1334 if ( p_array.length < 1 ) {
1335 ForesterUtil.fatalError( surfacing.PRG_NAME,
1336 "file [" + intree_file
1337 + "] does not contain any phylogeny in phyloXML format" );
1339 else if ( p_array.length > 1 ) {
1340 ForesterUtil.fatalError( surfacing.PRG_NAME,
1341 "file [" + intree_file
1342 + "] contains more than one phylogeny in phyloXML format" );
1344 intree = p_array[ 0 ];
1346 catch ( final Exception e ) {
1347 ForesterUtil.fatalError( surfacing.PRG_NAME,
1348 "failed to read input tree from file [" + intree_file + "]: " + error );
1350 if ( ( intree == null ) || intree.isEmpty() ) {
1351 ForesterUtil.fatalError( surfacing.PRG_NAME, "input tree [" + intree_file + "] is empty" );
1353 if ( !intree.isRooted() ) {
1354 ForesterUtil.fatalError( surfacing.PRG_NAME, "input tree [" + intree_file + "] is not rooted" );
1356 final StringBuilder parent_names = new StringBuilder();
1357 final int nodes_lacking_name = getNumberOfNodesLackingName( intree, parent_names );
1358 if ( nodes_lacking_name > 0 ) {
1359 ForesterUtil.fatalError( surfacing.PRG_NAME,
1360 "input tree [" + intree_file + "] has " + nodes_lacking_name
1361 + " node(s) lacking a name [parent names:" + parent_names + "]" );
1363 preparePhylogenyForParsimonyAnalyses( intree, input_file_properties );
1364 if ( !intree.isCompletelyBinary() ) {
1365 ForesterUtil.printWarningMessage( surfacing.PRG_NAME,
1366 "input tree [" + intree_file + "] is not completely binary" );
1368 intrees[ i++ ] = intree;
1373 public static Phylogeny obtainFirstIntree( final File intree_file ) {
1374 Phylogeny intree = null;
1375 final String error = ForesterUtil.isReadableFile( intree_file );
1376 if ( !ForesterUtil.isEmpty( error ) ) {
1377 ForesterUtil.fatalError( surfacing.PRG_NAME,
1378 "cannot read input tree file [" + intree_file + "]: " + error );
1381 final Phylogeny[] phys = ParserBasedPhylogenyFactory.getInstance()
1382 .create( intree_file, ParserUtils.createParserDependingOnFileType( intree_file, true ) );
1383 if ( phys.length < 1 ) {
1385 .fatalError( surfacing.PRG_NAME,
1386 "file [" + intree_file + "] does not contain any phylogeny in phyloXML format" );
1388 else if ( phys.length > 1 ) {
1390 .fatalError( surfacing.PRG_NAME,
1391 "file [" + intree_file + "] contains more than one phylogeny in phyloXML format" );
1395 catch ( final Exception e ) {
1396 ForesterUtil.fatalError( surfacing.PRG_NAME,
1397 "failed to read input tree from file [" + intree_file + "]: " + error );
1399 if ( ( intree == null ) || intree.isEmpty() ) {
1400 ForesterUtil.fatalError( surfacing.PRG_NAME, "input tree [" + intree_file + "] is empty" );
1402 if ( !intree.isRooted() ) {
1403 ForesterUtil.fatalError( surfacing.PRG_NAME, "input tree [" + intree_file + "] is not rooted" );
1408 public static String obtainHexColorStringDependingOnTaxonomyGroup( final String tax_code, final Phylogeny phy )
1409 throws IllegalArgumentException {
1410 if ( !_TAXCODE_HEXCOLORSTRING_MAP.containsKey( tax_code ) ) {
1411 if ( ( phy != null ) && !phy.isEmpty() ) {
1412 // final List<PhylogenyNode> nodes = phy.getNodesViaTaxonomyCode( tax_code );
1414 // if ( ( nodes == null ) || nodes.isEmpty() ) {
1415 // throw new IllegalArgumentException( "code " + tax_code + " is not found" );
1417 // if ( nodes.size() != 1 ) {
1418 // throw new IllegalArgumentException( "code " + tax_code + " is not unique" );
1420 // PhylogenyNode n = nodes.get( 0 );
1421 // while ( n != null ) {
1422 // if ( n.getNodeData().isHasTaxonomy()
1423 // && !ForesterUtil.isEmpty( n.getNodeData().getTaxonomy().getScientificName() ) ) {
1424 // c = ForesterUtil.obtainColorDependingOnTaxonomyGroup( n.getNodeData().getTaxonomy()
1425 // .getScientificName(), tax_code );
1427 // if ( ( c == null ) && !ForesterUtil.isEmpty( n.getName() ) ) {
1428 // c = ForesterUtil.obtainColorDependingOnTaxonomyGroup( n.getName(), tax_code );
1430 // if ( c != null ) {
1433 // n = n.getParent();
1435 final String group = obtainTaxonomyGroup( tax_code, phy );
1436 final Color c = ForesterUtil.obtainColorDependingOnTaxonomyGroup( group );
1438 throw new IllegalArgumentException( "no color found for taxonomy group \"" + group
1439 + "\" for code \"" + tax_code + "\"" );
1441 final String hex = String.format( "#%02x%02x%02x", c.getRed(), c.getGreen(), c.getBlue() );
1442 _TAXCODE_HEXCOLORSTRING_MAP.put( tax_code, hex );
1445 throw new IllegalArgumentException( "unable to obtain color for code " + tax_code
1446 + " (tree is null or empty and code is not in map)" );
1449 return _TAXCODE_HEXCOLORSTRING_MAP.get( tax_code );
1452 public static String obtainTaxonomyGroup( final String tax_code, final Phylogeny species_tree )
1453 throws IllegalArgumentException {
1454 if ( !_TAXCODE_TAXGROUP_MAP.containsKey( tax_code ) ) {
1455 if ( ( species_tree != null ) && !species_tree.isEmpty() ) {
1456 final List<PhylogenyNode> nodes = species_tree.getNodesViaTaxonomyCode( tax_code );
1457 if ( ( nodes == null ) || nodes.isEmpty() ) {
1458 throw new IllegalArgumentException( "code " + tax_code + " is not found" );
1460 if ( nodes.size() != 1 ) {
1461 throw new IllegalArgumentException( "code " + tax_code + " is not unique" );
1463 PhylogenyNode n = nodes.get( 0 );
1464 String group = null;
1465 while ( n != null ) {
1466 if ( n.getNodeData().isHasTaxonomy()
1467 && !ForesterUtil.isEmpty( n.getNodeData().getTaxonomy().getScientificName() ) ) {
1468 group = ForesterUtil
1469 .obtainNormalizedTaxonomyGroup( n.getNodeData().getTaxonomy().getScientificName() );
1471 if ( ForesterUtil.isEmpty( group ) && !ForesterUtil.isEmpty( n.getName() ) ) {
1472 group = ForesterUtil.obtainNormalizedTaxonomyGroup( n.getName() );
1474 if ( !ForesterUtil.isEmpty( group ) ) {
1479 if ( ForesterUtil.isEmpty( group ) ) {
1480 group = TaxonomyGroups.OTHER;
1482 _TAXCODE_TAXGROUP_MAP.put( tax_code, group );
1485 throw new IllegalArgumentException( "unable to obtain group for code " + tax_code
1486 + " (tree is null or empty and code is not in map)" );
1489 return _TAXCODE_TAXGROUP_MAP.get( tax_code );
1492 public static void performDomainArchitectureAnalysis( final SortedMap<String, Set<String>> domain_architecutures,
1493 final SortedMap<String, Integer> domain_architecuture_counts,
1494 final int min_count,
1495 final File da_counts_outfile,
1496 final File unique_da_outfile ) {
1497 checkForOutputFileWriteability( da_counts_outfile );
1498 checkForOutputFileWriteability( unique_da_outfile );
1500 final BufferedWriter da_counts_out = new BufferedWriter( new FileWriter( da_counts_outfile ) );
1501 final BufferedWriter unique_da_out = new BufferedWriter( new FileWriter( unique_da_outfile ) );
1502 final Iterator<Entry<String, Integer>> it = domain_architecuture_counts.entrySet().iterator();
1503 while ( it.hasNext() ) {
1504 final Map.Entry<String, Integer> e = it.next();
1505 final String da = e.getKey();
1506 final int count = e.getValue();
1507 if ( count >= min_count ) {
1508 da_counts_out.write( da );
1509 da_counts_out.write( "\t" );
1510 da_counts_out.write( String.valueOf( count ) );
1511 da_counts_out.write( ForesterUtil.LINE_SEPARATOR );
1514 final Iterator<Entry<String, Set<String>>> it2 = domain_architecutures.entrySet().iterator();
1515 while ( it2.hasNext() ) {
1516 final Map.Entry<String, Set<String>> e2 = it2.next();
1517 final String genome = e2.getKey();
1518 final Set<String> das = e2.getValue();
1519 if ( das.contains( da ) ) {
1520 unique_da_out.write( genome );
1521 unique_da_out.write( "\t" );
1522 unique_da_out.write( da );
1523 unique_da_out.write( ForesterUtil.LINE_SEPARATOR );
1528 unique_da_out.close();
1529 da_counts_out.close();
1531 catch ( final IOException e ) {
1532 ForesterUtil.fatalError( surfacing.PRG_NAME, e.getMessage() );
1534 ForesterUtil.programMessage( surfacing.PRG_NAME, "Wrote distance matrices to \"" + da_counts_outfile + "\"" );
1535 ForesterUtil.programMessage( surfacing.PRG_NAME, "Wrote distance matrices to \"" + unique_da_outfile + "\"" );
1539 public static void preparePhylogeny( final Phylogeny p,
1540 final DomainParsimonyCalculator domain_parsimony,
1541 final String date_time,
1542 final String method,
1544 final String parameters_str ) {
1545 domain_parsimony.decoratePhylogenyWithDomains( p );
1546 final StringBuilder desc = new StringBuilder();
1547 desc.append( "[Method: " + method + "] [Date: " + date_time + "] " );
1548 desc.append( "[Cost: " + domain_parsimony.getCost() + "] " );
1549 desc.append( "[Gains: " + domain_parsimony.getTotalGains() + "] " );
1550 desc.append( "[Losses: " + domain_parsimony.getTotalLosses() + "] " );
1551 desc.append( "[Unchanged: " + domain_parsimony.getTotalUnchanged() + "] " );
1552 desc.append( "[Parameters: " + parameters_str + "]" );
1554 p.setDescription( desc.toString() );
1555 p.setConfidence( new Confidence( domain_parsimony.getCost(), "parsimony" ) );
1556 p.setRerootable( false );
1557 p.setRooted( true );
1560 public static void preparePhylogenyForParsimonyAnalyses( final Phylogeny intree,
1561 final String[][] input_file_properties ) {
1562 final String[] genomes = new String[ input_file_properties.length ];
1563 for( int i = 0; i < input_file_properties.length; ++i ) {
1564 if ( intree.getNodes( input_file_properties[ i ][ 1 ] ).size() > 1 ) {
1565 ForesterUtil.fatalError( surfacing.PRG_NAME,
1566 "node named [" + input_file_properties[ i ][ 1 ]
1567 + "] is not unique in input tree " + intree.getName() );
1569 genomes[ i ] = input_file_properties[ i ][ 1 ];
1572 final PhylogenyNodeIterator it = intree.iteratorPostorder();
1573 while ( it.hasNext() ) {
1574 final PhylogenyNode n = it.next();
1575 if ( ForesterUtil.isEmpty( n.getName() ) ) {
1576 if ( n.getNodeData().isHasTaxonomy()
1577 && !ForesterUtil.isEmpty( n.getNodeData().getTaxonomy().getTaxonomyCode() ) ) {
1578 n.setName( n.getNodeData().getTaxonomy().getTaxonomyCode() );
1580 else if ( n.getNodeData().isHasTaxonomy()
1581 && !ForesterUtil.isEmpty( n.getNodeData().getTaxonomy().getScientificName() ) ) {
1582 n.setName( n.getNodeData().getTaxonomy().getScientificName() );
1584 else if ( n.getNodeData().isHasTaxonomy()
1585 && !ForesterUtil.isEmpty( n.getNodeData().getTaxonomy().getCommonName() ) ) {
1586 n.setName( n.getNodeData().getTaxonomy().getCommonName() );
1590 .fatalError( surfacing.PRG_NAME,
1591 "node with no name, scientific name, common name, or taxonomy code present" );
1595 final List<String> igns = PhylogenyMethods.deleteExternalNodesPositiveSelection( genomes, intree );
1596 if ( igns.size() > 0 ) {
1597 System.out.println( "Not using the following " + igns.size() + " nodes:" );
1598 for( int i = 0; i < igns.size(); ++i ) {
1599 System.out.println( " " + i + ": " + igns.get( i ) );
1601 System.out.println( "--" );
1603 //Test for node names:
1604 final SortedSet<String> not_found = new TreeSet<String>();
1605 final SortedSet<String> not_unique = new TreeSet<String>();
1606 for( final String[] input_file_propertie : input_file_properties ) {
1607 final String name = input_file_propertie[ 1 ];
1608 final List<PhylogenyNode> nodes = intree.getNodes( name );
1609 if ( ( nodes == null ) || ( nodes.size() < 1 ) ) {
1610 not_found.add( name );
1612 if ( nodes.size() > 1 ) {
1613 not_unique.add( name );
1616 if ( not_found.size() > 0 ) {
1617 ForesterUtil.fatalError( surfacing.PRG_NAME,
1618 "the following " + not_found.size()
1619 + " node(s) are not present in the input tree: " + not_found );
1621 if ( not_unique.size() > 0 ) {
1622 ForesterUtil.fatalError( surfacing.PRG_NAME,
1623 "the following " + not_unique.size()
1624 + " node(s) are not unique in the input tree: " + not_unique );
1628 public static void printOutPercentageOfMultidomainProteins( final SortedMap<Integer, Integer> all_genomes_domains_per_potein_histo,
1629 final Writer log_writer ) {
1631 for( final Entry<Integer, Integer> entry : all_genomes_domains_per_potein_histo.entrySet() ) {
1632 sum += entry.getValue();
1634 final double percentage = ( 100.0 * ( sum - all_genomes_domains_per_potein_histo.get( 1 ) ) ) / sum;
1635 ForesterUtil.programMessage( surfacing.PRG_NAME, "Percentage of multidomain proteins: " + percentage + "%" );
1636 log( "Percentage of multidomain proteins: : " + percentage + "%", log_writer );
1639 public static void processFilter( final File filter_file, final SortedSet<String> filter ) {
1640 SortedSet<String> filter_str = null;
1642 filter_str = ForesterUtil.file2set( filter_file );
1644 catch ( final IOException e ) {
1645 ForesterUtil.fatalError( surfacing.PRG_NAME, e.getMessage() );
1647 if ( filter_str != null ) {
1648 for( final String string : filter_str ) {
1649 filter.add( string );
1652 if ( surfacing.VERBOSE ) {
1653 System.out.println( "Filter:" );
1654 for( final String domainId : filter ) {
1655 System.out.println( domainId );
1660 public static String[][] processInputGenomesFile( final File input_genomes ) {
1661 String[][] input_file_properties = null;
1663 input_file_properties = ForesterUtil.file22dArray( input_genomes );
1665 catch ( final IOException e ) {
1666 ForesterUtil.fatalError( surfacing.PRG_NAME,
1667 "genomes files is to be in the following format \"<hmmpfam output file> <species>\": "
1668 + e.getLocalizedMessage() );
1670 final Set<String> specs = new HashSet<String>();
1671 final Set<String> paths = new HashSet<String>();
1672 for( int i = 0; i < input_file_properties.length; ++i ) {
1673 if ( !PhyloXmlUtil.TAXOMONY_CODE_PATTERN.matcher( input_file_properties[ i ][ 1 ] ).matches() ) {
1674 ForesterUtil.fatalError( surfacing.PRG_NAME,
1675 "illegal format for species code: " + input_file_properties[ i ][ 1 ] );
1677 if ( specs.contains( input_file_properties[ i ][ 1 ] ) ) {
1678 ForesterUtil.fatalError( surfacing.PRG_NAME,
1679 "species code " + input_file_properties[ i ][ 1 ] + " is not unique" );
1681 specs.add( input_file_properties[ i ][ 1 ] );
1682 if ( paths.contains( input_file_properties[ i ][ 0 ] ) ) {
1683 ForesterUtil.fatalError( surfacing.PRG_NAME,
1684 "path " + input_file_properties[ i ][ 0 ] + " is not unique" );
1686 paths.add( input_file_properties[ i ][ 0 ] );
1687 final String error = ForesterUtil.isReadableFile( new File( input_file_properties[ i ][ 0 ] ) );
1688 if ( !ForesterUtil.isEmpty( error ) ) {
1689 ForesterUtil.fatalError( surfacing.PRG_NAME, error );
1692 return input_file_properties;
1695 public static void processPlusMinusAnalysisOption( final CommandLineArguments cla,
1696 final List<String> high_copy_base,
1697 final List<String> high_copy_target,
1698 final List<String> low_copy,
1699 final List<Object> numbers ) {
1700 if ( cla.isOptionSet( surfacing.PLUS_MINUS_ANALYSIS_OPTION ) ) {
1701 if ( !cla.isOptionValueSet( surfacing.PLUS_MINUS_ANALYSIS_OPTION ) ) {
1702 ForesterUtil.fatalError( surfacing.PRG_NAME,
1703 "no value for 'plus-minus' file: -" + surfacing.PLUS_MINUS_ANALYSIS_OPTION
1706 final File plus_minus_file = new File( cla.getOptionValue( surfacing.PLUS_MINUS_ANALYSIS_OPTION ) );
1707 final String msg = ForesterUtil.isReadableFile( plus_minus_file );
1708 if ( !ForesterUtil.isEmpty( msg ) ) {
1709 ForesterUtil.fatalError( surfacing.PRG_NAME, "can not read from \"" + plus_minus_file + "\": " + msg );
1711 processPlusMinusFile( plus_minus_file, high_copy_base, high_copy_target, low_copy, numbers );
1715 // First numbers is minimal difference, second is factor.
1716 public static void processPlusMinusFile( final File plus_minus_file,
1717 final List<String> high_copy_base,
1718 final List<String> high_copy_target,
1719 final List<String> low_copy,
1720 final List<Object> numbers ) {
1721 Set<String> species_set = null;
1722 int min_diff = surfacing.PLUS_MINUS_ANALYSIS_MIN_DIFF_DEFAULT;
1723 double factor = surfacing.PLUS_MINUS_ANALYSIS_FACTOR_DEFAULT;
1725 species_set = ForesterUtil.file2set( plus_minus_file );
1727 catch ( final IOException e ) {
1728 ForesterUtil.fatalError( surfacing.PRG_NAME, e.getMessage() );
1730 if ( species_set != null ) {
1731 for( final String species : species_set ) {
1732 final String species_trimmed = species.substring( 1 );
1733 if ( species.startsWith( "+" ) ) {
1734 if ( low_copy.contains( species_trimmed ) ) {
1735 ForesterUtil.fatalError( surfacing.PRG_NAME,
1736 "species/genome names can not appear with both '+' and '-' suffix, as appears the case for: \""
1737 + species_trimmed + "\"" );
1739 high_copy_base.add( species_trimmed );
1741 else if ( species.startsWith( "*" ) ) {
1742 if ( low_copy.contains( species_trimmed ) ) {
1743 ForesterUtil.fatalError( surfacing.PRG_NAME,
1744 "species/genome names can not appear with both '*' and '-' suffix, as appears the case for: \""
1745 + species_trimmed + "\"" );
1747 high_copy_target.add( species_trimmed );
1749 else if ( species.startsWith( "-" ) ) {
1750 if ( high_copy_base.contains( species_trimmed ) || high_copy_target.contains( species_trimmed ) ) {
1751 ForesterUtil.fatalError( surfacing.PRG_NAME,
1752 "species/genome names can not appear with both '+' or '*' and '-' suffix, as appears the case for: \""
1753 + species_trimmed + "\"" );
1755 low_copy.add( species_trimmed );
1757 else if ( species.startsWith( "$D" ) ) {
1759 min_diff = Integer.parseInt( species.substring( 3 ) );
1761 catch ( final NumberFormatException e ) {
1762 ForesterUtil.fatalError( surfacing.PRG_NAME,
1763 "could not parse integer value for minimal difference from: \""
1764 + species.substring( 3 ) + "\"" );
1767 else if ( species.startsWith( "$F" ) ) {
1769 factor = Double.parseDouble( species.substring( 3 ) );
1771 catch ( final NumberFormatException e ) {
1772 ForesterUtil.fatalError( surfacing.PRG_NAME,
1773 "could not parse double value for factor from: \""
1774 + species.substring( 3 ) + "\"" );
1777 else if ( species.startsWith( "#" ) ) {
1781 ForesterUtil.fatalError( surfacing.PRG_NAME,
1782 "species/genome names in 'plus minus' file must begin with '*' (high copy target genome), '+' (high copy base genomes), '-' (low copy genomes), '$D=<integer>' minimal Difference (default is 1), '$F=<double>' factor (default is 1.0), double), or '#' (ignore) suffix, encountered: \""
1785 numbers.add( new Integer( min_diff + "" ) );
1786 numbers.add( new Double( factor + "" ) );
1790 ForesterUtil.fatalError( surfacing.PRG_NAME, "'plus minus' file [" + plus_minus_file + "] appears empty" );
1795 * species | protein id | n-terminal domain | c-terminal domain | n-terminal domain per domain E-value | c-terminal domain per domain E-value
1799 static public StringBuffer proteinToDomainCombinations( final Protein protein,
1800 final String protein_id,
1801 final String separator ) {
1802 final StringBuffer sb = new StringBuffer();
1803 if ( protein.getSpecies() == null ) {
1804 throw new IllegalArgumentException( "species must not be null" );
1806 if ( ForesterUtil.isEmpty( protein.getSpecies().getSpeciesId() ) ) {
1807 throw new IllegalArgumentException( "species id must not be empty" );
1809 final List<Domain> domains = protein.getProteinDomains();
1810 if ( domains.size() > 1 ) {
1811 final Map<String, Integer> counts = new HashMap<String, Integer>();
1812 for( final Domain domain : domains ) {
1813 final String id = domain.getDomainId();
1814 if ( counts.containsKey( id ) ) {
1815 counts.put( id, counts.get( id ) + 1 );
1818 counts.put( id, 1 );
1821 final Set<String> dcs = new HashSet<String>();
1822 for( int i = 1; i < domains.size(); ++i ) {
1823 for( int j = 0; j < i; ++j ) {
1824 Domain domain_n = domains.get( i );
1825 Domain domain_c = domains.get( j );
1826 if ( domain_n.getFrom() > domain_c.getFrom() ) {
1827 domain_n = domains.get( j );
1828 domain_c = domains.get( i );
1830 final String dc = domain_n.getDomainId() + domain_c.getDomainId();
1831 if ( !dcs.contains( dc ) ) {
1833 sb.append( protein.getSpecies() );
1834 sb.append( separator );
1835 sb.append( protein_id );
1836 sb.append( separator );
1837 sb.append( domain_n.getDomainId() );
1838 sb.append( separator );
1839 sb.append( domain_c.getDomainId() );
1840 sb.append( separator );
1841 sb.append( domain_n.getPerDomainEvalue() );
1842 sb.append( separator );
1843 sb.append( domain_c.getPerDomainEvalue() );
1844 sb.append( separator );
1845 sb.append( counts.get( domain_n.getDomainId() ) );
1846 sb.append( separator );
1847 sb.append( counts.get( domain_c.getDomainId() ) );
1848 sb.append( ForesterUtil.LINE_SEPARATOR );
1853 else if ( domains.size() == 1 ) {
1854 sb.append( protein.getSpecies() );
1855 sb.append( separator );
1856 sb.append( protein_id );
1857 sb.append( separator );
1858 sb.append( domains.get( 0 ).getDomainId() );
1859 sb.append( separator );
1860 sb.append( separator );
1861 sb.append( domains.get( 0 ).getPerDomainEvalue() );
1862 sb.append( separator );
1863 sb.append( separator );
1865 sb.append( separator );
1866 sb.append( ForesterUtil.LINE_SEPARATOR );
1869 sb.append( protein.getSpecies() );
1870 sb.append( separator );
1871 sb.append( protein_id );
1872 sb.append( separator );
1873 sb.append( separator );
1874 sb.append( separator );
1875 sb.append( separator );
1876 sb.append( separator );
1877 sb.append( separator );
1878 sb.append( ForesterUtil.LINE_SEPARATOR );
1883 public static List<Domain> sortDomainsWithAscendingConfidenceValues( final Protein protein ) {
1884 final List<Domain> domains = new ArrayList<Domain>();
1885 for( final Domain d : protein.getProteinDomains() ) {
1888 Collections.sort( domains, SurfacingUtil.ASCENDING_CONFIDENCE_VALUE_ORDER );
1892 public static int storeDomainArchitectures( final String genome,
1893 final SortedMap<String, Set<String>> domain_architecutures,
1894 final List<Protein> protein_list,
1895 final Map<String, Integer> distinct_domain_architecuture_counts ) {
1896 final Set<String> da = new HashSet<String>();
1897 domain_architecutures.put( genome, da );
1898 for( final Protein protein : protein_list ) {
1899 final String da_str = ( ( BasicProtein ) protein ).toDomainArchitectureString( "~", 3, "=" );
1900 if ( !da.contains( da_str ) ) {
1901 if ( !distinct_domain_architecuture_counts.containsKey( da_str ) ) {
1902 distinct_domain_architecuture_counts.put( da_str, 1 );
1905 distinct_domain_architecuture_counts.put( da_str,
1906 distinct_domain_architecuture_counts.get( da_str ) + 1 );
1914 public static void writeAllDomainsChangedOnAllSubtrees( final Phylogeny p,
1915 final boolean get_gains,
1916 final String outdir,
1917 final String suffix_for_filename )
1918 throws IOException {
1919 CharacterStateMatrix.GainLossStates state = CharacterStateMatrix.GainLossStates.GAIN;
1921 state = CharacterStateMatrix.GainLossStates.LOSS;
1923 final File base_dir = createBaseDirForPerNodeDomainFiles( surfacing.BASE_DIRECTORY_PER_SUBTREE_DOMAIN_GAIN_LOSS_FILES,
1927 for( final PhylogenyNodeIterator it = p.iteratorPostorder(); it.hasNext(); ) {
1928 final PhylogenyNode node = it.next();
1929 if ( !node.isExternal() ) {
1930 final SortedSet<String> domains = collectAllDomainsChangedOnSubtree( node, get_gains );
1931 if ( domains.size() > 0 ) {
1932 final Writer writer = ForesterUtil.createBufferedWriter( base_dir + ForesterUtil.FILE_SEPARATOR
1933 + node.getName() + suffix_for_filename );
1934 for( final String domain : domains ) {
1935 writer.write( domain );
1936 writer.write( ForesterUtil.LINE_SEPARATOR );
1944 public static void writeBinaryDomainCombinationsFileForGraphAnalysis( final String[][] input_file_properties,
1945 final File output_dir,
1946 final GenomeWideCombinableDomains gwcd,
1948 final GenomeWideCombinableDomainsSortOrder dc_sort_order ) {
1949 File dc_outfile_dot = new File( input_file_properties[ i ][ 1 ]
1950 + surfacing.DOMAIN_COMBINITONS_OUTPUTFILE_SUFFIX_FOR_GRAPH_ANALYSIS );
1951 if ( output_dir != null ) {
1952 dc_outfile_dot = new File( output_dir + ForesterUtil.FILE_SEPARATOR + dc_outfile_dot );
1954 checkForOutputFileWriteability( dc_outfile_dot );
1955 final SortedSet<BinaryDomainCombination> binary_combinations = createSetOfAllBinaryDomainCombinationsPerGenome( gwcd );
1957 final BufferedWriter out_dot = new BufferedWriter( new FileWriter( dc_outfile_dot ) );
1958 for( final BinaryDomainCombination bdc : binary_combinations ) {
1959 out_dot.write( bdc.toGraphDescribingLanguage( BinaryDomainCombination.OutputFormat.DOT, null, null )
1961 out_dot.write( SurfacingConstants.NL );
1965 catch ( final IOException e ) {
1966 ForesterUtil.fatalError( surfacing.PRG_NAME, e.getMessage() );
1968 if ( input_file_properties[ i ].length == 3 ) {
1970 .programMessage( surfacing.PRG_NAME,
1971 "Wrote binary domain combination for \"" + input_file_properties[ i ][ 0 ] + "\" ("
1972 + input_file_properties[ i ][ 1 ] + ", " + input_file_properties[ i ][ 2 ]
1973 + ") to: \"" + dc_outfile_dot + "\"" );
1976 ForesterUtil.programMessage( surfacing.PRG_NAME,
1977 "Wrote binary domain combination for \"" + input_file_properties[ i ][ 0 ]
1978 + "\" (" + input_file_properties[ i ][ 1 ] + ") to: \""
1979 + dc_outfile_dot + "\"" );
1983 public static void writeBinaryStatesMatrixAsListToFile( final CharacterStateMatrix<CharacterStateMatrix.GainLossStates> matrix,
1984 final CharacterStateMatrix.GainLossStates state,
1985 final String filename,
1986 final String indentifier_characters_separator,
1987 final String character_separator,
1988 final Map<String, String> descriptions ) {
1989 final File outfile = new File( filename );
1990 checkForOutputFileWriteability( outfile );
1991 final SortedSet<String> sorted_ids = new TreeSet<String>();
1992 for( int i = 0; i < matrix.getNumberOfIdentifiers(); ++i ) {
1993 sorted_ids.add( matrix.getIdentifier( i ) );
1996 final BufferedWriter out = new BufferedWriter( new FileWriter( outfile ) );
1997 for( final String id : sorted_ids ) {
1998 out.write( indentifier_characters_separator );
1999 out.write( "#" + id );
2000 out.write( indentifier_characters_separator );
2001 for( int c = 0; c < matrix.getNumberOfCharacters(); ++c ) {
2003 // using null to indicate either UNCHANGED_PRESENT or GAIN.
2004 if ( ( matrix.getState( id, c ) == state ) || ( ( state == null )
2005 && ( ( matrix.getState( id, c ) == CharacterStateMatrix.GainLossStates.GAIN ) || ( matrix
2007 c ) == CharacterStateMatrix.GainLossStates.UNCHANGED_PRESENT ) ) ) ) {
2008 out.write( matrix.getCharacter( c ) );
2009 if ( ( descriptions != null ) && !descriptions.isEmpty()
2010 && descriptions.containsKey( matrix.getCharacter( c ) ) ) {
2012 out.write( descriptions.get( matrix.getCharacter( c ) ) );
2014 out.write( character_separator );
2021 catch ( final IOException e ) {
2022 ForesterUtil.fatalError( surfacing.PRG_NAME, e.getMessage() );
2024 ForesterUtil.programMessage( surfacing.PRG_NAME, "Wrote characters list: \"" + filename + "\"" );
2027 public static void writeBinaryStatesMatrixAsListToFileForBinaryCombinationsForGraphAnalysis( final CharacterStateMatrix<CharacterStateMatrix.GainLossStates> matrix,
2028 final CharacterStateMatrix.GainLossStates state,
2029 final String filename,
2030 final String indentifier_characters_separator,
2031 final String character_separator,
2032 final BinaryDomainCombination.OutputFormat bc_output_format ) {
2033 final File outfile = new File( filename );
2034 checkForOutputFileWriteability( outfile );
2035 final SortedSet<String> sorted_ids = new TreeSet<String>();
2036 for( int i = 0; i < matrix.getNumberOfIdentifiers(); ++i ) {
2037 sorted_ids.add( matrix.getIdentifier( i ) );
2040 final BufferedWriter out = new BufferedWriter( new FileWriter( outfile ) );
2041 for( final String id : sorted_ids ) {
2042 out.write( indentifier_characters_separator );
2043 out.write( "#" + id );
2044 out.write( indentifier_characters_separator );
2045 for( int c = 0; c < matrix.getNumberOfCharacters(); ++c ) {
2047 // using null to indicate either UNCHANGED_PRESENT or GAIN.
2048 if ( ( matrix.getState( id, c ) == state ) || ( ( state == null )
2049 && ( ( matrix.getState( id, c ) == CharacterStateMatrix.GainLossStates.GAIN ) || ( matrix
2051 c ) == CharacterStateMatrix.GainLossStates.UNCHANGED_PRESENT ) ) ) ) {
2052 BinaryDomainCombination bdc = null;
2054 bdc = BasicBinaryDomainCombination.obtainInstance( matrix.getCharacter( c ) );
2056 catch ( final Exception e ) {
2057 ForesterUtil.fatalError( surfacing.PRG_NAME, e.getLocalizedMessage() );
2059 out.write( bdc.toGraphDescribingLanguage( bc_output_format, null, null ).toString() );
2060 out.write( character_separator );
2067 catch ( final IOException e ) {
2068 ForesterUtil.fatalError( surfacing.PRG_NAME, e.getMessage() );
2070 ForesterUtil.programMessage( surfacing.PRG_NAME, "Wrote characters list: \"" + filename + "\"" );
2073 public static void writeBinaryStatesMatrixToList( final Map<String, List<GoId>> domain_id_to_go_ids_map,
2074 final Map<GoId, GoTerm> go_id_to_term_map,
2075 final GoNameSpace go_namespace_limit,
2076 final boolean domain_combinations,
2077 final CharacterStateMatrix<CharacterStateMatrix.GainLossStates> matrix,
2078 final CharacterStateMatrix.GainLossStates state,
2079 final String filename,
2080 final String indentifier_characters_separator,
2081 final String character_separator,
2082 final String title_for_html,
2083 final String prefix_for_html,
2084 final Map<String, Set<String>>[] domain_id_to_secondary_features_maps,
2085 final SortedSet<String> all_pfams_encountered,
2086 final SortedSet<String> pfams_gained_or_lost,
2087 final String suffix_for_per_node_events_file,
2088 final Map<String, Integer> tax_code_to_id_map ) {
2089 if ( ( go_namespace_limit != null ) && ( ( go_id_to_term_map == null ) || ( go_id_to_term_map.size() < 1 ) ) ) {
2090 throw new IllegalArgumentException( "attempt to use GO namespace limit without a GO-id to term map" );
2092 else if ( ( ( domain_id_to_go_ids_map == null ) || ( domain_id_to_go_ids_map.size() < 1 ) ) ) {
2093 throw new IllegalArgumentException( "attempt to output detailed HTML without a Pfam to GO map" );
2095 else if ( ( ( go_id_to_term_map == null ) || ( go_id_to_term_map.size() < 1 ) ) ) {
2096 throw new IllegalArgumentException( "attempt to output detailed HTML without a GO-id to term map" );
2098 final File outfile = new File( filename );
2099 checkForOutputFileWriteability( outfile );
2100 final SortedSet<String> sorted_ids = new TreeSet<String>();
2101 for( int i = 0; i < matrix.getNumberOfIdentifiers(); ++i ) {
2102 sorted_ids.add( matrix.getIdentifier( i ) );
2105 final Writer out = new BufferedWriter( new FileWriter( outfile ) );
2106 final File per_node_go_mapped_domain_gain_loss_files_base_dir = createBaseDirForPerNodeDomainFiles( surfacing.BASE_DIRECTORY_PER_NODE_DOMAIN_GAIN_LOSS_FILES,
2107 domain_combinations,
2110 Writer per_node_go_mapped_domain_gain_loss_outfile_writer = null;
2111 File per_node_go_mapped_domain_gain_loss_outfile = null;
2112 int per_node_counter = 0;
2113 out.write( "<html>" );
2114 out.write( SurfacingConstants.NL );
2115 writeHtmlHead( out, title_for_html );
2116 out.write( SurfacingConstants.NL );
2117 out.write( "<body>" );
2118 out.write( SurfacingConstants.NL );
2119 out.write( "<h1>" );
2120 out.write( SurfacingConstants.NL );
2121 out.write( title_for_html );
2122 out.write( SurfacingConstants.NL );
2123 out.write( "</h1>" );
2124 out.write( SurfacingConstants.NL );
2125 out.write( "<table>" );
2126 out.write( SurfacingConstants.NL );
2127 for( final String id : sorted_ids ) {
2128 final Matcher matcher = PATTERN_SP_STYLE_TAXONOMY.matcher( id );
2129 if ( matcher.matches() ) {
2132 out.write( "<tr>" );
2133 out.write( "<td>" );
2134 out.write( "<a href=\"#" + id + "\">" + id + "</a>" );
2135 out.write( "</td>" );
2136 out.write( "</tr>" );
2137 out.write( SurfacingConstants.NL );
2139 out.write( "</table>" );
2140 out.write( SurfacingConstants.NL );
2141 for( final String id : sorted_ids ) {
2142 final Matcher matcher = PATTERN_SP_STYLE_TAXONOMY.matcher( id );
2143 if ( matcher.matches() ) {
2146 out.write( SurfacingConstants.NL );
2147 out.write( "<h2>" );
2148 out.write( "<a name=\"" + id + "\">" + id + "</a>" );
2149 writeTaxonomyLinks( out, id, tax_code_to_id_map );
2150 out.write( "</h2>" );
2151 out.write( SurfacingConstants.NL );
2152 out.write( "<table>" );
2153 out.write( SurfacingConstants.NL );
2154 out.write( "<tr>" );
2155 out.write( "<td><b>" );
2156 out.write( "Pfam domain(s)" );
2157 out.write( "</b></td><td><b>" );
2158 out.write( "GO term acc" );
2159 out.write( "</b></td><td><b>" );
2160 out.write( "GO term" );
2161 out.write( "</b></td><td><b>" );
2162 out.write( "GO namespace" );
2163 out.write( "</b></td>" );
2164 out.write( "</tr>" );
2165 out.write( SurfacingConstants.NL );
2166 out.write( "</tr>" );
2167 out.write( SurfacingConstants.NL );
2168 per_node_counter = 0;
2169 if ( matrix.getNumberOfCharacters() > 0 ) {
2170 per_node_go_mapped_domain_gain_loss_outfile = new File( per_node_go_mapped_domain_gain_loss_files_base_dir
2171 + ForesterUtil.FILE_SEPARATOR + id + suffix_for_per_node_events_file );
2172 SurfacingUtil.checkForOutputFileWriteability( per_node_go_mapped_domain_gain_loss_outfile );
2173 per_node_go_mapped_domain_gain_loss_outfile_writer = ForesterUtil
2174 .createBufferedWriter( per_node_go_mapped_domain_gain_loss_outfile );
2177 per_node_go_mapped_domain_gain_loss_outfile = null;
2178 per_node_go_mapped_domain_gain_loss_outfile_writer = null;
2180 for( int c = 0; c < matrix.getNumberOfCharacters(); ++c ) {
2182 // using null to indicate either UNCHANGED_PRESENT or GAIN.
2183 if ( ( matrix.getState( id, c ) == state ) || ( ( state == null )
2184 && ( ( matrix.getState( id, c ) == CharacterStateMatrix.GainLossStates.UNCHANGED_PRESENT )
2185 || ( matrix.getState( id, c ) == CharacterStateMatrix.GainLossStates.GAIN ) ) ) ) {
2186 final String character = matrix.getCharacter( c );
2187 String domain_0 = "";
2188 String domain_1 = "";
2189 if ( character.indexOf( BinaryDomainCombination.SEPARATOR ) > 0 ) {
2190 final String[] s = character.split( BinaryDomainCombination.SEPARATOR );
2191 if ( s.length != 2 ) {
2192 throw new AssertionError( "this should not have happened: unexpected format for domain combination: ["
2193 + character + "]" );
2199 domain_0 = character;
2201 writeDomainData( domain_id_to_go_ids_map,
2208 character_separator,
2209 domain_id_to_secondary_features_maps,
2211 all_pfams_encountered.add( domain_0 );
2212 if ( pfams_gained_or_lost != null ) {
2213 pfams_gained_or_lost.add( domain_0 );
2215 if ( !ForesterUtil.isEmpty( domain_1 ) ) {
2216 all_pfams_encountered.add( domain_1 );
2217 if ( pfams_gained_or_lost != null ) {
2218 pfams_gained_or_lost.add( domain_1 );
2221 if ( per_node_go_mapped_domain_gain_loss_outfile_writer != null ) {
2222 writeDomainsToIndividualFilePerTreeNode( per_node_go_mapped_domain_gain_loss_outfile_writer,
2229 if ( per_node_go_mapped_domain_gain_loss_outfile_writer != null ) {
2230 per_node_go_mapped_domain_gain_loss_outfile_writer.close();
2231 if ( per_node_counter < 1 ) {
2232 per_node_go_mapped_domain_gain_loss_outfile.delete();
2234 per_node_counter = 0;
2236 out.write( "</table>" );
2237 out.write( SurfacingConstants.NL );
2238 out.write( "<hr>" );
2239 out.write( SurfacingConstants.NL );
2240 } // for( final String id : sorted_ids ) {
2241 out.write( "</body>" );
2242 out.write( SurfacingConstants.NL );
2243 out.write( "</html>" );
2244 out.write( SurfacingConstants.NL );
2248 catch ( final IOException e ) {
2249 ForesterUtil.fatalError( surfacing.PRG_NAME, e.getMessage() );
2251 ForesterUtil.programMessage( surfacing.PRG_NAME, "Wrote characters detailed HTML list: \"" + filename + "\"" );
2254 public static void writeDomainCombinationsCountsFile( final String[][] input_file_properties,
2255 final File output_dir,
2256 final Writer per_genome_domain_promiscuity_statistics_writer,
2257 final GenomeWideCombinableDomains gwcd,
2259 final GenomeWideCombinableDomains.GenomeWideCombinableDomainsSortOrder dc_sort_order ) {
2260 File dc_outfile = new File( input_file_properties[ i ][ 1 ]
2261 + surfacing.DOMAIN_COMBINITON_COUNTS_OUTPUTFILE_SUFFIX );
2262 if ( output_dir != null ) {
2263 dc_outfile = new File( output_dir + ForesterUtil.FILE_SEPARATOR + dc_outfile );
2265 checkForOutputFileWriteability( dc_outfile );
2267 final BufferedWriter out = new BufferedWriter( new FileWriter( dc_outfile ) );
2268 out.write( gwcd.toStringBuilder( dc_sort_order ).toString() );
2271 catch ( final IOException e ) {
2272 ForesterUtil.fatalError( surfacing.PRG_NAME, e.getMessage() );
2274 final DescriptiveStatistics stats = gwcd.getPerGenomeDomainPromiscuityStatistics();
2276 per_genome_domain_promiscuity_statistics_writer.write( input_file_properties[ i ][ 1 ] + "\t" );
2277 per_genome_domain_promiscuity_statistics_writer
2278 .write( FORMATTER_3.format( stats.arithmeticMean() ) + "\t" );
2279 if ( stats.getN() < 2 ) {
2280 per_genome_domain_promiscuity_statistics_writer.write( "n/a" + "\t" );
2283 per_genome_domain_promiscuity_statistics_writer
2284 .write( FORMATTER_3.format( stats.sampleStandardDeviation() ) + "\t" );
2286 per_genome_domain_promiscuity_statistics_writer.write( FORMATTER_3.format( stats.median() ) + "\t" );
2287 per_genome_domain_promiscuity_statistics_writer.write( ( int ) stats.getMin() + "\t" );
2288 per_genome_domain_promiscuity_statistics_writer.write( ( int ) stats.getMax() + "\t" );
2289 per_genome_domain_promiscuity_statistics_writer.write( stats.getN() + "\t" );
2290 final SortedSet<String> mpds = gwcd.getMostPromiscuosDomain();
2291 for( final String mpd : mpds ) {
2292 per_genome_domain_promiscuity_statistics_writer.write( mpd + " " );
2294 per_genome_domain_promiscuity_statistics_writer.write( ForesterUtil.LINE_SEPARATOR );
2296 catch ( final IOException e ) {
2297 ForesterUtil.fatalError( surfacing.PRG_NAME, e.getMessage() );
2299 if ( input_file_properties[ i ].length == 3 ) {
2300 ForesterUtil.programMessage( surfacing.PRG_NAME,
2301 "Wrote domain combination counts for \"" + input_file_properties[ i ][ 0 ]
2302 + "\" (" + input_file_properties[ i ][ 1 ] + ", "
2303 + input_file_properties[ i ][ 2 ] + ") to: \"" + dc_outfile + "\"" );
2306 ForesterUtil.programMessage( surfacing.PRG_NAME,
2307 "Wrote domain combination counts for \"" + input_file_properties[ i ][ 0 ]
2308 + "\" (" + input_file_properties[ i ][ 1 ] + ") to: \"" + dc_outfile
2313 public static void writeDomainSimilaritiesToFile( final StringBuilder html_desc,
2314 final StringBuilder html_title,
2315 final Writer simple_tab_writer,
2316 final Writer single_writer,
2317 Map<Character, Writer> split_writers,
2318 final SortedSet<DomainSimilarity> similarities,
2319 final boolean treat_as_binary,
2320 final List<Species> species_order,
2321 final DomainSimilarity.PRINT_OPTION print_option,
2322 final DomainSimilarity.DomainSimilarityScoring scoring,
2323 final boolean verbose,
2324 final Map<String, Integer> tax_code_to_id_map,
2325 final Phylogeny phy,
2326 final Set<String> pos_filter_doms )
2327 throws IOException {
2328 if ( ( single_writer != null ) && ( ( split_writers == null ) || split_writers.isEmpty() ) ) {
2329 split_writers = new HashMap<Character, Writer>();
2330 split_writers.put( '_', single_writer );
2332 switch ( print_option ) {
2333 case SIMPLE_TAB_DELIMITED:
2336 for( final Character key : split_writers.keySet() ) {
2337 final Writer w = split_writers.get( key );
2338 w.write( "<html>" );
2339 w.write( SurfacingConstants.NL );
2341 writeHtmlHead( w, "DC analysis (" + html_title + ") " + key.toString().toUpperCase() );
2344 writeHtmlHead( w, "DC analysis (" + html_title + ")" );
2346 w.write( SurfacingConstants.NL );
2347 w.write( "<body>" );
2348 w.write( SurfacingConstants.NL );
2349 w.write( html_desc.toString() );
2350 w.write( SurfacingConstants.NL );
2352 w.write( SurfacingConstants.NL );
2354 w.write( SurfacingConstants.NL );
2355 w.write( "<table>" );
2356 w.write( SurfacingConstants.NL );
2357 w.write( "<tr><td><b>Domains:</b></td></tr>" );
2358 w.write( SurfacingConstants.NL );
2363 for( final DomainSimilarity similarity : similarities ) {
2364 if ( ( species_order != null ) && !species_order.isEmpty() ) {
2365 ( similarity ).setSpeciesOrder( species_order );
2367 if ( single_writer != null ) {
2368 if ( !ForesterUtil.isEmpty( pos_filter_doms )
2369 && pos_filter_doms.contains( similarity.getDomainId() ) ) {
2370 single_writer.write( "<tr><td><b><a href=\"#" + similarity.getDomainId()
2371 + "\"><span style=\"color:#00ff00\">" + similarity.getDomainId()
2372 + "</span></a></b></td></tr>" );
2375 single_writer.write( "<tr><td><b><a href=\"#" + similarity.getDomainId() + "\">"
2376 + similarity.getDomainId() + "</a></b></td></tr>" );
2378 single_writer.write( SurfacingConstants.NL );
2381 Writer local_writer = split_writers
2382 .get( ( similarity.getDomainId().charAt( 0 ) + "" ).toLowerCase().charAt( 0 ) );
2383 if ( local_writer == null ) {
2384 local_writer = split_writers.get( '0' );
2386 if ( !ForesterUtil.isEmpty( pos_filter_doms )
2387 && pos_filter_doms.contains( similarity.getDomainId() ) ) {
2388 local_writer.write( "<tr><td><b><a href=\"#" + similarity.getDomainId()
2389 + "\"><span style=\"color:#00ff00\">" + similarity.getDomainId()
2390 + "</span></a></b></td></tr>" );
2393 local_writer.write( "<tr><td><b><a href=\"#" + similarity.getDomainId() + "\">"
2394 + similarity.getDomainId() + "</a></b></td></tr>" );
2396 local_writer.write( SurfacingConstants.NL );
2399 for( final Writer w : split_writers.values() ) {
2400 w.write( "</table>" );
2401 w.write( SurfacingConstants.NL );
2403 w.write( SurfacingConstants.NL );
2405 w.write( "<table>" );
2406 w.write( SurfacingConstants.NL );
2407 w.write( "<tr><td><b>" );
2408 w.write( "Species group colors:" );
2409 w.write( "</b></td></tr>" );
2410 w.write( SurfacingConstants.NL );
2411 writeColorLabels( "Deuterostomia", TaxonomyColors.DEUTEROSTOMIA_COLOR, w );
2412 writeColorLabels( "Protostomia", TaxonomyColors.PROTOSTOMIA_COLOR, w );
2413 writeColorLabels( "Cnidaria", TaxonomyColors.CNIDARIA_COLOR, w );
2414 writeColorLabels( "Placozoa", TaxonomyColors.PLACOZOA_COLOR, w );
2415 writeColorLabels( "Ctenophora (comb jellies)", TaxonomyColors.CTENOPHORA_COLOR, w );
2416 writeColorLabels( "Porifera (sponges)", TaxonomyColors.PORIFERA_COLOR, w );
2417 writeColorLabels( "Choanoflagellida", TaxonomyColors.CHOANOFLAGELLIDA, w );
2418 writeColorLabels( "Ichthyosporea & Filasterea", TaxonomyColors.ICHTHYOSPOREA_AND_FILASTEREA, w );
2419 writeColorLabels( "Dikarya (Ascomycota & Basidiomycota, so-called \"higher fungi\")",
2420 TaxonomyColors.DIKARYA_COLOR,
2422 writeColorLabels( "other Fungi", TaxonomyColors.OTHER_FUNGI_COLOR, w );
2423 writeColorLabels( "Nucleariidae and Fonticula group",
2424 TaxonomyColors.NUCLEARIIDAE_AND_FONTICULA_GROUP_COLOR,
2426 writeColorLabels( "Amoebozoa", TaxonomyColors.AMOEBOZOA_COLOR, w );
2427 writeColorLabels( "Embryophyta (plants)", TaxonomyColors.EMBRYOPHYTA_COLOR, w );
2428 writeColorLabels( "Chlorophyta (green algae)", TaxonomyColors.CHLOROPHYTA_COLOR, w );
2429 writeColorLabels( "Rhodophyta (red algae)", TaxonomyColors.RHODOPHYTA_COLOR, w );
2430 writeColorLabels( "Glaucocystophyce (Glaucophyta)", TaxonomyColors.GLAUCOPHYTA_COLOR, w );
2431 writeColorLabels( "Hacrobia (Cryptophyta & Haptophyceae & Centroheliozoa)",
2432 TaxonomyColors.HACROBIA_COLOR,
2434 writeColorLabels( "Stramenopiles (Chromophyta, heterokonts)", TaxonomyColors.STRAMENOPILES_COLOR, w );
2435 writeColorLabels( "Alveolata", TaxonomyColors.ALVEOLATA_COLOR, w );
2436 writeColorLabels( "Rhizaria", TaxonomyColors.RHIZARIA_COLOR, w );
2437 writeColorLabels( "Excavata", TaxonomyColors.EXCAVATA_COLOR, w );
2438 writeColorLabels( "Apusozoa", TaxonomyColors.APUSOZOA_COLOR, w );
2439 writeColorLabels( "Archaea", TaxonomyColors.ARCHAEA_COLOR, w );
2440 writeColorLabels( "Bacteria", TaxonomyColors.BACTERIA_COLOR, w );
2441 w.write( "</table>" );
2442 w.write( SurfacingConstants.NL );
2445 w.write( SurfacingConstants.NL );
2446 w.write( "<table>" );
2447 w.write( SurfacingConstants.NL );
2450 for( final DomainSimilarity similarity : similarities ) {
2451 if ( ( species_order != null ) && !species_order.isEmpty() ) {
2452 ( similarity ).setSpeciesOrder( species_order );
2454 if ( simple_tab_writer != null ) {
2455 simple_tab_writer.write( similarity
2456 .toStringBuffer( PRINT_OPTION.SIMPLE_TAB_DELIMITED, tax_code_to_id_map, null ).toString() );
2458 if ( single_writer != null ) {
2459 single_writer.write( similarity.toStringBuffer( print_option, tax_code_to_id_map, phy ).toString() );
2460 single_writer.write( SurfacingConstants.NL );
2463 Writer local_writer = split_writers
2464 .get( ( similarity.getDomainId().charAt( 0 ) + "" ).toLowerCase().charAt( 0 ) );
2465 if ( local_writer == null ) {
2466 local_writer = split_writers.get( '0' );
2468 local_writer.write( similarity.toStringBuffer( print_option, tax_code_to_id_map, phy ).toString() );
2469 local_writer.write( SurfacingConstants.NL );
2472 switch ( print_option ) {
2474 for( final Writer w : split_writers.values() ) {
2475 w.write( SurfacingConstants.NL );
2476 w.write( "</table>" );
2477 w.write( SurfacingConstants.NL );
2478 w.write( "</font>" );
2479 w.write( SurfacingConstants.NL );
2480 w.write( "</body>" );
2481 w.write( SurfacingConstants.NL );
2482 w.write( "</html>" );
2483 w.write( SurfacingConstants.NL );
2489 for( final Writer w : split_writers.values() ) {
2494 public static void writeHtmlHead( final Writer w, final String title ) throws IOException {
2495 w.write( SurfacingConstants.NL );
2496 w.write( "<head>" );
2497 w.write( "<title>" );
2499 w.write( "</title>" );
2500 w.write( SurfacingConstants.NL );
2501 w.write( "<style>" );
2502 w.write( SurfacingConstants.NL );
2503 w.write( "a:visited { color : #000066; text-decoration : none; }" );
2504 w.write( SurfacingConstants.NL );
2505 w.write( "a:link { color : #000066; text-decoration : none; }" );
2506 w.write( SurfacingConstants.NL );
2507 w.write( "a:active { color : ##000066; text-decoration : none; }" );
2508 w.write( SurfacingConstants.NL );
2509 w.write( "a:hover { color : #FFFFFF; background-color : #000000; text-decoration : none; }" );
2510 w.write( SurfacingConstants.NL );
2512 w.write( "a.pl:visited { color : #505050; text-decoration : none; font-size: 7px;}" );
2513 w.write( SurfacingConstants.NL );
2514 w.write( "a.pl:link { color : #505050; text-decoration : none; font-size: 7px;}" );
2515 w.write( SurfacingConstants.NL );
2516 w.write( "a.pl:active { color : #505050; text-decoration : none; font-size: 7px;}" );
2517 w.write( SurfacingConstants.NL );
2518 w.write( "a.pl:hover { color : #FFFFFF; background-color : #000000; text-decoration : none; font-size: 7px;}" );
2519 w.write( SurfacingConstants.NL );
2521 w.write( "a.ps:visited { color : #707070; text-decoration : none; font-size: 7px;}" );
2522 w.write( SurfacingConstants.NL );
2523 w.write( "a.ps:link { color : #707070; text-decoration : none; font-size: 7px;}" );
2524 w.write( SurfacingConstants.NL );
2525 w.write( "a.ps:active { color : #707070; text-decoration : none; font-size: 7px;}" );
2526 w.write( SurfacingConstants.NL );
2527 w.write( "a.ps:hover { color : #FFFFFF; background-color : #000000; text-decoration : none; font-size: 7px;}" );
2528 w.write( SurfacingConstants.NL );
2530 w.write( "td { text-align: left; vertical-align: top; font-family: Verdana, Arial, Helvetica; font-size: 8pt}" );
2531 w.write( SurfacingConstants.NL );
2532 w.write( "h1 { color : #0000FF; font-family: Verdana, Arial, Helvetica; font-size: 18pt; font-weight: bold }" );
2533 w.write( SurfacingConstants.NL );
2534 w.write( "h2 { color : #0000FF; font-family: Verdana, Arial, Helvetica; font-size: 16pt; font-weight: bold }" );
2535 w.write( SurfacingConstants.NL );
2536 w.write( "</style>" );
2537 w.write( SurfacingConstants.NL );
2538 w.write( "</head>" );
2539 w.write( SurfacingConstants.NL );
2542 public static void writeMatrixToFile( final CharacterStateMatrix<?> matrix,
2543 final String filename,
2544 final Format format ) {
2545 final File outfile = new File( filename );
2546 checkForOutputFileWriteability( outfile );
2548 final BufferedWriter out = new BufferedWriter( new FileWriter( outfile ) );
2549 matrix.toWriter( out, format );
2553 catch ( final IOException e ) {
2554 ForesterUtil.fatalError( surfacing.PRG_NAME, e.getMessage() );
2556 ForesterUtil.programMessage( surfacing.PRG_NAME, "Wrote matrix: \"" + filename + "\"" );
2559 public static void writeMatrixToFile( final File matrix_outfile, final List<DistanceMatrix> matrices ) {
2560 checkForOutputFileWriteability( matrix_outfile );
2562 final BufferedWriter out = new BufferedWriter( new FileWriter( matrix_outfile ) );
2563 for( final DistanceMatrix distance_matrix : matrices ) {
2564 out.write( distance_matrix.toStringBuffer( DistanceMatrix.Format.PHYLIP ).toString() );
2565 out.write( ForesterUtil.LINE_SEPARATOR );
2570 catch ( final IOException e ) {
2571 ForesterUtil.fatalError( surfacing.PRG_NAME, e.getMessage() );
2573 ForesterUtil.programMessage( surfacing.PRG_NAME, "Wrote distance matrices to \"" + matrix_outfile + "\"" );
2576 public static void writePhylogenyToFile( final Phylogeny phylogeny, final String filename ) {
2577 final PhylogenyWriter writer = new PhylogenyWriter();
2579 writer.toPhyloXML( new File( filename ), phylogeny, 1 );
2581 catch ( final IOException e ) {
2582 ForesterUtil.printWarningMessage( surfacing.PRG_NAME,
2583 "failed to write phylogeny to \"" + filename + "\": " + e );
2585 ForesterUtil.programMessage( surfacing.PRG_NAME, "Wrote phylogeny to \"" + filename + "\"" );
2588 public static void writePresentToNexus( final File output_file,
2589 final File positive_filter_file,
2590 final SortedSet<String> filter,
2591 final List<GenomeWideCombinableDomains> gwcd_list ) {
2593 writeMatrixToFile( DomainParsimonyCalculator
2594 .createMatrixOfDomainPresenceOrAbsence( gwcd_list, positive_filter_file == null ? null : filter ),
2595 output_file + surfacing.DOMAINS_PRESENT_NEXUS,
2596 Format.NEXUS_BINARY );
2597 writeMatrixToFile( DomainParsimonyCalculator
2598 .createMatrixOfBinaryDomainCombinationPresenceOrAbsence( gwcd_list ),
2599 output_file + surfacing.BDC_PRESENT_NEXUS,
2600 Format.NEXUS_BINARY );
2602 catch ( final Exception e ) {
2603 ForesterUtil.fatalError( surfacing.PRG_NAME, e.getLocalizedMessage() );
2607 public static void writeProteinListsForAllSpecies( final File output_dir,
2608 final SortedMap<Species, List<Protein>> protein_lists_per_species,
2609 final List<GenomeWideCombinableDomains> gwcd_list,
2610 final double domain_e_cutoff,
2611 final Set<String> pos_filter_doms ) {
2612 final SortedSet<String> all_domains = new TreeSet<String>();
2613 for( final GenomeWideCombinableDomains gwcd : gwcd_list ) {
2614 all_domains.addAll( gwcd.getAllDomainIds() );
2616 for( final String domain : all_domains ) {
2617 if ( !ForesterUtil.isEmpty( pos_filter_doms ) && !pos_filter_doms.contains( domain ) ) {
2620 final File out = new File( output_dir + ForesterUtil.FILE_SEPARATOR + domain
2621 + surfacing.SEQ_EXTRACT_SUFFIX );
2622 checkForOutputFileWriteability( out );
2624 final Writer proteins_file_writer = new BufferedWriter( new FileWriter( out ) );
2625 extractProteinNames( protein_lists_per_species,
2627 proteins_file_writer,
2629 surfacing.LIMIT_SPEC_FOR_PROT_EX,
2631 proteins_file_writer.close();
2633 catch ( final IOException e ) {
2634 ForesterUtil.fatalError( surfacing.PRG_NAME, e.getLocalizedMessage() );
2636 ForesterUtil.programMessage( surfacing.PRG_NAME, "Wrote proteins list to \"" + out + "\"" );
2640 public static void writeTaxonomyLinks( final Writer writer,
2641 final String species,
2642 final Map<String, Integer> tax_code_to_id_map )
2643 throws IOException {
2644 if ( ( species.length() > 1 ) && ( species.indexOf( '_' ) < 1 ) ) {
2645 writer.write( " [" );
2646 if ( ( tax_code_to_id_map != null ) && tax_code_to_id_map.containsKey( species ) ) {
2647 writer.write( "<a href=\"" + SurfacingConstants.UNIPROT_TAXONOMY_ID_LINK
2648 + tax_code_to_id_map.get( species ) + "\" target=\"taxonomy_window\">uniprot</a>" );
2651 writer.write( "<a href=\"" + SurfacingConstants.EOL_LINK + species
2652 + "\" target=\"taxonomy_window\">eol</a>" );
2653 writer.write( "|" );
2654 writer.write( "<a href=\"" + SurfacingConstants.GOOGLE_SCHOLAR_SEARCH + species
2655 + "\" target=\"taxonomy_window\">scholar</a>" );
2656 writer.write( "|" );
2657 writer.write( "<a href=\"" + SurfacingConstants.GOOGLE_WEB_SEARCH_LINK + species
2658 + "\" target=\"taxonomy_window\">google</a>" );
2660 writer.write( "]" );
2664 private final static void addToCountMap( final Map<String, Integer> map, final String s ) {
2665 if ( map.containsKey( s ) ) {
2666 map.put( s, map.get( s ) + 1 );
2673 private static void calculateIndependentDomainCombinationGains( final Phylogeny local_phylogeny_l,
2674 final String outfilename_for_counts,
2675 final String outfilename_for_dc,
2676 final String outfilename_for_dc_for_go_mapping,
2677 final String outfilename_for_dc_for_go_mapping_unique,
2678 final String outfilename_for_rank_counts,
2679 final String outfilename_for_ancestor_species_counts,
2680 final String outfilename_for_protein_stats,
2681 final Map<String, DescriptiveStatistics> protein_length_stats_by_dc,
2682 final Map<String, DescriptiveStatistics> domain_number_stats_by_dc,
2683 final Map<String, DescriptiveStatistics> domain_length_stats_by_domain ) {
2686 // if ( protein_length_stats_by_dc != null ) {
2687 // for( final Entry<?, DescriptiveStatistics> entry : protein_length_stats_by_dc.entrySet() ) {
2688 // System.out.print( entry.getKey().toString() );
2689 // System.out.print( ": " );
2690 // double[] a = entry.getValue().getDataAsDoubleArray();
2691 // for( int i = 0; i < a.length; i++ ) {
2692 // System.out.print( a[ i ] + " " );
2694 // System.out.println();
2697 // if ( domain_number_stats_by_dc != null ) {
2698 // for( final Entry<?, DescriptiveStatistics> entry : domain_number_stats_by_dc.entrySet() ) {
2699 // System.out.print( entry.getKey().toString() );
2700 // System.out.print( ": " );
2701 // double[] a = entry.getValue().getDataAsDoubleArray();
2702 // for( int i = 0; i < a.length; i++ ) {
2703 // System.out.print( a[ i ] + " " );
2705 // System.out.println();
2709 final BufferedWriter out_counts = new BufferedWriter( new FileWriter( outfilename_for_counts ) );
2710 final BufferedWriter out_dc = new BufferedWriter( new FileWriter( outfilename_for_dc ) );
2711 final BufferedWriter out_dc_for_go_mapping = new BufferedWriter( new FileWriter( outfilename_for_dc_for_go_mapping ) );
2712 final BufferedWriter out_dc_for_go_mapping_unique = new BufferedWriter( new FileWriter( outfilename_for_dc_for_go_mapping_unique ) );
2713 final SortedMap<String, Integer> dc_gain_counts = new TreeMap<String, Integer>();
2714 for( final PhylogenyNodeIterator it = local_phylogeny_l.iteratorPostorder(); it.hasNext(); ) {
2715 final PhylogenyNode n = it.next();
2716 final Set<String> gained_dc = n.getNodeData().getBinaryCharacters().getGainedCharacters();
2717 for( final String dc : gained_dc ) {
2718 if ( dc_gain_counts.containsKey( dc ) ) {
2719 dc_gain_counts.put( dc, dc_gain_counts.get( dc ) + 1 );
2722 dc_gain_counts.put( dc, 1 );
2726 final SortedMap<Integer, Integer> histogram = new TreeMap<Integer, Integer>();
2727 final SortedMap<Integer, StringBuilder> domain_lists = new TreeMap<Integer, StringBuilder>();
2728 final SortedMap<Integer, DescriptiveStatistics> dc_reapp_counts_to_protein_length_stats = new TreeMap<Integer, DescriptiveStatistics>();
2729 final SortedMap<Integer, DescriptiveStatistics> dc_reapp_counts_to_domain_number_stats = new TreeMap<Integer, DescriptiveStatistics>();
2730 final SortedMap<Integer, DescriptiveStatistics> dc_reapp_counts_to_domain_lengths_stats = new TreeMap<Integer, DescriptiveStatistics>();
2731 final SortedMap<Integer, PriorityQueue<String>> domain_lists_go = new TreeMap<Integer, PriorityQueue<String>>();
2732 final SortedMap<Integer, SortedSet<String>> domain_lists_go_unique = new TreeMap<Integer, SortedSet<String>>();
2733 final Set<String> dcs = dc_gain_counts.keySet();
2734 final SortedSet<String> more_than_once = new TreeSet<String>();
2735 DescriptiveStatistics gained_once_lengths_stats = new BasicDescriptiveStatistics();
2736 DescriptiveStatistics gained_once_domain_count_stats = new BasicDescriptiveStatistics();
2737 DescriptiveStatistics gained_multiple_times_lengths_stats = new BasicDescriptiveStatistics();
2738 final DescriptiveStatistics gained_multiple_times_domain_count_stats = new BasicDescriptiveStatistics();
2739 long gained_multiple_times_domain_length_sum = 0;
2740 long gained_once_domain_length_sum = 0;
2741 long gained_multiple_times_domain_length_count = 0;
2742 long gained_once_domain_length_count = 0;
2743 for( final String dc : dcs ) {
2744 final int count = dc_gain_counts.get( dc );
2745 if ( histogram.containsKey( count ) ) {
2746 histogram.put( count, histogram.get( count ) + 1 );
2747 domain_lists.get( count ).append( ", " + dc );
2748 domain_lists_go.get( count ).addAll( splitDomainCombination( dc ) );
2749 domain_lists_go_unique.get( count ).addAll( splitDomainCombination( dc ) );
2752 histogram.put( count, 1 );
2753 domain_lists.put( count, new StringBuilder( dc ) );
2754 final PriorityQueue<String> q = new PriorityQueue<String>();
2755 q.addAll( splitDomainCombination( dc ) );
2756 domain_lists_go.put( count, q );
2757 final SortedSet<String> set = new TreeSet<String>();
2758 set.addAll( splitDomainCombination( dc ) );
2759 domain_lists_go_unique.put( count, set );
2761 if ( protein_length_stats_by_dc != null ) {
2762 if ( !dc_reapp_counts_to_protein_length_stats.containsKey( count ) ) {
2763 dc_reapp_counts_to_protein_length_stats.put( count, new BasicDescriptiveStatistics() );
2765 dc_reapp_counts_to_protein_length_stats.get( count )
2766 .addValue( protein_length_stats_by_dc.get( dc ).arithmeticMean() );
2768 if ( domain_number_stats_by_dc != null ) {
2769 if ( !dc_reapp_counts_to_domain_number_stats.containsKey( count ) ) {
2770 dc_reapp_counts_to_domain_number_stats.put( count, new BasicDescriptiveStatistics() );
2772 dc_reapp_counts_to_domain_number_stats.get( count )
2773 .addValue( domain_number_stats_by_dc.get( dc ).arithmeticMean() );
2775 if ( domain_length_stats_by_domain != null ) {
2776 if ( !dc_reapp_counts_to_domain_lengths_stats.containsKey( count ) ) {
2777 dc_reapp_counts_to_domain_lengths_stats.put( count, new BasicDescriptiveStatistics() );
2779 final String[] ds = dc.split( "=" );
2780 dc_reapp_counts_to_domain_lengths_stats.get( count )
2781 .addValue( domain_length_stats_by_domain.get( ds[ 0 ] ).arithmeticMean() );
2782 dc_reapp_counts_to_domain_lengths_stats.get( count )
2783 .addValue( domain_length_stats_by_domain.get( ds[ 1 ] ).arithmeticMean() );
2786 more_than_once.add( dc );
2787 if ( protein_length_stats_by_dc != null ) {
2788 final DescriptiveStatistics s = protein_length_stats_by_dc.get( dc );
2789 for( final double element : s.getData() ) {
2790 gained_multiple_times_lengths_stats.addValue( element );
2793 if ( domain_number_stats_by_dc != null ) {
2794 final DescriptiveStatistics s = domain_number_stats_by_dc.get( dc );
2795 for( final double element : s.getData() ) {
2796 gained_multiple_times_domain_count_stats.addValue( element );
2799 if ( domain_length_stats_by_domain != null ) {
2800 final String[] ds = dc.split( "=" );
2801 final DescriptiveStatistics s0 = domain_length_stats_by_domain.get( ds[ 0 ] );
2802 final DescriptiveStatistics s1 = domain_length_stats_by_domain.get( ds[ 1 ] );
2803 for( final double element : s0.getData() ) {
2804 gained_multiple_times_domain_length_sum += element;
2805 ++gained_multiple_times_domain_length_count;
2807 for( final double element : s1.getData() ) {
2808 gained_multiple_times_domain_length_sum += element;
2809 ++gained_multiple_times_domain_length_count;
2814 if ( protein_length_stats_by_dc != null ) {
2815 final DescriptiveStatistics s = protein_length_stats_by_dc.get( dc );
2816 for( final double element : s.getData() ) {
2817 gained_once_lengths_stats.addValue( element );
2820 if ( domain_number_stats_by_dc != null ) {
2821 final DescriptiveStatistics s = domain_number_stats_by_dc.get( dc );
2822 for( final double element : s.getData() ) {
2823 gained_once_domain_count_stats.addValue( element );
2826 if ( domain_length_stats_by_domain != null ) {
2827 final String[] ds = dc.split( "=" );
2828 final DescriptiveStatistics s0 = domain_length_stats_by_domain.get( ds[ 0 ] );
2829 final DescriptiveStatistics s1 = domain_length_stats_by_domain.get( ds[ 1 ] );
2830 for( final double element : s0.getData() ) {
2831 gained_once_domain_length_sum += element;
2832 ++gained_once_domain_length_count;
2834 for( final double element : s1.getData() ) {
2835 gained_once_domain_length_sum += element;
2836 ++gained_once_domain_length_count;
2841 final Set<Integer> histogram_keys = histogram.keySet();
2842 for( final Integer histogram_key : histogram_keys ) {
2843 final int count = histogram.get( histogram_key );
2844 final StringBuilder dc = domain_lists.get( histogram_key );
2845 out_counts.write( histogram_key + "\t" + count + ForesterUtil.LINE_SEPARATOR );
2846 out_dc.write( histogram_key + "\t" + dc + ForesterUtil.LINE_SEPARATOR );
2847 out_dc_for_go_mapping.write( "#" + histogram_key + ForesterUtil.LINE_SEPARATOR );
2848 final Object[] sorted = domain_lists_go.get( histogram_key ).toArray();
2849 Arrays.sort( sorted );
2850 for( final Object domain : sorted ) {
2851 out_dc_for_go_mapping.write( domain + ForesterUtil.LINE_SEPARATOR );
2853 out_dc_for_go_mapping_unique.write( "#" + histogram_key + ForesterUtil.LINE_SEPARATOR );
2854 for( final String domain : domain_lists_go_unique.get( histogram_key ) ) {
2855 out_dc_for_go_mapping_unique.write( domain + ForesterUtil.LINE_SEPARATOR );
2860 out_dc_for_go_mapping.close();
2861 out_dc_for_go_mapping_unique.close();
2862 final SortedMap<String, Integer> lca_rank_counts = new TreeMap<String, Integer>();
2863 final SortedMap<String, Integer> lca_ancestor_species_counts = new TreeMap<String, Integer>();
2864 for( final String dc : more_than_once ) {
2865 final List<PhylogenyNode> nodes = new ArrayList<PhylogenyNode>();
2866 for( final PhylogenyNodeIterator it = local_phylogeny_l.iteratorExternalForward(); it.hasNext(); ) {
2867 final PhylogenyNode n = it.next();
2868 if ( n.getNodeData().getBinaryCharacters().getGainedCharacters().contains( dc ) ) {
2872 for( int i = 0; i < ( nodes.size() - 1 ); ++i ) {
2873 for( int j = i + 1; j < nodes.size(); ++j ) {
2874 final PhylogenyNode lca = PhylogenyMethods.calculateLCA( nodes.get( i ), nodes.get( j ) );
2875 String rank = "unknown";
2876 if ( lca.getNodeData().isHasTaxonomy()
2877 && !ForesterUtil.isEmpty( lca.getNodeData().getTaxonomy().getRank() ) ) {
2878 rank = lca.getNodeData().getTaxonomy().getRank();
2880 addToCountMap( lca_rank_counts, rank );
2882 if ( lca.getNodeData().isHasTaxonomy()
2883 && !ForesterUtil.isEmpty( lca.getNodeData().getTaxonomy().getScientificName() ) ) {
2884 lca_species = lca.getNodeData().getTaxonomy().getScientificName();
2886 else if ( lca.getNodeData().isHasTaxonomy()
2887 && !ForesterUtil.isEmpty( lca.getNodeData().getTaxonomy().getCommonName() ) ) {
2888 lca_species = lca.getNodeData().getTaxonomy().getCommonName();
2891 lca_species = lca.getName();
2893 addToCountMap( lca_ancestor_species_counts, lca_species );
2897 final BufferedWriter out_for_rank_counts = new BufferedWriter( new FileWriter( outfilename_for_rank_counts ) );
2898 final BufferedWriter out_for_ancestor_species_counts = new BufferedWriter( new FileWriter( outfilename_for_ancestor_species_counts ) );
2899 ForesterUtil.map2writer( out_for_rank_counts, lca_rank_counts, "\t", ForesterUtil.LINE_SEPARATOR );
2900 ForesterUtil.map2writer( out_for_ancestor_species_counts,
2901 lca_ancestor_species_counts,
2903 ForesterUtil.LINE_SEPARATOR );
2904 out_for_rank_counts.close();
2905 out_for_ancestor_species_counts.close();
2906 if ( !ForesterUtil.isEmpty( outfilename_for_protein_stats ) && ( ( domain_length_stats_by_domain != null )
2907 || ( protein_length_stats_by_dc != null ) || ( domain_number_stats_by_dc != null ) ) ) {
2908 final BufferedWriter w = new BufferedWriter( new FileWriter( outfilename_for_protein_stats ) );
2909 w.write( "Domain Lengths: " );
2911 if ( domain_length_stats_by_domain != null ) {
2912 for( final Entry<Integer, DescriptiveStatistics> entry : dc_reapp_counts_to_domain_lengths_stats
2914 w.write( entry.getKey().toString() );
2915 w.write( "\t" + entry.getValue().arithmeticMean() );
2916 w.write( "\t" + entry.getValue().median() );
2923 w.write( "Protein Lengths: " );
2925 if ( protein_length_stats_by_dc != null ) {
2926 for( final Entry<Integer, DescriptiveStatistics> entry : dc_reapp_counts_to_protein_length_stats
2928 w.write( entry.getKey().toString() );
2929 w.write( "\t" + entry.getValue().arithmeticMean() );
2930 w.write( "\t" + entry.getValue().median() );
2937 w.write( "Number of domains: " );
2939 if ( domain_number_stats_by_dc != null ) {
2940 for( final Entry<Integer, DescriptiveStatistics> entry : dc_reapp_counts_to_domain_number_stats
2942 w.write( entry.getKey().toString() );
2943 w.write( "\t" + entry.getValue().arithmeticMean() );
2944 w.write( "\t" + entry.getValue().median() );
2951 w.write( "Gained once, domain lengths:" );
2953 w.write( "N: " + gained_once_domain_length_count );
2955 w.write( "Avg: " + ( ( double ) gained_once_domain_length_sum / gained_once_domain_length_count ) );
2958 w.write( "Gained multiple times, domain lengths:" );
2960 w.write( "N: " + gained_multiple_times_domain_length_count );
2962 w.write( "Avg: " + ( ( double ) gained_multiple_times_domain_length_sum
2963 / gained_multiple_times_domain_length_count ) );
2968 w.write( "Gained once, protein lengths:" );
2970 w.write( gained_once_lengths_stats.toString() );
2971 gained_once_lengths_stats = null;
2974 w.write( "Gained once, domain counts:" );
2976 w.write( gained_once_domain_count_stats.toString() );
2977 gained_once_domain_count_stats = null;
2980 w.write( "Gained multiple times, protein lengths:" );
2982 w.write( gained_multiple_times_lengths_stats.toString() );
2983 gained_multiple_times_lengths_stats = null;
2986 w.write( "Gained multiple times, domain counts:" );
2988 w.write( gained_multiple_times_domain_count_stats.toString() );
2993 catch ( final IOException e ) {
2994 ForesterUtil.printWarningMessage( surfacing.PRG_NAME, "Failure to write: " + e );
2996 ForesterUtil.programMessage( surfacing.PRG_NAME,
2997 "Wrote independent domain combination gains fitch counts to ["
2998 + outfilename_for_counts + "]" );
2999 ForesterUtil.programMessage( surfacing.PRG_NAME,
3000 "Wrote independent domain combination gains fitch lists to [" + outfilename_for_dc
3002 ForesterUtil.programMessage( surfacing.PRG_NAME,
3003 "Wrote independent domain combination gains fitch lists to (for GO mapping) ["
3004 + outfilename_for_dc_for_go_mapping + "]" );
3005 ForesterUtil.programMessage( surfacing.PRG_NAME,
3006 "Wrote independent domain combination gains fitch lists to (for GO mapping, unique) ["
3007 + outfilename_for_dc_for_go_mapping_unique + "]" );
3010 private static SortedSet<String> collectAllDomainsChangedOnSubtree( final PhylogenyNode subtree_root,
3011 final boolean get_gains ) {
3012 final SortedSet<String> domains = new TreeSet<String>();
3013 for( final PhylogenyNode descendant : PhylogenyMethods.getAllDescendants( subtree_root ) ) {
3014 final BinaryCharacters chars = descendant.getNodeData().getBinaryCharacters();
3016 domains.addAll( chars.getGainedCharacters() );
3019 domains.addAll( chars.getLostCharacters() );
3025 private static File createBaseDirForPerNodeDomainFiles( final String base_dir,
3026 final boolean domain_combinations,
3027 final CharacterStateMatrix.GainLossStates state,
3028 final String outfile ) {
3029 File per_node_go_mapped_domain_gain_loss_files_base_dir = new File( new File( outfile ).getParent()
3030 + ForesterUtil.FILE_SEPARATOR + base_dir );
3031 if ( !per_node_go_mapped_domain_gain_loss_files_base_dir.exists() ) {
3032 per_node_go_mapped_domain_gain_loss_files_base_dir.mkdir();
3034 if ( domain_combinations ) {
3035 per_node_go_mapped_domain_gain_loss_files_base_dir = new File( per_node_go_mapped_domain_gain_loss_files_base_dir
3036 + ForesterUtil.FILE_SEPARATOR + "DC" );
3039 per_node_go_mapped_domain_gain_loss_files_base_dir = new File( per_node_go_mapped_domain_gain_loss_files_base_dir
3040 + ForesterUtil.FILE_SEPARATOR + "DOMAINS" );
3042 if ( !per_node_go_mapped_domain_gain_loss_files_base_dir.exists() ) {
3043 per_node_go_mapped_domain_gain_loss_files_base_dir.mkdir();
3045 if ( state == GainLossStates.GAIN ) {
3046 per_node_go_mapped_domain_gain_loss_files_base_dir = new File( per_node_go_mapped_domain_gain_loss_files_base_dir
3047 + ForesterUtil.FILE_SEPARATOR + "GAINS" );
3049 else if ( state == GainLossStates.LOSS ) {
3050 per_node_go_mapped_domain_gain_loss_files_base_dir = new File( per_node_go_mapped_domain_gain_loss_files_base_dir
3051 + ForesterUtil.FILE_SEPARATOR + "LOSSES" );
3054 per_node_go_mapped_domain_gain_loss_files_base_dir = new File( per_node_go_mapped_domain_gain_loss_files_base_dir
3055 + ForesterUtil.FILE_SEPARATOR + "PRESENT" );
3057 if ( !per_node_go_mapped_domain_gain_loss_files_base_dir.exists() ) {
3058 per_node_go_mapped_domain_gain_loss_files_base_dir.mkdir();
3060 return per_node_go_mapped_domain_gain_loss_files_base_dir;
3063 private static SortedSet<BinaryDomainCombination> createSetOfAllBinaryDomainCombinationsPerGenome( final GenomeWideCombinableDomains gwcd ) {
3064 final SortedMap<String, CombinableDomains> cds = gwcd.getAllCombinableDomainsIds();
3065 final SortedSet<BinaryDomainCombination> binary_combinations = new TreeSet<BinaryDomainCombination>();
3066 for( final String domain_id : cds.keySet() ) {
3067 final CombinableDomains cd = cds.get( domain_id );
3068 binary_combinations.addAll( cd.toBinaryDomainCombinations() );
3070 return binary_combinations;
3073 private static void printSomeStats( final DescriptiveStatistics stats, final AsciiHistogram histo, final Writer w )
3074 throws IOException {
3077 w.write( SurfacingConstants.NL );
3078 w.write( "<tt><pre>" );
3079 w.write( SurfacingConstants.NL );
3080 if ( histo != null ) {
3081 w.write( histo.toStringBuffer( 20, '|', 40, 5 ).toString() );
3082 w.write( SurfacingConstants.NL );
3084 w.write( "</pre></tt>" );
3085 w.write( SurfacingConstants.NL );
3086 w.write( "<table>" );
3087 w.write( SurfacingConstants.NL );
3088 w.write( "<tr><td>N: </td><td>" + stats.getN() + "</td></tr>" );
3089 w.write( SurfacingConstants.NL );
3090 w.write( "<tr><td>Min: </td><td>" + stats.getMin() + "</td></tr>" );
3091 w.write( SurfacingConstants.NL );
3092 w.write( "<tr><td>Max: </td><td>" + stats.getMax() + "</td></tr>" );
3093 w.write( SurfacingConstants.NL );
3094 w.write( "<tr><td>Mean: </td><td>" + stats.arithmeticMean() + "</td></tr>" );
3095 w.write( SurfacingConstants.NL );
3096 if ( stats.getN() > 1 ) {
3097 w.write( "<tr><td>SD: </td><td>" + stats.sampleStandardDeviation() + "</td></tr>" );
3100 w.write( "<tr><td>SD: </td><td>n/a</td></tr>" );
3102 w.write( SurfacingConstants.NL );
3103 w.write( "</table>" );
3104 w.write( SurfacingConstants.NL );
3106 w.write( SurfacingConstants.NL );
3109 private static List<String> splitDomainCombination( final String dc ) {
3110 final String[] s = dc.split( "=" );
3111 if ( s.length != 2 ) {
3112 ForesterUtil.printErrorMessage( surfacing.PRG_NAME,
3113 "Stringyfied domain combination has illegal format: " + dc );
3116 final List<String> l = new ArrayList<String>( 2 );
3122 private static void writeAllEncounteredPfamsToFile( final Map<String, List<GoId>> domain_id_to_go_ids_map,
3123 final Map<GoId, GoTerm> go_id_to_term_map,
3124 final String outfile_name,
3125 final SortedSet<String> all_pfams_encountered ) {
3126 final File all_pfams_encountered_file = new File( outfile_name + surfacing.ALL_PFAMS_ENCOUNTERED_SUFFIX );
3127 final File all_pfams_encountered_with_go_annotation_file = new File( outfile_name
3128 + surfacing.ALL_PFAMS_ENCOUNTERED_WITH_GO_ANNOTATION_SUFFIX );
3129 final File encountered_pfams_summary_file = new File( outfile_name
3130 + surfacing.ENCOUNTERED_PFAMS_SUMMARY_SUFFIX );
3131 int biological_process_counter = 0;
3132 int cellular_component_counter = 0;
3133 int molecular_function_counter = 0;
3134 int pfams_with_mappings_counter = 0;
3135 int pfams_without_mappings_counter = 0;
3136 int pfams_without_mappings_to_bp_or_mf_counter = 0;
3137 int pfams_with_mappings_to_bp_or_mf_counter = 0;
3139 final Writer all_pfams_encountered_writer = new BufferedWriter( new FileWriter( all_pfams_encountered_file ) );
3140 final Writer all_pfams_encountered_with_go_annotation_writer = new BufferedWriter( new FileWriter( all_pfams_encountered_with_go_annotation_file ) );
3141 final Writer summary_writer = new BufferedWriter( new FileWriter( encountered_pfams_summary_file ) );
3142 summary_writer.write( "# Pfam to GO mapping summary" );
3143 summary_writer.write( ForesterUtil.LINE_SEPARATOR );
3144 summary_writer.write( "# Actual summary is at the end of this file." );
3145 summary_writer.write( ForesterUtil.LINE_SEPARATOR );
3146 summary_writer.write( "# Encountered Pfams without a GO mapping:" );
3147 summary_writer.write( ForesterUtil.LINE_SEPARATOR );
3148 for( final String pfam : all_pfams_encountered ) {
3149 all_pfams_encountered_writer.write( pfam );
3150 all_pfams_encountered_writer.write( ForesterUtil.LINE_SEPARATOR );
3151 final String domain_id = new String( pfam );
3152 if ( domain_id_to_go_ids_map.containsKey( domain_id ) ) {
3153 ++pfams_with_mappings_counter;
3154 all_pfams_encountered_with_go_annotation_writer.write( pfam );
3155 all_pfams_encountered_with_go_annotation_writer.write( ForesterUtil.LINE_SEPARATOR );
3156 final List<GoId> go_ids = domain_id_to_go_ids_map.get( domain_id );
3157 boolean maps_to_bp = false;
3158 boolean maps_to_cc = false;
3159 boolean maps_to_mf = false;
3160 for( final GoId go_id : go_ids ) {
3161 final GoTerm go_term = go_id_to_term_map.get( go_id );
3162 if ( go_term.getGoNameSpace().isBiologicalProcess() ) {
3165 else if ( go_term.getGoNameSpace().isCellularComponent() ) {
3168 else if ( go_term.getGoNameSpace().isMolecularFunction() ) {
3173 ++biological_process_counter;
3176 ++cellular_component_counter;
3179 ++molecular_function_counter;
3181 if ( maps_to_bp || maps_to_mf ) {
3182 ++pfams_with_mappings_to_bp_or_mf_counter;
3185 ++pfams_without_mappings_to_bp_or_mf_counter;
3189 ++pfams_without_mappings_to_bp_or_mf_counter;
3190 ++pfams_without_mappings_counter;
3191 summary_writer.write( pfam );
3192 summary_writer.write( ForesterUtil.LINE_SEPARATOR );
3195 all_pfams_encountered_writer.close();
3196 all_pfams_encountered_with_go_annotation_writer.close();
3197 ForesterUtil.programMessage( surfacing.PRG_NAME,
3198 "Wrote all [" + all_pfams_encountered.size() + "] encountered Pfams to: \""
3199 + all_pfams_encountered_file + "\"" );
3200 ForesterUtil.programMessage( surfacing.PRG_NAME,
3201 "Wrote all [" + pfams_with_mappings_counter
3202 + "] encountered Pfams with GO mappings to: \""
3203 + all_pfams_encountered_with_go_annotation_file + "\"" );
3204 ForesterUtil.programMessage( surfacing.PRG_NAME,
3205 "Wrote summary (including all [" + pfams_without_mappings_counter
3206 + "] encountered Pfams without GO mappings) to: \""
3207 + encountered_pfams_summary_file + "\"" );
3208 ForesterUtil.programMessage( surfacing.PRG_NAME,
3209 "Sum of Pfams encountered : " + all_pfams_encountered.size() );
3210 ForesterUtil.programMessage( surfacing.PRG_NAME,
3211 "Pfams without a mapping : " + pfams_without_mappings_counter
3212 + " [" + ( ( 100 * pfams_without_mappings_counter )
3213 / all_pfams_encountered.size() )
3215 ForesterUtil.programMessage( surfacing.PRG_NAME,
3216 "Pfams without mapping to proc. or func. : "
3217 + pfams_without_mappings_to_bp_or_mf_counter + " ["
3218 + ( ( 100 * pfams_without_mappings_to_bp_or_mf_counter )
3219 / all_pfams_encountered.size() )
3222 .programMessage( surfacing.PRG_NAME,
3223 "Pfams with a mapping : " + pfams_with_mappings_counter + " ["
3224 + ( ( 100 * pfams_with_mappings_counter ) / all_pfams_encountered.size() )
3226 ForesterUtil.programMessage( surfacing.PRG_NAME,
3227 "Pfams with a mapping to proc. or func. : "
3228 + pfams_with_mappings_to_bp_or_mf_counter + " ["
3229 + ( ( 100 * pfams_with_mappings_to_bp_or_mf_counter )
3230 / all_pfams_encountered.size() )
3233 .programMessage( surfacing.PRG_NAME,
3234 "Pfams with mapping to biological process: " + biological_process_counter + " ["
3235 + ( ( 100 * biological_process_counter ) / all_pfams_encountered.size() )
3238 .programMessage( surfacing.PRG_NAME,
3239 "Pfams with mapping to molecular function: " + molecular_function_counter + " ["
3240 + ( ( 100 * molecular_function_counter ) / all_pfams_encountered.size() )
3243 .programMessage( surfacing.PRG_NAME,
3244 "Pfams with mapping to cellular component: " + cellular_component_counter + " ["
3245 + ( ( 100 * cellular_component_counter ) / all_pfams_encountered.size() )
3247 summary_writer.write( ForesterUtil.LINE_SEPARATOR );
3248 summary_writer.write( "# Sum of Pfams encountered : " + all_pfams_encountered.size() );
3249 summary_writer.write( ForesterUtil.LINE_SEPARATOR );
3250 summary_writer.write( "# Pfams without a mapping : " + pfams_without_mappings_counter + " ["
3251 + ( ( 100 * pfams_without_mappings_counter ) / all_pfams_encountered.size() ) + "%]" );
3252 summary_writer.write( ForesterUtil.LINE_SEPARATOR );
3253 summary_writer.write( "# Pfams without mapping to proc. or func. : "
3254 + pfams_without_mappings_to_bp_or_mf_counter + " ["
3255 + ( ( 100 * pfams_without_mappings_to_bp_or_mf_counter ) / all_pfams_encountered.size() ) + "%]" );
3256 summary_writer.write( ForesterUtil.LINE_SEPARATOR );
3257 summary_writer.write( "# Pfams with a mapping : " + pfams_with_mappings_counter + " ["
3258 + ( ( 100 * pfams_with_mappings_counter ) / all_pfams_encountered.size() ) + "%]" );
3259 summary_writer.write( ForesterUtil.LINE_SEPARATOR );
3260 summary_writer.write( "# Pfams with a mapping to proc. or func. : "
3261 + pfams_with_mappings_to_bp_or_mf_counter + " ["
3262 + ( ( 100 * pfams_with_mappings_to_bp_or_mf_counter ) / all_pfams_encountered.size() ) + "%]" );
3263 summary_writer.write( ForesterUtil.LINE_SEPARATOR );
3264 summary_writer.write( "# Pfams with mapping to biological process: " + biological_process_counter + " ["
3265 + ( ( 100 * biological_process_counter ) / all_pfams_encountered.size() ) + "%]" );
3266 summary_writer.write( ForesterUtil.LINE_SEPARATOR );
3267 summary_writer.write( "# Pfams with mapping to molecular function: " + molecular_function_counter + " ["
3268 + ( ( 100 * molecular_function_counter ) / all_pfams_encountered.size() ) + "%]" );
3269 summary_writer.write( ForesterUtil.LINE_SEPARATOR );
3270 summary_writer.write( "# Pfams with mapping to cellular component: " + cellular_component_counter + " ["
3271 + ( ( 100 * cellular_component_counter ) / all_pfams_encountered.size() ) + "%]" );
3272 summary_writer.write( ForesterUtil.LINE_SEPARATOR );
3273 summary_writer.close();
3275 catch ( final IOException e ) {
3276 ForesterUtil.printWarningMessage( surfacing.PRG_NAME, "Failure to write: " + e );
3280 private final static void writeColorLabels( final String l, final Color c, final Writer w ) throws IOException {
3281 w.write( "<tr><td><b><span style=\"color:" );
3282 w.write( String.format( "#%02x%02x%02x", c.getRed(), c.getGreen(), c.getBlue() ) );
3285 w.write( "</span></b></td></tr>" );
3286 w.write( SurfacingConstants.NL );
3289 private static void writeDomainData( final Map<String, List<GoId>> domain_id_to_go_ids_map,
3290 final Map<GoId, GoTerm> go_id_to_term_map,
3291 final GoNameSpace go_namespace_limit,
3293 final String domain_0,
3294 final String domain_1,
3295 final String prefix_for_html,
3296 final String character_separator_for_non_html_output,
3297 final Map<String, Set<String>>[] domain_id_to_secondary_features_maps,
3298 final Set<GoId> all_go_ids )
3299 throws IOException {
3300 boolean any_go_annotation_present = false;
3301 boolean first_has_no_go = false;
3302 int domain_count = 2; // To distinguish between domains and binary domain combinations.
3303 if ( ForesterUtil.isEmpty( domain_1 ) ) {
3306 // The following has a difficult to understand logic.
3307 for( int d = 0; d < domain_count; ++d ) {
3308 List<GoId> go_ids = null;
3309 boolean go_annotation_present = false;
3311 if ( domain_id_to_go_ids_map.containsKey( domain_0 ) ) {
3312 go_annotation_present = true;
3313 any_go_annotation_present = true;
3314 go_ids = domain_id_to_go_ids_map.get( domain_0 );
3317 first_has_no_go = true;
3321 if ( domain_id_to_go_ids_map.containsKey( domain_1 ) ) {
3322 go_annotation_present = true;
3323 any_go_annotation_present = true;
3324 go_ids = domain_id_to_go_ids_map.get( domain_1 );
3327 if ( go_annotation_present ) {
3328 boolean first = ( ( d == 0 ) || ( ( d == 1 ) && first_has_no_go ) );
3329 for( final GoId go_id : go_ids ) {
3330 out.write( "<tr>" );
3333 writeDomainIdsToHtml( out,
3337 domain_id_to_secondary_features_maps );
3340 out.write( "<td></td>" );
3342 if ( !go_id_to_term_map.containsKey( go_id ) ) {
3343 throw new IllegalArgumentException( "GO-id [" + go_id + "] not found in GO-id to GO-term map" );
3345 final GoTerm go_term = go_id_to_term_map.get( go_id );
3346 if ( ( go_namespace_limit == null ) || go_namespace_limit.equals( go_term.getGoNameSpace() ) ) {
3347 // final String top = GoUtils.getPenultimateGoTerm( go_term, go_id_to_term_map ).getName();
3348 final String go_id_str = go_id.getId();
3349 out.write( "<td>" );
3350 out.write( "<a href=\"" + SurfacingConstants.AMIGO_LINK + go_id_str
3351 + "\" target=\"amigo_window\">" + go_id_str + "</a>" );
3352 out.write( "</td><td>" );
3353 out.write( go_term.getName() );
3354 if ( domain_count == 2 ) {
3355 out.write( " (" + d + ")" );
3357 out.write( "</td><td>" );
3358 // out.write( top );
3359 // out.write( "</td><td>" );
3361 out.write( go_term.getGoNameSpace().toShortString() );
3363 out.write( "</td>" );
3364 if ( all_go_ids != null ) {
3365 all_go_ids.add( go_id );
3369 out.write( "<td>" );
3370 out.write( "</td><td>" );
3371 out.write( "</td><td>" );
3372 out.write( "</td><td>" );
3373 out.write( "</td>" );
3375 out.write( "</tr>" );
3376 out.write( SurfacingConstants.NL );
3379 } // for( int d = 0; d < domain_count; ++d )
3380 if ( !any_go_annotation_present ) {
3381 out.write( "<tr>" );
3382 writeDomainIdsToHtml( out, domain_0, domain_1, prefix_for_html, domain_id_to_secondary_features_maps );
3383 out.write( "<td>" );
3384 out.write( "</td><td>" );
3385 out.write( "</td><td>" );
3386 out.write( "</td><td>" );
3387 out.write( "</td>" );
3388 out.write( "</tr>" );
3389 out.write( SurfacingConstants.NL );
3393 private static void writeDomainIdsToHtml( final Writer out,
3394 final String domain_0,
3395 final String domain_1,
3396 final String prefix_for_detailed_html,
3397 final Map<String, Set<String>>[] domain_id_to_secondary_features_maps )
3398 throws IOException {
3399 out.write( "<td>" );
3400 if ( !ForesterUtil.isEmpty( prefix_for_detailed_html ) ) {
3401 out.write( prefix_for_detailed_html );
3404 out.write( "<a href=\"" + SurfacingConstants.PFAM_FAMILY_ID_LINK + domain_0 + "\">" + domain_0 + "</a>" );
3405 out.write( "</td>" );
3408 private static void writeDomainsToIndividualFilePerTreeNode( final Writer individual_files_writer,
3409 final String domain_0,
3410 final String domain_1 )
3411 throws IOException {
3412 individual_files_writer.write( domain_0 );
3413 individual_files_writer.write( ForesterUtil.LINE_SEPARATOR );
3414 if ( !ForesterUtil.isEmpty( domain_1 ) ) {
3415 individual_files_writer.write( domain_1 );
3416 individual_files_writer.write( ForesterUtil.LINE_SEPARATOR );
3420 private static void writePfamsToFile( final String outfile_name, final SortedSet<String> pfams ) {
3422 final Writer writer = new BufferedWriter( new FileWriter( new File( outfile_name ) ) );
3423 for( final String pfam : pfams ) {
3424 writer.write( pfam );
3425 writer.write( ForesterUtil.LINE_SEPARATOR );
3428 ForesterUtil.programMessage( surfacing.PRG_NAME,
3429 "Wrote " + pfams.size() + " pfams to [" + outfile_name + "]" );
3431 catch ( final IOException e ) {
3432 ForesterUtil.printWarningMessage( surfacing.PRG_NAME, "Failure to write: " + e );
3436 private static void writeToNexus( final String outfile_name,
3437 final CharacterStateMatrix<BinaryStates> matrix,
3438 final Phylogeny phylogeny ) {
3439 if ( !( matrix instanceof BasicCharacterStateMatrix ) ) {
3440 throw new IllegalArgumentException( "can only write matrices of type [" + BasicCharacterStateMatrix.class
3443 final BasicCharacterStateMatrix<BinaryStates> my_matrix = ( org.forester.evoinference.matrix.character.BasicCharacterStateMatrix<BinaryStates> ) matrix;
3444 final List<Phylogeny> phylogenies = new ArrayList<Phylogeny>( 1 );
3445 phylogenies.add( phylogeny );
3447 final BufferedWriter w = new BufferedWriter( new FileWriter( outfile_name ) );
3448 w.write( NexusConstants.NEXUS );
3449 w.write( ForesterUtil.LINE_SEPARATOR );
3450 my_matrix.writeNexusTaxaBlock( w );
3451 my_matrix.writeNexusBinaryChractersBlock( w );
3452 PhylogenyWriter.writeNexusTreesBlock( w, phylogenies, NH_CONVERSION_SUPPORT_VALUE_STYLE.NONE );
3455 ForesterUtil.programMessage( surfacing.PRG_NAME, "Wrote Nexus file: \"" + outfile_name + "\"" );
3457 catch ( final IOException e ) {
3458 ForesterUtil.fatalError( surfacing.PRG_NAME, e.getMessage() );
3462 private static void writeToNexus( final String outfile_name,
3463 final DomainParsimonyCalculator domain_parsimony,
3464 final Phylogeny phylogeny ) {
3465 writeToNexus( outfile_name + surfacing.NEXUS_EXTERNAL_DOMAINS,
3466 domain_parsimony.createMatrixOfDomainPresenceOrAbsence(),
3468 writeToNexus( outfile_name + surfacing.NEXUS_EXTERNAL_DOMAIN_COMBINATIONS,
3469 domain_parsimony.createMatrixOfBinaryDomainCombinationPresenceOrAbsence(),
3473 final static class DomainComparator implements Comparator<Domain> {
3475 final private boolean _ascending;
3477 public DomainComparator( final boolean ascending ) {
3478 _ascending = ascending;
3482 public final int compare( final Domain d0, final Domain d1 ) {
3483 if ( d0.getFrom() < d1.getFrom() ) {
3484 return _ascending ? -1 : 1;
3486 else if ( d0.getFrom() > d1.getFrom() ) {
3487 return _ascending ? 1 : -1;