import java.util.SortedSet;
import java.util.TreeSet;
+import org.forester.archaeopteryx.tools.AncestralTaxonomyInferenceException;
import org.forester.io.parsers.phyloxml.PhyloXmlDataFormatException;
import org.forester.phylogeny.Phylogeny;
import org.forester.phylogeny.PhylogenyNode;
return UniProtWsTools.getTaxonomiesFromTaxonomyCode( query, MAX_TAXONOMIES_TO_RETURN );
}
- synchronized public static SortedSet<String> inferTaxonomyFromDescendents( final Phylogeny phy ) throws IOException {
+ synchronized public static void inferTaxonomyFromDescendents( final Phylogeny phy ) throws IOException,
+ AncestralTaxonomyInferenceException {
clearCachesIfTooLarge();
- final SortedSet<String> not_found = new TreeSet<String>();
for( final PhylogenyNodeIterator iter = phy.iteratorPostorder(); iter.hasNext(); ) {
final PhylogenyNode node = iter.next();
if ( !node.isExternal() ) {
- inferTaxonomyFromDescendents( node, not_found );
+ inferTaxonomyFromDescendents( node );
}
}
- return not_found;
}
- synchronized private static void inferTaxonomyFromDescendents( final PhylogenyNode n,
- final SortedSet<String> not_found )
- throws IOException {
+ synchronized private static void inferTaxonomyFromDescendents( final PhylogenyNode n ) throws IOException,
+ AncestralTaxonomyInferenceException {
if ( n.isExternal() ) {
throw new IllegalArgumentException( "attempt to infer taxonomy from descendants of external node" );
}
|| !ForesterUtil.isEmpty( desc.getNodeData().getTaxonomy().getScientificName() )
|| !ForesterUtil.isEmpty( desc.getNodeData().getTaxonomy().getTaxonomyCode() ) || !ForesterUtil
.isEmpty( desc.getNodeData().getTaxonomy().getCommonName() ) ) ) {
-
final UniProtTaxonomy up_tax = obtainUniProtTaxonomy( desc.getNodeData().getTaxonomy(), null, null );
String[] lineage = null;
if ( up_tax != null ) {
- //lineage = obtainLineagePlusOwnScientificName( up_tax );
lineage = up_tax.getLineageAsArray();
}
if ( ( lineage == null ) || ( lineage.length < 1 ) ) {
- //TODO remove me
- System.out.println( "node " + desc.getNodeData().getTaxonomy().toString() + " has no lineage!" );
- not_found.add( desc.getNodeData().getTaxonomy().asText().toString() );
- return;
+ throw new AncestralTaxonomyInferenceException( "a taxonomic lineage for node \""
+ + desc.getNodeData().getTaxonomy().toString() + "\" could not be found" );
}
if ( lineage.length < shortest_lin_length ) {
shortest_lin_length = lineage.length;
lineages.add( lineage );
}
else {
- String msg = "Node(s) with no or inappropriate taxonomic information found";
String node = "";
if ( !ForesterUtil.isEmpty( desc.getName() ) ) {
node = "\"" + desc.getName() + "\"";
else {
node = "[" + desc.getId() + "]";
}
- msg = "Node " + node + " has no or inappropriate taxonomic information";
- // final List<PhylogenyNode> e = desc.getAllExternalDescendants();
+ // final List<PhylogenyNode> e = desc.getAllExternalDescendants();
//TODO remove me!
-// System.out.println();
-// int x = 0;
-// for( final PhylogenyNode object : e ) {
-// System.out.println( x + ":" );
-// System.out.println( object.getName() + " " );
-// x++;
-// }
-// System.out.println();
+ // System.out.println();
+ // int x = 0;
+ // for( final PhylogenyNode object : e ) {
+ // System.out.println( x + ":" );
+ // System.out.println( object.getName() + " " );
+ // x++;
+ // }
+ // System.out.println();
//
- throw new IllegalArgumentException( msg );
+ throw new AncestralTaxonomyInferenceException( "node " + node
+ + " has no or inappropriate taxonomic information" );
}
}
- List<String> last_common_lineage = new ArrayList<String>();
+ final List<String> last_common_lineage = new ArrayList<String>();
String last_common = null;
if ( shortest_lin_length > 0 ) {
I: for( int i = 0; i < shortest_lin_length; ++i ) {
break I;
}
}
- // last_common_lineage = lineage_0;
- last_common_lineage.add( lineage_0 ) ;
- last_common =lineage_0;
+ // last_common_lineage = lineage_0;
+ last_common_lineage.add( lineage_0 );
+ last_common = lineage_0;
}
}
- // if ( last_common_lineage == null ) {
+ // if ( last_common_lineage == null ) {
if ( last_common_lineage.isEmpty() ) {
- System.out.println( "No common lineage for:" );
+ String msg = "no common lineage for:\n";
int counter = 0;
for( final String[] strings : lineages ) {
- System.out.print( counter + ": " );
+ msg += counter + ": ";
++counter;
for( final String string : strings ) {
- System.out.print( string + " " );
+ msg += string + " ";
}
- System.out.println();
+ msg += "\n";
}
- return;
+ throw new AncestralTaxonomyInferenceException( msg );
}
final Taxonomy tax = new Taxonomy();
n.getNodeData().setTaxonomy( tax );
}
}
}
-
}
for( final PhylogenyNode desc : descs ) {
if ( !desc.isExternal() && desc.getNodeData().isHasTaxonomy()
if ( isHasAppropriateId( tax ) ) {
query = tax.getIdentifier().getValue();
qt = QUERY_TYPE.ID;
- System.out.println( "query by id: " + query);
+ System.out.println( "query by id: " + query );
return getTaxonomies( getIdTaxCacheMap(), query, qt );
}
else if ( !ForesterUtil.isEmpty( tax.getScientificName() ) ) {
query = tax.getScientificName();
qt = QUERY_TYPE.SN;
- System.out.println( "query by sn: " + query);
+ System.out.println( "query by sn: " + query );
return getTaxonomies( getSnTaxCacheMap(), query, qt );
}
else if ( !ForesterUtil.isEmpty( tax.getTaxonomyCode() ) ) {
}
}
- synchronized private static UniProtTaxonomy obtainUniProtTaxonomyFromSn( final String sn) throws IOException {
+ synchronized private static UniProtTaxonomy obtainUniProtTaxonomyFromSn( final String sn ) throws IOException {
UniProtTaxonomy up_tax = null;
if ( getSnTaxCacheMap().containsKey( sn ) ) {
up_tax = getSnTaxCacheMap().get( sn ).copy();
if ( !ForesterUtil.isEmpty( up_tax.getId() ) ) {
getIdTaxCacheMap().put( up_tax.getId(), up_tax );
}
-
}
}
return up_tax;
}
-
- synchronized private static UniProtTaxonomy obtainUniProtTaxonomyFromCommonLineage( List<String> lineage ) throws IOException {
- UniProtTaxonomy up_tax = null;
- // -- if ( getSnTaxCacheMap().containsKey( sn ) ) {
- // -- up_tax = getSnTaxCacheMap().get( sn ).copy();
- // -- }
- // else {
- final List<UniProtTaxonomy> up_taxonomies = getTaxonomiesFromScientificName( lineage.get(lineage.size() -1 ) );
- //-- if ( ( up_taxonomies != null ) && ( up_taxonomies.size() == 1 ) ) {
-
- if ( ( up_taxonomies != null ) && ( up_taxonomies.size() > 0 ) ) {
- for( UniProtTaxonomy up_taxonomy : up_taxonomies ) {
- boolean match = true;
- I: for( int i = 0; i < lineage.size(); ++i ) {
- if ( !lineage.get( i ).equalsIgnoreCase( up_taxonomy.getLineage().get( i ) ) ) {
- match = false;
- break I;
- }
- }
- if ( match ) {
- if ( up_tax != null ) {
- throw new IOException( "not unique!");
- }
- up_tax = up_taxonomy;
+ synchronized private static UniProtTaxonomy obtainUniProtTaxonomyFromCommonLineage( final List<String> lineage )
+ throws AncestralTaxonomyInferenceException, IOException {
+ UniProtTaxonomy up_tax = null;
+ // -- if ( getSnTaxCacheMap().containsKey( sn ) ) {
+ // -- up_tax = getSnTaxCacheMap().get( sn ).copy();
+ // -- }
+ // else {
+ final List<UniProtTaxonomy> up_taxonomies = getTaxonomiesFromScientificName( lineage.get( lineage.size() - 1 ) );
+ //-- if ( ( up_taxonomies != null ) && ( up_taxonomies.size() == 1 ) ) {
+ if ( ( up_taxonomies != null ) && ( up_taxonomies.size() > 0 ) ) {
+ for( final UniProtTaxonomy up_taxonomy : up_taxonomies ) {
+ boolean match = true;
+ I: for( int i = 0; i < lineage.size(); ++i ) {
+ if ( !lineage.get( i ).equalsIgnoreCase( up_taxonomy.getLineage().get( i ) ) ) {
+ match = false;
+ break I;
}
}
-
- if ( up_tax == null ) {
- throw new IOException( "not found!");
- }
- //-- up_tax = up_taxonomies.get( 0 );
- //-- getSnTaxCacheMap().put( sn, up_tax );
- if ( !ForesterUtil.isEmpty( up_tax.getCode() ) ) {
- getCodeTaxCacheMap().put( up_tax.getCode(), up_tax );
- }
- if ( !ForesterUtil.isEmpty( up_tax.getCommonName() ) ) {
- getCnTaxCacheMap().put( up_tax.getCommonName(), up_tax );
- }
- if ( !ForesterUtil.isEmpty( up_tax.getId() ) ) {
- getIdTaxCacheMap().put( up_tax.getId(), up_tax );
+ if ( match ) {
+ if ( up_tax != null ) {
+ throw new AncestralTaxonomyInferenceException( "lineage \""
+ + ForesterUtil.stringListToString( lineage, " > " ) + "\" is not unique" );
+ }
+ up_tax = up_taxonomy;
}
-
}
- // }
+ if ( up_tax == null ) {
+ throw new AncestralTaxonomyInferenceException( "lineage \""
+ + ForesterUtil.stringListToString( lineage, " > " ) + "\" not found" );
+ }
+ //-- up_tax = up_taxonomies.get( 0 );
+ //-- getSnTaxCacheMap().put( sn, up_tax );
+ if ( !ForesterUtil.isEmpty( up_tax.getCode() ) ) {
+ getCodeTaxCacheMap().put( up_tax.getCode(), up_tax );
+ }
+ if ( !ForesterUtil.isEmpty( up_tax.getCommonName() ) ) {
+ getCnTaxCacheMap().put( up_tax.getCommonName(), up_tax );
+ }
+ if ( !ForesterUtil.isEmpty( up_tax.getId() ) ) {
+ getIdTaxCacheMap().put( up_tax.getId(), up_tax );
+ }
+ }
+ // }
return up_tax;
}
}
}
}
-
}
private enum QUERY_TYPE {
package org.forester.archaeopteryx.tools;
import java.net.UnknownHostException;
-import java.util.SortedSet;
import javax.swing.JOptionPane;
private void inferTaxonomies() {
_mf.getMainPanel().getCurrentTreePanel().setWaitCursor();
- SortedSet<String> not_found = null;
try {
- not_found = AncestralTaxonomyInference.inferTaxonomyFromDescendents( _phy );
+ AncestralTaxonomyInference.inferTaxonomyFromDescendents( _phy );
}
- catch ( final IllegalArgumentException e ) {
+ catch ( final AncestralTaxonomyInferenceException e ) {
_mf.getMainPanel().getCurrentTreePanel().setArrowCursor();
JOptionPane.showMessageDialog( _mf,
e.getMessage(),
_treepanel.setTree( _phy );
_mf.showWhole();
_treepanel.setEdited( true );
- if ( ( not_found != null ) && ( not_found.size() > 0 ) ) {
- int max = not_found.size();
- boolean more = false;
- if ( max > 20 ) {
- more = true;
- max = 20;
- }
- final StringBuffer sb = new StringBuffer();
- sb.append( "Not all taxonomies could be resolved.\n" );
- sb.append( "The result is incomplete, and, possibly, misleading.\n" );
- if ( not_found.size() == 1 ) {
- sb.append( "The following taxonomy was not found:\n" );
- }
- else {
- sb.append( "The following taxonomies were not found (total: " + not_found.size() + "):\n" );
- }
- int i = 0;
- for( final String string : not_found ) {
- if ( i > 19 ) {
- break;
- }
- sb.append( string );
- sb.append( "\n" );
- ++i;
- }
- if ( more ) {
- sb.append( "..." );
- }
- try {
- JOptionPane.showMessageDialog( _mf,
- sb.toString(),
- "Ancestral Taxonomy Inference Completed",
- JOptionPane.WARNING_MESSAGE );
- }
- catch ( final Exception e ) {
- // Not important if this fails, do nothing.
- }
+ try {
+ JOptionPane.showMessageDialog( _mf,
+ "Ancestral taxonomy inference successfully completed",
+ "Ancestral Taxonomy Inference Completed",
+ JOptionPane.INFORMATION_MESSAGE );
}
- else {
- try {
- JOptionPane.showMessageDialog( _mf,
- "Ancestral taxonomy inference successfully completed",
- "Ancestral Taxonomy Inference Completed",
- JOptionPane.INFORMATION_MESSAGE );
- }
- catch ( final Exception e ) {
- // Not important if this fails, do nothing.
- }
+ catch ( final Exception e ) {
+ // Not important if this fails, do nothing.
}
}