in progress (special coloring is still true)
[jalview.git] / forester / java / src / org / forester / sdi / GSDI.java
index b9fa794..fa7a52c 100644 (file)
@@ -37,7 +37,6 @@ import org.forester.phylogeny.Phylogeny;
 import org.forester.phylogeny.PhylogenyMethods;
 import org.forester.phylogeny.PhylogenyNode;
 import org.forester.phylogeny.data.Event;
-import org.forester.phylogeny.data.Taxonomy;
 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
 import org.forester.sdi.SDIutil.TaxonomyComparisonBase;
 import org.forester.util.ForesterUtil;
@@ -59,6 +58,15 @@ public final class GSDI implements GSDII {
                  final boolean most_parsimonious_duplication_model,
                  final boolean strip_gene_tree,
                  final boolean strip_species_tree ) throws SDIException {
+        this( gene_tree, species_tree, most_parsimonious_duplication_model, strip_gene_tree, strip_species_tree, true );
+    }
+
+    public GSDI( final Phylogeny gene_tree,
+                 final Phylogeny species_tree,
+                 final boolean most_parsimonious_duplication_model,
+                 final boolean strip_gene_tree,
+                 final boolean strip_species_tree,
+                 final boolean transfer_taxonomy ) throws SDIException {
         _most_parsimonious_duplication_model = most_parsimonious_duplication_model;
         if ( gene_tree.getRoot().getNumberOfDescendants() == 3 ) {
             gene_tree.reRoot( gene_tree.getRoot().getChildNode( 2 ) );
@@ -76,7 +84,7 @@ public final class GSDI implements GSDII {
         PhylogenyMethods.preOrderReId( species_tree );
         final GSDIsummaryResult gsdi_summary_result = geneTreePostOrderTraversal( gene_tree,
                                                                                   _most_parsimonious_duplication_model,
-                                                                                  true );
+                                                                                  transfer_taxonomy );
         _speciation_or_duplication_events_sum = gsdi_summary_result.getSpeciationOrDuplicationEventsSum();
         _speciations_sum = gsdi_summary_result.getSpeciationsSum();
         _duplications_sum = gsdi_summary_result.getDuplicationsSum();
@@ -171,46 +179,13 @@ public final class GSDI implements GSDII {
                     }
                 }
                 g.setLink( s1 );
-                if ( transfer_taxonomy ) {
-                    transferTaxonomy( g, s1 );
-                }
                 determineEvent( s1, g, most_parsimonious_duplication_model, res );
             }
-        }
-        return res;
-    }
-
-    private static final void transferTaxonomy( final PhylogenyNode g, final PhylogenyNode s ) {
-        if ( s.getNodeData().isHasTaxonomy() ) {
-            g.getNodeData().setTaxonomy( s.getNodeData().getTaxonomy() );
-            if ( g.isInternal() ) {
-                if ( g.getChildNode1().isInternal() ) {
-                    if ( g.getChildNode1().getNodeData().isHasTaxonomy() && g.getChildNode1().getNodeData().getTaxonomy() == s.getNodeData().getTaxonomy() ) {
-                        g.getChildNode1().getNodeData().setTaxonomy( null );
-                    }
-                }
-                if ( g.getChildNode2().isInternal() ) {
-                    if ( g.getChildNode2().getNodeData().isHasTaxonomy() && g.getChildNode2().getNodeData().getTaxonomy() == s.getNodeData().getTaxonomy() ) {
-                        g.getChildNode2().getNodeData().setTaxonomy( null );
-                    }
-                }
-            }
-        }
-        else if ( ForesterUtil.isEmpty( g.getName() ) && !ForesterUtil.isEmpty( s.getName() ) ) {
-            g.setName( s.getName() );
-            if ( g.isInternal() ) {
-                if ( g.getChildNode1().isInternal() ) {
-                    if ( g.getChildNode1().getName() == s.getName() ) {
-                        g.getChildNode1().setName( "" );
-                    }
-                }
-                if ( g.getChildNode2().isInternal() ) {
-                    if ( g.getChildNode2().getName() == s.getName() ) {
-                        g.getChildNode2().setName( "" );
-                    }
-                }
+            if ( transfer_taxonomy ) {
+                transferTaxonomy( g );
             }
         }
+        return res;
     }
 
     final static GSDIsummaryResult geneTreePostOrderTraversal( final Phylogeny gene_tree,
@@ -347,6 +322,40 @@ public final class GSDI implements GSDII {
         return res;
     }
 
+    static final void transferTaxonomy( final PhylogenyNode g ) {
+        if ( g == null ) {
+            throw new IllegalArgumentException( "gene tree node is null" );
+        }
+        final PhylogenyNode s = g.getLink();
+        if ( s == null ) {
+            throw new IllegalArgumentException( "mapped species tree node is null" );
+        }
+        if ( s.getNodeData().isHasTaxonomy() ) {
+            g.getNodeData().setTaxonomy( s.getNodeData().getTaxonomy() );
+            if ( g.isInternal() ) {
+                if ( g.getChildNode1().isInternal() && g.getChildNode1().getNodeData().isHasTaxonomy()
+                        && ( g.getChildNode1().getNodeData().getTaxonomy() == s.getNodeData().getTaxonomy() ) ) {
+                    g.getChildNode1().getNodeData().setTaxonomy( null );
+                }
+                if ( g.getChildNode2().isInternal() && g.getChildNode2().getNodeData().isHasTaxonomy()
+                        && ( g.getChildNode2().getNodeData().getTaxonomy() == s.getNodeData().getTaxonomy() ) ) {
+                    g.getChildNode2().getNodeData().setTaxonomy( null );
+                }
+            }
+        }
+        else if ( ForesterUtil.isEmpty( g.getName() ) && !ForesterUtil.isEmpty( s.getName() ) ) {
+            g.setName( s.getName() );
+            if ( g.isInternal() ) {
+                if ( g.getChildNode1().isInternal() && ( g.getChildNode1().getName() == s.getName() ) ) {
+                    g.getChildNode1().setName( "" );
+                }
+                if ( g.getChildNode2().isInternal() && ( g.getChildNode2().getName() == s.getName() ) ) {
+                    g.getChildNode2().setName( "" );
+                }
+            }
+        }
+    }
+
     private final static void addScientificNamesMappedToReducedSpecificity( final String s1,
                                                                             final String s2,
                                                                             final SortedSet<String> scientific_names_mapped_to_reduced_specificity ) {