in progress
authorcmzmasek@gmail.com <cmzmasek@gmail.com@ca865154-3058-d1c3-3e42-d8f55a55bdbd>
Wed, 28 Sep 2011 02:22:34 +0000 (02:22 +0000)
committercmzmasek@gmail.com <cmzmasek@gmail.com@ca865154-3058-d1c3-3e42-d8f55a55bdbd>
Wed, 28 Sep 2011 02:22:34 +0000 (02:22 +0000)
forester/java/src/org/forester/analysis/AncestralTaxonomyInference.java
forester/java/src/org/forester/archaeopteryx/TreePanel.java
forester/java/src/org/forester/archaeopteryx/Util.java

index 09291f6..73af2c1 100644 (file)
@@ -392,7 +392,7 @@ public final class AncestralTaxonomyInference {
         return not_found;
     }
 
-    synchronized private static UniProtTaxonomy obtainUniProtTaxonomy( final Taxonomy tax, Object query, QUERY_TYPE qt )
+    synchronized public static UniProtTaxonomy obtainUniProtTaxonomy( final Taxonomy tax, Object query, QUERY_TYPE qt )
             throws IOException, AncestralTaxonomyInferenceException {
         if ( isHasAppropriateId( tax ) ) {
             query = tax.getIdentifier().getValue();
@@ -499,7 +499,8 @@ public final class AncestralTaxonomyInference {
                 tax.setRank( "" );
             }
         }
-        if ( ( qt != QUERY_TYPE.ID ) && !ForesterUtil.isEmpty( up_tax.getId() ) && ( tax.getIdentifier() == null ) ) {
+        if ( ( qt != QUERY_TYPE.ID ) && !ForesterUtil.isEmpty( up_tax.getId() )
+                && ( ( tax.getIdentifier() == null ) || ForesterUtil.isEmpty( tax.getIdentifier().getValue() ) ) ) {
             tax.setIdentifier( new Identifier( up_tax.getId(), "uniprot" ) );
         }
         if ( up_tax.getLineage() != null ) {
index aa2582d..684f90c 100644 (file)
@@ -767,6 +767,7 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
             return;
         }
         setWaitCursor();
+        Util.removeBranchColors( _phylogeny );
         Util.colorPhylogenyAccordingToConfidenceValues( _phylogeny, this );
         _control_panel.setColorBranches( true );
         if ( _control_panel.getColorBranchesCb() != null ) {
@@ -781,6 +782,7 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
             return;
         }
         setWaitCursor();
+        Util.removeBranchColors( _phylogeny );
         Util.colorPhylogenyAccordingToRanks( _phylogeny, rank, this );
         _control_panel.setColorBranches( true );
         if ( _control_panel.getColorBranchesCb() != null ) {
index daceb3e..59eb617 100644 (file)
@@ -59,6 +59,7 @@ import javax.swing.JApplet;
 import javax.swing.JOptionPane;
 import javax.swing.text.MaskFormatter;
 
+import org.forester.analysis.AncestralTaxonomyInference;
 import org.forester.io.parsers.PhylogenyParser;
 import org.forester.io.parsers.tol.TolParser;
 import org.forester.phylogeny.Phylogeny;
@@ -72,6 +73,7 @@ import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
 import org.forester.phylogeny.iterators.PreorderTreeIterator;
 import org.forester.util.DescriptiveStatistics;
 import org.forester.util.ForesterUtil;
+import org.forester.ws.uniprot.UniProtTaxonomy;
 
 public final class Util {
 
@@ -295,7 +297,7 @@ public final class Util {
     final static void colorPhylogenyAccordingToRanks( final Phylogeny tree,
                                                       final String rank,
                                                       final TreePanel tree_panel ) {
-        final Map<String, Color> m = new HashMap<String, Color>();
+        final Map<String, Color> true_lineage_to_color_map = new HashMap<String, Color>();
         for( final PhylogenyNodeIterator it = tree.iteratorPostorder(); it.hasNext(); ) {
             final PhylogenyNode n = it.next();
             if ( n.getNodeData().isHasTaxonomy()
@@ -312,24 +314,52 @@ public final class Util {
                         desc.getBranchData().setBranchColor( c );
                     }
                     if ( !ForesterUtil.isEmpty( n.getNodeData().getTaxonomy().getScientificName() ) ) {
-                        m.put( n.getNodeData().getTaxonomy().getScientificName(), c.getValue() );
+                        true_lineage_to_color_map.put( n.getNodeData().getTaxonomy().getScientificName(), c.getValue() );
                     }
                 }
             }
         }
-        if ( !m.isEmpty() ) {
-            for( final PhylogenyNodeIterator it = tree.iteratorPostorder(); it.hasNext(); ) {
-                final PhylogenyNode n = it.next();
-                if ( ( n.getBranchData().getBranchColor() == null ) && n.getNodeData().isHasTaxonomy()
-                        && !ForesterUtil.isEmpty( n.getNodeData().getTaxonomy().getLineage() ) ) {
-                    for( final String lin : n.getNodeData().getTaxonomy().getLineage() ) {
-                        if ( m.containsKey( lin ) ) {
-                            final BranchColor c = new BranchColor( m.get( lin ) );
-                            n.getBranchData().setBranchColor( c );
-                            final List<PhylogenyNode> descs = PhylogenyMethods.getAllDescendants( n );
+        for( final PhylogenyNodeIterator it = tree.iteratorPostorder(); it.hasNext(); ) {
+            final PhylogenyNode node = it.next();
+            if ( ( node.getBranchData().getBranchColor() == null ) && node.getNodeData().isHasTaxonomy()
+                    && !ForesterUtil.isEmpty( node.getNodeData().getTaxonomy().getLineage() ) ) {
+                boolean success = false;
+                if ( !true_lineage_to_color_map.isEmpty() ) {
+                    for( final String lin : node.getNodeData().getTaxonomy().getLineage() ) {
+                        if ( true_lineage_to_color_map.containsKey( lin ) ) {
+                            final BranchColor c = new BranchColor( true_lineage_to_color_map.get( lin ) );
+                            node.getBranchData().setBranchColor( c );
+                            final List<PhylogenyNode> descs = PhylogenyMethods.getAllDescendants( node );
+                            for( final PhylogenyNode desc : descs ) {
+                                desc.getBranchData().setBranchColor( c );
+                            }
+                            success = true;
+                            break;
+                        }
+                    }
+                }
+                //TODO refactor refactor refactor refactor refactor refactor 
+                if ( !success ) {
+                    for( final String lin : node.getNodeData().getTaxonomy().getLineage() ) {
+                        final Taxonomy temp_tax = new Taxonomy();
+                        temp_tax.setScientificName( lin );
+                        UniProtTaxonomy up = null;
+                        try {
+                            up = AncestralTaxonomyInference.obtainUniProtTaxonomy( temp_tax, null, null );
+                        }
+                        catch ( final Exception e ) {
+                            e.printStackTrace();
+                        }
+                        if ( ( up != null ) && !ForesterUtil.isEmpty( up.getRank() )
+                                && up.getRank().equalsIgnoreCase( rank ) ) {
+                            final BranchColor c = new BranchColor( tree_panel.calculateTaxonomyBasedColor( temp_tax ) );
+                            node.getBranchData().setBranchColor( c );
+                            final List<PhylogenyNode> descs = PhylogenyMethods.getAllDescendants( node );
                             for( final PhylogenyNode desc : descs ) {
                                 desc.getBranchData().setBranchColor( c );
                             }
+                            true_lineage_to_color_map.put( lin, c.getValue() );
+                            break;
                         }
                     }
                 }