From 64731196184662d30d794bc339a5ecd567cd5e86 Mon Sep 17 00:00:00 2001 From: "cmzmasek@gmail.com" Date: Tue, 18 Feb 2014 22:59:08 +0000 Subject: [PATCH] inprogress --- .../src/org/forester/io/parsers/nhx/NHXParser.java | 24 +++--- forester/java/src/org/forester/test/Test.java | 91 +++++++++++++++----- 2 files changed, 84 insertions(+), 31 deletions(-) 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 b2543a0..daf3360 100644 --- a/forester/java/src/org/forester/io/parsers/nhx/NHXParser.java +++ b/forester/java/src/org/forester/io/parsers/nhx/NHXParser.java @@ -113,18 +113,15 @@ public final class NHXParser implements PhylogenyParser, IteratingPhylogenyParse @Override public final Phylogeny next() throws NHXFormatException, IOException { final Phylogeny phy = _next; - getNext(); - + parseNext(); return phy; } @Override public final Phylogeny[] parse() throws IOException { - final List l = new ArrayList(); int c = 0; while ( hasNext() ) { - l.add( next() ); c++; } @@ -132,7 +129,6 @@ public final class NHXParser implements PhylogenyParser, IteratingPhylogenyParse for( int i = 0; i < l.size(); ++i ) { p[ i ] = l.get( i ); } - reset(); return p; } @@ -185,7 +181,7 @@ public final class NHXParser implements PhylogenyParser, IteratingPhylogenyParse default: throw new RuntimeException( "unknown input type" ); } - getNext(); + parseNext(); } public final void setGuessRootedness( final boolean guess_rootedness ) { @@ -282,7 +278,6 @@ public final class NHXParser implements PhylogenyParser, IteratingPhylogenyParse _current_phylogeny.setRooted( true ); } } - return _current_phylogeny; } return null; @@ -297,7 +292,7 @@ public final class NHXParser implements PhylogenyParser, IteratingPhylogenyParse return _current_phylogeny; } - private final void getNext() throws IOException, NHXFormatException { + private final void parseNext() throws IOException, NHXFormatException { if ( _source == null ) { throw new IOException( "source is not set" ); } @@ -333,7 +328,6 @@ public final class NHXParser implements PhylogenyParser, IteratingPhylogenyParse } } } - if ( !_in_single_quote && !_in_double_quote ) { if ( c == ':' ) { _saw_colon = true; @@ -427,7 +421,6 @@ public final class NHXParser implements PhylogenyParser, IteratingPhylogenyParse throw new PhylogenyParserException( "error in NH (Newick) formatted data: most likely cause: number of open parens does not equal number of close parens" ); } if ( _current_phylogeny != null ) { - _next = finishPhylogeny(); _current_phylogeny = null; _current_anotation = null; @@ -510,7 +503,6 @@ public final class NHXParser implements PhylogenyParser, IteratingPhylogenyParse final PhylogenyNode new_node = new PhylogenyNode(); if ( _clade_level == 0 ) { if ( _current_phylogeny != null ) { - phy = finishPhylogeny(); } _clade_level = 1; @@ -713,6 +705,16 @@ public final class NHXParser implements PhylogenyParser, IteratingPhylogenyParse } } + public final static NHXParser createInstance( final Object nhx_source ) throws NHXFormatException, IOException { + final NHXParser parser = new NHXParser(); + parser.setSource( nhx_source ); + return parser; + } + + public final static Phylogeny[] parse( final Object nhx_source ) throws NHXFormatException, IOException { + return NHXParser.createInstance( nhx_source ).parse(); + } + @Override public String getName() { return "NN/NHX Parser"; diff --git a/forester/java/src/org/forester/test/Test.java b/forester/java/src/org/forester/test/Test.java index 08ae65e..629eb44 100644 --- a/forester/java/src/org/forester/test/Test.java +++ b/forester/java/src/org/forester/test/Test.java @@ -401,43 +401,35 @@ public final class Test { return ( ( Math.abs( a - b ) ) < Test.ZERO_DIFF ); } - public static final boolean testNHXparsingFromURL() { try { - String s = "https://sites.google.com/site/cmzmasek/home/software/archaeopteryx/examples/simple/simple_1.nh"; + final 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 Phylogeny[] phys = factory.create( u.openStream(), new NHXParser() ); - - - if ( phys == null || phys.length != 1 ) { + if ( ( phys == null ) || ( phys.length != 1 ) ) { return false; - } if ( !phys[ 0 ].toNewHampshire().equals( "((a,b),c);" ) ) { - System.out.println(phys[ 0 ].toNewHampshire() ); + System.out.println( phys[ 0 ].toNewHampshire() ); return false; } - final Phylogeny[] phys2 = factory.create( u.openStream(), new NHXParser() ); - if ( phys2 == null || phys2.length != 1 ) { + if ( ( phys2 == null ) || ( phys2.length != 1 ) ) { return false; - } if ( !phys2[ 0 ].toNewHampshire().equals( "((a,b),c);" ) ) { - System.out.println(phys2[ 0 ].toNewHampshire() ); + System.out.println( phys2[ 0 ].toNewHampshire() ); return false; } } - catch ( Exception e ) { + catch ( final Exception e ) { e.printStackTrace(); } return true; } - + public static void main( final String[] args ) { - - System.out.println( "[Java version: " + ForesterUtil.JAVA_VERSION + " " + ForesterUtil.JAVA_VENDOR + "]" ); System.out.println( "[OS: " + ForesterUtil.OS_NAME + " " + ForesterUtil.OS_ARCH + " " + ForesterUtil.OS_VERSION + "]" ); @@ -1238,8 +1230,6 @@ public final class Test { System.out.println( "failed." ); failed++; } - - System.out.print( "NHX parsing from URL: " ); if ( Test.testNHXparsingFromURL() ) { System.out.println( "OK." ); @@ -1249,7 +1239,6 @@ public final class Test { System.out.println( "failed." ); failed++; } - System.out.println(); final Runtime rt = java.lang.Runtime.getRuntime(); final long free_memory = rt.freeMemory() / 1000000; @@ -4802,14 +4791,15 @@ public final class Test { private static boolean testGetLCA2() { try { final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance(); - final Phylogeny p_a = factory.create( "(a)", new NHXParser() )[ 0 ]; + // final Phylogeny p_a = factory.create( "(a)", new NHXParser() )[ 0 ]; + final Phylogeny p_a = NHXParser.parse( "(a)" )[ 0 ]; PhylogenyMethods.preOrderReId( p_a ); final PhylogenyNode p_a_1 = PhylogenyMethods.calculateLCAonTreeWithIdsInPreOrder( p_a.getNode( "a" ), p_a.getNode( "a" ) ); if ( !p_a_1.getName().equals( "a" ) ) { return false; } - final Phylogeny p_b = factory.create( "((a)b)", new NHXParser() )[ 0 ]; + final Phylogeny p_b = NHXParser.parse( "((a)b)" )[ 0 ]; PhylogenyMethods.preOrderReId( p_b ); final PhylogenyNode p_b_1 = PhylogenyMethods.calculateLCAonTreeWithIdsInPreOrder( p_b.getNode( "b" ), p_b.getNode( "a" ) ); @@ -7868,6 +7858,67 @@ public final class Test { if ( p.next() != null ) { return false; } + // + final String p30_str = "(A,B);(C,D)"; + final NHXParser p30 = new NHXParser(); + p30.setSource( p30_str ); + if ( !p30.hasNext() ) { + return false; + } + Phylogeny phy30 = p30.next(); + if ( !phy30.toNewHampshire().equals( "(A,B);" ) ) { + System.out.println( phy30.toNewHampshire() ); + return false; + } + if ( !p30.hasNext() ) { + return false; + } + Phylogeny phy301 = p30.next(); + if ( !phy301.toNewHampshire().equals( "(C,D);" ) ) { + System.out.println( phy301.toNewHampshire() ); + return false; + } + if ( p30.hasNext() ) { + return false; + } + if ( p30.hasNext() ) { + return false; + } + if ( p30.next() != null ) { + return false; + } + if ( p30.next() != null ) { + return false; + } + p30.reset(); + if ( !p30.hasNext() ) { + return false; + } + phy30 = p30.next(); + if ( !phy30.toNewHampshire().equals( "(A,B);" ) ) { + System.out.println( phy30.toNewHampshire() ); + return false; + } + if ( !p30.hasNext() ) { + return false; + } + phy301 = p30.next(); + if ( !phy301.toNewHampshire().equals( "(C,D);" ) ) { + System.out.println( phy301.toNewHampshire() ); + return false; + } + if ( p30.hasNext() ) { + return false; + } + if ( p30.hasNext() ) { + return false; + } + if ( p30.next() != null ) { + return false; + } + if ( p30.next() != null ) { + return false; + } } catch ( final Exception e ) { e.printStackTrace( System.out ); -- 1.7.10.2