X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=forester%2Fjava%2Fsrc%2Forg%2Fforester%2Fanalysis%2FTaxonomyDataManager.java;h=f99dc7f9c736590f2e0188eac5ae678f15b0d4b9;hb=refs%2Fheads%2Fkjvdh%2Ffeatures%2Fforester;hp=16526037475e5f3bdaa0f5562d190a1cb9d0c943;hpb=ed9211a3bbd54e89e33a83655a9d4210c457a23a;p=jalview.git diff --git a/forester/java/src/org/forester/analysis/TaxonomyDataManager.java b/forester/java/src/org/forester/analysis/TaxonomyDataManager.java index 1652603..f99dc7f 100644 --- a/forester/java/src/org/forester/analysis/TaxonomyDataManager.java +++ b/forester/java/src/org/forester/analysis/TaxonomyDataManager.java @@ -33,6 +33,7 @@ import java.util.HashMap; import java.util.List; import java.util.SortedSet; import java.util.TreeSet; +import java.util.regex.Matcher; import javax.swing.JOptionPane; @@ -40,7 +41,9 @@ import org.forester.archaeopteryx.MainFrameApplication; import org.forester.archaeopteryx.TreePanel; import org.forester.archaeopteryx.tools.AncestralTaxonomyInferrer; import org.forester.archaeopteryx.tools.RunnableProcess; +import org.forester.io.parsers.nhx.NHXParser; import org.forester.io.parsers.phyloxml.PhyloXmlDataFormatException; +import org.forester.io.parsers.util.ParserUtils; import org.forester.phylogeny.Phylogeny; import org.forester.phylogeny.PhylogenyNode; import org.forester.phylogeny.data.Identifier; @@ -127,10 +130,12 @@ public final class TaxonomyDataManager extends RunnableProcess { return _sn_up_cache_map; } + + @SuppressWarnings("unchecked") private final static UniProtTaxonomy obtainTaxonomy( final HashMap cache, final Object query, final QUERY_TYPE qt ) throws IOException, - AncestralTaxonomyInferenceException { + AncestralTaxonomyInferenceException { if ( cache.containsKey( query ) ) { return cache.get( query ).copy(); } @@ -151,6 +156,7 @@ public final class TaxonomyDataManager extends RunnableProcess { break; case LIN: return obtainUniProtTaxonomyFromLineage( ( List ) query ); + default: throw new RuntimeException(); } @@ -196,7 +202,10 @@ public final class TaxonomyDataManager extends RunnableProcess { } private final static List getTaxonomiesFromTaxonomyCode( final String query ) throws IOException { - if ( ( query.indexOf( "XX" ) == 3 ) && TaxonomyUtil.isHasTaxIdFromFakeTaxCode( query ) ) { + //FIXME fix "SPHAR" issue + if ( ( ( query.indexOf( "XX" ) == 3 ) && TaxonomyUtil.isHasTaxIdFromFakeTaxCode( query ) ) + || query.equals( "SPHAR" ) /* TODO remove me, is same as Sphingomonas aromaticivorans */ + ) { final int id = TaxonomyUtil.getTaxIdFromFakeTaxCode( query ); return SequenceDbWsTools.getTaxonomiesFromId( String.valueOf( id ), MAX_TAXONOMIES_TO_RETURN ); } @@ -213,7 +222,7 @@ public final class TaxonomyDataManager extends RunnableProcess { synchronized final private static SortedSet obtainDetailedTaxonomicInformation( final Phylogeny phy, final boolean delete, final boolean allow_to_use_basic_node_names ) - throws IOException, AncestralTaxonomyInferenceException { + throws IOException, AncestralTaxonomyInferenceException { clearCachesIfTooLarge(); final SortedSet not_found = new TreeSet(); List not_found_external_nodes = null; @@ -245,7 +254,10 @@ public final class TaxonomyDataManager extends RunnableProcess { if ( ( ( tax != null ) && ( isHasAppropriateId( tax ) || !ForesterUtil.isEmpty( tax.getScientificName() ) || !ForesterUtil.isEmpty( tax.getTaxonomyCode() ) || !ForesterUtil.isEmpty( tax.getCommonName() ) ) ) || ( allow_to_use_basic_node_names && !ForesterUtil.isEmpty( node.getName() ) ) ) { - if ( tax != null ) { + if ( ( ( tax != null ) && ( isHasAppropriateId( tax ) + || !ForesterUtil.isEmpty( tax.getScientificName() ) + || !ForesterUtil.isEmpty( tax.getTaxonomyCode() ) || !ForesterUtil + .isEmpty( tax.getCommonName() ) ) ) ) { uniprot_tax = obtainUniProtTaxonomy( tax, null, qt ); } else { @@ -255,7 +267,6 @@ public final class TaxonomyDataManager extends RunnableProcess { if ( tax == null ) { tax = new Taxonomy(); node.getNodeData().addTaxonomy( tax ); - node.setName( "" ); } updateTaxonomy( qt, node, tax, uniprot_tax ); } @@ -322,15 +333,39 @@ public final class TaxonomyDataManager extends RunnableProcess { if ( ForesterUtil.isEmpty( simple_name ) ) { throw new IllegalArgumentException( "illegal attempt to use empty simple name" ); } - qt = QUERY_TYPE.SN; - UniProtTaxonomy ut = obtainTaxonomy( TaxonomyDataManager.getSnTaxCacheMap(), simple_name, qt ); - if ( ut == null ) { + UniProtTaxonomy ut = null; + final String code = ParserUtils.extractTaxonomyCodeFromNodeName( simple_name, + NHXParser.TAXONOMY_EXTRACTION.AGGRESSIVE ); + if ( !ForesterUtil.isEmpty( code ) ) { qt = QUERY_TYPE.CODE; - ut = obtainTaxonomy( TaxonomyDataManager.getCodeTaxCacheMap(), simple_name, qt ); + ut = obtainTaxonomy( TaxonomyDataManager.getCodeTaxCacheMap(), code, qt ); } if ( ut == null ) { - qt = QUERY_TYPE.CN; - ut = obtainTaxonomy( TaxonomyDataManager.getCnTaxCacheMap(), simple_name, qt ); + final String sn = ParserUtils.extractScientificNameFromNodeName( simple_name ); + if ( !ForesterUtil.isEmpty( sn ) ) { + qt = QUERY_TYPE.SN; + ut = obtainTaxonomy( TaxonomyDataManager.getSnTaxCacheMap(), sn, qt ); + } + } + if ( ut == null ) { + final String id = ParserUtils + .extractUniprotTaxonomyIdFromNodeName( simple_name, + NHXParser.TAXONOMY_EXTRACTION.PFAM_STYLE_RELAXED ); + if ( !ForesterUtil.isEmpty( id ) ) { + qt = QUERY_TYPE.ID; + ut = obtainTaxonomy( TaxonomyDataManager.getIdTaxCacheMap(), id, qt ); + } + } + if ( ut == null ) { + String sn = ""; + final Matcher m = ParserUtils.TAXOMONY_SN_PATTERN_GENUS.matcher( simple_name ); + if ( m.matches() ) { + sn = m.group( 1 ); + } + if ( !ForesterUtil.isEmpty( sn ) ) { + qt = QUERY_TYPE.SN; + ut = obtainTaxonomy( TaxonomyDataManager.getSnTaxCacheMap(), sn, qt ); + } } return ut; } @@ -344,7 +379,7 @@ public final class TaxonomyDataManager extends RunnableProcess { else { final List matching_taxonomies = new ArrayList(); final List up_taxonomies = getTaxonomiesFromScientificName( lineage - .get( lineage.size() - 1 ) ); + .get( lineage.size() - 1 ) ); if ( ( up_taxonomies != null ) && ( up_taxonomies.size() > 0 ) ) { for( final UniProtTaxonomy up_taxonomy : up_taxonomies ) { boolean match = true; @@ -393,7 +428,7 @@ public final class TaxonomyDataManager extends RunnableProcess { } else { throw new AncestralTaxonomyInferenceException( "taxonomy \"" + ( lineage.get( lineage.size() - 1 ) ) - + "\" not found" ); + + "\" not found" ); } } } @@ -402,7 +437,7 @@ public final class TaxonomyDataManager extends RunnableProcess { final PhylogenyNode node, final Taxonomy tax, final UniProtTaxonomy up_tax ) - throws PhyloXmlDataFormatException { + throws PhyloXmlDataFormatException { if ( ( qt != QUERY_TYPE.SN ) && !ForesterUtil.isEmpty( up_tax.getScientificName() ) && ForesterUtil.isEmpty( tax.getScientificName() ) ) { tax.setScientificName( up_tax.getScientificName() ); @@ -447,7 +482,7 @@ public final class TaxonomyDataManager extends RunnableProcess { not_found = obtainDetailedTaxonomicInformation( _phy, _delete, _allow_simple_names ); } catch ( final UnknownHostException e ) { - JOptionPane.showMessageDialog( _mf, + JOptionPane.showMessageDialog( _mf.getThisFrame(), "Could not connect to \"" + getBaseUrl() + "\"", "Network error during taxonomic information gathering", JOptionPane.ERROR_MESSAGE ); @@ -455,7 +490,7 @@ public final class TaxonomyDataManager extends RunnableProcess { } catch ( final IOException e ) { e.printStackTrace(); - JOptionPane.showMessageDialog( _mf, + JOptionPane.showMessageDialog( _mf.getThisFrame(), e.toString(), "Failed to obtain taxonomic information", JOptionPane.ERROR_MESSAGE ); @@ -463,7 +498,7 @@ public final class TaxonomyDataManager extends RunnableProcess { } catch ( final AncestralTaxonomyInferenceException e ) { e.printStackTrace(); - JOptionPane.showMessageDialog( _mf, + JOptionPane.showMessageDialog( _mf.getThisFrame(), e.toString(), "Failed to obtain taxonomic information", JOptionPane.ERROR_MESSAGE ); @@ -474,13 +509,13 @@ public final class TaxonomyDataManager extends RunnableProcess { } if ( ( _phy == null ) || _phy.isEmpty() ) { try { - JOptionPane.showMessageDialog( _mf, + JOptionPane.showMessageDialog( _mf.getThisFrame(), "None of the external node taxonomies could be resolved", "Taxonomy Tool Failed", JOptionPane.WARNING_MESSAGE ); } catch ( final Exception e ) { - // Not important if this fails, do nothing. + // Not important if this fails, do nothing. } return; } @@ -526,18 +561,18 @@ public final class TaxonomyDataManager extends RunnableProcess { sb.append( "..." ); } try { - JOptionPane.showMessageDialog( _mf, + JOptionPane.showMessageDialog( _mf.getThisFrame(), sb.toString(), "Taxonomy Tool Completed", JOptionPane.WARNING_MESSAGE ); } catch ( final Exception e ) { - // Not important if this fails, do nothing. + // Not important if this fails, do nothing. } } else { try { - JOptionPane.showMessageDialog( _mf, + JOptionPane.showMessageDialog( _mf.getThisFrame(), "Taxonomy tool successfully completed", "Taxonomy Tool Completed", JOptionPane.INFORMATION_MESSAGE );