X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=forester%2Fjava%2Fsrc%2Forg%2Fforester%2Farchaeopteryx%2FAptxUtil.java;h=627e1523c31579758399dbe04b893f4a52f9d9e1;hb=a9cc2cc5c9cda0ab49f26ccfb7a1aa30531e6ecf;hp=1078656bd7ab1308f9819783c9bc82302df49b23;hpb=ebabb46561f996b5fcd59bd202defd7b189ea5b6;p=jalview.git diff --git a/forester/java/src/org/forester/archaeopteryx/AptxUtil.java b/forester/java/src/org/forester/archaeopteryx/AptxUtil.java index 1078656..627e152 100644 --- a/forester/java/src/org/forester/archaeopteryx/AptxUtil.java +++ b/forester/java/src/org/forester/archaeopteryx/AptxUtil.java @@ -21,7 +21,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.archaeopteryx; @@ -43,6 +43,7 @@ import java.net.URL; 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; @@ -64,6 +65,9 @@ import javax.swing.text.MaskFormatter; import org.forester.analysis.TaxonomyDataManager; import org.forester.io.parsers.PhylogenyParser; +import org.forester.io.parsers.nexus.NexusPhylogeniesParser; +import org.forester.io.parsers.nhx.NHXParser; +import org.forester.io.parsers.nhx.NHXParser.TAXONOMY_EXTRACTION; import org.forester.io.parsers.phyloxml.PhyloXmlUtil; import org.forester.io.parsers.tol.TolParser; import org.forester.io.parsers.util.ParserUtils; @@ -84,6 +88,12 @@ import org.forester.ws.seqdb.UniProtTaxonomy; public final class AptxUtil { + final static String UNIPROT_KB = "http://www.uniprot.org/uniprot/"; + final static Pattern UNIPROT_KB_PATTERN_1 = Pattern + .compile( "\\bsp|tr.\\S([A-Z0-9]{5,6})\\b" ); + + final static Pattern UNIPROT_KB_PATTERN_2 = Pattern + .compile( "\\b[A-Z0-9]{5,6}_[A-Z9][A-Z]{2}[A-Z0-9]{2}|RAT|PIG|PEA\\b" ); private final static Pattern seq_identifier_pattern_1 = Pattern .compile( "^([A-Za-z]{2,5})[|=:]([0-9A-Za-z_\\.]{5,40})\\s*$" ); private final static Pattern seq_identifier_pattern_2 = Pattern @@ -182,6 +192,36 @@ public final class AptxUtil { } } + public static Set obtainAllDistinctTaxonomies( final PhylogenyNode node ) { + final List descs = node.getAllExternalDescendants(); + final Set tax_set = new HashSet(); + 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 obtainDistinctTaxonomies( final PhylogenyNode node ) { + final List descs = node.getAllExternalDescendants(); + final Set tax_set = new HashSet(); + 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 ); @@ -297,7 +337,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, @@ -471,7 +511,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 taxs = PhylogenyMethods.obtainDistinctTaxonomies( n ); + final Set taxs = obtainDistinctTaxonomies( n ); if ( ( taxs != null ) && ( taxs.size() == 1 ) ) { AptxUtil.collapseSubtree( n, true ); if ( !n.getNodeData().isHasTaxonomy() ) { @@ -633,7 +673,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: " ); @@ -642,16 +697,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: " ); @@ -660,7 +718,7 @@ public final class AptxUtil { desc.append( "Maximum distance to root: " ); desc.append( ForesterUtil.round( PhylogenyMethods.calculateMaxDistanceToRoot( phy ), 6 ) ); desc.append( "\n" ); - final Set taxs = PhylogenyMethods.obtainDistinctTaxonomies( phy.getRoot() ); + final Set taxs = obtainAllDistinctTaxonomies( phy.getRoot() ); if ( taxs != null ) { desc.append( "Distinct external taxonomies: " ); desc.append( taxs.size() ); @@ -791,24 +849,6 @@ public final class AptxUtil { return AVAILABLE_FONT_FAMILIES_SORTED; } - final static void inferCommonPartOfScientificNames( final Phylogeny tree ) { - boolean inferred = false; - for( final PhylogenyNodeIterator it = tree.iteratorPostorder(); it.hasNext(); ) { - final PhylogenyNode n = it.next(); - if ( !n.getNodeData().isHasTaxonomy() && !n.isExternal() ) { - final String sn = PhylogenyMethods.inferCommonPartOfScientificNameOfDescendants( n ); - if ( !ForesterUtil.isEmpty( sn ) ) { - n.getNodeData().setTaxonomy( new Taxonomy() ); - n.getNodeData().getTaxonomy().setScientificName( sn ); - inferred = true; - } - } - } - if ( inferred ) { - tree.setRerootable( false ); - } - } - final static boolean isHasAssignedEvent( final PhylogenyNode node ) { if ( !node.getNodeData().isHasEvent() ) { return false; @@ -819,17 +859,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(); @@ -915,21 +944,59 @@ 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 ); } - final static Phylogeny[] readPhylogeniesFromUrl( final URL url, final boolean phyloxml_validate_against_xsd ) + final static Phylogeny[] readPhylogeniesFromUrl( final URL url, + 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 PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance(); - PhylogenyParser parser = null; + final PhylogenyParser parser; + boolean nhx_or_nexus = false; if ( url.getHost().toLowerCase().indexOf( "tolweb" ) >= 0 ) { parser = new TolParser(); } else { parser = ParserUtils.createParserDependingOnUrlContents( url, phyloxml_validate_against_xsd ); + if ( parser instanceof NHXParser ) { + nhx_or_nexus = true; + final NHXParser nhx = ( NHXParser ) parser; + nhx.setReplaceUnderscores( replace_underscores ); + nhx.setIgnoreQuotes( false ); + nhx.setTaxonomyExtraction( taxonomy_extraction ); + } + else if ( parser instanceof NexusPhylogeniesParser ) { + nhx_or_nexus = true; + final NexusPhylogeniesParser nex = ( NexusPhylogeniesParser ) parser; + nex.setReplaceUnderscores( replace_underscores ); + nex.setIgnoreQuotes( false ); + } + } + final Phylogeny[] phys = factory.create( url.openStream(), parser ); + if ( nhx_or_nexus && internal_numbers_are_confidences ) { + for( final Phylogeny phy : phys ) { + PhylogenyMethods.transferInternalNodeNamesToConfidence( phy ); + } } - return factory.create( url.openStream(), parser ); + return phys; } final static void removeBranchColors( final Phylogeny phy ) { @@ -938,30 +1005,38 @@ public final class AptxUtil { } } - final static void unexpectedError( final Error err ) { - err.printStackTrace(); + final static void unexpectedError( final Error e ) { + System.err.println(); + e.printStackTrace( System.err ); + System.err.println(); final StringBuffer sb = new StringBuffer(); - for( final StackTraceElement s : err.getStackTrace() ) { + for( final StackTraceElement s : e.getStackTrace() ) { sb.append( s + "\n" ); } JOptionPane .showMessageDialog( null, "An unexpected (possibly severe) error has occured - terminating. \nPlease contact: " - + Constants.AUTHOR_EMAIL + " \nError: " + err + "\n" + sb, + + Constants.AUTHOR_EMAIL + " \nError: " + e.getLocalizedMessage() + "\n" + + sb, "Unexpected Severe Error [" + Constants.PRG_NAME + " " + Constants.VERSION + "]", JOptionPane.ERROR_MESSAGE ); System.exit( -1 ); } - final static void unexpectedException( final Exception ex ) { - ex.printStackTrace(); + final static void unexpectedException( final Exception e ) { + System.err.println(); + e.printStackTrace( System.err ); + System.err.println(); final StringBuffer sb = new StringBuffer(); - for( final StackTraceElement s : ex.getStackTrace() ) { + for( final StackTraceElement s : e.getStackTrace() ) { sb.append( s + "\n" ); } - JOptionPane.showMessageDialog( null, "An unexpected exception has occured. \nPlease contact: " - + Constants.AUTHOR_EMAIL + " \nException: " + ex + "\n" + sb, "Unexpected Exception [" - + Constants.PRG_NAME + Constants.VERSION + "]", JOptionPane.ERROR_MESSAGE ); + JOptionPane.showMessageDialog( null, + "An unexpected exception has occured. \nPlease contact: " + + Constants.AUTHOR_EMAIL + " \nException: " + e.getLocalizedMessage() + + "\n" + sb, + "Unexpected Exception [" + Constants.PRG_NAME + Constants.VERSION + "]", + JOptionPane.ERROR_MESSAGE ); } final static String writePhylogenyToGraphicsByteArrayOutputStream( final ByteArrayOutputStream baos, @@ -975,7 +1050,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(); } @@ -1040,7 +1115,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(); }