inprogress
[jalview.git] / forester / java / src / org / forester / surfacing / SurfacingUtil.java
index 974409d..abcd31f 100644 (file)
@@ -22,7 +22,7 @@
 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 //
 // Contact: phylosoft @ gmail . com
-// WWW: www.phylosoft.org/forester
+// WWW: https://sites.google.com/site/cmzmasek/home/software/forester
 
 package org.forester.surfacing;
 
@@ -39,6 +39,7 @@ import java.util.Collections;
 import java.util.Comparator;
 import java.util.HashMap;
 import java.util.HashSet;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
@@ -58,6 +59,7 @@ import org.forester.evoinference.matrix.character.CharacterStateMatrix;
 import org.forester.evoinference.matrix.character.CharacterStateMatrix.BinaryStates;
 import org.forester.evoinference.matrix.character.CharacterStateMatrix.Format;
 import org.forester.evoinference.matrix.character.CharacterStateMatrix.GainLossStates;
+import org.forester.evoinference.matrix.distance.BasicSymmetricalDistanceMatrix;
 import org.forester.evoinference.matrix.distance.DistanceMatrix;
 import org.forester.go.GoId;
 import org.forester.go.GoNameSpace;
@@ -68,7 +70,7 @@ import org.forester.io.writers.PhylogenyWriter;
 import org.forester.phylogeny.Phylogeny;
 import org.forester.phylogeny.PhylogenyMethods;
 import org.forester.phylogeny.PhylogenyNode;
-import org.forester.phylogeny.PhylogenyNodeI.NH_CONVERSION_SUPPORT_VALUE_STYLE;
+import org.forester.phylogeny.PhylogenyNode.NH_CONVERSION_SUPPORT_VALUE_STYLE;
 import org.forester.phylogeny.data.BinaryCharacters;
 import org.forester.phylogeny.data.Confidence;
 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
@@ -119,6 +121,75 @@ public final class SurfacingUtil {
         // Hidden constructor.
     }
 
+    public static void performDomainArchitectureAnalysis( final SortedMap<String, Set<String>> domain_architecutures,
+                                                          final SortedMap<String, Integer> domain_architecuture_counts,
+                                                          final int min_count,
+                                                          final File da_counts_outfile,
+                                                          final File unique_da_outfile ) {
+        checkForOutputFileWriteability( da_counts_outfile );
+        checkForOutputFileWriteability( unique_da_outfile );
+        try {
+            final BufferedWriter da_counts_out = new BufferedWriter( new FileWriter( da_counts_outfile ) );
+            final BufferedWriter unique_da_out = new BufferedWriter( new FileWriter( unique_da_outfile ) );
+            final Iterator<Entry<String, Integer>> it = domain_architecuture_counts.entrySet().iterator();
+            while ( it.hasNext() ) {
+                final Map.Entry<String, Integer> e = it.next();
+                final String da = e.getKey();
+                final int count = e.getValue();
+                if ( count >= min_count ) {
+                    da_counts_out.write( da );
+                    da_counts_out.write( "\t" );
+                    da_counts_out.write( String.valueOf( count ) );
+                    da_counts_out.write( ForesterUtil.LINE_SEPARATOR );
+                }
+                if ( count == 1 ) {
+                    final Iterator<Entry<String, Set<String>>> it2 = domain_architecutures.entrySet().iterator();
+                    while ( it2.hasNext() ) {
+                        final Map.Entry<String, Set<String>> e2 = it2.next();
+                        final String genome = e2.getKey();
+                        final Set<String> das = e2.getValue();
+                        if ( das.contains( da ) ) {
+                            unique_da_out.write( genome );
+                            unique_da_out.write( "\t" );
+                            unique_da_out.write( da );
+                            unique_da_out.write( ForesterUtil.LINE_SEPARATOR );
+                        }
+                    }
+                }
+            }
+            unique_da_out.close();
+            da_counts_out.close();
+        }
+        catch ( final IOException e ) {
+            ForesterUtil.fatalError( surfacing.PRG_NAME, e.getMessage() );
+        }
+        ForesterUtil.programMessage( surfacing.PRG_NAME, "Wrote distance matrices to \"" + da_counts_outfile + "\"" );
+        ForesterUtil.programMessage( surfacing.PRG_NAME, "Wrote distance matrices to \"" + unique_da_outfile + "\"" );
+        //
+    }
+
+    public static int storeDomainArchitectures( final String genome,
+                                                final SortedMap<String, Set<String>> domain_architecutures,
+                                                final List<Protein> protein_list,
+                                                final Map<String, Integer> distinct_domain_architecuture_counts ) {
+        final Set<String> da = new HashSet<String>();
+        domain_architecutures.put( genome, da );
+        for( final Protein protein : protein_list ) {
+            final String da_str = ( ( BasicProtein ) protein ).toDomainArchitectureString( "~", 3, "=" );
+            if ( !da.contains( da_str ) ) {
+                if ( !distinct_domain_architecuture_counts.containsKey( da_str ) ) {
+                    distinct_domain_architecuture_counts.put( da_str, 1 );
+                }
+                else {
+                    distinct_domain_architecuture_counts.put( da_str,
+                                                              distinct_domain_architecuture_counts.get( da_str ) + 1 );
+                }
+                da.add( da_str );
+            }
+        }
+        return da.size();
+    }
+
     public static void addAllBinaryDomainCombinationToSet( final GenomeWideCombinableDomains genome,
                                                            final SortedSet<BinaryDomainCombination> binary_domain_combinations ) {
         final SortedMap<DomainId, CombinableDomains> all_cd = genome.getAllCombinableDomainsIds();
@@ -238,8 +309,10 @@ public final class SurfacingUtil {
             final DescriptiveStatistics gained_once_domain_count_stats = new BasicDescriptiveStatistics();
             final DescriptiveStatistics gained_multiple_times_lengths_stats = new BasicDescriptiveStatistics();
             final DescriptiveStatistics gained_multiple_times_domain_count_stats = new BasicDescriptiveStatistics();
-            final DescriptiveStatistics gained_multiple_times_domain_length_stats = new BasicDescriptiveStatistics();
-            final DescriptiveStatistics gained_once_domain_length_stats = new BasicDescriptiveStatistics();
+            long gained_multiple_times_domain_length_sum = 0;
+            long gained_once_domain_length_sum = 0;
+            long gained_multiple_times_domain_length_count = 0;
+            long gained_once_domain_length_count = 0;
             for( final String dc : dcs ) {
                 final int count = dc_gain_counts.get( dc );
                 if ( histogram.containsKey( count ) ) {
@@ -286,15 +359,13 @@ public final class SurfacingUtil {
                     more_than_once.add( dc );
                     if ( protein_length_stats_by_dc != null ) {
                         final DescriptiveStatistics s = protein_length_stats_by_dc.get( dc );
-                        final double[] a = s.getDataAsDoubleArray();
-                        for( final double element : a ) {
+                        for( final double element : s.getData() ) {
                             gained_multiple_times_lengths_stats.addValue( element );
                         }
                     }
                     if ( domain_number_stats_by_dc != null ) {
                         final DescriptiveStatistics s = domain_number_stats_by_dc.get( dc );
-                        final double[] a = s.getDataAsDoubleArray();
-                        for( final double element : a ) {
+                        for( final double element : s.getData() ) {
                             gained_multiple_times_domain_count_stats.addValue( element );
                         }
                     }
@@ -302,28 +373,26 @@ public final class SurfacingUtil {
                         final String[] ds = dc.split( "=" );
                         final DescriptiveStatistics s0 = domain_length_stats_by_domain.get( ds[ 0 ] );
                         final DescriptiveStatistics s1 = domain_length_stats_by_domain.get( ds[ 1 ] );
-                        final double[] a0 = s0.getDataAsDoubleArray();
-                        final double[] a1 = s1.getDataAsDoubleArray();
-                        for( final double element : a0 ) {
-                            gained_multiple_times_domain_length_stats.addValue( element );
+                        for( final double element : s0.getData() ) {
+                            gained_multiple_times_domain_length_sum += element;
+                            ++gained_multiple_times_domain_length_count;
                         }
-                        for( final double element : a1 ) {
-                            gained_multiple_times_domain_length_stats.addValue( element );
+                        for( final double element : s1.getData() ) {
+                            gained_multiple_times_domain_length_sum += element;
+                            ++gained_multiple_times_domain_length_count;
                         }
                     }
                 }
                 else {
                     if ( protein_length_stats_by_dc != null ) {
                         final DescriptiveStatistics s = protein_length_stats_by_dc.get( dc );
-                        final double[] a = s.getDataAsDoubleArray();
-                        for( final double element : a ) {
+                        for( final double element : s.getData() ) {
                             gained_once_lengths_stats.addValue( element );
                         }
                     }
                     if ( domain_number_stats_by_dc != null ) {
                         final DescriptiveStatistics s = domain_number_stats_by_dc.get( dc );
-                        final double[] a = s.getDataAsDoubleArray();
-                        for( final double element : a ) {
+                        for( final double element : s.getData() ) {
                             gained_once_domain_count_stats.addValue( element );
                         }
                     }
@@ -331,13 +400,13 @@ public final class SurfacingUtil {
                         final String[] ds = dc.split( "=" );
                         final DescriptiveStatistics s0 = domain_length_stats_by_domain.get( ds[ 0 ] );
                         final DescriptiveStatistics s1 = domain_length_stats_by_domain.get( ds[ 1 ] );
-                        final double[] a0 = s0.getDataAsDoubleArray();
-                        final double[] a1 = s1.getDataAsDoubleArray();
-                        for( final double element : a0 ) {
-                            gained_once_domain_length_stats.addValue( element );
+                        for( final double element : s0.getData() ) {
+                            gained_once_domain_length_sum += element;
+                            ++gained_once_domain_length_count;
                         }
-                        for( final double element : a1 ) {
-                            gained_once_domain_length_stats.addValue( element );
+                        for( final double element : s1.getData() ) {
+                            gained_once_domain_length_sum += element;
+                            ++gained_once_domain_length_count;
                         }
                     }
                 }
@@ -373,10 +442,9 @@ public final class SurfacingUtil {
                         nodes.add( n );
                     }
                 }
-                for( int i = 0; i < nodes.size() - 1; ++i ) {
+                for( int i = 0; i < ( nodes.size() - 1 ); ++i ) {
                     for( int j = i + 1; j < nodes.size(); ++j ) {
-                        final PhylogenyNode lca = PhylogenyMethods.getInstance().obtainLCA( nodes.get( i ),
-                                                                                            nodes.get( j ) );
+                        final PhylogenyNode lca = PhylogenyMethods.calculateLCA( nodes.get( i ), nodes.get( j ) );
                         String rank = "unknown";
                         if ( lca.getNodeData().isHasTaxonomy()
                                 && !ForesterUtil.isEmpty( lca.getNodeData().getTaxonomy().getRank() ) ) {
@@ -409,7 +477,7 @@ public final class SurfacingUtil {
             out_for_rank_counts.close();
             out_for_ancestor_species_counts.close();
             if ( !ForesterUtil.isEmpty( outfilename_for_protein_stats )
-                    && ( ( protein_length_stats_by_dc != null ) || ( domain_number_stats_by_dc != null ) ) ) {
+                    && ( ( 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" );
@@ -455,12 +523,17 @@ public final class SurfacingUtil {
                 w.write( "\n" );
                 w.write( "Gained once, domain lengths:" );
                 w.write( "\n" );
-                w.write( gained_once_domain_length_stats.toString() );
+                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( gained_multiple_times_domain_length_stats.toString() );
+                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" );
@@ -624,7 +697,7 @@ public final class SurfacingUtil {
 
     public static Map<DomainId, Set<String>> createDomainIdToSecondaryFeaturesMap( final File secondary_features_map_file )
             throws IOException {
-        final BasicTable<String> primary_table = BasicTableParser.parse( secondary_features_map_file, "\t" );
+        final BasicTable<String> primary_table = BasicTableParser.parse( secondary_features_map_file, '\t' );
         final Map<DomainId, Set<String>> map = new TreeMap<DomainId, Set<String>>();
         for( int r = 0; r < primary_table.getNumberOfRows(); ++r ) {
             final DomainId domain_id = new DomainId( primary_table.getValue( 0, r ) );
@@ -639,7 +712,7 @@ public final class SurfacingUtil {
     public static Phylogeny createNjTreeBasedOnMatrixToFile( final File nj_tree_outfile, final DistanceMatrix distance ) {
         checkForOutputFileWriteability( nj_tree_outfile );
         final NeighborJoining nj = NeighborJoining.createInstance();
-        final Phylogeny phylogeny = nj.execute( distance );
+        final Phylogeny phylogeny = nj.execute( ( BasicSymmetricalDistanceMatrix ) distance );
         phylogeny.setName( nj_tree_outfile.getName() );
         writePhylogenyToFile( phylogeny, nj_tree_outfile.toString() );
         return phylogeny;
@@ -1181,23 +1254,61 @@ public final class SurfacingUtil {
                                             final DomainId domain_id,
                                             final Writer out,
                                             final String separator,
-                                            final String limit_to_species ) throws IOException {
+                                            final String limit_to_species,
+                                            final double domain_e_cutoff ) throws IOException {
+        System.out.println( "Per domain E-value: " + domain_e_cutoff );
         for( final Species species : protein_lists_per_species.keySet() ) {
+            System.out.println( species + ":" );
             for( final Protein protein : protein_lists_per_species.get( species ) ) {
                 if ( ForesterUtil.isEmpty( limit_to_species )
                         || protein.getSpecies().getSpeciesId().equalsIgnoreCase( limit_to_species ) ) {
                     final List<Domain> domains = protein.getProteinDomains( domain_id );
                     if ( domains.size() > 0 ) {
-                        final DescriptiveStatistics stats = new BasicDescriptiveStatistics();
-                        for( final Domain domain : domains ) {
-                            stats.addValue( domain.getPerSequenceEvalue() );
-                        }
                         out.write( protein.getSpecies().getSpeciesId() );
                         out.write( separator );
                         out.write( protein.getProteinId().getId() );
                         out.write( separator );
-                        out.write( "[" + FORMATTER.format( stats.median() ) + "]" );
+                        out.write( domain_id.toString() );
+                        out.write( separator );
+                        int prev_to = -1;
+                        for( final Domain domain : domains ) {
+                            if ( ( domain_e_cutoff < 0 ) || ( domain.getPerDomainEvalue() <= domain_e_cutoff ) ) {
+                                out.write( "/" );
+                                out.write( domain.getFrom() + "-" + domain.getTo() );
+                                if ( prev_to >= 0 ) {
+                                    final int l = domain.getFrom() - prev_to;
+                                    System.out.println( l );
+                                }
+                                prev_to = domain.getTo();
+                            }
+                        }
+                        out.write( "/" );
                         out.write( separator );
+                        final List<Domain> domain_list = new ArrayList<Domain>();
+                        for( final Domain domain : protein.getProteinDomains() ) {
+                            if ( ( domain_e_cutoff < 0 ) || ( domain.getPerDomainEvalue() <= domain_e_cutoff ) ) {
+                                domain_list.add( domain );
+                            }
+                        }
+                        final Domain domain_ary[] = new Domain[ domain_list.size() ];
+                        for( int i = 0; i < domain_list.size(); ++i ) {
+                            domain_ary[ i ] = domain_list.get( i );
+                        }
+                        Arrays.sort( domain_ary, new DomainComparator( true ) );
+                        out.write( "{" );
+                        boolean first = true;
+                        for( final Domain domain : domain_ary ) {
+                            if ( first ) {
+                                first = false;
+                            }
+                            else {
+                                out.write( "," );
+                            }
+                            out.write( domain.getDomainId().toString() );
+                            out.write( ":" + domain.getFrom() + "-" + domain.getTo() );
+                            out.write( ":" + domain.getPerDomainEvalue() );
+                        }
+                        out.write( "}" );
                         if ( !( ForesterUtil.isEmpty( protein.getDescription() ) || protein.getDescription()
                                 .equals( SurfacingConstants.NONE ) ) ) {
                             out.write( protein.getDescription() );
@@ -1580,58 +1691,50 @@ public final class SurfacingUtil {
                     + 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() ) + "%]" );
+                    + ( ( 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() )
-                                                 + "%]" );
+                    + ( ( 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() )
-                                                 + "%]" );
+                    + ( ( 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() ) + "%]" );
+                    + " [" + ( ( 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() ) + "%]" );
+                    + ( ( 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() ) + "%]" );
+                    + ( ( 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() ) + "%]" );
+                    + ( ( 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() ) + "%]" );
+                    + ( ( 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() ) + "%]" );
+                    + ( ( 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() ) + "%]" );
+                    + ( ( 100 * cellular_component_counter ) / all_pfams_encountered.size() ) + "%]" );
             summary_writer.write( ForesterUtil.LINE_SEPARATOR );
             summary_writer.close();
         }
@@ -2588,4 +2691,24 @@ public final class SurfacingUtil {
             e.printStackTrace();
         }
     }
+
+    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;
+        }
+    }
 }