in progress...
[jalview.git] / forester / java / src / org / forester / surfacing / MinimalDomainomeCalculator.java
index ba667ce..1fa7929 100644 (file)
@@ -5,11 +5,11 @@ import java.io.BufferedWriter;
 import java.io.File;
 import java.io.FileWriter;
 import java.io.IOException;
+import java.io.Writer;
 import java.util.ArrayList;
-import java.util.HashMap;
+import java.util.Arrays;
 import java.util.HashSet;
 import java.util.List;
-import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Set;
 import java.util.SortedMap;
@@ -19,74 +19,27 @@ import java.util.TreeSet;
 
 import org.forester.application.surfacing;
 import org.forester.phylogeny.Phylogeny;
+import org.forester.phylogeny.PhylogenyMethods;
 import org.forester.phylogeny.PhylogenyNode;
 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
 import org.forester.protein.Domain;
 import org.forester.protein.Protein;
 import org.forester.species.BasicSpecies;
 import org.forester.species.Species;
+import org.forester.surfacing.SurfacingUtil.DomainComparator;
 import org.forester.util.ForesterUtil;
 
 public final class MinimalDomainomeCalculator {
 
-    static final public void calcDomainome( final Phylogeny tre,
-                                            final SortedMap<Species, List<Protein>> protein_lists_per_species,
-                                            final double ie_cutoff ) {
-        if ( protein_lists_per_species == null || tre == null ) {
-            throw new IllegalArgumentException( "argument is null" );
-        }
-        if ( protein_lists_per_species.size() < 2 ) {
-            throw new IllegalArgumentException( "not enough genomes" );
-        }
-        for( final PhylogenyNodeIterator iter = tre.iteratorPostorder(); iter.hasNext(); ) {
-            final PhylogenyNode node = iter.next();
-            if ( node.isInternal() ) {
-                System.out.println();
-                if ( node.getNodeData().isHasTaxonomy() ) {
-                    System.out.println( node.getNodeData().getTaxonomy().getScientificName() + ":" );
-                }
-                else {
-                    System.out.println( node.getName() + ":" );
-                }
-                final List<PhylogenyNode> external_descs = node.getAllExternalDescendants();
-                final List<Set<String>> domains_per_genome_list = new ArrayList<Set<String>>();
-                for( final PhylogenyNode external_desc : external_descs ) {
-                    final String code = external_desc.getNodeData().getTaxonomy().getTaxonomyCode();
-                    System.out.print( code + " " );
-                    final List<Protein> proteins_per_species = protein_lists_per_species
-                            .get( new BasicSpecies( code ) );
-                    if ( proteins_per_species != null ) {
-                        final SortedSet<String> domains_per_genome = new TreeSet<String>();
-                        for( final Protein protein : proteins_per_species ) {
-                            List<Domain> domains = protein.getProteinDomains();
-                            for( final Domain domain : domains ) {
-                                if ( ( domain.getPerDomainEvalue() <= ie_cutoff ) || ( ie_cutoff <= -1 ) ) {
-                                    domains_per_genome.add( domain.getDomainId() );
-                                }
-                            }
-                        }
-                        if ( domains_per_genome.size() > 0 ) {
-                            domains_per_genome_list.add( domains_per_genome );
-                        }
-                    }
-                }
-                System.out.println();
-                if ( domains_per_genome_list.size() > 0 ) {
-                    Set<String> intersection = calcIntersection( domains_per_genome_list );
-                    System.out.println( intersection );
-                }
-            }
-        }
-    }
-
-    static final public void calcOme( final boolean use_domain_architectures,
-                                      final Phylogeny tre,
-                                      final SortedMap<Species, List<Protein>> protein_lists_per_species,
-                                      final String separator,
-                                      final double ie_cutoff,
-                                      final String outfile_base )
+    public final static void calc( final boolean use_domain_architectures,
+                                   final Phylogeny tre,
+                                   final SortedMap<Species, List<Protein>> protein_lists_per_species,
+                                   final String separator,
+                                   final double ie_cutoff,
+                                   final String outfile_base,
+                                   final boolean write_protein_files )
             throws IOException {
-        final SortedMap<String, SortedSet<String>> species_to_das_map = new TreeMap<String, SortedSet<String>>();
+        final SortedMap<String, SortedSet<String>> species_to_features_map = new TreeMap<String, SortedSet<String>>();
         if ( protein_lists_per_species == null || tre == null ) {
             throw new IllegalArgumentException( "argument is null" );
         }
@@ -106,7 +59,7 @@ public final class MinimalDomainomeCalculator {
         SurfacingUtil.checkForOutputFileWriteability( outfile_table );
         final BufferedWriter out = new BufferedWriter( new FileWriter( outfile ) );
         final BufferedWriter out_table = new BufferedWriter( new FileWriter( outfile_table ) );
-        out.write( "SPECIES\tCOMMON NAME\tCODE\tRANK\t#EXT NODES\tEXT NODE CODES\t#DA\tDA" );
+        out.write( "SPECIES\tCOMMON NAME\tCODE\tRANK\t#EXT NODES\tEXT NODE CODES\t#" + x + "\t" + x + "" );
         out.write( ForesterUtil.LINE_SEPARATOR );
         for( final PhylogenyNodeIterator iter = tre.iteratorPostorder(); iter.hasNext(); ) {
             final PhylogenyNode node = iter.next();
@@ -143,7 +96,7 @@ public final class MinimalDomainomeCalculator {
             else {
                 out.write( "\t\t" );
             }
-            final List<Set<String>> das_per_genome_list = new ArrayList<Set<String>>();
+            final List<Set<String>> features_per_genome_list = new ArrayList<Set<String>>();
             boolean first = true;
             for( final PhylogenyNode external_desc : external_descs ) {
                 final String code = external_desc.getNodeData().getTaxonomy().getTaxonomyCode();
@@ -158,28 +111,28 @@ public final class MinimalDomainomeCalculator {
                 }
                 final List<Protein> proteins_per_species = protein_lists_per_species.get( new BasicSpecies( code ) );
                 if ( proteins_per_species != null ) {
-                    final SortedSet<String> das_per_genome = new TreeSet<String>();
+                    final SortedSet<String> features_per_genome = new TreeSet<String>();
                     for( final Protein protein : proteins_per_species ) {
                         if ( use_domain_architectures ) {
                             final String da = protein.toDomainArchitectureString( separator, ie_cutoff );
-                            das_per_genome.add( da );
+                            features_per_genome.add( da );
                         }
                         else {
                             List<Domain> domains = protein.getProteinDomains();
                             for( final Domain domain : domains ) {
                                 if ( ( ie_cutoff <= -1 ) || ( domain.getPerDomainEvalue() <= ie_cutoff ) ) {
-                                    das_per_genome.add( domain.getDomainId() );
+                                    features_per_genome.add( domain.getDomainId() );
                                 }
                             }
                         }
                     }
-                    if ( das_per_genome.size() > 0 ) {
-                        das_per_genome_list.add( das_per_genome );
+                    if ( features_per_genome.size() > 0 ) {
+                        features_per_genome_list.add( features_per_genome );
                     }
                 }
             }
-            if ( das_per_genome_list.size() > 0 ) {
-                SortedSet<String> intersection = calcIntersection( das_per_genome_list );
+            if ( features_per_genome_list.size() > 0 ) {
+                SortedSet<String> intersection = calcIntersection( features_per_genome_list );
                 out.write( "\t" + intersection.size() + "\t" );
                 first = true;
                 for( final String s : intersection ) {
@@ -192,15 +145,15 @@ public final class MinimalDomainomeCalculator {
                     out.write( s );
                 }
                 out.write( ForesterUtil.LINE_SEPARATOR );
-                species_to_das_map.put( species_name, intersection );
+                species_to_features_map.put( species_name, intersection );
             }
         }
         final SortedSet<String> all_species_names = new TreeSet<String>();
-        final SortedSet<String> all_das = new TreeSet<String>();
-        for( final Entry<String, SortedSet<String>> e : species_to_das_map.entrySet() ) {
+        final SortedSet<String> all_features = new TreeSet<String>();
+        for( final Entry<String, SortedSet<String>> e : species_to_features_map.entrySet() ) {
             all_species_names.add( e.getKey() );
-            for( final String das : e.getValue() ) {
-                all_das.add( das );
+            for( final String f : e.getValue() ) {
+                all_features.add( f );
             }
         }
         out_table.write( '\t' );
@@ -215,7 +168,7 @@ public final class MinimalDomainomeCalculator {
             out_table.write( species_name );
         }
         out_table.write( ForesterUtil.LINE_SEPARATOR );
-        for( final String das : all_das ) {
+        for( final String das : all_features ) {
             out_table.write( das );
             out_table.write( '\t' );
             first = true;
@@ -226,7 +179,7 @@ public final class MinimalDomainomeCalculator {
                 else {
                     out_table.write( '\t' );
                 }
-                if ( species_to_das_map.get( species_name ).contains( das ) ) {
+                if ( species_to_features_map.get( species_name ).contains( das ) ) {
                     out_table.write( '1' );
                 }
                 else {
@@ -241,29 +194,88 @@ public final class MinimalDomainomeCalculator {
         out_table.close();
         ForesterUtil.programMessage( surfacing.PRG_NAME, "Wrote minimal DAome data to           : " + outfile );
         ForesterUtil.programMessage( surfacing.PRG_NAME, "Wrote minimal DAome data to (as table): " + outfile_table );
+        if ( write_protein_files ) {
+            final String protdirname;
+            final String a;
+            final String b;
+            if ( use_domain_architectures ) {
+                a = "_DA";
+                b = "domain architectures (DAs)";
+                protdirname = "_DAS";
+            }
+            else {
+                a = "_domain";
+                b = "domains";
+                protdirname = "_DOMAINS";
+            }
+            final File prot_dir = new File( outfile_base + protdirname );
+            final boolean success = prot_dir.mkdir();
+            if ( !success ) {
+                throw new IOException( "failed to create dir " + prot_dir );
+            }
+            int total = 0;
+            final String dir = outfile_base + protdirname + "/";
+            for( final String feat : all_features ) {
+                final File extract_outfile = new File( dir + feat + a + surfacing.SEQ_EXTRACT_SUFFIX );
+                SurfacingUtil.checkForOutputFileWriteability( extract_outfile );
+                final Writer proteins_file_writer = new BufferedWriter( new FileWriter( extract_outfile ) );
+                final int counter = extractProteinFeatures( use_domain_architectures,
+                                                            protein_lists_per_species,
+                                                            feat,
+                                                            proteins_file_writer,
+                                                            ie_cutoff,
+                                                            separator );
+                if ( counter < 1 ) {
+                    ForesterUtil.printWarningMessage( "surfacing", feat + " not present (in " + b + " extraction)" );
+                }
+                total += counter;
+                proteins_file_writer.close();
+            }
+            ForesterUtil.programMessage( "surfacing",
+                                         "Wrote " + total + " individual " + b + " from a total of "
+                                                 + all_features.size() + " into: " + dir );
+        }
     }
 
-    static final public void calcDAome( final Phylogeny tre,
-                                        final SortedMap<Species, List<Protein>> protein_lists_per_species,
-                                        final String separator,
-                                        final double ie_cutoff,
-                                        final String outfile_base )
+    public final static void calcNEW( final boolean use_domain_architectures,
+                                      final Phylogeny tre,
+                                      final int level,
+                                      final SortedMap<Species, List<Protein>> protein_lists_per_species,
+                                      final String separator,
+                                      final double ie_cutoff,
+                                      final String outfile_base,
+                                      final boolean write_protein_files )
             throws IOException {
-        final SortedMap<String, SortedSet<String>> species_to_das_map = new TreeMap<String, SortedSet<String>>();
+        final SortedMap<String, SortedSet<String>> species_to_features_map = new TreeMap<String, SortedSet<String>>();
         if ( protein_lists_per_species == null || tre == null ) {
             throw new IllegalArgumentException( "argument is null" );
         }
         if ( protein_lists_per_species.size() < 2 ) {
             throw new IllegalArgumentException( "not enough genomes" );
         }
-        final File outfile = new File( outfile_base + "_minimal_daome.txt" );
-        final File outfile_table = new File( outfile_base + "_minimal_daome.tsv" );
+        final String x;
+        if ( use_domain_architectures ) {
+            x = "DA";
+        }
+        else {
+            x = "domain";
+        }
+        final File outfile = new File( outfile_base + "_minimal_" + x + "ome.tsv" );
+        final File outfile_table = new File( outfile_base + "_minimal_" + x + "ome_matrix.tsv" );
         SurfacingUtil.checkForOutputFileWriteability( outfile );
         SurfacingUtil.checkForOutputFileWriteability( outfile_table );
         final BufferedWriter out = new BufferedWriter( new FileWriter( outfile ) );
         final BufferedWriter out_table = new BufferedWriter( new FileWriter( outfile_table ) );
-        out.write( "SPECIES\tCOMMON NAME\tCODE\tRANK\t#EXT NODES\tEXT NODE CODES\t#DA\tDA" );
+        out.write( "SPECIES\tCOMMON NAME\tCODE\tRANK\t#EXT NODES\tEXT NODE CODES\t#" + x + "\t" + x + "" );
         out.write( ForesterUtil.LINE_SEPARATOR );
+        ///////////
+        //////////
+        SortedMap<String, List<Protein>> protein_lists_per_quasi_species = null;
+        if ( level >= 1 ) {
+            protein_lists_per_quasi_species = makeProteinListsPerQuasiSpecies( tre, level, protein_lists_per_species );
+        }
+        /////////
+        ///////////
         for( final PhylogenyNodeIterator iter = tre.iteratorPostorder(); iter.hasNext(); ) {
             final PhylogenyNode node = iter.next();
             final String species_name = node.getNodeData().isHasTaxonomy()
@@ -299,33 +311,61 @@ public final class MinimalDomainomeCalculator {
             else {
                 out.write( "\t\t" );
             }
-            final List<Set<String>> das_per_genome_list = new ArrayList<Set<String>>();
+            final List<Set<String>> features_per_genome_list = new ArrayList<Set<String>>();
             boolean first = true;
-            for( final PhylogenyNode external_desc : external_descs ) {
-                final String code = external_desc.getNodeData().getTaxonomy().getTaxonomyCode();
-                if ( node.isInternal() ) {
-                    if ( first ) {
-                        first = false;
+            if ( level >= 1 ) {
+                ////////////
+                ////////////
+                final int node_level = PhylogenyMethods.calculateLevel( node );
+                if ( node_level >= level ) {
+                    final List<PhylogenyNode> given_level_descs = PhylogenyMethods
+                            .getAllDescendantsOfGivenLevel( node, level );
+                    for( final PhylogenyNode given_level_desc : given_level_descs ) {
+                        final String spec_name = given_level_desc.getNodeData().isHasTaxonomy()
+                                ? given_level_desc.getNodeData().getTaxonomy().getScientificName() : given_level_desc.getName();
                     }
-                    else {
-                        out.write( ", " );
-                    }
-                    out.write( code );
                 }
-                final List<Protein> proteins_per_species = protein_lists_per_species.get( new BasicSpecies( code ) );
-                if ( proteins_per_species != null ) {
-                    final SortedSet<String> das_per_genome = new TreeSet<String>();
-                    for( final Protein protein : proteins_per_species ) {
-                        final String da = protein.toDomainArchitectureString( separator, ie_cutoff );
-                        das_per_genome.add( da );
+                ///////////
+                ///////////
+            }
+            else {
+                for( final PhylogenyNode external_desc : external_descs ) {
+                    final String code = external_desc.getNodeData().getTaxonomy().getTaxonomyCode();
+                    if ( node.isInternal() ) {
+                        if ( first ) {
+                            first = false;
+                        }
+                        else {
+                            out.write( ", " );
+                        }
+                        out.write( code );
                     }
-                    if ( das_per_genome.size() > 0 ) {
-                        das_per_genome_list.add( das_per_genome );
+                    final List<Protein> proteins_per_species = protein_lists_per_species
+                            .get( new BasicSpecies( code ) );
+                    if ( proteins_per_species != null ) {
+                        final SortedSet<String> features_per_genome = new TreeSet<String>();
+                        for( final Protein protein : proteins_per_species ) {
+                            if ( use_domain_architectures ) {
+                                final String da = protein.toDomainArchitectureString( separator, ie_cutoff );
+                                features_per_genome.add( da );
+                            }
+                            else {
+                                List<Domain> domains = protein.getProteinDomains();
+                                for( final Domain domain : domains ) {
+                                    if ( ( ie_cutoff <= -1 ) || ( domain.getPerDomainEvalue() <= ie_cutoff ) ) {
+                                        features_per_genome.add( domain.getDomainId() );
+                                    }
+                                }
+                            }
+                        }
+                        if ( features_per_genome.size() > 0 ) {
+                            features_per_genome_list.add( features_per_genome );
+                        }
                     }
-                }
-            }
-            if ( das_per_genome_list.size() > 0 ) {
-                SortedSet<String> intersection = calcIntersection( das_per_genome_list );
+                } // for( final PhylogenyNode external_desc : external_descs )
+            } // else
+            if ( features_per_genome_list.size() > 0 ) {
+                SortedSet<String> intersection = calcIntersection( features_per_genome_list );
                 out.write( "\t" + intersection.size() + "\t" );
                 first = true;
                 for( final String s : intersection ) {
@@ -338,15 +378,15 @@ public final class MinimalDomainomeCalculator {
                     out.write( s );
                 }
                 out.write( ForesterUtil.LINE_SEPARATOR );
-                species_to_das_map.put( species_name, intersection );
+                species_to_features_map.put( species_name, intersection );
             }
         }
         final SortedSet<String> all_species_names = new TreeSet<String>();
-        final SortedSet<String> all_das = new TreeSet<String>();
-        for( final Entry<String, SortedSet<String>> e : species_to_das_map.entrySet() ) {
+        final SortedSet<String> all_features = new TreeSet<String>();
+        for( final Entry<String, SortedSet<String>> e : species_to_features_map.entrySet() ) {
             all_species_names.add( e.getKey() );
-            for( final String das : e.getValue() ) {
-                all_das.add( das );
+            for( final String f : e.getValue() ) {
+                all_features.add( f );
             }
         }
         out_table.write( '\t' );
@@ -361,7 +401,7 @@ public final class MinimalDomainomeCalculator {
             out_table.write( species_name );
         }
         out_table.write( ForesterUtil.LINE_SEPARATOR );
-        for( final String das : all_das ) {
+        for( final String das : all_features ) {
             out_table.write( das );
             out_table.write( '\t' );
             first = true;
@@ -372,7 +412,7 @@ public final class MinimalDomainomeCalculator {
                 else {
                     out_table.write( '\t' );
                 }
-                if ( species_to_das_map.get( species_name ).contains( das ) ) {
+                if ( species_to_features_map.get( species_name ).contains( das ) ) {
                     out_table.write( '1' );
                 }
                 else {
@@ -387,6 +427,73 @@ public final class MinimalDomainomeCalculator {
         out_table.close();
         ForesterUtil.programMessage( surfacing.PRG_NAME, "Wrote minimal DAome data to           : " + outfile );
         ForesterUtil.programMessage( surfacing.PRG_NAME, "Wrote minimal DAome data to (as table): " + outfile_table );
+        if ( write_protein_files ) {
+            final String protdirname;
+            final String a;
+            final String b;
+            if ( use_domain_architectures ) {
+                a = "_DA";
+                b = "domain architectures (DAs)";
+                protdirname = "_DAS";
+            }
+            else {
+                a = "_domain";
+                b = "domains";
+                protdirname = "_DOMAINS";
+            }
+            final File prot_dir = new File( outfile_base + protdirname );
+            final boolean success = prot_dir.mkdir();
+            if ( !success ) {
+                throw new IOException( "failed to create dir " + prot_dir );
+            }
+            int total = 0;
+            final String dir = outfile_base + protdirname + "/";
+            for( final String feat : all_features ) {
+                final File extract_outfile = new File( dir + feat + a + surfacing.SEQ_EXTRACT_SUFFIX );
+                SurfacingUtil.checkForOutputFileWriteability( extract_outfile );
+                final Writer proteins_file_writer = new BufferedWriter( new FileWriter( extract_outfile ) );
+                final int counter = extractProteinFeatures( use_domain_architectures,
+                                                            protein_lists_per_species,
+                                                            feat,
+                                                            proteins_file_writer,
+                                                            ie_cutoff,
+                                                            separator );
+                if ( counter < 1 ) {
+                    ForesterUtil.printWarningMessage( "surfacing", feat + " not present (in " + b + " extraction)" );
+                }
+                total += counter;
+                proteins_file_writer.close();
+            }
+            ForesterUtil.programMessage( "surfacing",
+                                         "Wrote " + total + " individual " + b + " from a total of "
+                                                 + all_features.size() + " into: " + dir );
+        }
+    }
+
+    private final static SortedMap<String, List<Protein>> makeProteinListsPerQuasiSpecies( final Phylogeny tre,
+                                                                                           final int level,
+                                                                                           final SortedMap<Species, List<Protein>> protein_lists_per_species ) {
+        final SortedMap<String, List<Protein>> protein_lists_per_quasi_species = new TreeMap<String, List<Protein>>();
+        for( final PhylogenyNodeIterator iter = tre.iteratorPostorder(); iter.hasNext(); ) {
+            final PhylogenyNode node = iter.next();
+            final int node_level = PhylogenyMethods.calculateLevel( node );
+            if ( node_level == level ) {
+                final List<PhylogenyNode> external_descs = node.getAllExternalDescendants();
+                final List<Protein> protein_list_per_quasi_species = new ArrayList<Protein>();
+                for( final PhylogenyNode external_desc : external_descs ) {
+                    final String code = external_desc.getNodeData().getTaxonomy().getTaxonomyCode();
+                    final List<Protein> proteins_per_species = protein_lists_per_species
+                            .get( new BasicSpecies( code ) );
+                    for( Protein protein : proteins_per_species ) {
+                        protein_list_per_quasi_species.add( protein );
+                    }
+                }
+                final String species_name = node.getNodeData().isHasTaxonomy()
+                        ? node.getNodeData().getTaxonomy().getScientificName() : node.getName();
+                protein_lists_per_quasi_species.put( species_name, protein_list_per_quasi_species );
+            }
+        }
+        return protein_lists_per_quasi_species;
     }
 
     private final static SortedSet<String> calcIntersection( final List<Set<String>> features_per_genome_list ) {
@@ -401,6 +508,106 @@ public final class MinimalDomainomeCalculator {
         return my_first;
     }
 
+    private final static int extractProteinFeatures( final boolean use_domain_architectures,
+                                                     final SortedMap<Species, List<Protein>> protein_lists_per_species,
+                                                     final String domain_id,
+                                                     final Writer out,
+                                                     final double ie_cutoff,
+                                                     final String domain_separator )
+            throws IOException {
+        int counter = 0;
+        final String separator_for_output = "\t";
+        for( final Species species : protein_lists_per_species.keySet() ) {
+            final List<Protein> proteins_per_species = protein_lists_per_species.get( species );
+            for( final Protein protein : proteins_per_species ) {
+                if ( use_domain_architectures ) {
+                    if ( domain_id.equals( protein.toDomainArchitectureString( domain_separator, ie_cutoff ) ) ) {
+                        int from = Integer.MAX_VALUE;
+                        int to = -1;
+                        for( final Domain d : protein.getProteinDomains() ) {
+                            if ( ( ie_cutoff <= -1 ) || ( d.getPerDomainEvalue() <= ie_cutoff ) ) {
+                                if ( d.getFrom() < from ) {
+                                    from = d.getFrom();
+                                }
+                                if ( d.getTo() > to ) {
+                                    to = d.getTo();
+                                }
+                            }
+                        }
+                        out.write( protein.getSpecies().getSpeciesId() );
+                        out.write( separator_for_output );
+                        out.write( protein.getProteinId().getId() );
+                        out.write( separator_for_output );
+                        out.write( domain_id );
+                        out.write( separator_for_output );
+                        out.write( "/" );
+                        out.write( from + "-" + to );
+                        out.write( "/" );
+                        out.write( SurfacingConstants.NL );
+                        ++counter;
+                    }
+                }
+                else {
+                    final List<Domain> domains = protein.getProteinDomains( domain_id );
+                    if ( domains.size() > 0 ) {
+                        out.write( protein.getSpecies().getSpeciesId() );
+                        out.write( separator_for_output );
+                        out.write( protein.getProteinId().getId() );
+                        out.write( separator_for_output );
+                        out.write( domain_id );
+                        out.write( separator_for_output );
+                        for( final Domain domain : domains ) {
+                            if ( ( ie_cutoff < 0 ) || ( domain.getPerDomainEvalue() <= ie_cutoff ) ) {
+                                out.write( "/" );
+                                out.write( domain.getFrom() + "-" + domain.getTo() );
+                            }
+                        }
+                        out.write( "/" );
+                        out.write( separator_for_output );
+                        final List<Domain> domain_list = new ArrayList<Domain>();
+                        for( final Domain domain : protein.getProteinDomains() ) {
+                            if ( ( ie_cutoff < 0 ) || ( domain.getPerDomainEvalue() <= ie_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() );
+                        }
+                        out.write( separator_for_output );
+                        if ( !( ForesterUtil.isEmpty( protein.getAccession() )
+                                || protein.getAccession().equals( SurfacingConstants.NONE ) ) ) {
+                            out.write( protein.getAccession() );
+                        }
+                        out.write( SurfacingConstants.NL );
+                        ++counter;
+                    }
+                }
+            }
+        }
+        out.flush();
+        return counter;
+    }
+
     public static void main( final String[] args ) {
         Set<String> a = new HashSet<String>();
         Set<String> b = new HashSet<String>();