X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=forester%2Fjava%2Fsrc%2Forg%2Fforester%2Fanalysis%2FTaxonomyDataManager.java;h=630150422ec6d0bf585ff12d99af193925d59f78;hb=4f293d657212059c78fb7649e330842bfea5909a;hp=50cd1cc7b5cc65429d7d7c050a8ea454b11c7317;hpb=08bd889bc13de7415319ea7202d8afb27879c18d;p=jalview.git diff --git a/forester/java/src/org/forester/analysis/TaxonomyDataManager.java b/forester/java/src/org/forester/analysis/TaxonomyDataManager.java index 50cd1cc..6301504 100644 --- a/forester/java/src/org/forester/analysis/TaxonomyDataManager.java +++ b/forester/java/src/org/forester/analysis/TaxonomyDataManager.java @@ -22,7 +22,7 @@ // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA // // Contact: phylosoft @ gmail . com -// WWW: www.phylosoft.org/forester +// WWW: https://sites.google.com/site/cmzmasek/home/software/forester package org.forester.analysis; @@ -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,13 +41,16 @@ 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; import org.forester.phylogeny.data.Taxonomy; import org.forester.phylogeny.iterators.PhylogenyNodeIterator; import org.forester.util.ForesterUtil; +import org.forester.util.TaxonomyUtil; import org.forester.ws.seqdb.SequenceDbWsTools; import org.forester.ws.seqdb.UniProtTaxonomy; @@ -56,7 +60,7 @@ public final class TaxonomyDataManager extends RunnableProcess { CODE, SN, CN, ID, LIN; } private static final int MAX_CACHE_SIZE = 100000; - private static final int MAX_TAXONOMIES_TO_RETURN = 10; + private static final int MAX_TAXONOMIES_TO_RETURN = 2000; private static final HashMap _sn_up_cache_map = new HashMap(); private static final HashMap _lineage_up_cache_map = new HashMap(); private static final HashMap _code_up_cache_map = new HashMap(); @@ -184,10 +188,21 @@ public final class TaxonomyDataManager extends RunnableProcess { } private final static List getTaxonomiesFromScientificName( final String query ) throws IOException { + if ( query.equalsIgnoreCase( UniProtTaxonomy.BACTERIA ) || query.equalsIgnoreCase( UniProtTaxonomy.ARCHAEA ) + || query.equalsIgnoreCase( UniProtTaxonomy.VIRUSES ) + || query.equalsIgnoreCase( UniProtTaxonomy.EUKARYOTA ) || query.equalsIgnoreCase( UniProtTaxonomy.X ) ) { + final List l = new ArrayList(); + l.add( UniProtTaxonomy.createSpecialFromScientificName( query ) ); + return l; + } return SequenceDbWsTools.getTaxonomiesFromScientificNameStrict( query, MAX_TAXONOMIES_TO_RETURN ); } private final static List getTaxonomiesFromTaxonomyCode( final String query ) throws IOException { + if ( ( query.indexOf( "XX" ) == 3 ) && TaxonomyUtil.isHasTaxIdFromFakeTaxCode( query ) ) { + final int id = TaxonomyUtil.getTaxIdFromFakeTaxCode( query ); + return SequenceDbWsTools.getTaxonomiesFromId( String.valueOf( id ), MAX_TAXONOMIES_TO_RETURN ); + } return SequenceDbWsTools.getTaxonomiesFromTaxonomyCode( query, MAX_TAXONOMIES_TO_RETURN ); } @@ -233,7 +248,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 { @@ -243,7 +261,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 ); } @@ -310,15 +327,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; } @@ -326,51 +367,64 @@ public final class TaxonomyDataManager extends RunnableProcess { static final UniProtTaxonomy obtainUniProtTaxonomyFromLineage( final List lineage ) throws AncestralTaxonomyInferenceException, IOException { final String lineage_str = ForesterUtil.stringListToString( lineage, ">" ); - UniProtTaxonomy up_tax = null; if ( TaxonomyDataManager.getLineageTaxCacheMap().containsKey( lineage_str ) ) { - up_tax = TaxonomyDataManager.getLineageTaxCacheMap().get( lineage_str ).copy(); + return TaxonomyDataManager.getLineageTaxCacheMap().get( lineage_str ).copy(); } else { + final List matching_taxonomies = new ArrayList(); final List up_taxonomies = getTaxonomiesFromScientificName( lineage .get( lineage.size() - 1 ) ); if ( ( up_taxonomies != null ) && ( up_taxonomies.size() > 0 ) ) { for( final UniProtTaxonomy up_taxonomy : up_taxonomies ) { - System.out.println( "up_taxonomy=" + up_taxonomy.getScientificName() ); boolean match = true; I: for( int i = 0; i < lineage.size(); ++i ) { - if ( !lineage.get( i ).equalsIgnoreCase( up_taxonomy.getLineage().get( i ) ) ) { + if ( ( i == up_taxonomy.getLineage().size() ) + || !lineage.get( i ).equalsIgnoreCase( up_taxonomy.getLineage().get( i ) ) ) { match = false; break I; } } if ( match ) { - if ( up_tax != null ) { - throw new AncestralTaxonomyInferenceException( "lineage \"" - + ForesterUtil.stringListToString( lineage, " > " ) + "\" is not unique" ); - } - up_tax = up_taxonomy; + matching_taxonomies.add( up_taxonomy ); } } - if ( up_tax == null ) { + if ( matching_taxonomies.isEmpty() ) { throw new AncestralTaxonomyInferenceException( "lineage \"" + ForesterUtil.stringListToString( lineage, " > " ) + "\" not found" ); } - TaxonomyDataManager.getLineageTaxCacheMap().put( lineage_str, up_tax ); - if ( !ForesterUtil.isEmpty( up_tax.getScientificName() ) ) { - TaxonomyDataManager.getSnTaxCacheMap().put( up_tax.getScientificName(), up_tax ); + //in case of more than one (e.g. "Xenopus" Genus and Subgenus), keep shorter, less specific one: + int shortest = Integer.MAX_VALUE; + UniProtTaxonomy least_specific_up_tax = null; + for( final UniProtTaxonomy m : matching_taxonomies ) { + final int s = m.getLineage().size(); + if ( s < shortest ) { + shortest = s; + least_specific_up_tax = m; + } } - if ( !ForesterUtil.isEmpty( up_tax.getCode() ) ) { - TaxonomyDataManager.getCodeTaxCacheMap().put( up_tax.getCode(), up_tax ); + TaxonomyDataManager.getLineageTaxCacheMap().put( lineage_str, least_specific_up_tax ); + if ( !ForesterUtil.isEmpty( least_specific_up_tax.getScientificName() ) ) { + TaxonomyDataManager.getSnTaxCacheMap().put( least_specific_up_tax.getScientificName(), + least_specific_up_tax ); } - if ( !ForesterUtil.isEmpty( up_tax.getCommonName() ) ) { - TaxonomyDataManager.getCnTaxCacheMap().put( up_tax.getCommonName(), up_tax ); + if ( !ForesterUtil.isEmpty( least_specific_up_tax.getCode() ) ) { + TaxonomyDataManager.getCodeTaxCacheMap().put( least_specific_up_tax.getCode(), + least_specific_up_tax ); } - if ( !ForesterUtil.isEmpty( up_tax.getId() ) ) { - TaxonomyDataManager.getIdTaxCacheMap().put( up_tax.getId(), up_tax ); + if ( !ForesterUtil.isEmpty( least_specific_up_tax.getCommonName() ) ) { + TaxonomyDataManager.getCnTaxCacheMap().put( least_specific_up_tax.getCommonName(), + least_specific_up_tax ); } + if ( !ForesterUtil.isEmpty( least_specific_up_tax.getId() ) ) { + TaxonomyDataManager.getIdTaxCacheMap().put( least_specific_up_tax.getId(), least_specific_up_tax ); + } + return least_specific_up_tax; + } + else { + throw new AncestralTaxonomyInferenceException( "taxonomy \"" + ( lineage.get( lineage.size() - 1 ) ) + + "\" not found" ); } } - return up_tax; } synchronized final private static void updateTaxonomy( final QUERY_TYPE qt,