3 // forester -- software libraries and applications
4 // for genomics and evolutionary biology research.
6 // Copyright (C) 2010 Christian M Zmasek
7 // Copyright (C) 2010 Sanford-Burnham Medical Research Institute
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Lesser General Public
12 // License as published by the Free Software Foundation; either
13 // version 2.1 of the License, or (at your option) any later version.
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 // Contact: phylosoft @ gmail . com
25 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
27 package org.forester.analysis;
29 import java.io.IOException;
30 import java.net.UnknownHostException;
31 import java.util.ArrayList;
32 import java.util.HashMap;
33 import java.util.List;
34 import java.util.SortedSet;
35 import java.util.TreeSet;
37 import javax.swing.JOptionPane;
39 import org.forester.archaeopteryx.MainFrameApplication;
40 import org.forester.archaeopteryx.TreePanel;
41 import org.forester.archaeopteryx.tools.AncestralTaxonomyInferrer;
42 import org.forester.archaeopteryx.tools.RunnableProcess;
43 import org.forester.io.parsers.phyloxml.PhyloXmlDataFormatException;
44 import org.forester.phylogeny.Phylogeny;
45 import org.forester.phylogeny.PhylogenyNode;
46 import org.forester.phylogeny.data.Identifier;
47 import org.forester.phylogeny.data.Taxonomy;
48 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
49 import org.forester.util.ForesterUtil;
50 import org.forester.ws.seqdb.SequenceDbWsTools;
51 import org.forester.ws.seqdb.UniProtTaxonomy;
53 public final class TaxonomyDataManager extends RunnableProcess {
56 CODE, SN, CN, ID, LIN;
58 private static final int MAX_CACHE_SIZE = 100000;
59 private static final int MAX_TAXONOMIES_TO_RETURN = 10;
60 private static final HashMap<String, UniProtTaxonomy> _sn_up_cache_map = new HashMap<String, UniProtTaxonomy>();
61 private static final HashMap<String, UniProtTaxonomy> _lineage_up_cache_map = new HashMap<String, UniProtTaxonomy>();
62 private static final HashMap<String, UniProtTaxonomy> _code_up_cache_map = new HashMap<String, UniProtTaxonomy>();
63 private static final HashMap<String, UniProtTaxonomy> _cn_up_cache_map = new HashMap<String, UniProtTaxonomy>();
64 private static final HashMap<String, UniProtTaxonomy> _id_up_cache_map = new HashMap<String, UniProtTaxonomy>();
65 private final Phylogeny _phy;
66 private final MainFrameApplication _mf;
67 private final TreePanel _treepanel;
68 private final boolean _delete;
69 private final boolean _allow_simple_names;
71 public TaxonomyDataManager( final MainFrameApplication mf, final TreePanel treepanel, final Phylogeny phy ) {
74 _treepanel = treepanel;
76 _allow_simple_names = false;
79 public TaxonomyDataManager( final MainFrameApplication mf,
80 final TreePanel treepanel,
83 final boolean allow_simple_name ) {
86 _treepanel = treepanel;
88 _allow_simple_names = allow_simple_name;
91 synchronized static void clearCachesIfTooLarge() {
92 if ( getSnTaxCacheMap().size() > MAX_CACHE_SIZE ) {
93 getSnTaxCacheMap().clear();
95 if ( getLineageTaxCacheMap().size() > MAX_CACHE_SIZE ) {
96 getLineageTaxCacheMap().clear();
98 if ( getCnTaxCacheMap().size() > MAX_CACHE_SIZE ) {
99 getCnTaxCacheMap().clear();
101 if ( getCodeTaxCacheMap().size() > MAX_CACHE_SIZE ) {
102 getCodeTaxCacheMap().clear();
104 if ( getIdTaxCacheMap().size() > MAX_CACHE_SIZE ) {
105 getIdTaxCacheMap().clear();
109 synchronized final static HashMap<String, UniProtTaxonomy> getCnTaxCacheMap() {
110 return _cn_up_cache_map;
113 synchronized final static HashMap<String, UniProtTaxonomy> getCodeTaxCacheMap() {
114 return _code_up_cache_map;
117 synchronized final static HashMap<String, UniProtTaxonomy> getIdTaxCacheMap() {
118 return _id_up_cache_map;
121 synchronized final static HashMap<String, UniProtTaxonomy> getLineageTaxCacheMap() {
122 return _lineage_up_cache_map;
125 synchronized final static HashMap<String, UniProtTaxonomy> getSnTaxCacheMap() {
126 return _sn_up_cache_map;
129 private final static UniProtTaxonomy obtainTaxonomy( final HashMap<String, UniProtTaxonomy> cache,
131 final QUERY_TYPE qt ) throws IOException,
132 AncestralTaxonomyInferenceException {
133 if ( cache.containsKey( query ) ) {
134 return cache.get( query ).copy();
137 List<UniProtTaxonomy> up_taxonomies = null;
140 up_taxonomies = getTaxonomiesFromId( ( String ) query );
143 up_taxonomies = getTaxonomiesFromTaxonomyCode( ( String ) query );
146 up_taxonomies = getTaxonomiesFromScientificName( ( String ) query );
149 up_taxonomies = getTaxonomiesFromCommonName( ( String ) query );
152 return obtainUniProtTaxonomyFromLineage( ( List<String> ) query );
154 throw new RuntimeException();
156 if ( ( up_taxonomies != null ) && ( up_taxonomies.size() == 1 ) ) {
157 final UniProtTaxonomy up_tax = up_taxonomies.get( 0 );
158 if ( !ForesterUtil.isEmpty( up_tax.getScientificName() ) ) {
159 TaxonomyDataManager.getSnTaxCacheMap().put( up_tax.getScientificName(), up_tax );
161 if ( !ForesterUtil.isEmpty( up_tax.getCode() ) ) {
162 TaxonomyDataManager.getCodeTaxCacheMap().put( up_tax.getCode(), up_tax );
164 if ( !ForesterUtil.isEmpty( up_tax.getCommonName() ) ) {
165 TaxonomyDataManager.getCnTaxCacheMap().put( up_tax.getCommonName(), up_tax );
167 if ( !ForesterUtil.isEmpty( up_tax.getId() ) ) {
168 TaxonomyDataManager.getIdTaxCacheMap().put( up_tax.getId(), up_tax );
178 private final static List<UniProtTaxonomy> getTaxonomiesFromCommonName( final String query ) throws IOException {
179 return SequenceDbWsTools.getTaxonomiesFromCommonNameStrict( query, MAX_TAXONOMIES_TO_RETURN );
182 private final static List<UniProtTaxonomy> getTaxonomiesFromId( final String query ) throws IOException {
183 return SequenceDbWsTools.getTaxonomiesFromId( query, MAX_TAXONOMIES_TO_RETURN );
186 private final static List<UniProtTaxonomy> getTaxonomiesFromScientificName( final String query ) throws IOException {
187 return SequenceDbWsTools.getTaxonomiesFromScientificNameStrict( query, MAX_TAXONOMIES_TO_RETURN );
190 private final static List<UniProtTaxonomy> getTaxonomiesFromTaxonomyCode( final String query ) throws IOException {
191 return SequenceDbWsTools.getTaxonomiesFromTaxonomyCode( query, MAX_TAXONOMIES_TO_RETURN );
194 static final boolean isHasAppropriateId( final Taxonomy tax ) {
195 return ( ( tax.getIdentifier() != null ) && ( !ForesterUtil.isEmpty( tax.getIdentifier().getValue() ) && ( tax
196 .getIdentifier().getProvider().equalsIgnoreCase( "ncbi" )
197 || tax.getIdentifier().getProvider().equalsIgnoreCase( "uniprot" ) || tax.getIdentifier().getProvider()
198 .equalsIgnoreCase( "uniprotkb" ) ) ) );
201 synchronized final private static SortedSet<String> obtainDetailedTaxonomicInformation( final Phylogeny phy,
202 final boolean delete,
203 final boolean allow_to_use_basic_node_names )
204 throws IOException, AncestralTaxonomyInferenceException {
205 clearCachesIfTooLarge();
206 final SortedSet<String> not_found = new TreeSet<String>();
207 List<PhylogenyNode> not_found_external_nodes = null;
209 not_found_external_nodes = new ArrayList<PhylogenyNode>();
211 for( final PhylogenyNodeIterator iter = phy.iteratorPostorder(); iter.hasNext(); ) {
212 final PhylogenyNode node = iter.next();
213 final QUERY_TYPE qt = null;
215 if ( node.getNodeData().isHasTaxonomy() ) {
216 tax = node.getNodeData().getTaxonomy();
218 else if ( allow_to_use_basic_node_names && !ForesterUtil.isEmpty( node.getName() ) ) {
219 // Nothing to be done.
221 else if ( node.isExternal() ) {
222 if ( !ForesterUtil.isEmpty( node.getName() ) ) {
223 not_found.add( node.getName() );
226 not_found.add( node.toString() );
229 not_found_external_nodes.add( node );
232 UniProtTaxonomy uniprot_tax = null;
233 if ( ( ( tax != null ) && ( isHasAppropriateId( tax ) || !ForesterUtil.isEmpty( tax.getScientificName() )
234 || !ForesterUtil.isEmpty( tax.getTaxonomyCode() ) || !ForesterUtil.isEmpty( tax.getCommonName() ) ) )
235 || ( allow_to_use_basic_node_names && !ForesterUtil.isEmpty( node.getName() ) ) ) {
237 uniprot_tax = obtainUniProtTaxonomy( tax, null, qt );
240 uniprot_tax = obtainUniProtTaxonomy( node.getName(), qt );
242 if ( uniprot_tax != null ) {
244 tax = new Taxonomy();
245 node.getNodeData().addTaxonomy( tax );
248 updateTaxonomy( qt, node, tax, uniprot_tax );
252 not_found.add( tax.toString() );
255 not_found.add( node.getName() );
257 if ( delete && node.isExternal() ) {
258 not_found_external_nodes.add( node );
264 for( final PhylogenyNode node : not_found_external_nodes ) {
265 phy.deleteSubtree( node, true );
267 phy.externalNodesHaveChanged();
268 phy.clearHashIdToNodeMap();
269 phy.recalculateNumberOfExternalDescendants( true );
274 public final static UniProtTaxonomy obtainUniProtTaxonomy( final Taxonomy tax, Object query, QUERY_TYPE qt )
275 throws IOException, AncestralTaxonomyInferenceException {
277 throw new IllegalArgumentException( "illegal attempt to use empty taxonomy object" );
279 if ( TaxonomyDataManager.isHasAppropriateId( tax ) ) {
280 query = tax.getIdentifier().getValue();
282 return obtainTaxonomy( TaxonomyDataManager.getIdTaxCacheMap(), query, qt );
284 else if ( !ForesterUtil.isEmpty( tax.getScientificName() ) ) {
285 if ( !ForesterUtil.isEmpty( tax.getLineage() ) ) {
286 query = tax.getLineage();
288 return obtainTaxonomy( TaxonomyDataManager.getLineageTaxCacheMap(), query, qt );
291 query = tax.getScientificName();
293 return obtainTaxonomy( TaxonomyDataManager.getSnTaxCacheMap(), query, qt );
296 else if ( !ForesterUtil.isEmpty( tax.getTaxonomyCode() ) ) {
297 query = tax.getTaxonomyCode();
298 qt = QUERY_TYPE.CODE;
299 return obtainTaxonomy( TaxonomyDataManager.getCodeTaxCacheMap(), query, qt );
302 query = tax.getCommonName();
304 return obtainTaxonomy( TaxonomyDataManager.getCnTaxCacheMap(), query, qt );
308 public final static UniProtTaxonomy obtainUniProtTaxonomy( final String simple_name, QUERY_TYPE qt )
309 throws IOException, AncestralTaxonomyInferenceException {
310 if ( ForesterUtil.isEmpty( simple_name ) ) {
311 throw new IllegalArgumentException( "illegal attempt to use empty simple name" );
314 UniProtTaxonomy ut = obtainTaxonomy( TaxonomyDataManager.getSnTaxCacheMap(), simple_name, qt );
316 qt = QUERY_TYPE.CODE;
317 ut = obtainTaxonomy( TaxonomyDataManager.getCodeTaxCacheMap(), simple_name, qt );
321 ut = obtainTaxonomy( TaxonomyDataManager.getCnTaxCacheMap(), simple_name, qt );
326 static final UniProtTaxonomy obtainUniProtTaxonomyFromLineage( final List<String> lineage )
327 throws AncestralTaxonomyInferenceException, IOException {
328 final String lineage_str = ForesterUtil.stringListToString( lineage, ">" );
329 UniProtTaxonomy up_tax = null;
330 if ( TaxonomyDataManager.getLineageTaxCacheMap().containsKey( lineage_str ) ) {
331 up_tax = TaxonomyDataManager.getLineageTaxCacheMap().get( lineage_str ).copy();
334 final List<UniProtTaxonomy> up_taxonomies = getTaxonomiesFromScientificName( lineage
335 .get( lineage.size() - 1 ) );
336 if ( ( up_taxonomies != null ) && ( up_taxonomies.size() > 0 ) ) {
337 for( final UniProtTaxonomy up_taxonomy : up_taxonomies ) {
338 boolean match = true;
339 I: for( int i = 0; i < lineage.size(); ++i ) {
340 if ( !lineage.get( i ).equalsIgnoreCase( up_taxonomy.getLineage().get( i ) ) ) {
346 if ( up_tax != null ) {
347 //TODO this is dead code?!
348 throw new AncestralTaxonomyInferenceException( "lineage \""
349 + ForesterUtil.stringListToString( lineage, " > " ) + "\" is not unique" );
351 up_tax = up_taxonomy;
354 if ( up_tax == null ) {
355 throw new AncestralTaxonomyInferenceException( "lineage \""
356 + ForesterUtil.stringListToString( lineage, " > " ) + "\" not found" );
358 TaxonomyDataManager.getLineageTaxCacheMap().put( lineage_str, up_tax );
359 if ( !ForesterUtil.isEmpty( up_tax.getScientificName() ) ) {
360 TaxonomyDataManager.getSnTaxCacheMap().put( up_tax.getScientificName(), up_tax );
362 if ( !ForesterUtil.isEmpty( up_tax.getCode() ) ) {
363 TaxonomyDataManager.getCodeTaxCacheMap().put( up_tax.getCode(), up_tax );
365 if ( !ForesterUtil.isEmpty( up_tax.getCommonName() ) ) {
366 TaxonomyDataManager.getCnTaxCacheMap().put( up_tax.getCommonName(), up_tax );
368 if ( !ForesterUtil.isEmpty( up_tax.getId() ) ) {
369 TaxonomyDataManager.getIdTaxCacheMap().put( up_tax.getId(), up_tax );
376 synchronized final private static void updateTaxonomy( final QUERY_TYPE qt,
377 final PhylogenyNode node,
379 final UniProtTaxonomy up_tax )
380 throws PhyloXmlDataFormatException {
381 if ( ( qt != QUERY_TYPE.SN ) && !ForesterUtil.isEmpty( up_tax.getScientificName() )
382 && ForesterUtil.isEmpty( tax.getScientificName() ) ) {
383 tax.setScientificName( up_tax.getScientificName() );
385 if ( node.isExternal() && ( qt != QUERY_TYPE.CODE ) && !ForesterUtil.isEmpty( up_tax.getCode() )
386 && ForesterUtil.isEmpty( tax.getTaxonomyCode() ) ) {
387 tax.setTaxonomyCode( up_tax.getCode() );
389 if ( ( qt != QUERY_TYPE.CN ) && !ForesterUtil.isEmpty( up_tax.getCommonName() )
390 && ForesterUtil.isEmpty( tax.getCommonName() ) ) {
391 tax.setCommonName( up_tax.getCommonName() );
393 if ( !ForesterUtil.isEmpty( up_tax.getSynonym() ) && !tax.getSynonyms().contains( up_tax.getSynonym() ) ) {
394 tax.getSynonyms().add( up_tax.getSynonym() );
396 if ( !ForesterUtil.isEmpty( up_tax.getRank() ) && ForesterUtil.isEmpty( tax.getRank() ) ) {
398 tax.setRank( up_tax.getRank().toLowerCase() );
400 catch ( final PhyloXmlDataFormatException ex ) {
404 if ( ( qt != QUERY_TYPE.ID ) && !ForesterUtil.isEmpty( up_tax.getId() )
405 && ( ( tax.getIdentifier() == null ) || ForesterUtil.isEmpty( tax.getIdentifier().getValue() ) ) ) {
406 tax.setIdentifier( new Identifier( up_tax.getId(), "uniprot" ) );
408 if ( up_tax.getLineage() != null ) {
409 tax.setLineage( new ArrayList<String>() );
410 for( final String lin : up_tax.getLineage() ) {
411 if ( !ForesterUtil.isEmpty( lin ) ) {
412 tax.getLineage().add( lin );
418 private final void execute() {
419 start( _mf, "taxonomy data" );
420 SortedSet<String> not_found = null;
422 not_found = obtainDetailedTaxonomicInformation( _phy, _delete, _allow_simple_names );
424 catch ( final UnknownHostException e ) {
425 JOptionPane.showMessageDialog( _mf,
426 "Could not connect to \"" + getBaseUrl() + "\"",
427 "Network error during taxonomic information gathering",
428 JOptionPane.ERROR_MESSAGE );
431 catch ( final IOException e ) {
433 JOptionPane.showMessageDialog( _mf,
435 "Failed to obtain taxonomic information",
436 JOptionPane.ERROR_MESSAGE );
439 catch ( final AncestralTaxonomyInferenceException e ) {
441 JOptionPane.showMessageDialog( _mf,
443 "Failed to obtain taxonomic information",
444 JOptionPane.ERROR_MESSAGE );
450 if ( ( _phy == null ) || _phy.isEmpty() ) {
452 JOptionPane.showMessageDialog( _mf,
453 "None of the external node taxonomies could be resolved",
454 "Taxonomy Tool Failed",
455 JOptionPane.WARNING_MESSAGE );
457 catch ( final Exception e ) {
458 // Not important if this fails, do nothing.
462 _treepanel.setTree( _phy );
464 _treepanel.setEdited( true );
465 if ( ( not_found != null ) && ( not_found.size() > 0 ) ) {
466 int max = not_found.size();
467 boolean more = false;
472 final StringBuffer sb = new StringBuffer();
473 sb.append( "Not all taxonomies could be resolved.\n" );
474 if ( not_found.size() == 1 ) {
476 sb.append( "The following taxonomy was not found and deleted (if external):\n" );
479 sb.append( "The following taxonomy was not found:\n" );
484 sb.append( "The following taxonomies were not found and deleted (if external) (total: "
485 + not_found.size() + "):\n" );
488 sb.append( "The following taxonomies were not found (total: " + not_found.size() + "):\n" );
492 for( final String string : not_found ) {
504 JOptionPane.showMessageDialog( _mf,
506 "Taxonomy Tool Completed",
507 JOptionPane.WARNING_MESSAGE );
509 catch ( final Exception e ) {
510 // Not important if this fails, do nothing.
515 JOptionPane.showMessageDialog( _mf,
516 "Taxonomy tool successfully completed",
517 "Taxonomy Tool Completed",
518 JOptionPane.INFORMATION_MESSAGE );
520 catch ( final Exception e ) {
521 // Not important if this fails, do nothing.
526 private final String getBaseUrl() {
527 return AncestralTaxonomyInferrer.getBaseUrl();