From 50d01021d87a55004279b31162c8418a42686dc3 Mon Sep 17 00:00:00 2001 From: "cmzmasek@gmail.com" Date: Fri, 4 Jan 2013 02:28:05 +0000 Subject: [PATCH] inprogress --- .../src/org/forester/io/parsers/nhx/NHXParser.java | 12 - .../org/forester/io/parsers/nhx/NHXParser2.java | 413 ++++++++++---------- forester/java/src/org/forester/test/Test.java | 207 +++++++--- 3 files changed, 363 insertions(+), 269 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 5a47ab0..4711c8b 100644 --- a/forester/java/src/org/forester/io/parsers/nhx/NHXParser.java +++ b/forester/java/src/org/forester/io/parsers/nhx/NHXParser.java @@ -70,7 +70,6 @@ public final class NHXParser implements PhylogenyParser { final static private byte BUFFERED_READER = 3; final static private byte STRING_BUILDER = 4; private boolean _guess_rootedness; - private boolean _has_next; private boolean _ignore_quotes; private byte _input_type; private int _source_length; @@ -96,10 +95,6 @@ public final class NHXParser implements PhylogenyParser { return _taxonomy_extraction; } - public boolean hasNext() { - return _has_next; - } - /** * Parses the source set with setSource( final Object nhx_source ). Returns * the Phylogenies found in the source as Phylogeny[]. @@ -118,7 +113,6 @@ public final class NHXParser implements PhylogenyParser { */ @Override public Phylogeny[] parse() throws IOException, NHXFormatException { - setHasNext( false ); boolean in_comment = false; boolean saw_colon = false; boolean saw_open_bracket = false; @@ -372,7 +366,6 @@ public final class NHXParser implements PhylogenyParser { + " StringBuffer, char[], File," + " or InputStream " + " [attempt to parse object of " + nhx_source.getClass() + "]." ); } - setHasNext( true ); } public void setTaxonomyExtraction( final NHXParser.TAXONOMY_EXTRACTION taxonomy_extraction ) { @@ -490,7 +483,6 @@ public final class NHXParser implements PhylogenyParser { setReplaceUnderscores( REPLACE_UNDERSCORES_DEFAULT ); setGuessRootedness( GUESS_ROOTEDNESS_DEFAULT ); setIgnoreQuotes( IGNORE_QUOTES_DEFAULT ); - setHasNext( false ); } private boolean isGuessRootedness() { @@ -615,10 +607,6 @@ public final class NHXParser implements PhylogenyParser { _current_phylogeny = current_phylogeny; } - private void setHasNext( final boolean has_next ) { - _has_next = has_next; - } - private void setInputType( final byte input_type ) { _input_type = input_type; } diff --git a/forester/java/src/org/forester/io/parsers/nhx/NHXParser2.java b/forester/java/src/org/forester/io/parsers/nhx/NHXParser2.java index ae04b6d..b3b764a 100644 --- a/forester/java/src/org/forester/io/parsers/nhx/NHXParser2.java +++ b/forester/java/src/org/forester/io/parsers/nhx/NHXParser2.java @@ -32,6 +32,8 @@ import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.List; import java.util.StringTokenizer; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -69,7 +71,6 @@ public final class NHXParser2 implements PhylogenyParser { final static private byte BUFFERED_READER = 3; final static private byte STRING_BUILDER = 4; private boolean _guess_rootedness; - private boolean _has_next; private boolean _ignore_quotes; private byte _input_type; private int _source_length; @@ -97,153 +98,77 @@ public final class NHXParser2 implements PhylogenyParser { char[] _my_source_charary = null; BufferedReader _my_source_br = null; int _i; + private Phylogeny _next; public NHXParser2() { init(); } - public Phylogeny getNext() throws IOException, NHXFormatException { - while ( true ) { - char c = '\b'; - if ( getInputType() == BUFFERED_READER ) { - final int ci = _my_source_br.read(); - if ( ci >= 0 ) { - c = ( char ) ci; - } - else { - break; - } - } - else { - if ( _i >= getSourceLength() ) { - break; - } - else { - switch ( getInputType() ) { - case STRING: - c = _my_source_str.charAt( _i ); - break; - case STRING_BUFFER: - c = _my_source_sbuff.charAt( _i ); - break; - case STRING_BUILDER: - c = _my_source_sbuil.charAt( _i ); - break; - case CHAR_ARRAY: - c = _my_source_charary[ _i ]; - break; - } - } - } - if ( !_in_single_quote && !_in_double_quote ) { - if ( c == ':' ) { - _saw_colon = true; - } - else if ( !( ( c < 33 ) || ( c > 126 ) ) && _saw_colon - && ( ( c != '[' ) && ( c != '.' ) && ( ( c < 48 ) || ( c > 57 ) ) ) ) { - _saw_colon = false; - } - if ( _in_open_bracket && ( c == ']' ) ) { - _in_open_bracket = false; - } - } - // \n\t is always ignored, - // as is " (34) and ' (39) (space is 32): - if ( ( isIgnoreQuotes() && ( ( c < 33 ) || ( c > 126 ) || ( c == 34 ) || ( c == 39 ) || ( ( getCladeLevel() == 0 ) && ( c == ';' ) ) ) ) - || ( !isIgnoreQuotes() && ( ( c < 32 ) || ( c > 126 ) || ( ( getCladeLevel() == 0 ) && ( c == ';' ) ) ) ) ) { - // Do nothing. - } - else if ( ( c == 32 ) && ( !_in_single_quote && !_in_double_quote ) ) { - // Do nothing. - } - else if ( _in_comment ) { - if ( c == ']' ) { - _in_comment = false; - } - } - else if ( _in_double_quote ) { - if ( c == '"' ) { - _in_double_quote = false; - } - else { - getCurrentAnotation().append( c ); - } - } - else if ( c == '"' ) { - _in_double_quote = true; - } - else if ( _in_single_quote ) { - if ( c == 39 ) { - _in_single_quote = false; - } - else { - getCurrentAnotation().append( c ); - } - } - else if ( c == 39 ) { - _in_single_quote = true; - } - else if ( c == '[' ) { - _saw_open_bracket = true; - _in_open_bracket = true; - } - else if ( _saw_open_bracket ) { - if ( c != ']' ) { - // everything not starting with "[&" is considered a comment - // unless ":digits and/or . [bootstrap]": - if ( c == '&' ) { - getCurrentAnotation().append( "[&" ); - } - else if ( _saw_colon ) { - getCurrentAnotation().append( "[" + c ); - } - else { - _in_comment = true; - } - } - // comment consisting just of "[]": - _saw_open_bracket = false; - } - else if ( ( c == '(' ) && !_in_open_bracket ) { - Phylogeny phy = processOpenParen2(); - if ( phy != null ) { - ++_i; - return phy; - } - } - else if ( ( c == ')' ) && !_in_open_bracket ) { - processCloseParen(); - } - else if ( ( c == ',' ) && !_in_open_bracket ) { - processComma(); - } - else { - getCurrentAnotation().append( c ); - } - ++_i; - } // while ( true ) - System.out.println( "done with loop" ); - if ( getCladeLevel() != 0 ) { - throw new PhylogenyParserException( "error in NH (Newick)/NHX formatted data: most likely cause: number of open parens does not equal number of close parens" ); - } - if ( getCurrentPhylogeny() != null ) { - return finishPhylogeny2(); - } - else if ( getCurrentAnotation().length() > 0 ) { - System.out.println( "1node=" + getCurrentAnotation() ); - return finishSingleNodePhylogeny2(); - } - else { - return null; - } - } // parse() - public TAXONOMY_EXTRACTION getTaxonomyExtraction() { return _taxonomy_extraction; } public boolean hasNext() { - return _has_next; + return _next != null; + } + + public Phylogeny next() throws NHXFormatException, IOException { + final Phylogeny phy = _next; + getNext(); + return phy; + } + + @Override + public Phylogeny[] parse() throws IOException { + reset(); + List l = new ArrayList(); + while ( hasNext() ) { + l.add( next() ); + } + final Phylogeny[] p = new Phylogeny[ l.size() ]; + for( int i = 0; i < l.size(); ++i ) { + p[ i ] = l.get( i ); + } + return p; + } + + public void reset() throws NHXFormatException, IOException { + _i = 0; + _in_comment = false; + _saw_colon = false; + _saw_open_bracket = false; + _in_open_bracket = false; + _in_double_quote = false; + _in_single_quote = false; + setCladeLevel( 0 ); + newCurrentAnotation(); + setCurrentPhylogeny( null ); + setCurrentNode( null ); + _my_source_str = null; + _my_source_sbuff = null; + _my_source_sbuil = null; + _my_source_charary = null; + _my_source_br = null; + switch ( getInputType() ) { + case STRING: + _my_source_str = ( String ) getNhxSource(); + break; + case STRING_BUFFER: + _my_source_sbuff = ( StringBuffer ) getNhxSource(); + break; + case STRING_BUILDER: + _my_source_sbuil = ( StringBuilder ) getNhxSource(); + break; + case CHAR_ARRAY: + _my_source_charary = ( char[] ) getNhxSource(); + break; + case BUFFERED_READER: + _my_source_br = ( BufferedReader ) getNhxSource(); + break; + default: + throw new RuntimeException( "unknown input type" ); + } + getNext(); } public void setGuessRootedness( final boolean guess_rootedness ) { @@ -329,10 +254,9 @@ public final class NHXParser2 implements PhylogenyParser { } else { throw new IllegalArgumentException( getClass() + " can only parse objects of type String," - + " StringBuffer, char[], File," + " or InputStream " + " [attempt to parse object of " - + nhx_source.getClass() + "]." ); + + " StringBuffer, StringBuilder, char[], File," + " or InputStream " + + " [attempt to parse object of " + nhx_source.getClass() + "]." ); } - setHasNext( true ); reset(); } @@ -340,45 +264,6 @@ public final class NHXParser2 implements PhylogenyParser { _taxonomy_extraction = taxonomy_extraction; } - public void reset() { - setHasNext( false ); - _i = 0; - _in_comment = false; - _saw_colon = false; - _saw_open_bracket = false; - _in_open_bracket = false; - _in_double_quote = false; - _in_single_quote = false; - setCladeLevel( 0 ); - newCurrentAnotation(); - setCurrentPhylogeny( null ); - setCurrentNode( null ); - _my_source_str = null; - _my_source_sbuff = null; - _my_source_sbuil = null; - _my_source_charary = null; - _my_source_br = null; - switch ( getInputType() ) { - case STRING: - _my_source_str = ( String ) getNhxSource(); - break; - case STRING_BUFFER: - _my_source_sbuff = ( StringBuffer ) getNhxSource(); - break; - case STRING_BUILDER: - _my_source_sbuil = ( StringBuilder ) getNhxSource(); - break; - case CHAR_ARRAY: - _my_source_charary = ( char[] ) getNhxSource(); - break; - case BUFFERED_READER: - _my_source_br = ( BufferedReader ) getNhxSource(); - break; - default: - throw new RuntimeException( "unknown input type" ); - } - } - /** * Decreases the clade level by one. * @@ -397,11 +282,14 @@ public final class NHXParser2 implements PhylogenyParser { //setCladeLevel( 0 ); if ( getCurrentPhylogeny() != null ) { System.out.println( "cp=" + getCurrentPhylogeny() ); - System.out.println( "ca=" + getCurrentAnotation().toString() ); - parseNHX( getCurrentAnotation().toString(), - getCurrentPhylogeny().getRoot(), - getTaxonomyExtraction(), - isReplaceUnderscores() ); + if ( getCurrentAnotation() != null ) { + System.out.println( "ca=" + getCurrentAnotation().toString() ); + } + else { + System.out.println( "ca=null" ); + } + parseNHX( getCurrentAnotation() != null ? getCurrentAnotation().toString() : "", getCurrentPhylogeny() + .getRoot(), getTaxonomyExtraction(), isReplaceUnderscores() ); if ( GUESS_IF_SUPPORT_VALUES ) { if ( isBranchLengthsLikeBootstrapValues( getCurrentPhylogeny() ) ) { moveBranchLengthsToConfidenceValues( getCurrentPhylogeny() ); @@ -449,6 +337,150 @@ public final class NHXParser2 implements PhylogenyParser { return _input_type; } + private void getNext() throws IOException, NHXFormatException { + while ( true ) { + char c = '\b'; + if ( getInputType() == BUFFERED_READER ) { + final int ci = _my_source_br.read(); + if ( ci >= 0 ) { + c = ( char ) ci; + } + else { + break; + } + } + else { + if ( _i >= getSourceLength() ) { + break; + } + else { + switch ( getInputType() ) { + case STRING: + c = _my_source_str.charAt( _i ); + break; + case STRING_BUFFER: + c = _my_source_sbuff.charAt( _i ); + break; + case STRING_BUILDER: + c = _my_source_sbuil.charAt( _i ); + break; + case CHAR_ARRAY: + c = _my_source_charary[ _i ]; + break; + } + } + } + if ( !_in_single_quote && !_in_double_quote ) { + if ( c == ':' ) { + _saw_colon = true; + } + else if ( !( ( c < 33 ) || ( c > 126 ) ) && _saw_colon + && ( ( c != '[' ) && ( c != '.' ) && ( ( c < 48 ) || ( c > 57 ) ) ) ) { + _saw_colon = false; + } + if ( _in_open_bracket && ( c == ']' ) ) { + _in_open_bracket = false; + } + } + // \n\t is always ignored, + // as is " (34) and ' (39) (space is 32): + if ( ( isIgnoreQuotes() && ( ( c < 33 ) || ( c > 126 ) || ( c == 34 ) || ( c == 39 ) || ( ( getCladeLevel() == 0 ) && ( c == ';' ) ) ) ) + || ( !isIgnoreQuotes() && ( ( c < 32 ) || ( c > 126 ) || ( ( getCladeLevel() == 0 ) && ( c == ';' ) ) ) ) ) { + // Do nothing. + } + else if ( ( c == 32 ) && ( !_in_single_quote && !_in_double_quote ) ) { + // Do nothing. + } + else if ( _in_comment ) { + if ( c == ']' ) { + _in_comment = false; + } + } + else if ( _in_double_quote ) { + if ( c == '"' ) { + _in_double_quote = false; + } + else { + getCurrentAnotation().append( c ); + } + } + else if ( c == '"' ) { + _in_double_quote = true; + } + else if ( _in_single_quote ) { + if ( c == 39 ) { + _in_single_quote = false; + } + else { + getCurrentAnotation().append( c ); + } + } + else if ( c == 39 ) { + _in_single_quote = true; + } + else if ( c == '[' ) { + _saw_open_bracket = true; + _in_open_bracket = true; + } + else if ( _saw_open_bracket ) { + if ( c != ']' ) { + // everything not starting with "[&" is considered a comment + // unless ":digits and/or . [bootstrap]": + if ( c == '&' ) { + getCurrentAnotation().append( "[&" ); + } + else if ( _saw_colon ) { + getCurrentAnotation().append( "[" + c ); + } + else { + _in_comment = true; + } + } + // comment consisting just of "[]": + _saw_open_bracket = false; + } + else if ( ( c == '(' ) && !_in_open_bracket ) { + final Phylogeny phy = processOpenParen2(); + if ( phy != null ) { + ++_i; + // return phy; + _next = phy; + return; + } + } + else if ( ( c == ')' ) && !_in_open_bracket ) { + processCloseParen(); + } + else if ( ( c == ',' ) && !_in_open_bracket ) { + processComma(); + } + else { + getCurrentAnotation().append( c ); + } + ++_i; + } // while ( true ) + System.out.println( "done with loop" ); + if ( getCladeLevel() != 0 ) { + throw new PhylogenyParserException( "error in NH (Newick) formatted data: most likely cause: number of open parens does not equal number of close parens" ); + } + if ( getCurrentPhylogeny() != null ) { + System.out.println( "current=" + getCurrentPhylogeny() ); + _next = finishPhylogeny2(); + setCurrentPhylogeny( null ); + //return finishPhylogeny2(); + } + else if ( ( getCurrentAnotation() != null ) && ( getCurrentAnotation().length() > 0 ) ) { + System.out.println( "1node=" + getCurrentAnotation() ); + _next = finishSingleNodePhylogeny2(); + setCurrentAnotation( null ); + //return finishSingleNodePhylogeny2(); + } + else { + _next = null; + //return null; + } + } + private Object getNhxSource() { return _nhx_source; } @@ -466,7 +498,6 @@ public final class NHXParser2 implements PhylogenyParser { setReplaceUnderscores( REPLACE_UNDERSCORES_DEFAULT ); setGuessRootedness( GUESS_ROOTEDNESS_DEFAULT ); setIgnoreQuotes( IGNORE_QUOTES_DEFAULT ); - setHasNext( false ); } private boolean isGuessRootedness() { @@ -587,10 +618,6 @@ public final class NHXParser2 implements PhylogenyParser { _current_phylogeny = current_phylogeny; } - private void setHasNext( final boolean has_next ) { - _has_next = has_next; - } - private void setInputType( final byte input_type ) { _input_type = input_type; } @@ -823,10 +850,4 @@ public final class NHXParser2 implements PhylogenyParser { final int blu = ForesterUtil.limitRangeForColor( Integer.parseInt( st.nextToken() ) ); return new Color( red, green, blu ); } - - @Override - public Phylogeny[] parse() throws IOException { - // TODO Auto-generated method stub - return null; - } } diff --git a/forester/java/src/org/forester/test/Test.java b/forester/java/src/org/forester/test/Test.java index 683955e..dff58d6 100644 --- a/forester/java/src/org/forester/test/Test.java +++ b/forester/java/src/org/forester/test/Test.java @@ -280,7 +280,6 @@ public final class Test { System.out.println( "failed." ); failed++; } - System.exit( 0 ); System.out.print( "Nexus characters parsing: " ); if ( Test.testNexusCharactersParsing() ) { System.out.println( "OK." ); @@ -4569,7 +4568,7 @@ public final class Test { private static boolean testNHParsing() { try { final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance(); - final Phylogeny p1 = factory.create( "(A,B1)", new NHXParser() )[ 0 ]; + final Phylogeny p1 = factory.create( "(A,B1)", new NHXParser2() )[ 0 ]; if ( !p1.toNewHampshireX().equals( "(A,B1)" ) ) { return false; } @@ -4592,20 +4591,20 @@ public final class Test { if ( !p1b.toNewHampshire().equals( "(';A;',';B;1;');" ) ) { return false; } - final Phylogeny p2 = factory.create( new StringBuffer( "(A,B2)" ), new NHXParser() )[ 0 ]; - final Phylogeny p3 = factory.create( new char[] { '(', 'A', ',', 'B', '3', ')' }, new NHXParser() )[ 0 ]; - final Phylogeny p4 = factory.create( "(A,B4);", new NHXParser() )[ 0 ]; - final Phylogeny p5 = factory.create( new StringBuffer( "(A,B5);" ), new NHXParser() )[ 0 ]; - final Phylogeny[] p7 = factory.create( "(A,B7);(C,D7)", new NHXParser() ); - final Phylogeny[] p8 = factory.create( "(A,B8) (C,D8)", new NHXParser() ); - final Phylogeny[] p9 = factory.create( "(A,B9)\n(C,D9)", new NHXParser() ); - final Phylogeny[] p10 = factory.create( "(A,B10);(C,D10);", new NHXParser() ); - final Phylogeny[] p11 = factory.create( "(A,B11);(C,D11) (E,F11)\t(G,H11)", new NHXParser() ); - final Phylogeny[] p12 = factory.create( "(A,B12) (C,D12) (E,F12) (G,H12)", new NHXParser() ); + final Phylogeny p2 = factory.create( new StringBuffer( "(A,B2)" ), new NHXParser2() )[ 0 ]; + final Phylogeny p3 = factory.create( new char[] { '(', 'A', ',', 'B', '3', ')' }, new NHXParser2() )[ 0 ]; + final Phylogeny p4 = factory.create( "(A,B4);", new NHXParser2() )[ 0 ]; + final Phylogeny p5 = factory.create( new StringBuffer( "(A,B5);" ), new NHXParser2() )[ 0 ]; + final Phylogeny[] p7 = factory.create( "(A,B7);(C,D7)", new NHXParser2() ); + final Phylogeny[] p8 = factory.create( "(A,B8) (C,D8)", new NHXParser2() ); + final Phylogeny[] p9 = factory.create( "(A,B9)\n(C,D9)", new NHXParser2() ); + final Phylogeny[] p10 = factory.create( "(A,B10);(C,D10);", new NHXParser2() ); + final Phylogeny[] p11 = factory.create( "(A,B11);(C,D11) (E,F11)\t(G,H11)", new NHXParser2() ); + final Phylogeny[] p12 = factory.create( "(A,B12) (C,D12) (E,F12) (G,H12)", new NHXParser2() ); final Phylogeny[] p13 = factory.create( " ; (;A; , ; B ; 1 3 ; \n)\t ( \n ;" + " C ; ,; D;13;);;;;;;(;E;,;F;13 ;) ; " + "; ; ( \t\n\r\b; G ;, ;H ;1 3; ) ; ; ;", - new NHXParser() ); + new NHXParser2() ); if ( !p13[ 0 ].toNewHampshireX().equals( "(';A;',';B;13;')" ) ) { return false; } @@ -4618,56 +4617,56 @@ public final class Test { if ( !p13[ 3 ].toNewHampshireX().equals( "(';G;',';H;13;')" ) ) { return false; } - final Phylogeny[] p14 = factory.create( "(A,B14)ab", new NHXParser() ); - final Phylogeny[] p15 = factory.create( "(A,B15)ab;", new NHXParser() ); + final Phylogeny[] p14 = factory.create( "(A,B14)ab", new NHXParser2() ); + final Phylogeny[] p15 = factory.create( "(A,B15)ab;", new NHXParser2() ); final String p16_S = "((A,B),C)"; - final Phylogeny[] p16 = factory.create( p16_S, new NHXParser() ); + final Phylogeny[] p16 = factory.create( p16_S, new NHXParser2() ); if ( !p16[ 0 ].toNewHampshireX().equals( p16_S ) ) { return false; } final String p17_S = "(C,(A,B))"; - final Phylogeny[] p17 = factory.create( p17_S, new NHXParser() ); + final Phylogeny[] p17 = factory.create( p17_S, new NHXParser2() ); if ( !p17[ 0 ].toNewHampshireX().equals( p17_S ) ) { return false; } final String p18_S = "((A,B),(C,D))"; - final Phylogeny[] p18 = factory.create( p18_S, new NHXParser() ); + final Phylogeny[] p18 = factory.create( p18_S, new NHXParser2() ); if ( !p18[ 0 ].toNewHampshireX().equals( p18_S ) ) { return false; } final String p19_S = "(((A,B),C),D)"; - final Phylogeny[] p19 = factory.create( p19_S, new NHXParser() ); + final Phylogeny[] p19 = factory.create( p19_S, new NHXParser2() ); if ( !p19[ 0 ].toNewHampshireX().equals( p19_S ) ) { return false; } final String p20_S = "(A,(B,(C,D)))"; - final Phylogeny[] p20 = factory.create( p20_S, new NHXParser() ); + final Phylogeny[] p20 = factory.create( p20_S, new NHXParser2() ); if ( !p20[ 0 ].toNewHampshireX().equals( p20_S ) ) { return false; } final String p21_S = "(A,(B,(C,(D,E))))"; - final Phylogeny[] p21 = factory.create( p21_S, new NHXParser() ); + final Phylogeny[] p21 = factory.create( p21_S, new NHXParser2() ); if ( !p21[ 0 ].toNewHampshireX().equals( p21_S ) ) { return false; } final String p22_S = "((((A,B),C),D),E)"; - final Phylogeny[] p22 = factory.create( p22_S, new NHXParser() ); + final Phylogeny[] p22 = factory.create( p22_S, new NHXParser2() ); if ( !p22[ 0 ].toNewHampshireX().equals( p22_S ) ) { return false; } final String p23_S = "(A,(B,(C,(D,E)de)cde)bcde)abcde"; - final Phylogeny[] p23 = factory.create( p23_S, new NHXParser() ); + final Phylogeny[] p23 = factory.create( p23_S, new NHXParser2() ); if ( !p23[ 0 ].toNewHampshireX().equals( p23_S ) ) { return false; } final String p24_S = "((((A,B)ab,C)abc,D)abcd,E)abcde"; - final Phylogeny[] p24 = factory.create( p24_S, new NHXParser() ); + final Phylogeny[] p24 = factory.create( p24_S, new NHXParser2() ); if ( !p24[ 0 ].toNewHampshireX().equals( p24_S ) ) { return false; } final String p241_S1 = "(A,(B,(C,(D,E)de)cde)bcde)abcde"; final String p241_S2 = "((((A,B)ab,C)abc,D)abcd,E)abcde"; - final Phylogeny[] p241 = factory.create( p241_S1 + p241_S2, new NHXParser() ); + final Phylogeny[] p241 = factory.create( p241_S1 + p241_S2, new NHXParser2() ); if ( !p241[ 0 ].toNewHampshireX().equals( p241_S1 ) ) { return false; } @@ -4682,19 +4681,21 @@ public final class Test { + "E)abcde)abcd,E)abcde,((((A,B)ab,C)abc,D)abcd,E)abcde)" + "ab,C)abc,((((A,B)ab,C)abc,D)abcd,E)abcde)abcd,E)abcde" + ")ab,C)abc,D)abcd,E)abcde)ab,C)abc,((((A,B)ab,C)abc,D)" + "abcd,E)abcde)abcd,E)abcde"; - final Phylogeny[] p25 = factory.create( p25_S, new NHXParser() ); + final Phylogeny[] p25 = factory.create( p25_S, new NHXParser2() ); if ( !p25[ 0 ].toNewHampshireX().equals( p25_S ) ) { return false; } final String p26_S = "(A,B)ab"; - final Phylogeny[] p26 = factory.create( p26_S, new NHXParser() ); + final Phylogeny[] p26 = factory.create( p26_S, new NHXParser2() ); if ( !p26[ 0 ].toNewHampshireX().equals( p26_S ) ) { return false; } final String p27_S = "((((A,B)ab,C)abc,D)abcd,E)abcde"; final Phylogeny[] p27 = factory.create( new File( Test.PATH_TO_TEST_DATA + "phylogeny27.nhx" ), - new NHXParser() ); + new NHXParser2() ); if ( !p27[ 0 ].toNewHampshireX().equals( p27_S ) ) { + System.out.println( p27[ 0 ].toNewHampshireX() ); + System.exit( -1 ); return false; } final String p28_S1 = "((((A,B)ab,C)abc,D)abcd,E)abcde"; @@ -4865,19 +4866,24 @@ public final class Test { String p0_str = "(A,B);"; NHXParser2 p = new NHXParser2(); p.setSource( p0_str ); - Phylogeny p0 = p.getNext(); + if ( !p.hasNext() ) { + return false; + } + Phylogeny p0 = p.next(); if ( !p0.toNewHampshire().equals( p0_str ) ) { System.out.println( p0.toNewHampshire() ); return false; } - //if ( p.getNext() != null ) { - // return false; - //} + if ( p.hasNext() ) { + return false; + } + if ( p.next() != null ) { + return false; + } // String p00_str = "(A,B)root;"; - //p = new NHXParser2(); p.setSource( p00_str ); - Phylogeny p00 = p.getNext(); + Phylogeny p00 = p.next(); if ( !p00.toNewHampshire().equals( p00_str ) ) { System.out.println( p00.toNewHampshire() ); return false; @@ -4885,7 +4891,7 @@ public final class Test { // String p000_str = "A;"; p.setSource( p000_str ); - Phylogeny p000 = p.getNext(); + Phylogeny p000 = p.next(); if ( !p000.toNewHampshire().equals( p000_str ) ) { System.out.println( p000.toNewHampshire() ); return false; @@ -4893,76 +4899,156 @@ public final class Test { // String p0000_str = "A"; p.setSource( p0000_str ); - Phylogeny p0000 = p.getNext(); + Phylogeny p0000 = p.next(); if ( !p0000.toNewHampshire().equals( "A;" ) ) { System.out.println( p0000.toNewHampshire() ); return false; } // p.setSource( "(A)" ); - Phylogeny p00000 = p.getNext(); + Phylogeny p00000 = p.next(); if ( !p00000.toNewHampshire().equals( "(A);" ) ) { System.out.println( p00000.toNewHampshire() ); return false; } // - // p.setSource( " " ); - // Phylogeny p000000 = p.getNext(); - // if ( !p000000.toNewHampshire().equals( "(A);" ) ) { - // System.out.println( p000000.toNewHampshire() ); - // return false; - // } - // String p1_str = "(A,B)(C,D)(E,F)(G,H)"; p.setSource( p1_str ); - Phylogeny p1_0 = p.getNext(); + if ( !p.hasNext() ) { + return false; + } + Phylogeny p1_0 = p.next(); if ( !p1_0.toNewHampshire().equals( "(A,B);" ) ) { System.out.println( p1_0.toNewHampshire() ); return false; } - Phylogeny p1_1 = p.getNext(); + if ( !p.hasNext() ) { + return false; + } + Phylogeny p1_1 = p.next(); if ( !p1_1.toNewHampshire().equals( "(C,D);" ) ) { System.out.println( "(C,D) != " + p1_1.toNewHampshire() ); return false; } - Phylogeny p1_2 = p.getNext(); + if ( !p.hasNext() ) { + return false; + } + Phylogeny p1_2 = p.next(); if ( !p1_2.toNewHampshire().equals( "(E,F);" ) ) { System.out.println( "(E,F) != " + p1_2.toNewHampshire() ); return false; } - Phylogeny p1_3 = p.getNext(); + if ( !p.hasNext() ) { + return false; + } + Phylogeny p1_3 = p.next(); if ( !p1_3.toNewHampshire().equals( "(G,H);" ) ) { System.out.println( "(G,H) != " + p1_3.toNewHampshire() ); return false; } + if ( p.hasNext() ) { + return false; + } + if ( p.next() != null ) { + return false; + } // String p2_str = "((1,2,3),B);(C,D) (E,F)root;(G,H); ;(X)"; p.setSource( p2_str ); - Phylogeny p2_0 = p.getNext(); + if ( !p.hasNext() ) { + return false; + } + Phylogeny p2_0 = p.next(); if ( !p2_0.toNewHampshire().equals( "((1,2,3),B);" ) ) { System.out.println( p2_0.toNewHampshire() ); return false; } - Phylogeny p2_1 = p.getNext(); + if ( !p.hasNext() ) { + return false; + } + Phylogeny p2_1 = p.next(); if ( !p2_1.toNewHampshire().equals( "(C,D);" ) ) { System.out.println( "(C,D) != " + p2_1.toNewHampshire() ); return false; } - Phylogeny p2_2 = p.getNext(); + if ( !p.hasNext() ) { + return false; + } + Phylogeny p2_2 = p.next(); if ( !p2_2.toNewHampshire().equals( "(E,F)root;" ) ) { System.out.println( "(E,F)root != " + p2_2.toNewHampshire() ); return false; } - Phylogeny p2_3 = p.getNext(); + if ( !p.hasNext() ) { + return false; + } + Phylogeny p2_3 = p.next(); if ( !p2_3.toNewHampshire().equals( "(G,H);" ) ) { System.out.println( "(G,H) != " + p2_3.toNewHampshire() ); return false; } - Phylogeny p2_4 = p.getNext(); + if ( !p.hasNext() ) { + return false; + } + Phylogeny p2_4 = p.next(); if ( !p2_4.toNewHampshire().equals( "(X);" ) ) { System.out.println( "(X) != " + p2_4.toNewHampshire() ); return false; } + if ( p.hasNext() ) { + return false; + } + if ( p.next() != null ) { + return false; + } + //// + p.reset(); + if ( !p.hasNext() ) { + return false; + } + p2_0 = p.next(); + if ( !p2_0.toNewHampshire().equals( "((1,2,3),B);" ) ) { + System.out.println( p2_0.toNewHampshire() ); + return false; + } + if ( !p.hasNext() ) { + return false; + } + p2_1 = p.next(); + if ( !p2_1.toNewHampshire().equals( "(C,D);" ) ) { + System.out.println( "(C,D) != " + p2_1.toNewHampshire() ); + return false; + } + if ( !p.hasNext() ) { + return false; + } + p2_2 = p.next(); + if ( !p2_2.toNewHampshire().equals( "(E,F)root;" ) ) { + System.out.println( "(E,F)root != " + p2_2.toNewHampshire() ); + return false; + } + if ( !p.hasNext() ) { + return false; + } + p2_3 = p.next(); + if ( !p2_3.toNewHampshire().equals( "(G,H);" ) ) { + System.out.println( "(G,H) != " + p2_3.toNewHampshire() ); + return false; + } + if ( !p.hasNext() ) { + return false; + } + p2_4 = p.next(); + if ( !p2_4.toNewHampshire().equals( "(X);" ) ) { + System.out.println( "(X) != " + p2_4.toNewHampshire() ); + return false; + } + if ( p.hasNext() ) { + return false; + } + if ( p.next() != null ) { + return false; + } } catch ( final Exception e ) { e.printStackTrace( System.out ); @@ -4978,9 +5064,9 @@ public final class Test { final PhylogenyNode n3 = PhylogenyNode.createInstanceFromNhxString( "n3" ); final PhylogenyNode n4 = PhylogenyNode.createInstanceFromNhxString( "n4:0.01" ); final PhylogenyNode n5 = PhylogenyNode - .createInstanceFromNhxString( "n5:0.1[&&NHX:S=Ecoli:E=1.1.1.1:D=Y:Co=Y:B=56:T=1:W=2:C=10.20.30:XN=S=tag1=value1=unit1]" ); + .createInstanceFromNhxString( "n5:0.1[&&NHX:S=Ecoli:E=1.1.1.1:D=Y:Co=Y:B=56:T=1:W=2:C=10.20.30]" ); final PhylogenyNode n6 = PhylogenyNode - .createInstanceFromNhxString( "n6:0.000001[&&NHX:S=Ecoli:E=1.1.1.1:D=N:Co=N:B=100:T=1:W=2:C=0.0.0:XN=B=bool_tag=T]" ); + .createInstanceFromNhxString( "n6:0.000001[&&NHX:S=Ecoli:E=1.1.1.1:D=N:Co=N:B=100:T=1:W=2:C=0.0.0]" ); if ( !n1.toNewHampshireX().equals( "" ) ) { return false; } @@ -4993,11 +5079,10 @@ public final class Test { if ( !n4.toNewHampshireX().equals( "n4:0.01" ) ) { return false; } - if ( !n5.toNewHampshireX() - .equals( "n5:0.1[&&NHX:T=1:S=Ecoli:D=Y:XN=S=tag1=value1=unit1:B=56:W=2.0:C=10.20.30]" ) ) { + if ( !n5.toNewHampshireX().equals( "n5:0.1[&&NHX:T=1:S=Ecoli:D=Y:B=56:W=2.0:C=10.20.30]" ) ) { return false; } - if ( !n6.toNewHampshireX().equals( "n6:1.0E-6[&&NHX:T=1:S=Ecoli:D=N:XN=B=bool_tag=T:B=100:W=2.0:C=0.0.0]" ) ) { + if ( !n6.toNewHampshireX().equals( "n6:1.0E-6[&&NHX:T=1:S=Ecoli:D=N:B=100:W=2.0:C=0.0.0]" ) ) { return false; } } @@ -5402,7 +5487,7 @@ public final class Test { return false; } final PhylogenyNode n00 = PhylogenyNode - .createInstanceFromNhxString( "n7:0.000001[&&NHX:GN=gene_name:AC=accession123:ID=node_identifier:S=Ecoli:D=N:Co=N:B=100:T=1:On=100:SOn=100:SNn=100:W=2:C=0.0.0:XN=U=url_tag=www.yahoo.com]" ); + .createInstanceFromNhxString( "n7:0.000001[&&NHX:GN=gene_name:AC=accession123:S=Ecoli:D=N:Co=N:B=100:T=1:W=2:C=0.0.0]" ); if ( !n00.getNodeData().getSequence().getName().equals( "gene_name" ) ) { return false; } @@ -5693,7 +5778,7 @@ public final class Test { + "prob_range={1.000000000000000e+00,1.000000000000000e+00},prob(percent)=\"100\"," + "prob+-sd=\"100+-0\"]:6.375699999999999e-02[&length_mean=6.395210411945065e-02," + "length_median=6.375699999999999e-02,length_95%HPD={5.388600000000000e-02," - + "7.369400000000000e-02}])", new NHXParser() )[ 0 ]; + + "7.369400000000000e-02}])", new NHXParser2() )[ 0 ]; if ( !isEqual( p1.getNode( "1" ).getDistanceToParent(), 4.129e-02 ) ) { return false; } @@ -5720,7 +5805,7 @@ public final class Test { + "prob+-sd=\"100+-0\"]:6.375699999999999e-02[&length_mean=6.395210411945065e-02," + "length_median=6.375699999999999e-02,length_95%HPD={5.388600000000000e-02," + "7.369400000000000e-02}])", - new NHXParser() )[ 0 ]; + new NHXParser2() )[ 0 ]; if ( p2.getNode( "1" ) == null ) { return false; } -- 1.7.10.2