From: cmzmasek@gmail.com Date: Wed, 5 Dec 2012 23:56:39 +0000 (+0000) Subject: "rio" work X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=5b11e68cf46c363bd5ae256f794871c194776bd7;p=jalview.git "rio" work --- diff --git a/forester/java/src/org/forester/application/rio.java b/forester/java/src/org/forester/application/rio.java index 94e6f5c..3c2684b 100644 --- a/forester/java/src/org/forester/application/rio.java +++ b/forester/java/src/org/forester/application/rio.java @@ -40,6 +40,8 @@ import org.forester.phylogeny.Phylogeny; import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory; import org.forester.phylogeny.factories.PhylogenyFactory; import org.forester.sdi.RIO; +import org.forester.sdi.RioException; +import org.forester.sdi.SDIException; import org.forester.util.CommandLineArguments; import org.forester.util.EasyWriter; import org.forester.util.ForesterUtil; @@ -47,8 +49,8 @@ import org.forester.util.ForesterUtil; public class rio { final static private String PRG_NAME = "rio"; - final static private String PRG_VERSION = "3.00 beta 2"; - final static private String PRG_DATE = "2012.11.30"; + final static private String PRG_VERSION = "3.00 beta 3"; + final static private String PRG_DATE = "2012.12.05"; final static private String E_MAIL = "czmasek@burnham.org"; final static private String WWW = "www.phylosoft.org/forester/"; final static private String HELP_OPTION_1 = "help"; @@ -187,7 +189,7 @@ public class rio { ForesterUtil.fatalError( PRG_NAME, "species tree is not completely binary" ); } try { - RIO rio; + final RIO rio; if ( ForesterUtil.isEmpty( query ) ) { rio = new RIO( gene_trees_file, species_tree ); } @@ -212,13 +214,17 @@ public class rio { tableOutput( table_outfile, rio ); } } - catch ( final IllegalArgumentException e ) { + catch ( final RioException e ) { + ForesterUtil.fatalError( PRG_NAME, e.getLocalizedMessage() ); + } + catch ( final SDIException e ) { + ForesterUtil.fatalError( PRG_NAME, e.getLocalizedMessage() ); + } + catch ( final IOException e ) { ForesterUtil.fatalError( PRG_NAME, e.getLocalizedMessage() ); } catch ( final Exception e ) { - ForesterUtil.printErrorMessage( PRG_NAME, e.getLocalizedMessage() ); - e.printStackTrace(); - System.exit( -1 ); + ForesterUtil.unexpectedFatalError( PRG_NAME, e ); } if ( outfile != null ) { ForesterUtil.programMessage( PRG_NAME, "wrote results to \"" + outfile + "\"" ); @@ -229,7 +235,7 @@ public class rio { System.exit( 0 ); } - private static void tableOutput( final File table_outfile, final RIO rio ) throws IOException { + private static void tableOutput( final File table_outfile, final RIO rio ) throws IOException, RioException { final IntMatrix m = RIO.calculateOrthologTable( rio.getAnalyzedGeneTrees() ); writeTable( table_outfile, rio, m ); } @@ -238,7 +244,6 @@ public class rio { final EasyWriter w = ForesterUtil.createEasyWriter( table_outfile ); final java.text.DecimalFormat df = new java.text.DecimalFormat( "0.###" ); df.setDecimalSeparatorAlwaysShown( false ); - w.print( "\t" ); for( int i = 0; i < m.size(); ++i ) { w.print( "\t" ); w.print( m.getLabel( i ) ); @@ -246,7 +251,6 @@ public class rio { w.println(); for( int x = 0; x < m.size(); ++x ) { w.print( m.getLabel( x ) ); - w.print( "\t" ); for( int y = 0; y < m.size(); ++y ) { w.print( "\t" ); if ( x == y ) { diff --git a/forester/java/src/org/forester/phylogeny/PhylogenyMethods.java b/forester/java/src/org/forester/phylogeny/PhylogenyMethods.java index d6949fb..f839fd6 100644 --- a/forester/java/src/org/forester/phylogeny/PhylogenyMethods.java +++ b/forester/java/src/org/forester/phylogeny/PhylogenyMethods.java @@ -159,6 +159,12 @@ public class PhylogenyMethods { * @return LCA of node1 and node2 */ public final static PhylogenyNode calculateLCA( PhylogenyNode node1, PhylogenyNode node2 ) { + if ( node1 == null ) { + throw new IllegalArgumentException( "first argument (node) is null" ); + } + if ( node2 == null ) { + throw new IllegalArgumentException( "second argument (node) is null" ); + } if ( node1 == node2 ) { return node1; } @@ -211,6 +217,12 @@ public class PhylogenyMethods { * @return LCA of node1 and node2 */ public final static PhylogenyNode calculateLCAonTreeWithIdsInPreOrder( PhylogenyNode node1, PhylogenyNode node2 ) { + if ( node1 == null ) { + throw new IllegalArgumentException( "first argument (node) is null" ); + } + if ( node2 == null ) { + throw new IllegalArgumentException( "second argument (node) is null" ); + } while ( node1 != node2 ) { if ( node1.getId() > node2.getId() ) { node1 = node1.getParent(); diff --git a/forester/java/src/org/forester/sdi/RIO.java b/forester/java/src/org/forester/sdi/RIO.java index 2b51b69..4515756 100644 --- a/forester/java/src/org/forester/sdi/RIO.java +++ b/forester/java/src/org/forester/sdi/RIO.java @@ -28,6 +28,7 @@ package org.forester.sdi; import java.io.File; +import java.io.FileNotFoundException; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; @@ -64,9 +65,10 @@ public final class RIO { * Default constructor. * @throws SDIException * @throws IOException + * @throws RioException */ public RIO( final File gene_trees_file, final Phylogeny species_tree, final String query ) throws IOException, - SDIException { + SDIException, RioException { if ( ForesterUtil.isEmpty( query ) ) { throw new IllegalArgumentException( "query is empty" ); } @@ -74,7 +76,8 @@ public final class RIO { inferOrthologs( gene_trees_file, species_tree, query ); } - public RIO( final File gene_trees_file, final Phylogeny species_tree ) throws IOException, SDIException { + public RIO( final File gene_trees_file, final Phylogeny species_tree ) throws IOException, SDIException, + RioException { init(); inferOrthologs( gene_trees_file, species_tree, null ); } @@ -371,9 +374,12 @@ public final class RIO { * the sequence name of the squence whose orthologs are to be * inferred * @throws SDIException + * @throws RioException + * @throws IOException + * @throws FileNotFoundException */ private final void inferOrthologs( final File gene_trees_file, final Phylogeny species_tree, final String query ) - throws IOException, SDIException { + throws SDIException, RioException, FileNotFoundException, IOException { // Read in first tree to get its sequence names // and strip species_tree. final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance(); @@ -387,11 +393,17 @@ public final class RIO { final Phylogeny[] gene_trees = factory.create( gene_trees_file, p ); // Removes from species_tree all species not found in gene_tree. PhylogenyMethods.taxonomyBasedDeletionOfExternalNodes( gene_trees[ 0 ], species_tree ); + if ( species_tree.isEmpty() ) { + throw new RioException( "failed to establish species based mapping between gene and species trees" ); + } if ( !ForesterUtil.isEmpty( query ) ) { PhylogenyMethods.taxonomyBasedDeletionOfExternalNodes( species_tree, gene_trees[ 0 ] ); + if ( gene_trees[ 0 ].isEmpty() ) { + throw new RioException( "failed to establish species based mapping between gene and species trees" ); + } _seq_names = getAllExternalSequenceNames( gene_trees[ 0 ] ); if ( ( _seq_names == null ) || ( _seq_names.size() < 1 ) ) { - throw new IOException( "could not get sequence names" ); + throw new RioException( "could not get sequence names" ); } _o_maps = new HashMap>(); _so_maps = new HashMap>(); @@ -405,6 +417,9 @@ public final class RIO { for( final Phylogeny gt : gene_trees ) { // Removes from gene_tree all species not found in species_tree. PhylogenyMethods.taxonomyBasedDeletionOfExternalNodes( species_tree, gt ); + if ( gt.isEmpty() ) { + throw new RioException( "failed to establish species based mapping between gene and species trees" ); + } _analyzed_gene_trees[ c++ ] = inferOrthologsHelper( gt, species_tree, query ); } setNumberOfSamples( gene_trees.length ); @@ -414,7 +429,7 @@ public final class RIO { // the external node with seqname query. private final Phylogeny inferOrthologsHelper( final Phylogeny gene_tree, final Phylogeny species_tree, - final String query ) throws SDIException { + final String query ) throws SDIException, RioException { final SDIR sdiunrooted = new SDIR(); final Phylogeny assigned_tree = sdiunrooted.infer( gene_tree, species_tree, @@ -427,10 +442,10 @@ public final class RIO { if ( !ForesterUtil.isEmpty( query ) ) { final List nodes = getNodesViaSequenceName( assigned_tree, query ); if ( nodes.size() > 1 ) { - throw new IllegalArgumentException( "node named [" + query + "] not unique" ); + throw new RioException( "node named [" + query + "] not unique" ); } else if ( nodes.isEmpty() ) { - throw new IllegalArgumentException( "no node containing a sequence named [" + query + "] found" ); + throw new RioException( "no node containing a sequence named [" + query + "] found" ); } final PhylogenyNode query_node = nodes.get( 0 ); updateCounts( _o_maps, query, PhylogenyMethods.getOrthologousNodes( assigned_tree, query_node ) ); @@ -467,7 +482,7 @@ public final class RIO { final List nodes ) { final HashMap hash_map = counter_map.get( query_seq_name ); if ( hash_map == null ) { - throw new RuntimeException( "Unexpected failure in method updateHash." ); + throw new RuntimeException( "unexpected error in updateCounts" ); } for( int j = 0; j < nodes.size(); ++j ) { String seq_name; @@ -487,7 +502,7 @@ public final class RIO { } } - public final static IntMatrix calculateOrthologTable( final Phylogeny[] analyzed_gene_trees ) { + public final static IntMatrix calculateOrthologTable( final Phylogeny[] analyzed_gene_trees ) throws RioException { final List labels = new ArrayList(); final Set labels_set = new HashSet(); String label; @@ -518,10 +533,18 @@ public final class RIO { PhylogenyMethods.preOrderReId( gt ); final HashMap map = PhylogenyMethods.createNameToExtNodeMap( gt ); for( int x = 0; x < m.size(); ++x ) { - final PhylogenyNode nx = map.get( m.getLabel( x ) ); + final String mx = m.getLabel( x ); + final PhylogenyNode nx = map.get( mx ); + if ( nx == null ) { + throw new RioException( "node \"" + mx + "\" not present in gene tree #" + counter ); + } for( int y = 0; y < m.size(); ++y ) { - if ( !PhylogenyMethods.calculateLCAonTreeWithIdsInPreOrder( nx, map.get( m.getLabel( y ) ) ) - .isDuplication() ) { + final String my = m.getLabel( y ); + final PhylogenyNode ny = map.get( my ); + if ( ny == null ) { + throw new RioException( "node \"" + my + "\" not present in gene tree #" + counter ); + } + if ( !PhylogenyMethods.calculateLCAonTreeWithIdsInPreOrder( nx, ny ).isDuplication() ) { m.inreaseByOne( x, y ); } } @@ -624,7 +647,7 @@ public final class RIO { return s; } - private final static List getAllExternalSequenceNames( final Phylogeny phy ) { + private final static List getAllExternalSequenceNames( final Phylogeny phy ) throws RioException { final List names = new ArrayList(); for( final PhylogenyNodeIterator iter = phy.iteratorExternalForward(); iter.hasNext(); ) { final PhylogenyNode n = iter.next(); @@ -635,7 +658,7 @@ public final class RIO { names.add( n.getName() ); } else { - throw new IllegalArgumentException( "node has no (sequence) name: " + n ); + throw new RioException( "node has no (sequence) name: " + n ); } } return names; diff --git a/forester/java/src/org/forester/sdi/RioException.java b/forester/java/src/org/forester/sdi/RioException.java new file mode 100644 index 0000000..25eeb8e --- /dev/null +++ b/forester/java/src/org/forester/sdi/RioException.java @@ -0,0 +1,18 @@ + +package org.forester.sdi; + +public class RioException extends Exception { + + /** + * + */ + private static final long serialVersionUID = 4691098852783522097L; + + public RioException() { + super(); + } + + public RioException( final String message ) { + super( message ); + } +} diff --git a/forester/java/src/org/forester/test/Test.java b/forester/java/src/org/forester/test/Test.java index c2797fb..ce30298 100644 --- a/forester/java/src/org/forester/test/Test.java +++ b/forester/java/src/org/forester/test/Test.java @@ -3334,6 +3334,50 @@ public final class Test { private static boolean testGetLCA2() { try { final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance(); + final Phylogeny p_a = factory.create( "(a)", new NHXParser() )[ 0 ]; + PhylogenyMethods.preOrderReId( p_a ); + final PhylogenyNode p_a_1 = PhylogenyMethods.calculateLCAonTreeWithIdsInPreOrder( p_a.getNode( "a" ), + p_a.getNode( "a" ) ); + if ( !p_a_1.getName().equals( "a" ) ) { + return false; + } + final Phylogeny p_b = factory.create( "((a)b)", new NHXParser() )[ 0 ]; + PhylogenyMethods.preOrderReId( p_b ); + final PhylogenyNode p_b_1 = PhylogenyMethods.calculateLCAonTreeWithIdsInPreOrder( p_b.getNode( "b" ), + p_b.getNode( "a" ) ); + if ( !p_b_1.getName().equals( "b" ) ) { + return false; + } + final PhylogenyNode p_b_2 = PhylogenyMethods.calculateLCAonTreeWithIdsInPreOrder( p_b.getNode( "a" ), + p_b.getNode( "b" ) ); + if ( !p_b_2.getName().equals( "b" ) ) { + return false; + } + final Phylogeny p_c = factory.create( "(((a)b)c)", new NHXParser() )[ 0 ]; + System.out.println( p_c.toNewHampshireX() ); + PhylogenyMethods.preOrderReId( p_c ); + final PhylogenyNode p_c_1 = PhylogenyMethods.calculateLCAonTreeWithIdsInPreOrder( p_c.getNode( "b" ), + p_c.getNode( "a" ) ); + if ( !p_c_1.getName().equals( "b" ) ) { + return false; + } + final PhylogenyNode p_c_2 = PhylogenyMethods.calculateLCAonTreeWithIdsInPreOrder( p_c.getNode( "a" ), + p_c.getNode( "c" ) ); + if ( !p_c_2.getName().equals( "c" ) ) { + System.out.println( p_c_2.getName() ); + System.exit( -1 ); + return false; + } + final PhylogenyNode p_c_3 = PhylogenyMethods.calculateLCAonTreeWithIdsInPreOrder( p_c.getNode( "a" ), + p_c.getNode( "b" ) ); + if ( !p_c_3.getName().equals( "b" ) ) { + return false; + } + final PhylogenyNode p_c_4 = PhylogenyMethods.calculateLCAonTreeWithIdsInPreOrder( p_c.getNode( "c" ), + p_c.getNode( "a" ) ); + if ( !p_c_4.getName().equals( "c" ) ) { + return false; + } final Phylogeny p1 = factory.create( "((((((A,B)ab,C)abc,D)abcd,E)abcde,F)abcdef,(G,H)gh)abcdefgh", new NHXParser() )[ 0 ]; PhylogenyMethods.preOrderReId( p1 ); diff --git a/forester/java/src/org/forester/util/ForesterUtil.java b/forester/java/src/org/forester/util/ForesterUtil.java index ef30200..336c887 100644 --- a/forester/java/src/org/forester/util/ForesterUtil.java +++ b/forester/java/src/org/forester/util/ForesterUtil.java @@ -955,7 +955,7 @@ public final class ForesterUtil { final public static void unexpectedFatalError( final String prg_name, final Exception e ) { System.err.println(); System.err.println( "[" + prg_name - + "] > unexpected error (Should not have occured! Please contact program author(s).)" ); + + "] > Unexpected error. Should not have occured! Please contact program author(s)." ); e.printStackTrace( System.err ); System.err.println(); System.exit( -1 ); @@ -964,7 +964,7 @@ public final class ForesterUtil { final public static void unexpectedFatalError( final String prg_name, final String message ) { System.err.println(); System.err.println( "[" + prg_name - + "] > unexpected error. Should not have occured! Please contact program author(s)." ); + + "] > Unexpected error. Should not have occured! Please contact program author(s)." ); System.err.println( message ); System.err.println(); System.exit( -1 ); @@ -973,7 +973,7 @@ public final class ForesterUtil { final public static void unexpectedFatalError( final String prg_name, final String message, final Exception e ) { System.err.println(); System.err.println( "[" + prg_name - + "] > unexpected error. Should not have occured! Please contact program author(s)." ); + + "] > Unexpected error. Should not have occured! Please contact program author(s)." ); System.err.println( message ); e.printStackTrace( System.err ); System.err.println();