in progress...
[jalview.git] / forester / java / src / org / forester / sdi / TestGSDI.java
index a9dfa66..7b4b17e 100644 (file)
@@ -21,7 +21,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.sdi;
 
@@ -35,22 +35,27 @@ import org.forester.phylogeny.PhylogenyMethods;
 import org.forester.phylogeny.data.Event;
 import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
 import org.forester.phylogeny.factories.PhylogenyFactory;
-import org.forester.sdi.SDI.TaxonomyComparisonBase;
+import org.forester.sdi.SDIutil.TaxonomyComparisonBase;
 import org.forester.util.ForesterUtil;
 
 public final class TestGSDI {
 
     private final static String PATH_TO_TEST_DATA = System.getProperty( "user.dir" ) + ForesterUtil.getFileSeparator()
-                                                          + "test_data" + ForesterUtil.getFileSeparator();
+            + "test_data" + ForesterUtil.getFileSeparator();
 
-    private final static Phylogeny createPhylogeny( final String nhx ) throws IOException {
-        final Phylogeny p = ParserBasedPhylogenyFactory.getInstance().create( nhx, new NHXParser() )[ 0 ];
-        p.setRooted( true );
-        return p;
-    }
-
-    private final static Event getEvent( final Phylogeny p, final String n1, final String n2 ) {
-        return PhylogenyMethods.calculateLCA( p.getNode( n1 ), p.getNode( n2 ) ).getNodeData().getEvent();
+    public static void main( final String[] args ) {
+        if ( !TestGSDI.testGSDI_against_binary_gene_tree() ) {
+            System.out.println( "binary failed" );
+        }
+        if ( !TestGSDI.testGSDI_general() ) {
+            System.out.println( "general failed" );
+        }
+        if ( !TestGSDI.testGSDIR_general() ) {
+            System.out.println( "general re-rooting failed" );
+        }
+        else {
+            System.out.println( "OK" );
+        }
     }
 
     public static boolean test() {
@@ -66,8 +71,35 @@ public final class TestGSDI {
         return true;
     }
 
+    private final static Phylogeny createPhylogeny( final String nhx ) throws IOException {
+        final Phylogeny p = ParserBasedPhylogenyFactory.getInstance().create( nhx, new NHXParser() )[ 0 ];
+        p.setRooted( true );
+        return p;
+    }
+
+    private final static Event getEvent( final Phylogeny p, final String n1, final String n2 ) {
+        return PhylogenyMethods.calculateLCA( p.getNode( n1 ), p.getNode( n2 ) ).getNodeData().getEvent();
+    }
+
     private static boolean testGSDI_against_binary_gene_tree() {
         try {
+            final PhylogenyFactory factory0 = ParserBasedPhylogenyFactory.getInstance();
+            final String s0 = "([&&NHX:S=1]);";
+            final String gene_0_str = "([&&NHX:S=1],[&&NHX:S=1]);";
+            final Phylogeny s_0 = factory0.create( s0, new NHXParser() )[ 0 ];
+            final Phylogeny gene_0 = factory0.create( gene_0_str, new NHXParser() )[ 0 ];
+            s_0.setRooted( true );
+            gene_0.setRooted( true );
+            final GSDI sdi0 = new GSDI( gene_0, s_0, false, false, false );
+            if ( sdi0.getSpeciationOrDuplicationEventsSum() != 0 ) {
+                return false;
+            }
+            if ( sdi0.getDuplicationsSum() != 1 ) {
+                return false;
+            }
+            if ( sdi0.getSpeciationsSum() != 0 ) {
+                return false;
+            }
             final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
             final String multi_species_2_str = "(((((([&&NHX:S=1],[&&NHX:S=2]),"
                     + "([&&NHX:S=3],[&&NHX:S=4],[&&NHX:S=5])),"
@@ -1251,7 +1283,7 @@ public final class TestGSDI {
             //--
             final Phylogeny tol_143_ = ParserUtils.readPhylogenies( PATH_TO_TEST_DATA + "tol_143.xml" )[ 0 ];
             final Phylogeny gene_tree_tax_code_4_ = ParserUtils.readPhylogenies( PATH_TO_TEST_DATA
-                    + "gene_tree_tax_code_4.xml" )[ 0 ];
+                                                                                 + "gene_tree_tax_code_4.xml" )[ 0 ];
             final GSDI gsdi_143_4_1 = new GSDI( gene_tree_tax_code_4_.copy(), tol_143_.copy(), false, true, true );
             if ( gsdi_143_4_1.getDuplicationsSum() != 21 ) {
                 return false;
@@ -1264,7 +1296,7 @@ public final class TestGSDI {
             }
             //--
             final Phylogeny gsdi_test_gene_tree_sn_wnt = ParserUtils.readPhylogenies( PATH_TO_TEST_DATA
-                    + "gsdi_test_gene_tree_sn_wnt.xml" )[ 0 ];
+                                                                                      + "gsdi_test_gene_tree_sn_wnt.xml" )[ 0 ];
             gsdi_test_gene_tree_sn_wnt.setRooted( true );
             final GSDI a = new GSDI( gsdi_test_gene_tree_sn_wnt.copy(), tol_143_.copy(), false, true, true );
             if ( a.getDuplicationsSum() != 33 ) {
@@ -1287,7 +1319,7 @@ public final class TestGSDI {
             }
             //--
             final Phylogeny gsdi_test_species_tree_sn_xml = ParserUtils.readPhylogenies( PATH_TO_TEST_DATA
-                    + "gsdi_test_species_tree_sn.xml" )[ 0 ];
+                                                                                         + "gsdi_test_species_tree_sn.xml" )[ 0 ];
             final GSDI b = new GSDI( gsdi_test_gene_tree_sn_wnt.copy(),
                                      gsdi_test_species_tree_sn_xml.copy(),
                                      false,
@@ -1354,9 +1386,9 @@ public final class TestGSDI {
             }
             //--
             final Phylogeny gsdi_test_gene_tree_codes_xml = ParserUtils.readPhylogenies( PATH_TO_TEST_DATA
-                    + "gsdi_test_gene_tree_codes.xml" )[ 0 ];
+                                                                                         + "gsdi_test_gene_tree_codes.xml" )[ 0 ];
             final Phylogeny gsdi_test_species_tree_codes_xml = ParserUtils.readPhylogenies( PATH_TO_TEST_DATA
-                    + "gsdi_test_species_tree_codes.xml" )[ 0 ];
+                                                                                            + "gsdi_test_species_tree_codes.xml" )[ 0 ];
             final GSDI d = new GSDI( gsdi_test_gene_tree_codes_xml.copy(),
                                      gsdi_test_species_tree_codes_xml.copy(),
                                      false,
@@ -1388,7 +1420,7 @@ public final class TestGSDI {
             }
             //--
             final Phylogeny gsdi_test_gene_tree_sn_xml = ParserUtils.readPhylogenies( PATH_TO_TEST_DATA
-                    + "gsdi_test_gene_tree_sn.xml" )[ 0 ];
+                                                                                      + "gsdi_test_gene_tree_sn.xml" )[ 0 ];
             final GSDI e = new GSDI( gsdi_test_gene_tree_sn_xml.copy(),
                                      gsdi_test_species_tree_sn_xml.copy(),
                                      false,
@@ -1428,33 +1460,73 @@ public final class TestGSDI {
 
     private static boolean testGSDIR_general() {
         try {
+            final PhylogenyFactory factory0 = ParserBasedPhylogenyFactory.getInstance();
+            final String s0 = "([&&NHX:S=1]);";
+            final String gene_0_str = "([&&NHX:S=1],[&&NHX:S=1]);";
+            final Phylogeny s_0 = factory0.create( s0, new NHXParser() )[ 0 ];
+            final Phylogeny gene_0 = factory0.create( gene_0_str, new NHXParser() )[ 0 ];
+            s_0.setRooted( true );
+            gene_0.setRooted( true );
+            final GSDIR sdi0 = new GSDIR( gene_0, s_0, true, true, true );
+            if ( sdi0.getSpeciationsSum() != 0 ) {
+                return false;
+            }
+            if ( sdi0.getMinDuplicationsSum() != 1 ) {
+                return false;
+            }
+            //
+            final PhylogenyFactory factory00 = ParserBasedPhylogenyFactory.getInstance();
+            final String s00 = "[&&NHX:S=1];";
+            final String gene_00_str = "([&&NHX:S=1],[&&NHX:S=1]);";
+            final Phylogeny s_00 = factory00.create( s00, new NHXParser() )[ 0 ];
+            final Phylogeny gene_00 = factory00.create( gene_00_str, new NHXParser() )[ 0 ];
+            s_00.setRooted( true );
+            gene_00.setRooted( true );
+            final GSDIR sdi00 = new GSDIR( gene_00, s_00, true, true, true );
+            if ( sdi00.getSpeciationsSum() != 0 ) {
+                return false;
+            }
+            if ( sdi00.getMinDuplicationsSum() != 1 ) {
+                return false;
+            }
+            //
             final String s1str = "(((([&&NHX:S=HUMAN],([&&NHX:S=MOUSE],[&&NHX:S=RAT])),([&&NHX:S=CAEEL],[&&NHX:S=CAEBR])),[&&NHX:S=YEAST]),[&&NHX:S=ARATH])";
             final Phylogeny s1 = ParserBasedPhylogenyFactory.getInstance().create( s1str, new NHXParser() )[ 0 ];
             s1.setRooted( true );
             final Phylogeny g1 = TestGSDI
                     .createPhylogeny( "(HUMAN[&&NHX:S=HUMAN],(RAT[&&NHX:S=RAT],(CAEEL[&&NHX:T=:S=CAEEL],YEAST[&&NHX:S=YEAST])))" );
-            final GSDIR sdi1 = new GSDIR( g1.copy(), s1.copy(), false, false );
+            final GSDIR sdi1 = new GSDIR( g1.copy(), s1.copy(), false, false, true );
             if ( sdi1.getMinDuplicationsSum() != 0 ) {
                 return false;
             }
             final Phylogeny g2 = TestGSDI
                     .createPhylogeny( "(((HUMAN[&&NHX:S=HUMAN],RAT[&&NHX:S=RAT]),CAEEL[&&NHX:T=:S=CAEEL]),YEAST[&&NHX:S=YEAST])" );
-            final GSDIR sdi2 = new GSDIR( g2.copy(), s1.copy(), false, false );
+            final GSDIR sdi2 = new GSDIR( g2.copy(), s1.copy(), false, false, true );
             if ( sdi2.getMinDuplicationsSum() != 0 ) {
                 return false;
             }
             final Phylogeny g3 = TestGSDI
                     .createPhylogeny( "(RAT[&&NHX:S=RAT],HUMAN[&&NHX:S=HUMAN],(YEAST[&&NHX:S=YEAST],CAEEL[&&NHX:T=:S=CAEEL]))" );
-            final GSDIR sdi3 = new GSDIR( g3.copy(), s1.copy(), false, false );
+            final GSDIR sdi3 = new GSDIR( g3.copy(), s1.copy(), false, false, true );
             if ( sdi3.getMinDuplicationsSum() != 0 ) {
                 return false;
             }
             final Phylogeny g4 = TestGSDI
                     .createPhylogeny( "(((((MOUSE[&&NHX:S=MOUSE],[&&NHX:S=RAT]),[&&NHX:S=HUMAN]),([&&NHX:S=ARATH],[&&NHX:S=YEAST])),[&&NHX:S=CAEEL]),[&&NHX:S=CAEBR])" );
-            final GSDIR sdi4 = new GSDIR( g4.copy(), s1.copy(), false, false );
+            final GSDIR sdi4 = new GSDIR( g4.copy(), s1.copy(), false, false, true );
             if ( sdi4.getMinDuplicationsSum() != 0 ) {
                 return false;
             }
+            //
+            final String s2str = "(((([&&NHX:S=HUMAN],([&&NHX:S=MOUSE],[&&NHX:S=RAT])),([&&NHX:S=CAEEL],[&&NHX:S=CAEBR])),[&&NHX:S=YEAST]),([&&NHX:S=ARATH],[&&NHX:S=SOYBN]))";
+            final Phylogeny s2 = ParserBasedPhylogenyFactory.getInstance().create( s2str, new NHXParser() )[ 0 ];
+            s2.setRooted( true );
+            final Phylogeny g5 = TestGSDI.createPhylogeny( s2str );
+            final GSDIR sdi5 = new GSDIR( g5, s2, false, false, true );
+            if ( sdi5.getMinDuplicationsSum() != 0 ) {
+                System.out.println( sdi5.getMinDuplicationsSum() );
+                return false;
+            }
         }
         catch ( final Exception e ) {
             e.printStackTrace( System.out );
@@ -1462,26 +1534,4 @@ public final class TestGSDI {
         }
         return true;
     }
-
-    public static void main( final String[] args ) {
-        if ( !TestGSDI.testGSDI_against_binary_gene_tree() ) {
-            System.out.println( "binary failed" );
-        }
-        if ( !TestGSDI.testGSDI_general() ) {
-            System.out.println( "general failed" );
-        }
-        if ( !TestGSDI.testGSDIR_general() ) {
-            System.out.println( "general re-rooting failed" );
-        }
-        else {
-            System.out.println( "OK" );
-        }
-        //        boolean success = test();
-        //        if ( success ) {
-        //            System.out.println( "OK" );
-        //        }
-        //        else {
-        //            System.out.println( "failed" );
-        //        }
-    }
 }