From b59302eba1039cb46f634ffe77cebc1b5c9bbcbf Mon Sep 17 00:00:00 2001 From: "cmzmasek@gmail.com" Date: Fri, 14 Feb 2014 18:05:20 +0000 Subject: [PATCH] inprogress --- .../src/org/forester/archaeopteryx/AptxUtil.java | 4 + .../org/forester/archaeopteryx/ArchaeopteryxE.java | 88 ++++++++++++-------- .../org/forester/io/parsers/PhylogenyParser.java | 2 + .../io/parsers/nexus/NexusPhylogeniesParser.java | 5 ++ .../src/org/forester/io/parsers/nhx/NHXParser.java | 28 +++++-- .../io/parsers/phyloxml/PhyloXmlParser.java | 5 ++ .../src/org/forester/io/parsers/tol/TolParser.java | 5 ++ .../phylogeny/factories/BasicPhylogenyFactory.java | 48 ----------- .../factories/ParserBasedPhylogenyFactory.java | 12 +-- .../phylogeny/factories/PhylogenyFactory.java | 27 +----- forester/java/src/org/forester/test/Test.java | 27 +++++- 11 files changed, 123 insertions(+), 128 deletions(-) delete mode 100644 forester/java/src/org/forester/phylogeny/factories/BasicPhylogenyFactory.java diff --git a/forester/java/src/org/forester/archaeopteryx/AptxUtil.java b/forester/java/src/org/forester/archaeopteryx/AptxUtil.java index d26127a..623a83e 100644 --- a/forester/java/src/org/forester/archaeopteryx/AptxUtil.java +++ b/forester/java/src/org/forester/archaeopteryx/AptxUtil.java @@ -707,7 +707,11 @@ public final class AptxUtil { nex.setIgnoreQuotes( false ); } } + AptxUtil.printAppletMessage( "Archaeopteryx", "parser is " + parser.getName() ); final Phylogeny[] phys = factory.create( url.openStream(), parser ); + for( final Phylogeny phy : phys ) { + System.out.println( phy.toString() ); + } if ( nhx_or_nexus && internal_numbers_are_confidences ) { for( final Phylogeny phy : phys ) { PhylogenyMethods.transferInternalNodeNamesToConfidence( phy ); diff --git a/forester/java/src/org/forester/archaeopteryx/ArchaeopteryxE.java b/forester/java/src/org/forester/archaeopteryx/ArchaeopteryxE.java index 36767b6..e5bb03f 100644 --- a/forester/java/src/org/forester/archaeopteryx/ArchaeopteryxE.java +++ b/forester/java/src/org/forester/archaeopteryx/ArchaeopteryxE.java @@ -446,45 +446,65 @@ public class ArchaeopteryxE extends JApplet implements ActionListener { setConfiguration( configuration ); setOptions( Options.createInstance( configuration ) ); setupUI(); - URL phys_url = null; - Phylogeny[] phys = null; final String tree_url_str = getParameter( Constants.APPLET_PARAM_NAME_FOR_URL_OF_TREE_TO_LOAD ); + if ( ForesterUtil.isEmpty( tree_url_str ) ) { + ForesterUtil.printErrorMessage( NAME, "could not get tree URL from " + + Constants.APPLET_PARAM_NAME_FOR_URL_OF_TREE_TO_LOAD ); + JOptionPane.showMessageDialog( this, NAME + ": could not get tree URL from " + + Constants.APPLET_PARAM_NAME_FOR_URL_OF_TREE_TO_LOAD, "Failed get URL", JOptionPane.ERROR_MESSAGE ); + return; + } AptxUtil.printAppletMessage( NAME, "URL for phylogenies is " + tree_url_str ); // Get URL to tree file - if ( tree_url_str != null ) { - try { - phys_url = new URL( tree_url_str ); - } - catch ( final Exception e ) { - ForesterUtil.printErrorMessage( NAME, "error: " + e ); - e.printStackTrace(); - JOptionPane.showMessageDialog( this, NAME + ": Could not create URL from: \"" + tree_url_str - + "\"\nException: " + e, "Failed to create URL", JOptionPane.ERROR_MESSAGE ); - } + URL phys_url = null; + try { + phys_url = new URL( tree_url_str ); + } + catch ( final Exception e ) { + ForesterUtil.printErrorMessage( NAME, "error: " + e ); + e.printStackTrace(); + JOptionPane.showMessageDialog( this, NAME + ": Could not create URL from: \"" + tree_url_str + + "\"\nException: " + e, "Failed to create URL", JOptionPane.ERROR_MESSAGE ); + } + if ( phys_url == null ) { + ForesterUtil.printErrorMessage( NAME, "failed to get tree URL from " + + Constants.APPLET_PARAM_NAME_FOR_URL_OF_TREE_TO_LOAD ); + JOptionPane.showMessageDialog( this, + NAME + ": Could not create URL from: \"" + tree_url_str, + "Failed to create URL", + JOptionPane.ERROR_MESSAGE ); + return; } // Load the tree from URL - if ( phys_url != null ) { - try { - phys = AptxUtil.readPhylogeniesFromUrl( phys_url, - getConfiguration().isValidatePhyloXmlAgainstSchema(), - getConfiguration().isReplaceUnderscoresInNhParsing(), - getConfiguration().isInternalNumberAreConfidenceForNhParsing(), - getConfiguration().getTaxonomyExtraction(), - getConfiguration().isMidpointReroot() ); - } - catch ( final Exception e ) { - ForesterUtil.printErrorMessage( NAME, e.toString() ); - e.printStackTrace(); - JOptionPane.showMessageDialog( this, - NAME + ": Failed to read phylogenies: " + "\nException: " + e, - "Failed to read phylogenies", - JOptionPane.ERROR_MESSAGE ); - } + Phylogeny[] phys = null; + try { + phys = AptxUtil.readPhylogeniesFromUrl( phys_url, + getConfiguration().isValidatePhyloXmlAgainstSchema(), + getConfiguration().isReplaceUnderscoresInNhParsing(), + getConfiguration().isInternalNumberAreConfidenceForNhParsing(), + getConfiguration().getTaxonomyExtraction(), + getConfiguration().isMidpointReroot() ); + } + catch ( final Exception e ) { + ForesterUtil.printErrorMessage( NAME, e.toString() ); + e.printStackTrace(); + JOptionPane.showMessageDialog( this, + NAME + ": Failed to read phylogenies: " + "\nException: " + e, + "Failed to read phylogenies", + JOptionPane.ERROR_MESSAGE ); + } + if ( phys == null ) { + ForesterUtil.printErrorMessage( NAME, "phylogenies from [" + phys_url + "] are null" ); + JOptionPane.showMessageDialog( this, + NAME + ": phylogenies from [" + phys_url + "] are null", + "Failed to read phylogenies", + JOptionPane.ERROR_MESSAGE ); + return; } - if ( ( phys == null ) || ( phys.length < 1 ) ) { - ForesterUtil.printErrorMessage( NAME, "phylogenies from [" + phys_url + "] are null or empty" ); + else if ( phys.length < 1 ) { + ForesterUtil.printErrorMessage( NAME, "phylogenies from [" + phys_url + "] are empty" ); JOptionPane.showMessageDialog( this, - NAME + ": phylogenies from [" + phys_url + "] are null or empty", + NAME + ": phylogenies from [" + phys_url + "] are empty", "Failed to read phylogenies", JOptionPane.ERROR_MESSAGE ); return; @@ -584,8 +604,6 @@ public class ArchaeopteryxE extends JApplet implements ActionListener { setName( NAME ); getMainPanel().getControlPanel().showWholeAll(); getMainPanel().getControlPanel().showWhole(); - System.gc(); - AptxUtil.printAppletMessage( NAME, "successfully initialized" ); /* GUILHEM_BEG */ getCurrentTreePanel().getControlPanel().getSequenceRelationTypeBox().removeAllItems(); for( final SequenceRelation.SEQUENCE_RELATION_TYPE type : getMainPanel().getCurrentPhylogeny() @@ -601,6 +619,8 @@ public class ArchaeopteryxE extends JApplet implements ActionListener { getCurrentTreePanel().getControlPanel().getSequenceRelationBox().setSelectedItem( default_sequence ); } /* GUILHEM_END */ + System.gc(); + AptxUtil.printAppletMessage( NAME, "successfully initialized" ); setVisible( true ); } diff --git a/forester/java/src/org/forester/io/parsers/PhylogenyParser.java b/forester/java/src/org/forester/io/parsers/PhylogenyParser.java index b0fe08a..df27bc9 100644 --- a/forester/java/src/org/forester/io/parsers/PhylogenyParser.java +++ b/forester/java/src/org/forester/io/parsers/PhylogenyParser.java @@ -41,4 +41,6 @@ public interface PhylogenyParser { public Phylogeny[] parse() throws IOException; public void setSource( Object source ) throws PhylogenyParserException, IOException; + + public String getName(); } diff --git a/forester/java/src/org/forester/io/parsers/nexus/NexusPhylogeniesParser.java b/forester/java/src/org/forester/io/parsers/nexus/NexusPhylogeniesParser.java index 4ba9ca9..fbbce85 100644 --- a/forester/java/src/org/forester/io/parsers/nexus/NexusPhylogeniesParser.java +++ b/forester/java/src/org/forester/io/parsers/nexus/NexusPhylogeniesParser.java @@ -371,4 +371,9 @@ public final class NexusPhylogeniesParser implements IteratingPhylogenyParser, P private final static String removeWhiteSpaceBeforeSemicolon( final String s ) { return s.replaceAll( "\\s+;", ";" ); } + + @Override + public String getName() { + return "Nexus Phylogenies Parser"; + } } diff --git a/forester/java/src/org/forester/io/parsers/nhx/NHXParser.java b/forester/java/src/org/forester/io/parsers/nhx/NHXParser.java index d40e3f8..2e2caf9 100644 --- a/forester/java/src/org/forester/io/parsers/nhx/NHXParser.java +++ b/forester/java/src/org/forester/io/parsers/nhx/NHXParser.java @@ -128,6 +128,10 @@ public final class NHXParser implements PhylogenyParser, IteratingPhylogenyParse for( int i = 0; i < l.size(); ++i ) { p[ i ] = l.get( i ); } + System.out.println( "going to return:" ); + for( Phylogeny phylogeny : p ) { + System.out.println( phylogeny.toString() ); + } return p; } @@ -165,14 +169,15 @@ public final class NHXParser implements PhylogenyParser, IteratingPhylogenyParse _my_source_charary = ( char[] ) _nhx_source; break; case BUFFERED_READER: - if ( _my_source_br != null ) { - try { - _my_source_br.close(); - } - catch ( final IOException e ) { - //do nothing - } - } + //never called: + // if ( _my_source_br != null ) { + // try { + // _my_source_br.close(); + // } + // catch ( final IOException e ) { + // //do nothing + // } + // } _my_source_br = ( BufferedReader ) _nhx_source; break; default: @@ -258,6 +263,7 @@ public final class NHXParser implements PhylogenyParser, IteratingPhylogenyParse private final Phylogeny finishPhylogeny() throws PhylogenyParserException, NHXFormatException, PhyloXmlDataFormatException { + System.out.println( "finishPhylogeny()" + _current_phylogeny ); if ( _current_phylogeny != null ) { parseNHX( _current_anotation != null ? _current_anotation.toString() : "", _current_phylogeny.getRoot(), @@ -325,6 +331,7 @@ public final class NHXParser implements PhylogenyParser, IteratingPhylogenyParse } } } + System.out.print( c ); if ( !_in_single_quote && !_in_double_quote ) { if ( c == ':' ) { _saw_colon = true; @@ -702,6 +709,11 @@ public final class NHXParser implements PhylogenyParser, IteratingPhylogenyParse } } + @Override + public String getName() { + return "NN/NHX Parser"; + } + public static enum TAXONOMY_EXTRACTION { NO, PFAM_STYLE_RELAXED, PFAM_STYLE_STRICT, AGGRESSIVE; } diff --git a/forester/java/src/org/forester/io/parsers/phyloxml/PhyloXmlParser.java b/forester/java/src/org/forester/io/parsers/phyloxml/PhyloXmlParser.java index 8ca07b9..735de4b 100644 --- a/forester/java/src/org/forester/io/parsers/phyloxml/PhyloXmlParser.java +++ b/forester/java/src/org/forester/io/parsers/phyloxml/PhyloXmlParser.java @@ -311,4 +311,9 @@ public class PhyloXmlParser implements PhylogenyParser { _warning_messages.append( "[line: " + e.getLineNumber() + "] " + e.getMessage() ); } } + + @Override + public String getName() { + return "phyloXML Parser"; + } } diff --git a/forester/java/src/org/forester/io/parsers/tol/TolParser.java b/forester/java/src/org/forester/io/parsers/tol/TolParser.java index de48b4c..8299b78 100644 --- a/forester/java/src/org/forester/io/parsers/tol/TolParser.java +++ b/forester/java/src/org/forester/io/parsers/tol/TolParser.java @@ -285,4 +285,9 @@ public class TolParser implements PhylogenyParser { _warning_messages.append( "[line: " + e.getLineNumber() + "] " + e.getMessage() ); } } + + @Override + public String getName() { + return "ToL Parser"; + } } \ No newline at end of file diff --git a/forester/java/src/org/forester/phylogeny/factories/BasicPhylogenyFactory.java b/forester/java/src/org/forester/phylogeny/factories/BasicPhylogenyFactory.java deleted file mode 100644 index d0abe40..0000000 --- a/forester/java/src/org/forester/phylogeny/factories/BasicPhylogenyFactory.java +++ /dev/null @@ -1,48 +0,0 @@ -// $Id: -// FORESTER -- software libraries and applications -// for evolutionary biology research and applications. -// -// Copyright (C) 2008-2009 Christian M. Zmasek -// Copyright (C) 2008-2009 Burnham Institute for Medical Research -// All rights reserved -// -// This library is free software; you can redistribute it and/or -// modify it under the terms of the GNU Lesser General Public -// License as published by the Free Software Foundation; either -// version 2.1 of the License, or (at your option) any later version. -// -// This library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -// Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public -// License along with this library; if not, write to the Free Software -// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA -// -// Contact: phylosoft @ gmail . com -// WWW: https://sites.google.com/site/cmzmasek/home/software/forester - -package org.forester.phylogeny.factories; - -import java.io.IOException; - -import org.forester.phylogeny.Phylogeny; - -/* - * Convinience class for PhylogenyFactories not using parameters. - * - * @author Christian M. Zmasek - */ -public abstract class BasicPhylogenyFactory implements PhylogenyFactory { - - @Override - public Phylogeny create() { - return new Phylogeny(); - } - - @Override - public Phylogeny[] create( final Object source, final Object creator ) throws IOException { - return create( source, creator, null ); - } -} diff --git a/forester/java/src/org/forester/phylogeny/factories/ParserBasedPhylogenyFactory.java b/forester/java/src/org/forester/phylogeny/factories/ParserBasedPhylogenyFactory.java index 14f2f20..66ec2e0 100644 --- a/forester/java/src/org/forester/phylogeny/factories/ParserBasedPhylogenyFactory.java +++ b/forester/java/src/org/forester/phylogeny/factories/ParserBasedPhylogenyFactory.java @@ -27,14 +27,13 @@ package org.forester.phylogeny.factories; import java.io.IOException; -import java.util.List; import org.forester.io.parsers.PhylogenyParser; import org.forester.io.parsers.phyloxml.PhyloXmlParser; import org.forester.phylogeny.Phylogeny; import org.forester.util.ForesterUtil; -public class ParserBasedPhylogenyFactory extends BasicPhylogenyFactory { +public class ParserBasedPhylogenyFactory implements PhylogenyFactory { private final static PhylogenyFactory _instance; static { @@ -56,8 +55,7 @@ public class ParserBasedPhylogenyFactory extends BasicPhylogenyFactory { } @Override - public synchronized Phylogeny[] create( final Object source, final Object parser, final List parameters ) - throws IOException { + public synchronized Phylogeny[] create( final Object source, final Object parser ) throws IOException { if ( !( parser instanceof PhylogenyParser ) ) { throw new IllegalArgumentException( "attempt to use object of type other than PhylogenyParser as creator for ParserBasedPhylogenyFactory" ); } @@ -66,10 +64,8 @@ public class ParserBasedPhylogenyFactory extends BasicPhylogenyFactory { return my_parser.parse(); } - public synchronized Phylogeny[] create( final Object source, - final Object parser, - final String schema_location, - final List parameters ) throws IOException { + public synchronized Phylogeny[] create( final Object source, final Object parser, final String schema_location ) + throws IOException { if ( !( parser instanceof PhylogenyParser ) ) { throw new IllegalArgumentException( "attempt to use object of type other than PhylogenyParser as creator for ParserBasedPhylogenyFactory." ); } diff --git a/forester/java/src/org/forester/phylogeny/factories/PhylogenyFactory.java b/forester/java/src/org/forester/phylogeny/factories/PhylogenyFactory.java index 7b2eac9..d1cc3d1 100644 --- a/forester/java/src/org/forester/phylogeny/factories/PhylogenyFactory.java +++ b/forester/java/src/org/forester/phylogeny/factories/PhylogenyFactory.java @@ -26,7 +26,6 @@ package org.forester.phylogeny.factories; import java.io.IOException; -import java.util.List; import org.forester.phylogeny.Phylogeny; @@ -38,14 +37,6 @@ import org.forester.phylogeny.Phylogeny; public interface PhylogenyFactory { /** - * This must be implemented in such a way that it returns an empty - * Phylogeny. - * - * @return an empty Phylogeny - */ - public Phylogeny create(); - - /** * This must create a Phylogeny from source (e.g. an XML file, an alignment, * pairwise distances) by using creator (e.g. an XML file parser, an * algorithm implementation). @@ -58,20 +49,4 @@ public interface PhylogenyFactory { * @throws IOException */ public Phylogeny[] create( Object source, Object creator ) throws IOException; - - /** - * This must create a Phylogeny from source (e.g. an XML file, an alignment, - * pairwise distances) by using creator (e.g. an XML file parser, an - * algorithm implementation) with parameters listed in parameters. - * - * @param source - * a source to create a Phylogeny from - * @param creator - * a means to create a Phylogeny - * @param parameters - * a List of parameters for Phylogeny creation - * @return a Phylogeny[] based on argument source - * @throws IOException - */ - public Phylogeny[] create( Object source, Object creator, List parameters ) throws IOException; -} // PhylogenyFactory +} diff --git a/forester/java/src/org/forester/test/Test.java b/forester/java/src/org/forester/test/Test.java index cd648f8..e7aa418 100644 --- a/forester/java/src/org/forester/test/Test.java +++ b/forester/java/src/org/forester/test/Test.java @@ -29,6 +29,7 @@ import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; +import java.net.URL; import java.util.ArrayList; import java.util.Date; import java.util.HashSet; @@ -49,6 +50,7 @@ import org.forester.io.parsers.FastaParser; import org.forester.io.parsers.GeneralMsaParser; import org.forester.io.parsers.HmmscanPerDomainTableParser; import org.forester.io.parsers.HmmscanPerDomainTableParser.INDIVIDUAL_SCORE_CUTOFF; +import org.forester.io.parsers.PhylogenyParser; import org.forester.io.parsers.nexus.NexusBinaryStatesMatrixParser; import org.forester.io.parsers.nexus.NexusCharactersParser; import org.forester.io.parsers.nexus.NexusPhylogeniesParser; @@ -401,6 +403,27 @@ public final class Test { } public static void main( final String[] args ) { + try { + String s = "https://sites.google.com/site/cmzmasek/home/software/archaeopteryx/examples/simple/simple_1.nh"; + final URL u = new URL( s ); + final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance(); + final PhylogenyParser parser = ParserUtils.createParserDependingOnUrlContents( u, true ); + final Phylogeny[] phys = factory.create( u.openStream(), parser ); + System.out.println( "results 1:" ); + for( final Phylogeny phy : phys ) { + System.out.println( phy.toString() ); + } + System.out.println( "" ); + final Phylogeny[] phys3 = factory.create( "((a,b),c)", parser ); + System.out.println( "results 3:" ); + for( final Phylogeny phy : phys3 ) { + System.out.println( phy.toString() ); + } + } + catch ( Exception e ) { + e.printStackTrace(); + } + System.exit( 0 ); System.out.println( "[Java version: " + ForesterUtil.JAVA_VERSION + " " + ForesterUtil.JAVA_VENDOR + "]" ); System.out.println( "[OS: " + ForesterUtil.OS_NAME + " " + ForesterUtil.OS_ARCH + " " + ForesterUtil.OS_VERSION + "]" ); @@ -2586,10 +2609,6 @@ public final class Test { private static boolean testBasicTreeMethods() { try { final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance(); - final Phylogeny t1 = factory.create(); - if ( !t1.isEmpty() ) { - return false; - } final Phylogeny t2 = factory.create( "((A:1,B:2)AB:1,(C:3,D:5)CD:3)ABCD:0.5", new NHXParser() )[ 0 ]; if ( t2.getNumberOfExternalNodes() != 4 ) { return false; -- 1.7.10.2