work on: add ability to perform GSDI in applets
[jalview.git] / forester / java / src / org / forester / archaeopteryx / AptxUtil.java
index 7dc6243..89a0cda 100644 (file)
@@ -23,8 +23,6 @@
 // Contact: phylosoft @ gmail . com
 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
 
-
-
 package org.forester.archaeopteryx;
 
 import java.awt.Color;
@@ -38,13 +36,16 @@ import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.IOException;
+import java.io.UnsupportedEncodingException;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.net.URI;
 import java.net.URL;
+import java.net.URLEncoder;
 import java.text.ParseException;
 import java.util.Arrays;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Locale;
@@ -74,6 +75,7 @@ import org.forester.io.parsers.tol.TolParser;
 import org.forester.io.parsers.util.ParserUtils;
 import org.forester.phylogeny.Phylogeny;
 import org.forester.phylogeny.PhylogenyMethods;
+import org.forester.phylogeny.PhylogenyMethods.DESCENDANT_SORT_PRIORITY;
 import org.forester.phylogeny.PhylogenyNode;
 import org.forester.phylogeny.data.Accession;
 import org.forester.phylogeny.data.BranchColor;
@@ -84,7 +86,9 @@ import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
 import org.forester.phylogeny.iterators.PreorderTreeIterator;
 import org.forester.util.AsciiHistogram;
 import org.forester.util.DescriptiveStatistics;
+import org.forester.util.ForesterConstants;
 import org.forester.util.ForesterUtil;
+import org.forester.util.SequenceIdParser;
 import org.forester.ws.seqdb.UniProtTaxonomy;
 
 public final class AptxUtil {
@@ -99,6 +103,69 @@ public final class AptxUtil {
         Arrays.sort( AVAILABLE_FONT_FAMILIES_SORTED );
     }
 
+    public final static String createUriForSeqWeb( final PhylogenyNode node,
+                                                   final Configuration conf,
+                                                   final TreePanel tp ) {
+        String uri_str = null;
+        final String upkb = ForesterUtil.extractUniProtKbProteinSeqIdentifier( node );
+        if ( !ForesterUtil.isEmpty( upkb ) ) {
+            try {
+                uri_str = ForesterUtil.UNIPROT_KB + URLEncoder.encode( upkb, ForesterConstants.UTF8 );
+            }
+            catch ( final UnsupportedEncodingException e ) {
+                showErrorMessage( tp, e.toString() );
+                e.printStackTrace();
+            }
+        }
+        if ( ForesterUtil.isEmpty( uri_str ) ) {
+            final String v = ForesterUtil.extractGenbankAccessor( node );
+            if ( !ForesterUtil.isEmpty( v ) ) {
+                try {
+                    if ( SequenceIdParser.isProtein( v ) ) {
+                        uri_str = ForesterUtil.NCBI_PROTEIN + URLEncoder.encode( v, ForesterConstants.UTF8 );
+                    }
+                    else {
+                        uri_str = ForesterUtil.NCBI_NUCCORE + URLEncoder.encode( v, ForesterConstants.UTF8 );
+                    }
+                }
+                catch ( final UnsupportedEncodingException e ) {
+                    showErrorMessage( tp, e.toString() );
+                    e.printStackTrace();
+                }
+            }
+        }
+        if ( ForesterUtil.isEmpty( uri_str ) ) {
+            final String v = ForesterUtil.extractRefSeqAccessorAccessor( node );
+            if ( !ForesterUtil.isEmpty( v ) ) {
+                try {
+                    if ( SequenceIdParser.isProtein( v ) ) {
+                        uri_str = ForesterUtil.NCBI_PROTEIN + URLEncoder.encode( v, ForesterConstants.UTF8 );
+                    }
+                    else {
+                        uri_str = ForesterUtil.NCBI_NUCCORE + URLEncoder.encode( v, ForesterConstants.UTF8 );
+                    }
+                }
+                catch ( final UnsupportedEncodingException e ) {
+                    showErrorMessage( tp, e.toString() );
+                    e.printStackTrace();
+                }
+            }
+        }
+        if ( ForesterUtil.isEmpty( uri_str ) ) {
+            final String v = ForesterUtil.extractGInumber( node );
+            if ( !ForesterUtil.isEmpty( v ) ) {
+                try {
+                    uri_str = ForesterUtil.NCBI_GI + URLEncoder.encode( v, ForesterConstants.UTF8 );
+                }
+                catch ( final UnsupportedEncodingException e ) {
+                    showErrorMessage( tp, e.toString() );
+                    e.printStackTrace();
+                }
+            }
+        }
+        return uri_str;
+    }
+
     public static MaskFormatter createMaskFormatter( final String s ) {
         MaskFormatter formatter = null;
         try {
@@ -187,6 +254,36 @@ public final class AptxUtil {
         }
     }
 
+    public static Set<Taxonomy> obtainAllDistinctTaxonomies( final PhylogenyNode node ) {
+        final List<PhylogenyNode> descs = node.getAllExternalDescendants();
+        final Set<Taxonomy> tax_set = new HashSet<Taxonomy>();
+        for( final PhylogenyNode n : descs ) {
+            if ( n.getNodeData().isHasTaxonomy() && !n.getNodeData().getTaxonomy().isEmpty() ) {
+                tax_set.add( n.getNodeData().getTaxonomy() );
+            }
+        }
+        return tax_set;
+    }
+
+    /**
+     * Returns the set of distinct taxonomies of
+     * all external nodes of node.
+     * If at least one the external nodes has no taxonomy,
+     * null is returned.
+     * 
+     */
+    public static Set<Taxonomy> obtainDistinctTaxonomies( final PhylogenyNode node ) {
+        final List<PhylogenyNode> descs = node.getAllExternalDescendants();
+        final Set<Taxonomy> tax_set = new HashSet<Taxonomy>();
+        for( final PhylogenyNode n : descs ) {
+            if ( !n.getNodeData().isHasTaxonomy() || n.getNodeData().getTaxonomy().isEmpty() ) {
+                return null;
+            }
+            tax_set.add( n.getNodeData().getTaxonomy() );
+        }
+        return tax_set;
+    }
+
     public final static Accession obtainSequenceAccessionFromName( final String sequence_name ) {
         final String n = sequence_name.trim();
         final Matcher matcher1 = seq_identifier_pattern_1.matcher( n );
@@ -302,7 +399,7 @@ public final class AptxUtil {
                                                                          final ControlPanel ac,
                                                                          final GraphicsExportType type,
                                                                          final Options options ) throws IOException {
-        tree_panel.setParametersForPainting( width, height, true );
+        tree_panel.calcParametersForPainting( width, height, true );
         tree_panel.resetPreferredSize();
         tree_panel.repaint();
         final RenderingHints rendering_hints = new RenderingHints( RenderingHints.KEY_RENDERING,
@@ -476,7 +573,7 @@ public final class AptxUtil {
         for( final PhylogenyNodeIterator it = phy.iteratorPreorder(); it.hasNext(); ) {
             final PhylogenyNode n = it.next();
             if ( !n.isExternal() && !n.isCollapse() && ( n.getNumberOfDescendants() > 1 ) ) {
-                final Set<Taxonomy> taxs = PhylogenyMethods.obtainDistinctTaxonomies( n );
+                final Set<Taxonomy> taxs = obtainDistinctTaxonomies( n );
                 if ( ( taxs != null ) && ( taxs.size() == 1 ) ) {
                     AptxUtil.collapseSubtree( n, true );
                     if ( !n.getNodeData().isHasTaxonomy() ) {
@@ -638,7 +735,22 @@ public final class AptxUtil {
             }
             if ( phy.getIdentifier() != null ) {
                 desc.append( "Id: " );
-                desc.append( phy.getIdentifier() );
+                desc.append( phy.getIdentifier().toString() );
+                desc.append( "\n" );
+            }
+            if ( !ForesterUtil.isEmpty( phy.getDescription() ) ) {
+                desc.append( "Description: " );
+                desc.append( phy.getDescription() );
+                desc.append( "\n" );
+            }
+            if ( !ForesterUtil.isEmpty( phy.getDistanceUnit() ) ) {
+                desc.append( "Distance Unit: " );
+                desc.append( phy.getDistanceUnit() );
+                desc.append( "\n" );
+            }
+            if ( !ForesterUtil.isEmpty( phy.getType() ) ) {
+                desc.append( "Type: " );
+                desc.append( phy.getType() );
                 desc.append( "\n" );
             }
             desc.append( "Rooted: " );
@@ -647,16 +759,19 @@ public final class AptxUtil {
             desc.append( "Rerootable: " );
             desc.append( phy.isRerootable() );
             desc.append( "\n" );
-            desc.append( "Node sum: " );
+            desc.append( "Nodes: " );
             desc.append( phy.getNodeCount() );
             desc.append( "\n" );
-            desc.append( "External node sum: " );
+            desc.append( "External nodes: " );
             desc.append( phy.getNumberOfExternalNodes() );
             desc.append( "\n" );
-            desc.append( "Internal node sum: " );
+            desc.append( "Internal nodes: " );
             desc.append( phy.getNodeCount() - phy.getNumberOfExternalNodes() );
             desc.append( "\n" );
-            desc.append( "Branche sum: " );
+            desc.append( "Internal nodes with polytomies: " );
+            desc.append( PhylogenyMethods.countNumberOfPolytomies( phy ) );
+            desc.append( "\n" );
+            desc.append( "Branches: " );
             desc.append( phy.getNumberOfBranches() );
             desc.append( "\n" );
             desc.append( "Depth: " );
@@ -665,7 +780,7 @@ public final class AptxUtil {
             desc.append( "Maximum distance to root: " );
             desc.append( ForesterUtil.round( PhylogenyMethods.calculateMaxDistanceToRoot( phy ), 6 ) );
             desc.append( "\n" );
-            final Set<Taxonomy> taxs = PhylogenyMethods.obtainDistinctTaxonomies( phy.getRoot() );
+            final Set<Taxonomy> taxs = obtainAllDistinctTaxonomies( phy.getRoot() );
             if ( taxs != null ) {
                 desc.append( "Distinct external taxonomies: " );
                 desc.append( taxs.size() );
@@ -806,17 +921,6 @@ public final class AptxUtil {
         return true;
     }
 
-    final static boolean isJava15() {
-        try {
-            final String s = ForesterUtil.JAVA_VERSION;
-            return s.startsWith( "1.5" );
-        }
-        catch ( final Exception e ) {
-            ForesterUtil.printWarningMessage( Constants.PRG_NAME, "minor error: " + e );
-            return false;
-        }
-    }
-
     final static boolean isMac() {
         try {
             final String s = ForesterUtil.OS_NAME.toLowerCase();
@@ -902,6 +1006,20 @@ public final class AptxUtil {
         }
     }
 
+    final static void outOfMemoryError( final OutOfMemoryError e ) {
+        System.err.println();
+        System.err.println( "Java memory allocation might be too small, try \"-Xmx2048m\" java command line option" );
+        System.err.println();
+        e.printStackTrace();
+        System.err.println();
+        JOptionPane.showMessageDialog( null,
+                                       "Java memory allocation might be too small, try \"-Xmx2048m\" java command line option"
+                                               + "\n\nError: " + e.getLocalizedMessage(),
+                                       "Out of Memory Error [" + Constants.PRG_NAME + " " + Constants.VERSION + "]",
+                                       JOptionPane.ERROR_MESSAGE );
+        System.exit( -1 );
+    }
+
     final static void printAppletMessage( final String applet_name, final String message ) {
         System.out.println( "[" + applet_name + "] > " + message );
     }
@@ -910,8 +1028,9 @@ public final class AptxUtil {
                                                      final boolean phyloxml_validate_against_xsd,
                                                      final boolean replace_underscores,
                                                      final boolean internal_numbers_are_confidences,
-                                                     final TAXONOMY_EXTRACTION taxonomy_extraction )
-            throws FileNotFoundException, IOException {
+                                                     final TAXONOMY_EXTRACTION taxonomy_extraction,
+                                                     final boolean midpoint_reroot ) throws FileNotFoundException,
+            IOException {
         final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
         final PhylogenyParser parser;
         boolean nhx_or_nexus = false;
@@ -940,6 +1059,12 @@ public final class AptxUtil {
                 PhylogenyMethods.transferInternalNodeNamesToConfidence( phy );
             }
         }
+        if ( midpoint_reroot ) {
+            for( final Phylogeny phy : phys ) {
+                PhylogenyMethods.midpointRoot( phy );
+                PhylogenyMethods.orderAppearance( phy.getRoot(), true, true, DESCENDANT_SORT_PRIORITY.NODE_NAME );
+            }
+        }
         return phys;
     }
 
@@ -967,20 +1092,6 @@ public final class AptxUtil {
         System.exit( -1 );
     }
 
-    final static void outOfMemoryError( final OutOfMemoryError e ) {
-        System.err.println();
-        System.err.println( "Java memory allocation might be too small, try \"-Xmx2048m\" java command line option" );
-        System.err.println();
-        e.printStackTrace();
-        System.err.println();
-        JOptionPane.showMessageDialog( null,
-                                       "Java memory allocation might be too small, try \"-Xmx2048m\" java command line option"
-                                               + "\n\nError: " + e.getLocalizedMessage(),
-                                       "Out of Memory Error [" + Constants.PRG_NAME + " " + Constants.VERSION + "]",
-                                       JOptionPane.ERROR_MESSAGE );
-        System.exit( -1 );
-    }
-
     final static void unexpectedException( final Exception e ) {
         System.err.println();
         e.printStackTrace( System.err );
@@ -1008,7 +1119,7 @@ public final class AptxUtil {
             if ( options.isGraphicsExportVisibleOnly() ) {
                 throw new IllegalArgumentException( "cannot export visible rectangle only without exporting in actual size" );
             }
-            tree_panel.setParametersForPainting( options.getPrintSizeX(), options.getPrintSizeY(), true );
+            tree_panel.calcParametersForPainting( options.getPrintSizeX(), options.getPrintSizeY(), true );
             tree_panel.resetPreferredSize();
             tree_panel.repaint();
         }
@@ -1073,7 +1184,7 @@ public final class AptxUtil {
             if ( options.isGraphicsExportVisibleOnly() ) {
                 throw new IllegalArgumentException( "cannot export visible rectangle only without exporting in actual size" );
             }
-            tree_panel.setParametersForPainting( options.getPrintSizeX(), options.getPrintSizeY(), true );
+            tree_panel.calcParametersForPainting( options.getPrintSizeX(), options.getPrintSizeY(), true );
             tree_panel.resetPreferredSize();
             tree_panel.repaint();
         }