+ final BufferedWriter out_for_rank_counts = new BufferedWriter( new FileWriter( outfilename_for_rank_counts ) );
+ final BufferedWriter out_for_ancestor_species_counts = new BufferedWriter( new FileWriter( outfilename_for_ancestor_species_counts ) );
+ ForesterUtil.map2writer( out_for_rank_counts, lca_rank_counts, "\t", ForesterUtil.LINE_SEPARATOR );
+ ForesterUtil.map2writer( out_for_ancestor_species_counts,
+ lca_ancestor_species_counts,
+ "\t",
+ ForesterUtil.LINE_SEPARATOR );
+ out_for_rank_counts.close();
+ out_for_ancestor_species_counts.close();
+ if ( !ForesterUtil.isEmpty( outfilename_for_protein_stats )
+ && ( ( domain_length_stats_by_domain != null ) || ( protein_length_stats_by_dc != null ) || ( domain_number_stats_by_dc != null ) ) ) {
+ final BufferedWriter w = new BufferedWriter( new FileWriter( outfilename_for_protein_stats ) );
+ w.write( "Domain Lengths: " );
+ w.write( "\n" );
+ if ( domain_length_stats_by_domain != null ) {
+ for( final Entry<Integer, DescriptiveStatistics> entry : dc_reapp_counts_to_domain_lengths_stats
+ .entrySet() ) {
+ w.write( entry.getKey().toString() );
+ w.write( "\t" + entry.getValue().arithmeticMean() );
+ w.write( "\t" + entry.getValue().median() );
+ w.write( "\n" );
+ }
+ }
+ w.flush();
+ w.write( "\n" );
+ w.write( "\n" );
+ w.write( "Protein Lengths: " );
+ w.write( "\n" );
+ if ( protein_length_stats_by_dc != null ) {
+ for( final Entry<Integer, DescriptiveStatistics> entry : dc_reapp_counts_to_protein_length_stats
+ .entrySet() ) {
+ w.write( entry.getKey().toString() );
+ w.write( "\t" + entry.getValue().arithmeticMean() );
+ w.write( "\t" + entry.getValue().median() );
+ w.write( "\n" );
+ }
+ }
+ w.flush();
+ w.write( "\n" );
+ w.write( "\n" );
+ w.write( "Number of domains: " );
+ w.write( "\n" );
+ if ( domain_number_stats_by_dc != null ) {
+ for( final Entry<Integer, DescriptiveStatistics> entry : dc_reapp_counts_to_domain_number_stats
+ .entrySet() ) {
+ w.write( entry.getKey().toString() );
+ w.write( "\t" + entry.getValue().arithmeticMean() );
+ w.write( "\t" + entry.getValue().median() );
+ w.write( "\n" );
+ }
+ }
+ w.flush();
+ w.write( "\n" );
+ w.write( "\n" );
+ w.write( "Gained once, domain lengths:" );
+ w.write( "\n" );
+ w.write( "N: " + gained_once_domain_length_count );
+ w.write( "\n" );
+ w.write( "Avg: " + ( ( double ) gained_once_domain_length_sum / gained_once_domain_length_count ) );
+ w.write( "\n" );
+ w.write( "\n" );
+ w.write( "Gained multiple times, domain lengths:" );
+ w.write( "\n" );
+ w.write( "N: " + gained_multiple_times_domain_length_count );
+ w.write( "\n" );
+ w.write( "Avg: "
+ + ( ( double ) gained_multiple_times_domain_length_sum / gained_multiple_times_domain_length_count ) );
+ w.write( "\n" );
+ w.write( "\n" );
+ w.write( "\n" );
+ w.write( "\n" );
+ w.write( "Gained once, protein lengths:" );
+ w.write( "\n" );
+ w.write( gained_once_lengths_stats.toString() );
+ gained_once_lengths_stats = null;
+ w.write( "\n" );
+ w.write( "\n" );
+ w.write( "Gained once, domain counts:" );
+ w.write( "\n" );
+ w.write( gained_once_domain_count_stats.toString() );
+ gained_once_domain_count_stats = null;
+ w.write( "\n" );
+ w.write( "\n" );
+ w.write( "Gained multiple times, protein lengths:" );
+ w.write( "\n" );
+ w.write( gained_multiple_times_lengths_stats.toString() );
+ gained_multiple_times_lengths_stats = null;
+ w.write( "\n" );
+ w.write( "\n" );
+ w.write( "Gained multiple times, domain counts:" );
+ w.write( "\n" );
+ w.write( gained_multiple_times_domain_count_stats.toString() );
+ w.flush();
+ w.close();
+ }
+ }
+ catch ( final IOException e ) {
+ ForesterUtil.printWarningMessage( surfacing.PRG_NAME, "Failure to write: " + e );
+ }
+ ForesterUtil.programMessage( surfacing.PRG_NAME, "Wrote independent domain combination gains fitch counts to ["
+ + outfilename_for_counts + "]" );
+ ForesterUtil.programMessage( surfacing.PRG_NAME, "Wrote independent domain combination gains fitch lists to ["
+ + outfilename_for_dc + "]" );
+ ForesterUtil.programMessage( surfacing.PRG_NAME,
+ "Wrote independent domain combination gains fitch lists to (for GO mapping) ["
+ + outfilename_for_dc_for_go_mapping + "]" );
+ ForesterUtil.programMessage( surfacing.PRG_NAME,
+ "Wrote independent domain combination gains fitch lists to (for GO mapping, unique) ["
+ + outfilename_for_dc_for_go_mapping_unique + "]" );
+ }
+
+ private static SortedSet<String> collectAllDomainsChangedOnSubtree( final PhylogenyNode subtree_root,
+ final boolean get_gains ) {
+ final SortedSet<String> domains = new TreeSet<String>();
+ for( final PhylogenyNode descendant : PhylogenyMethods.getAllDescendants( subtree_root ) ) {
+ final BinaryCharacters chars = descendant.getNodeData().getBinaryCharacters();
+ if ( get_gains ) {
+ domains.addAll( chars.getGainedCharacters() );
+ }
+ else {
+ domains.addAll( chars.getLostCharacters() );
+ }
+ }
+ return domains;
+ }
+
+ private static File createBaseDirForPerNodeDomainFiles( final String base_dir,
+ final boolean domain_combinations,
+ final CharacterStateMatrix.GainLossStates state,
+ final String outfile ) {
+ File per_node_go_mapped_domain_gain_loss_files_base_dir = new File( new File( outfile ).getParent()
+ + ForesterUtil.FILE_SEPARATOR + base_dir );
+ if ( !per_node_go_mapped_domain_gain_loss_files_base_dir.exists() ) {
+ per_node_go_mapped_domain_gain_loss_files_base_dir.mkdir();
+ }
+ if ( domain_combinations ) {
+ per_node_go_mapped_domain_gain_loss_files_base_dir = new File( per_node_go_mapped_domain_gain_loss_files_base_dir
+ + ForesterUtil.FILE_SEPARATOR + "DC" );
+ }
+ else {
+ per_node_go_mapped_domain_gain_loss_files_base_dir = new File( per_node_go_mapped_domain_gain_loss_files_base_dir
+ + ForesterUtil.FILE_SEPARATOR + "DOMAINS" );
+ }
+ if ( !per_node_go_mapped_domain_gain_loss_files_base_dir.exists() ) {
+ per_node_go_mapped_domain_gain_loss_files_base_dir.mkdir();
+ }
+ if ( state == GainLossStates.GAIN ) {
+ per_node_go_mapped_domain_gain_loss_files_base_dir = new File( per_node_go_mapped_domain_gain_loss_files_base_dir
+ + ForesterUtil.FILE_SEPARATOR + "GAINS" );
+ }
+ else if ( state == GainLossStates.LOSS ) {
+ per_node_go_mapped_domain_gain_loss_files_base_dir = new File( per_node_go_mapped_domain_gain_loss_files_base_dir
+ + ForesterUtil.FILE_SEPARATOR + "LOSSES" );
+ }
+ else {
+ per_node_go_mapped_domain_gain_loss_files_base_dir = new File( per_node_go_mapped_domain_gain_loss_files_base_dir
+ + ForesterUtil.FILE_SEPARATOR + "PRESENT" );
+ }
+ if ( !per_node_go_mapped_domain_gain_loss_files_base_dir.exists() ) {
+ per_node_go_mapped_domain_gain_loss_files_base_dir.mkdir();
+ }
+ return per_node_go_mapped_domain_gain_loss_files_base_dir;
+ }
+
+ private static SortedSet<BinaryDomainCombination> createSetOfAllBinaryDomainCombinationsPerGenome( final GenomeWideCombinableDomains gwcd ) {
+ final SortedMap<String, CombinableDomains> cds = gwcd.getAllCombinableDomainsIds();
+ final SortedSet<BinaryDomainCombination> binary_combinations = new TreeSet<BinaryDomainCombination>();
+ for( final String domain_id : cds.keySet() ) {
+ final CombinableDomains cd = cds.get( domain_id );
+ binary_combinations.addAll( cd.toBinaryDomainCombinations() );
+ }
+ return binary_combinations;
+ }
+
+ private static List<String> splitDomainCombination( final String dc ) {
+ final String[] s = dc.split( "=" );
+ if ( s.length != 2 ) {
+ ForesterUtil.printErrorMessage( surfacing.PRG_NAME, "Stringyfied domain combination has illegal format: "
+ + dc );
+ System.exit( -1 );
+ }
+ final List<String> l = new ArrayList<String>( 2 );
+ l.add( s[ 0 ] );
+ l.add( s[ 1 ] );
+ return l;
+ }
+
+ private static void writeAllEncounteredPfamsToFile( final Map<String, List<GoId>> domain_id_to_go_ids_map,
+ final Map<GoId, GoTerm> go_id_to_term_map,
+ final String outfile_name,
+ final SortedSet<String> all_pfams_encountered ) {
+ final File all_pfams_encountered_file = new File( outfile_name + surfacing.ALL_PFAMS_ENCOUNTERED_SUFFIX );
+ final File all_pfams_encountered_with_go_annotation_file = new File( outfile_name
+ + surfacing.ALL_PFAMS_ENCOUNTERED_WITH_GO_ANNOTATION_SUFFIX );
+ final File encountered_pfams_summary_file = new File( outfile_name + surfacing.ENCOUNTERED_PFAMS_SUMMARY_SUFFIX );
+ int biological_process_counter = 0;
+ int cellular_component_counter = 0;
+ int molecular_function_counter = 0;
+ int pfams_with_mappings_counter = 0;
+ int pfams_without_mappings_counter = 0;
+ int pfams_without_mappings_to_bp_or_mf_counter = 0;
+ int pfams_with_mappings_to_bp_or_mf_counter = 0;
+ try {
+ final Writer all_pfams_encountered_writer = new BufferedWriter( new FileWriter( all_pfams_encountered_file ) );
+ final Writer all_pfams_encountered_with_go_annotation_writer = new BufferedWriter( new FileWriter( all_pfams_encountered_with_go_annotation_file ) );
+ final Writer summary_writer = new BufferedWriter( new FileWriter( encountered_pfams_summary_file ) );
+ summary_writer.write( "# Pfam to GO mapping summary" );
+ summary_writer.write( ForesterUtil.LINE_SEPARATOR );
+ summary_writer.write( "# Actual summary is at the end of this file." );
+ summary_writer.write( ForesterUtil.LINE_SEPARATOR );
+ summary_writer.write( "# Encountered Pfams without a GO mapping:" );
+ summary_writer.write( ForesterUtil.LINE_SEPARATOR );
+ for( final String pfam : all_pfams_encountered ) {
+ all_pfams_encountered_writer.write( pfam );
+ all_pfams_encountered_writer.write( ForesterUtil.LINE_SEPARATOR );
+ final String domain_id = new String( pfam );
+ if ( domain_id_to_go_ids_map.containsKey( domain_id ) ) {
+ ++pfams_with_mappings_counter;
+ all_pfams_encountered_with_go_annotation_writer.write( pfam );
+ all_pfams_encountered_with_go_annotation_writer.write( ForesterUtil.LINE_SEPARATOR );
+ final List<GoId> go_ids = domain_id_to_go_ids_map.get( domain_id );
+ boolean maps_to_bp = false;
+ boolean maps_to_cc = false;
+ boolean maps_to_mf = false;
+ for( final GoId go_id : go_ids ) {
+ final GoTerm go_term = go_id_to_term_map.get( go_id );
+ if ( go_term.getGoNameSpace().isBiologicalProcess() ) {
+ maps_to_bp = true;
+ }
+ else if ( go_term.getGoNameSpace().isCellularComponent() ) {
+ maps_to_cc = true;
+ }
+ else if ( go_term.getGoNameSpace().isMolecularFunction() ) {
+ maps_to_mf = true;
+ }
+ }
+ if ( maps_to_bp ) {
+ ++biological_process_counter;
+ }
+ if ( maps_to_cc ) {
+ ++cellular_component_counter;
+ }
+ if ( maps_to_mf ) {
+ ++molecular_function_counter;
+ }
+ if ( maps_to_bp || maps_to_mf ) {
+ ++pfams_with_mappings_to_bp_or_mf_counter;
+ }
+ else {
+ ++pfams_without_mappings_to_bp_or_mf_counter;
+ }
+ }
+ else {
+ ++pfams_without_mappings_to_bp_or_mf_counter;
+ ++pfams_without_mappings_counter;
+ summary_writer.write( pfam );
+ summary_writer.write( ForesterUtil.LINE_SEPARATOR );
+ }
+ }
+ all_pfams_encountered_writer.close();
+ all_pfams_encountered_with_go_annotation_writer.close();
+ ForesterUtil.programMessage( surfacing.PRG_NAME, "Wrote all [" + all_pfams_encountered.size()
+ + "] encountered Pfams to: \"" + all_pfams_encountered_file + "\"" );
+ ForesterUtil.programMessage( surfacing.PRG_NAME, "Wrote all [" + pfams_with_mappings_counter
+ + "] encountered Pfams with GO mappings to: \"" + all_pfams_encountered_with_go_annotation_file
+ + "\"" );
+ ForesterUtil.programMessage( surfacing.PRG_NAME, "Wrote summary (including all ["
+ + pfams_without_mappings_counter + "] encountered Pfams without GO mappings) to: \""
+ + encountered_pfams_summary_file + "\"" );
+ ForesterUtil.programMessage( surfacing.PRG_NAME, "Sum of Pfams encountered : "
+ + all_pfams_encountered.size() );
+ ForesterUtil.programMessage( surfacing.PRG_NAME, "Pfams without a mapping : "
+ + pfams_without_mappings_counter + " ["
+ + ( ( 100 * pfams_without_mappings_counter ) / all_pfams_encountered.size() ) + "%]" );
+ ForesterUtil.programMessage( surfacing.PRG_NAME, "Pfams without mapping to proc. or func. : "
+ + pfams_without_mappings_to_bp_or_mf_counter + " ["
+ + ( ( 100 * pfams_without_mappings_to_bp_or_mf_counter ) / all_pfams_encountered.size() ) + "%]" );
+ ForesterUtil.programMessage( surfacing.PRG_NAME, "Pfams with a mapping : "
+ + pfams_with_mappings_counter + " ["
+ + ( ( 100 * pfams_with_mappings_counter ) / all_pfams_encountered.size() ) + "%]" );
+ ForesterUtil.programMessage( surfacing.PRG_NAME, "Pfams with a mapping to proc. or func. : "
+ + pfams_with_mappings_to_bp_or_mf_counter + " ["
+ + ( ( 100 * pfams_with_mappings_to_bp_or_mf_counter ) / all_pfams_encountered.size() ) + "%]" );
+ ForesterUtil.programMessage( surfacing.PRG_NAME, "Pfams with mapping to biological process: "
+ + biological_process_counter + " ["
+ + ( ( 100 * biological_process_counter ) / all_pfams_encountered.size() ) + "%]" );
+ ForesterUtil.programMessage( surfacing.PRG_NAME, "Pfams with mapping to molecular function: "
+ + molecular_function_counter + " ["
+ + ( ( 100 * molecular_function_counter ) / all_pfams_encountered.size() ) + "%]" );
+ ForesterUtil.programMessage( surfacing.PRG_NAME, "Pfams with mapping to cellular component: "
+ + cellular_component_counter + " ["
+ + ( ( 100 * cellular_component_counter ) / all_pfams_encountered.size() ) + "%]" );
+ summary_writer.write( ForesterUtil.LINE_SEPARATOR );
+ summary_writer.write( "# Sum of Pfams encountered : " + all_pfams_encountered.size() );
+ summary_writer.write( ForesterUtil.LINE_SEPARATOR );
+ summary_writer.write( "# Pfams without a mapping : " + pfams_without_mappings_counter
+ + " [" + ( ( 100 * pfams_without_mappings_counter ) / all_pfams_encountered.size() ) + "%]" );
+ summary_writer.write( ForesterUtil.LINE_SEPARATOR );
+ summary_writer.write( "# Pfams without mapping to proc. or func. : "
+ + pfams_without_mappings_to_bp_or_mf_counter + " ["
+ + ( ( 100 * pfams_without_mappings_to_bp_or_mf_counter ) / all_pfams_encountered.size() ) + "%]" );
+ summary_writer.write( ForesterUtil.LINE_SEPARATOR );
+ summary_writer.write( "# Pfams with a mapping : " + pfams_with_mappings_counter + " ["
+ + ( ( 100 * pfams_with_mappings_counter ) / all_pfams_encountered.size() ) + "%]" );
+ summary_writer.write( ForesterUtil.LINE_SEPARATOR );
+ summary_writer.write( "# Pfams with a mapping to proc. or func. : "
+ + pfams_with_mappings_to_bp_or_mf_counter + " ["
+ + ( ( 100 * pfams_with_mappings_to_bp_or_mf_counter ) / all_pfams_encountered.size() ) + "%]" );
+ summary_writer.write( ForesterUtil.LINE_SEPARATOR );
+ summary_writer.write( "# Pfams with mapping to biological process: " + biological_process_counter + " ["
+ + ( ( 100 * biological_process_counter ) / all_pfams_encountered.size() ) + "%]" );
+ summary_writer.write( ForesterUtil.LINE_SEPARATOR );
+ summary_writer.write( "# Pfams with mapping to molecular function: " + molecular_function_counter + " ["
+ + ( ( 100 * molecular_function_counter ) / all_pfams_encountered.size() ) + "%]" );
+ summary_writer.write( ForesterUtil.LINE_SEPARATOR );
+ summary_writer.write( "# Pfams with mapping to cellular component: " + cellular_component_counter + " ["
+ + ( ( 100 * cellular_component_counter ) / all_pfams_encountered.size() ) + "%]" );
+ summary_writer.write( ForesterUtil.LINE_SEPARATOR );
+ summary_writer.close();
+ }
+ catch ( final IOException e ) {
+ ForesterUtil.printWarningMessage( surfacing.PRG_NAME, "Failure to write: " + e );
+ }
+ }
+
+ private static void writeDomainData( final Map<String, List<GoId>> domain_id_to_go_ids_map,
+ final Map<GoId, GoTerm> go_id_to_term_map,
+ final GoNameSpace go_namespace_limit,
+ final Writer out,
+ final String domain_0,
+ final String domain_1,
+ final String prefix_for_html,
+ final String character_separator_for_non_html_output,
+ final Map<String, Set<String>>[] domain_id_to_secondary_features_maps,
+ final Set<GoId> all_go_ids ) throws IOException {
+ boolean any_go_annotation_present = false;
+ boolean first_has_no_go = false;
+ int domain_count = 2; // To distinguish between domains and binary domain combinations.
+ if ( ForesterUtil.isEmpty( domain_1 ) ) {
+ domain_count = 1;
+ }
+ // The following has a difficult to understand logic.
+ for( int d = 0; d < domain_count; ++d ) {
+ List<GoId> go_ids = null;
+ boolean go_annotation_present = false;
+ if ( d == 0 ) {
+ if ( domain_id_to_go_ids_map.containsKey( domain_0 ) ) {
+ go_annotation_present = true;
+ any_go_annotation_present = true;
+ go_ids = domain_id_to_go_ids_map.get( domain_0 );
+ }
+ else {
+ first_has_no_go = true;
+ }
+ }
+ else {
+ if ( domain_id_to_go_ids_map.containsKey( domain_1 ) ) {
+ go_annotation_present = true;
+ any_go_annotation_present = true;
+ go_ids = domain_id_to_go_ids_map.get( domain_1 );
+ }
+ }
+ if ( go_annotation_present ) {
+ boolean first = ( ( d == 0 ) || ( ( d == 1 ) && first_has_no_go ) );
+ for( final GoId go_id : go_ids ) {
+ out.write( "<tr>" );
+ if ( first ) {
+ first = false;
+ writeDomainIdsToHtml( out,
+ domain_0,
+ domain_1,
+ prefix_for_html,
+ domain_id_to_secondary_features_maps );
+ }
+ else {
+ out.write( "<td></td>" );
+ }
+ if ( !go_id_to_term_map.containsKey( go_id ) ) {
+ throw new IllegalArgumentException( "GO-id [" + go_id + "] not found in GO-id to GO-term map" );
+ }
+ final GoTerm go_term = go_id_to_term_map.get( go_id );
+ if ( ( go_namespace_limit == null ) || go_namespace_limit.equals( go_term.getGoNameSpace() ) ) {
+ // final String top = GoUtils.getPenultimateGoTerm( go_term, go_id_to_term_map ).getName();
+ final String go_id_str = go_id.getId();
+ out.write( "<td>" );
+ out.write( "<a href=\"" + SurfacingConstants.AMIGO_LINK + go_id_str
+ + "\" target=\"amigo_window\">" + go_id_str + "</a>" );
+ out.write( "</td><td>" );
+ out.write( go_term.getName() );
+ if ( domain_count == 2 ) {
+ out.write( " (" + d + ")" );
+ }
+ out.write( "</td><td>" );
+ // out.write( top );
+ // out.write( "</td><td>" );
+ out.write( "[" );
+ out.write( go_term.getGoNameSpace().toShortString() );
+ out.write( "]" );
+ out.write( "</td>" );
+ if ( all_go_ids != null ) {
+ all_go_ids.add( go_id );
+ }
+ }
+ else {
+ out.write( "<td>" );
+ out.write( "</td><td>" );
+ out.write( "</td><td>" );
+ out.write( "</td><td>" );
+ out.write( "</td>" );
+ }
+ out.write( "</tr>" );
+ out.write( SurfacingConstants.NL );
+ }
+ }
+ } // for( int d = 0; d < domain_count; ++d )
+ if ( !any_go_annotation_present ) {
+ out.write( "<tr>" );
+ writeDomainIdsToHtml( out, domain_0, domain_1, prefix_for_html, domain_id_to_secondary_features_maps );
+ out.write( "<td>" );
+ out.write( "</td><td>" );
+ out.write( "</td><td>" );
+ out.write( "</td><td>" );
+ out.write( "</td>" );
+ out.write( "</tr>" );
+ out.write( SurfacingConstants.NL );
+ }
+ }
+
+ private static void writeDomainIdsToHtml( final Writer out,
+ final String domain_0,
+ final String domain_1,
+ final String prefix_for_detailed_html,
+ final Map<String, Set<String>>[] domain_id_to_secondary_features_maps )
+ throws IOException {
+ out.write( "<td>" );
+ if ( !ForesterUtil.isEmpty( prefix_for_detailed_html ) ) {
+ out.write( prefix_for_detailed_html );
+ out.write( " " );
+ }
+ out.write( "<a href=\"" + SurfacingConstants.PFAM_FAMILY_ID_LINK + domain_0 + "\">" + domain_0 + "</a>" );
+ out.write( "</td>" );
+ }
+
+ private static void writeDomainsToIndividualFilePerTreeNode( final Writer individual_files_writer,
+ final String domain_0,
+ final String domain_1 ) throws IOException {
+ individual_files_writer.write( domain_0 );
+ individual_files_writer.write( ForesterUtil.LINE_SEPARATOR );
+ if ( !ForesterUtil.isEmpty( domain_1 ) ) {
+ individual_files_writer.write( domain_1 );
+ individual_files_writer.write( ForesterUtil.LINE_SEPARATOR );
+ }
+ }
+
+ private static void writePfamsToFile( final String outfile_name, final SortedSet<String> pfams ) {
+ try {
+ final Writer writer = new BufferedWriter( new FileWriter( new File( outfile_name ) ) );
+ for( final String pfam : pfams ) {
+ writer.write( pfam );
+ writer.write( ForesterUtil.LINE_SEPARATOR );
+ }
+ writer.close();
+ ForesterUtil.programMessage( surfacing.PRG_NAME, "Wrote " + pfams.size() + " pfams to [" + outfile_name
+ + "]" );
+ }
+ catch ( final IOException e ) {
+ ForesterUtil.printWarningMessage( surfacing.PRG_NAME, "Failure to write: " + e );
+ }
+ }
+
+ private static void writeToNexus( final String outfile_name,
+ final CharacterStateMatrix<BinaryStates> matrix,
+ final Phylogeny phylogeny ) {
+ if ( !( matrix instanceof BasicCharacterStateMatrix ) ) {
+ throw new IllegalArgumentException( "can only write matrices of type [" + BasicCharacterStateMatrix.class
+ + "] to nexus" );
+ }
+ final BasicCharacterStateMatrix<BinaryStates> my_matrix = ( org.forester.evoinference.matrix.character.BasicCharacterStateMatrix<BinaryStates> ) matrix;
+ final List<Phylogeny> phylogenies = new ArrayList<Phylogeny>( 1 );
+ phylogenies.add( phylogeny );
+ try {
+ final BufferedWriter w = new BufferedWriter( new FileWriter( outfile_name ) );
+ w.write( NexusConstants.NEXUS );
+ w.write( ForesterUtil.LINE_SEPARATOR );
+ my_matrix.writeNexusTaxaBlock( w );
+ my_matrix.writeNexusBinaryChractersBlock( w );
+ PhylogenyWriter.writeNexusTreesBlock( w, phylogenies, NH_CONVERSION_SUPPORT_VALUE_STYLE.NONE );
+ w.flush();
+ w.close();
+ ForesterUtil.programMessage( surfacing.PRG_NAME, "Wrote Nexus file: \"" + outfile_name + "\"" );
+ }
+ catch ( final IOException e ) {
+ ForesterUtil.fatalError( surfacing.PRG_NAME, e.getMessage() );
+ }
+ }
+
+ private static void writeToNexus( final String outfile_name,
+ final DomainParsimonyCalculator domain_parsimony,
+ final Phylogeny phylogeny ) {
+ writeToNexus( outfile_name + surfacing.NEXUS_EXTERNAL_DOMAINS,
+ domain_parsimony.createMatrixOfDomainPresenceOrAbsence(),
+ phylogeny );
+ writeToNexus( outfile_name + surfacing.NEXUS_EXTERNAL_DOMAIN_COMBINATIONS,
+ domain_parsimony.createMatrixOfBinaryDomainCombinationPresenceOrAbsence(),
+ phylogeny );
+ }
+
+ final static class DomainComparator implements Comparator<Domain> {
+
+ final private boolean _ascending;
+
+ public DomainComparator( final boolean ascending ) {
+ _ascending = ascending;
+ }
+
+ @Override
+ public final int compare( final Domain d0, final Domain d1 ) {
+ if ( d0.getFrom() < d1.getFrom() ) {
+ return _ascending ? -1 : 1;
+ }
+ else if ( d0.getFrom() > d1.getFrom() ) {
+ return _ascending ? 1 : -1;
+ }
+ return 0;