JAL-2797 added constructor including embedded/standalone boolean
[jalview.git] / forester / java / src / org / forester / analysis / AncestralTaxonomyInference.java
index 6999f8b..94453ff 100644 (file)
@@ -20,7 +20,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;
 
@@ -35,13 +35,13 @@ 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.ws.uniprot.UniProtTaxonomy;
+import org.forester.ws.seqdb.UniProtTaxonomy;
 
 public final class AncestralTaxonomyInference {
 
     public static void inferTaxonomyFromDescendents( final Phylogeny phy ) throws IOException,
-            AncestralTaxonomyInferenceException {
-        TaxonomyDataObtainer.clearCachesIfTooLarge();
+    AncestralTaxonomyInferenceException {
+        TaxonomyDataManager.clearCachesIfTooLarge();
         for( final PhylogenyNodeIterator iter = phy.iteratorPostorder(); iter.hasNext(); ) {
             final PhylogenyNode node = iter.next();
             if ( !node.isExternal() ) {
@@ -51,7 +51,7 @@ public final class AncestralTaxonomyInference {
     }
 
     private static void inferTaxonomyFromDescendents( final PhylogenyNode n ) throws IOException,
-            AncestralTaxonomyInferenceException {
+    AncestralTaxonomyInferenceException {
         if ( n.isExternal() ) {
             throw new IllegalArgumentException( "attempt to infer taxonomy from descendants of external node" );
         }
@@ -61,13 +61,13 @@ public final class AncestralTaxonomyInference {
         int shortest_lin_length = Integer.MAX_VALUE;
         for( final PhylogenyNode desc : descs ) {
             if ( desc.getNodeData().isHasTaxonomy()
-                    && ( TaxonomyDataObtainer.isHasAppropriateId( desc.getNodeData().getTaxonomy() )
+                    && ( TaxonomyDataManager.isHasAppropriateId( desc.getNodeData().getTaxonomy() )
                             || !ForesterUtil.isEmpty( desc.getNodeData().getTaxonomy().getScientificName() )
                             || !ForesterUtil.isEmpty( desc.getNodeData().getTaxonomy().getLineage() )
                             || !ForesterUtil.isEmpty( desc.getNodeData().getTaxonomy().getTaxonomyCode() ) || !ForesterUtil
                             .isEmpty( desc.getNodeData().getTaxonomy().getCommonName() ) ) ) {
-                final UniProtTaxonomy up_tax = TaxonomyDataObtainer.obtainUniProtTaxonomy( desc.getNodeData()
-                        .getTaxonomy(), null, null );
+                final UniProtTaxonomy up_tax = TaxonomyDataManager.obtainUniProtTaxonomy( desc.getNodeData()
+                                                                                          .getTaxonomy(), null, null );
                 if ( ( up_tax == null ) && ForesterUtil.isEmpty( desc.getNodeData().getTaxonomy().getLineage() ) ) {
                     String desc_str = "";
                     if ( !ForesterUtil.isEmpty( desc.getName() ) ) {
@@ -78,9 +78,9 @@ public final class AncestralTaxonomyInference {
                     }
                     System.out.println( desc.getNodeData().getTaxonomy().toString() );
                     System.out.println( ForesterUtil.stringListToString( desc.getNodeData().getTaxonomy().getLineage(),
-                                                                         "  >  " ) );
+                            "  >  " ) );
                     throw new AncestralTaxonomyInferenceException( "a taxonomy for node " + desc_str
-                            + " could not be established from the database" );
+                                                                   + " could not be established from the database" );
                 }
                 String[] lineage = ForesterUtil.stringListToArray( desc.getNodeData().getTaxonomy().getLineage() );
                 if ( ( lineage == null ) || ( lineage.length < 1 ) ) {
@@ -103,19 +103,8 @@ public final class AncestralTaxonomyInference {
                 else {
                     node = "[" + desc.getId() + "]";
                 }
-                //   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();
-                //
                 throw new AncestralTaxonomyInferenceException( "node " + node
-                        + " has no or inappropriate taxonomic information" );
+                                                               + " has no or inappropriate taxonomic information" );
             }
         }
         final List<String> last_common_lineage = new ArrayList<String>();
@@ -135,6 +124,7 @@ public final class AncestralTaxonomyInference {
         if ( last_common_lineage.isEmpty() ) {
             boolean saw_viruses = false;
             boolean saw_cellular_organism = false;
+            boolean saw_x = false;
             for( final String[] lineage : lineages ) {
                 if ( lineage.length > 0 ) {
                     if ( lineage[ 0 ].equalsIgnoreCase( UniProtTaxonomy.VIRUSES ) ) {
@@ -143,14 +133,17 @@ public final class AncestralTaxonomyInference {
                     else if ( lineage[ 0 ].equalsIgnoreCase( UniProtTaxonomy.CELLULAR_ORGANISMS ) ) {
                         saw_cellular_organism = true;
                     }
-                    if ( saw_cellular_organism && saw_viruses ) {
+                    else if ( lineage[ 0 ].equalsIgnoreCase( UniProtTaxonomy.X ) ) {
+                        saw_x = true;
+                    }
+                    if ( ( saw_cellular_organism && saw_viruses ) || saw_x ) {
                         break;
                     }
                 }
             }
-            if ( saw_cellular_organism && saw_viruses ) {
-                last_common_lineage.add( UniProtTaxonomy.CELLULAR_ORGANISMS );
-                last_common = UniProtTaxonomy.CELLULAR_ORGANISMS;
+            if ( ( saw_cellular_organism && saw_viruses ) || saw_x ) {
+                last_common_lineage.add( UniProtTaxonomy.X );
+                last_common = UniProtTaxonomy.X;
             }
             else {
                 String msg = "no common lineage for:\n";
@@ -169,7 +162,7 @@ public final class AncestralTaxonomyInference {
         final Taxonomy tax = new Taxonomy();
         n.getNodeData().setTaxonomy( tax );
         tax.setScientificName( last_common );
-        final UniProtTaxonomy up_tax = TaxonomyDataObtainer.obtainUniProtTaxonomyFromLineage( last_common_lineage );
+        final UniProtTaxonomy up_tax = TaxonomyDataManager.obtainUniProtTaxonomyFromLineage( last_common_lineage );
         if ( up_tax != null ) {
             if ( !ForesterUtil.isEmpty( up_tax.getRank() ) ) {
                 try {