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;
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";
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 );
}
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 + "\"" );
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 );
}
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 ) );
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 ) {
* @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;
}
* @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();
package org.forester.sdi;
import java.io.File;
+import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
* 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" );
}
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 );
}
* 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();
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<String, HashMap<String, Integer>>();
_so_maps = new HashMap<String, HashMap<String, Integer>>();
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 );
// 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,
if ( !ForesterUtil.isEmpty( query ) ) {
final List<PhylogenyNode> 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 ) );
final List<PhylogenyNode> nodes ) {
final HashMap<String, Integer> 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;
}
}
- public final static IntMatrix calculateOrthologTable( final Phylogeny[] analyzed_gene_trees ) {
+ public final static IntMatrix calculateOrthologTable( final Phylogeny[] analyzed_gene_trees ) throws RioException {
final List<String> labels = new ArrayList<String>();
final Set<String> labels_set = new HashSet<String>();
String label;
PhylogenyMethods.preOrderReId( gt );
final HashMap<String, PhylogenyNode> 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 );
}
}
return s;
}
- private final static List<String> getAllExternalSequenceNames( final Phylogeny phy ) {
+ private final static List<String> getAllExternalSequenceNames( final Phylogeny phy ) throws RioException {
final List<String> names = new ArrayList<String>();
for( final PhylogenyNodeIterator iter = phy.iteratorExternalForward(); iter.hasNext(); ) {
final PhylogenyNode n = iter.next();
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;
--- /dev/null
+
+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 );
+ }
+}
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 );
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 );
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 );
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();