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 NHXParser nhx = ( NHXParser ) p;
nhx.setReplaceUnderscores( false );
nhx.setIgnoreQuotes( true );
- nhx.setTaxonomyExtraction( PhylogenyMethods.TAXONOMY_EXTRACTION.YES );
+ nhx.setTaxonomyExtraction( NHXParser.TAXONOMY_EXTRACTION.YES );
}
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 );
+ }
+ String my;
+ PhylogenyNode ny;
for( int y = 0; y < m.size(); ++y ) {
- if ( !PhylogenyMethods.calculateLCAonTreeWithIdsInPreOrder( nx, map.get( m.getLabel( y ) ) )
- .isDuplication() ) {
+ my = m.getLabel( y );
+ 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;