2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 // Contact: phylosoft @ gmail . com
24 // WWW: www.phylosoft.org/forester
26 package org.forester.io.parsers.nhx;
28 import java.awt.Color;
29 import java.io.BufferedReader;
31 import java.io.FileReader;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.io.InputStreamReader;
35 import java.util.ArrayList;
36 import java.util.List;
37 import java.util.StringTokenizer;
38 import java.util.regex.Matcher;
39 import java.util.regex.Pattern;
41 import org.forester.io.parsers.PhylogenyParser;
42 import org.forester.io.parsers.util.ParserUtils;
43 import org.forester.io.parsers.util.PhylogenyParserException;
44 import org.forester.phylogeny.Phylogeny;
45 import org.forester.phylogeny.PhylogenyMethods;
46 import org.forester.phylogeny.PhylogenyNode;
47 import org.forester.phylogeny.data.Accession;
48 import org.forester.phylogeny.data.Annotation;
49 import org.forester.phylogeny.data.DomainArchitecture;
50 import org.forester.phylogeny.data.Event;
51 import org.forester.phylogeny.data.Identifier;
52 import org.forester.phylogeny.data.PropertiesMap;
53 import org.forester.phylogeny.data.Property;
54 import org.forester.phylogeny.data.Sequence;
55 import org.forester.phylogeny.data.Taxonomy;
56 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
57 import org.forester.util.ForesterUtil;
59 public final class NHXParser implements PhylogenyParser {
61 public static final boolean LIMIT_SPECIES_NAMES_TO_FIVE_CHARS = true;
62 public static final PhylogenyMethods.TAXONOMY_EXTRACTION TAXONOMY_EXTRACTION_DEFAULT = PhylogenyMethods.TAXONOMY_EXTRACTION.NO;
63 final static private boolean GUESS_ROOTEDNESS_DEFAULT = true;
64 final static private boolean GUESS_IF_SUPPORT_VALUES = true;
65 final static private boolean IGNORE_QUOTES_DEFAULT = false;
66 final static public boolean REPLACE_UNDERSCORES_DEFAULT = false;
67 private boolean _saw_closing_paren;
68 final static private byte STRING = 0;
69 final static private byte STRING_BUFFER = 1;
70 final static private byte CHAR_ARRAY = 2;
71 final static private byte BUFFERED_READER = 3;
72 private boolean _guess_rootedness;
73 private boolean _has_next;
74 private boolean _ignore_quotes;
75 private byte _input_type;
76 private int _source_length;
77 private PhylogenyNode _current_node;
78 private StringBuilder _current_anotation;
79 private Object _nhx_source;
80 private int _clade_level;
81 private List<Phylogeny> _phylogenies;
82 private Phylogeny _current_phylogeny;
83 private PhylogenyMethods.TAXONOMY_EXTRACTION _taxonomy_extraction;
84 private boolean _replace_underscores;
85 public final static Pattern UC_LETTERS_NUMBERS_PATTERN = Pattern
86 .compile( "^[A-Z0-9]+$" );
87 public final static Pattern NUMBERS_ONLY_PATTERN = Pattern
88 .compile( "^[0-9]+$" );
95 * Decreases the clade level by one.
97 * @throws PhylogenyParserException
98 * if level goes below zero.
100 private void decreaseCladeLevel() throws PhylogenyParserException {
101 if ( getCladeLevel() < 0 ) {
102 throw new PhylogenyParserException( "error in NH (Newick)/NHX formatted data: most likely cause: number of close parens is larger than number of open parens" );
108 * Finishes the current Phylogeny and adds it to the list of Phylogenies
111 * @throws PhylogenyParserException
112 * @throws NHXFormatException
114 private void finishPhylogeny() throws PhylogenyParserException, NHXFormatException {
116 if ( getCurrentPhylogeny() != null ) {
117 parseNHX( getCurrentAnotation().toString(),
118 getCurrentPhylogeny().getRoot(),
119 getTaxonomyExtraction(),
120 isReplaceUnderscores() );
121 if ( NHXParser.GUESS_IF_SUPPORT_VALUES ) {
122 if ( NHXParser.isBranchLengthsLikeBootstrapValues( getCurrentPhylogeny() ) ) {
123 NHXParser.moveBranchLengthsToBootstrapValues( getCurrentPhylogeny() );
126 if ( isGuessRootedness() ) {
127 final PhylogenyNode root = getCurrentPhylogeny().getRoot();
128 if ( ( root.getDistanceToParent() >= 0.0 ) || !ForesterUtil.isEmpty( root.getName() )
129 || !ForesterUtil.isEmpty( PhylogenyMethods.getSpecies( root ) ) || root.isHasAssignedEvent() ) {
130 getCurrentPhylogeny().setRooted( true );
133 getPhylogenies().add( getCurrentPhylogeny() );
137 private void finishSingleNodePhylogeny() throws PhylogenyParserException, NHXFormatException {
139 final PhylogenyNode new_node = new PhylogenyNode();
140 parseNHX( getCurrentAnotation().toString(), new_node, getTaxonomyExtraction(), isReplaceUnderscores() );
141 setCurrentPhylogeny( new Phylogeny() );
142 getCurrentPhylogeny().setRoot( new_node );
143 getPhylogenies().add( getCurrentPhylogeny() );
146 private int getCladeLevel() {
150 private StringBuilder getCurrentAnotation() {
151 return _current_anotation;
154 private PhylogenyNode getCurrentNode() {
155 return _current_node;
158 private Phylogeny getCurrentPhylogeny() {
159 return _current_phylogeny;
162 private byte getInputType() {
166 private Object getNhxSource() {
170 private List<Phylogeny> getPhylogenies() {
175 * Returns the Phylogenies created as Array.
177 * @return the Phylogenies created as Array
179 private Phylogeny[] getPhylogeniesAsArray() {
180 final Phylogeny[] p = new Phylogeny[ getPhylogenies().size() ];
181 for( int i = 0; i < getPhylogenies().size(); ++i ) {
182 p[ i ] = getPhylogenies().get( i );
187 private int getSourceLength() {
188 return _source_length;
191 public PhylogenyMethods.TAXONOMY_EXTRACTION getTaxonomyExtraction() {
192 return _taxonomy_extraction;
195 public boolean hasNext() {
200 * Increases the clade level by one.
202 private void increaseCladeLevel() {
206 private void init() {
207 setTaxonomyExtraction( TAXONOMY_EXTRACTION_DEFAULT );
208 setReplaceUnderscores( REPLACE_UNDERSCORES_DEFAULT );
209 setGuessRootedness( GUESS_ROOTEDNESS_DEFAULT );
210 setIgnoreQuotes( IGNORE_QUOTES_DEFAULT );
214 private boolean isGuessRootedness() {
215 return _guess_rootedness;
218 private boolean isIgnoreQuotes() {
219 return _ignore_quotes;
222 private boolean isReplaceUnderscores() {
223 return _replace_underscores;
226 private boolean isSawClosingParen() {
227 return _saw_closing_paren;
231 * Replaces the current annotation with a new StringBuffer.
233 private void newCurrentAnotation() {
234 setCurrentAnotation( new StringBuilder() );
238 * Parses the source set with setSource( final Object nhx_source ). Returns
239 * the Phylogenies found in the source as Phylogeny[].
240 * Everything between [ and ] is considered comment and ignored,
244 * ":digits and/or.[bootstrap]"
246 * @see #setSource( final Object nhx_source )
247 * @see org.forester.io.parsers.PhylogenyParser#parse()
248 * @return Phylogeny[]
249 * @throws IOException
250 * @throws NHXFormatException
251 * @throws PhylogenyParserException
254 public Phylogeny[] parse() throws IOException, NHXFormatException {
256 boolean in_comment = false;
257 boolean saw_colon = false;
258 boolean saw_open_bracket = false;
259 boolean in_double_quote = false;
260 boolean in_single_quote = false;
261 setPhylogenies( new ArrayList<Phylogeny>() );
263 newCurrentAnotation();
267 if ( getInputType() == NHXParser.BUFFERED_READER ) {
268 final int ci = ( ( BufferedReader ) getNhxSource() ).read();
277 if ( i >= getSourceLength() ) {
281 switch ( getInputType() ) {
283 c = ( ( String ) getNhxSource() ).charAt( i );
286 c = ( ( StringBuffer ) getNhxSource() ).charAt( i );
289 c = ( ( char[] ) getNhxSource() )[ i ];
294 if ( !in_single_quote && !in_double_quote ) {
298 else if ( !( ( c < 33 ) || ( c > 126 ) ) && saw_colon
299 && ( ( c != '[' ) && ( c != '.' ) && ( ( c < 48 ) || ( c > 57 ) ) ) ) {
303 // \n\t is always ignored,
304 // as is " (34) and ' (39) (space is 32):
305 if ( ( isIgnoreQuotes() && ( ( c < 33 ) || ( c > 126 ) || ( c == 34 ) || ( c == 39 ) || ( ( getCladeLevel() == 0 ) && ( c == ';' ) ) ) )
306 || ( !isIgnoreQuotes() && ( ( c < 32 ) || ( c > 126 ) || ( ( getCladeLevel() == 0 ) && ( c == ';' ) ) ) ) ) {
309 else if ( ( c == 32 ) && ( !in_single_quote && !in_double_quote ) ) {
312 else if ( in_comment ) {
317 else if ( in_double_quote ) {
319 in_double_quote = false;
322 getCurrentAnotation().append( c );
325 else if ( c == '"' ) {
326 in_double_quote = true;
328 else if ( in_single_quote ) {
330 in_single_quote = false;
333 getCurrentAnotation().append( c );
336 else if ( c == 39 ) {
337 in_single_quote = true;
339 else if ( c == '[' ) {
340 saw_open_bracket = true;
342 else if ( saw_open_bracket ) {
344 // everything not starting with "[&" is considered a comment
345 // unless ":digits and/or . [bootstrap]":
347 getCurrentAnotation().append( "[&" );
349 else if ( saw_colon ) {
350 getCurrentAnotation().append( "[" + c );
356 // comment consisting just of "[]":
357 saw_open_bracket = false;
359 else if ( c == '(' ) {
362 else if ( c == ')' ) {
365 else if ( c == ',' ) {
369 getCurrentAnotation().append( c );
373 if ( getCladeLevel() != 0 ) {
374 setPhylogenies( null );
375 throw new PhylogenyParserException( "error in NH (Newick)/NHX formatted data: most likely cause: number of open parens does not equal number of close parens" );
377 if ( getCurrentPhylogeny() != null ) {
380 else if ( getCurrentAnotation().length() > 0 ) {
381 finishSingleNodePhylogeny();
383 else if ( getPhylogenies().size() < 1 ) {
384 getPhylogenies().add( new Phylogeny() );
386 return getPhylogeniesAsArray();
389 public Phylogeny parseNext() throws IOException, NHXFormatException {
394 * Called if a closing paren is encountered.
396 * @throws PhylogenyParserException
397 * @throws NHXFormatException
399 private void processCloseParen() throws PhylogenyParserException, NHXFormatException {
400 decreaseCladeLevel();
401 if ( !isSawClosingParen() ) {
402 final PhylogenyNode new_node = new PhylogenyNode();
403 parseNHX( getCurrentAnotation().toString(), new_node, getTaxonomyExtraction(), isReplaceUnderscores() );
404 newCurrentAnotation();
405 getCurrentNode().addAsChild( new_node );
408 parseNHX( getCurrentAnotation().toString(),
409 getCurrentNode().getLastChildNode(),
410 getTaxonomyExtraction(),
411 isReplaceUnderscores() );
412 newCurrentAnotation();
414 if ( !getCurrentNode().isRoot() ) {
415 setCurrentNode( getCurrentNode().getParent() );
417 setSawClosingParen( true );
418 } // processCloseParen()
421 * Called if a comma is encountered.
423 * @throws PhylogenyParserException
424 * @throws NHXFormatException
426 private void processComma() throws PhylogenyParserException, NHXFormatException {
427 if ( !isSawClosingParen() ) {
428 final PhylogenyNode new_node = new PhylogenyNode();
429 parseNHX( getCurrentAnotation().toString(), new_node, getTaxonomyExtraction(), isReplaceUnderscores() );
430 if ( getCurrentNode() == null ) {
431 throw new NHXFormatException( "format might not be NH or NHX" );
433 getCurrentNode().addAsChild( new_node );
436 parseNHX( getCurrentAnotation().toString(),
437 getCurrentNode().getLastChildNode(),
438 getTaxonomyExtraction(),
439 isReplaceUnderscores() );
441 newCurrentAnotation();
442 setSawClosingParen( false );
446 * Called if a opening paren is encountered.
448 * @throws PhylogenyParserException
449 * @throws NHXFormatException
451 private void processOpenParen() throws PhylogenyParserException, NHXFormatException {
452 final PhylogenyNode new_node = new PhylogenyNode();
453 if ( getCladeLevel() == 0 ) {
454 if ( getCurrentPhylogeny() != null ) {
458 newCurrentAnotation();
459 setCurrentPhylogeny( new Phylogeny() );
460 getCurrentPhylogeny().setRoot( new_node );
463 increaseCladeLevel();
464 getCurrentNode().addAsChild( new_node );
466 setCurrentNode( new_node );
467 setSawClosingParen( false );
470 private void setCladeLevel( final int clade_level ) {
471 if ( clade_level < 0 ) {
472 throw new IllegalArgumentException( "Attempt to set clade level to a number smaller than zero." );
474 _clade_level = clade_level;
477 private void setCurrentAnotation( final StringBuilder current_anotation ) {
478 _current_anotation = current_anotation;
481 private void setCurrentNode( final PhylogenyNode current_node ) {
482 _current_node = current_node;
485 private void setCurrentPhylogeny( final Phylogeny current_phylogeny ) {
486 _current_phylogeny = current_phylogeny;
489 public void setGuessRootedness( final boolean guess_rootedness ) {
490 _guess_rootedness = guess_rootedness;
493 private void setHasNext( final boolean has_next ) {
494 _has_next = has_next;
497 public void setIgnoreQuotes( final boolean ignore_quotes ) {
498 _ignore_quotes = ignore_quotes;
501 private void setInputType( final byte input_type ) {
502 _input_type = input_type;
505 private void setNhxSource( final Object nhx_source ) {
506 _nhx_source = nhx_source;
509 private void setPhylogenies( final ArrayList<Phylogeny> phylogenies ) {
510 _phylogenies = phylogenies;
513 public void setReplaceUnderscores( final boolean replace_underscores ) {
514 _replace_underscores = replace_underscores;
517 private void setSawClosingParen( final boolean saw_closing_paren ) {
518 _saw_closing_paren = saw_closing_paren;
522 * This sets the source to be parsed. The source can be: String,
523 * StringBuffer, char[], File, or InputStream. The source can contain more
524 * than one phylogenies in either New Hamphshire (NH) or New Hamphshire
525 * Extended (NHX) format. There is no need to separate phylogenies with any
526 * special character. White space is always ignored, as are semicolons
527 * inbetween phylogenies. Example of a source describing two phylogenies
528 * (source is a String, in this example): "(A,(B,(C,(D,E)de)cde)bcde)abcde
529 * ((((A,B)ab,C)abc,D)abcd,E)abcde". Everything between a '[' followed by any
530 * character other than '&' and ']' is considered a comment and ignored
531 * (example: "[this is a comment]"). NHX tags are surrounded by '[&&NHX' and
532 * ']' (example: "[&&NHX:S=Varanus_storri]"). A sequence like "[& some
533 * info]" is ignored, too (at the PhylogenyNode level, though).
534 * Exception: numbers only between [ and ] (e.g. [90]) are interpreted as support values.
537 * @see org.forester.io.parsers.PhylogenyParser#setSource(java.lang.Object)
539 * the source to be parsed (String, StringBuffer, char[], File,
541 * @throws IOException
542 * @throws PhylogenyParserException
545 public void setSource( final Object nhx_source ) throws PhylogenyParserException, IOException {
546 if ( nhx_source == null ) {
547 throw new PhylogenyParserException( getClass() + ": attempt to parse null object." );
549 else if ( nhx_source instanceof String ) {
550 setInputType( NHXParser.STRING );
551 setSourceLength( ( ( String ) nhx_source ).length() );
552 setNhxSource( nhx_source );
554 else if ( nhx_source instanceof StringBuffer ) {
555 setInputType( NHXParser.STRING_BUFFER );
556 setSourceLength( ( ( StringBuffer ) nhx_source ).length() );
557 setNhxSource( nhx_source );
559 else if ( nhx_source instanceof char[] ) {
560 setInputType( NHXParser.CHAR_ARRAY );
561 setSourceLength( ( ( char[] ) nhx_source ).length );
562 setNhxSource( nhx_source );
564 else if ( nhx_source instanceof File ) {
565 setInputType( NHXParser.BUFFERED_READER );
566 setSourceLength( 0 );
567 final File f = ( File ) nhx_source;
568 final String error = ForesterUtil.isReadableFile( f );
569 if ( !ForesterUtil.isEmpty( error ) ) {
570 throw new PhylogenyParserException( error );
572 setNhxSource( new BufferedReader( new FileReader( f ) ) );
574 else if ( nhx_source instanceof InputStream ) {
575 setInputType( NHXParser.BUFFERED_READER );
576 setSourceLength( 0 );
577 final InputStreamReader isr = new InputStreamReader( ( InputStream ) nhx_source );
578 setNhxSource( new BufferedReader( isr ) );
581 throw new IllegalArgumentException( getClass() + " can only parse objects of type String,"
582 + " StringBuffer, char[], File," + " or InputStream " + " [attempt to parse object of "
583 + nhx_source.getClass() + "]." );
588 private void setSourceLength( final int source_length ) {
589 _source_length = source_length;
592 public void setTaxonomyExtraction( final PhylogenyMethods.TAXONOMY_EXTRACTION taxonomy_extraction ) {
593 _taxonomy_extraction = taxonomy_extraction;
596 private static double doubleValue( final String str ) throws NHXFormatException {
598 return Double.valueOf( str ).doubleValue();
600 catch ( final NumberFormatException ex ) {
601 throw new NHXFormatException( "error in NH/NHX formatted data: failed to parse number from :" + "\"" + str
606 private static boolean isBranchLengthsLikeBootstrapValues( final Phylogeny p ) {
607 final PhylogenyNodeIterator it = p.iteratorExternalForward();
608 final double d0 = it.next().getDistanceToParent();
609 if ( ( d0 < 10 ) || !it.hasNext() ) {
612 while ( it.hasNext() ) {
613 final double d = it.next().getDistanceToParent();
614 if ( ( d != d0 ) || ( d < 10 ) ) {
621 private static void moveBranchLengthsToBootstrapValues( final Phylogeny p ) {
622 final PhylogenyNodeIterator it = p.iteratorPostorder();
623 while ( it.hasNext() ) {
624 final PhylogenyNode n = it.next();
625 PhylogenyMethods.setBootstrapConfidence( n, n.getDistanceToParent() );
626 n.setDistanceToParent( PhylogenyNode.DISTANCE_DEFAULT );
630 public static void parseNHX( String s,
631 final PhylogenyNode node_to_annotate,
632 final PhylogenyMethods.TAXONOMY_EXTRACTION taxonomy_extraction,
633 final boolean replace_underscores ) throws NHXFormatException {
634 if ( ( taxonomy_extraction != PhylogenyMethods.TAXONOMY_EXTRACTION.NO ) && replace_underscores ) {
635 throw new IllegalArgumentException( "cannot extract taxonomies and replace under scores at the same time" );
637 if ( ( s != null ) && ( s.length() > 0 ) ) {
638 if ( replace_underscores ) {
639 s = s.replaceAll( "_+", " " );
645 StringTokenizer t = null;
646 boolean is_nhx = false;
647 ob = s.indexOf( "[" );
648 cb = s.indexOf( "]" );
654 throw new NHXFormatException( "error in NHX formatted data: no closing \"]\"" );
656 if ( s.indexOf( "&&NHX" ) == ( ob + 1 ) ) {
657 b = s.substring( ob + 6, cb );
660 // No &&NHX and digits only: is likely to be a support value.
661 final String bracketed = s.substring( ob + 1, cb );
662 final Matcher numbers_only = NUMBERS_ONLY_PATTERN.matcher( bracketed );
663 if ( numbers_only.matches() ) {
664 b = ":" + NHXtags.SUPPORT + bracketed;
667 a = s.substring( 0, ob );
669 if ( ( s.indexOf( "[" ) > -1 ) || ( s.indexOf( "]" ) > -1 ) ) {
670 throw new NHXFormatException( "error in NHX formatted data: more than one \"]\" or \"[\"" );
673 t = new StringTokenizer( s, ":" );
674 if ( t.countTokens() >= 1 ) {
675 if ( !s.startsWith( ":" ) ) {
676 node_to_annotate.setName( t.nextToken() );
677 if ( !replace_underscores
678 && ( !is_nhx && ( taxonomy_extraction != PhylogenyMethods.TAXONOMY_EXTRACTION.NO ) ) ) {
679 final String tax = ParserUtils
680 .extractTaxonomyCodeFromNodeName( node_to_annotate.getName(),
681 LIMIT_SPECIES_NAMES_TO_FIVE_CHARS,
682 taxonomy_extraction );
683 if ( !ForesterUtil.isEmpty( tax ) ) {
684 if ( !node_to_annotate.getNodeData().isHasTaxonomy() ) {
685 node_to_annotate.getNodeData().setTaxonomy( new Taxonomy() );
687 node_to_annotate.getNodeData().getTaxonomy().setTaxonomyCode( tax );
691 while ( t.hasMoreTokens() ) {
693 if ( s.startsWith( org.forester.io.parsers.nhx.NHXtags.SPECIES_NAME ) ) {
694 if ( !node_to_annotate.getNodeData().isHasTaxonomy() ) {
695 node_to_annotate.getNodeData().setTaxonomy( new Taxonomy() );
697 node_to_annotate.getNodeData().getTaxonomy().setScientificName( s.substring( 2 ) );
699 else if ( s.startsWith( org.forester.io.parsers.nhx.NHXtags.ANNOTATION ) ) {
700 if ( !node_to_annotate.getNodeData().isHasSequence() ) {
701 node_to_annotate.getNodeData().setSequence( new Sequence() );
703 final Annotation annotation = new Annotation( "_:_" );
704 annotation.setDesc( s.substring( 3 ) );
705 node_to_annotate.getNodeData().getSequence().addAnnotation( annotation );
707 else if ( s.startsWith( org.forester.io.parsers.nhx.NHXtags.IS_DUPLICATION ) ) {
708 if ( ( s.charAt( 2 ) == 'Y' ) || ( s.charAt( 2 ) == 'T' ) ) {
709 node_to_annotate.getNodeData().setEvent( Event.createSingleDuplicationEvent() );
711 else if ( ( s.charAt( 2 ) == 'N' ) || ( s.charAt( 2 ) == 'F' ) ) {
712 node_to_annotate.getNodeData().setEvent( Event.createSingleSpeciationEvent() );
714 else if ( s.charAt( 2 ) == '?' ) {
715 node_to_annotate.getNodeData().setEvent( Event.createSingleSpeciationOrDuplicationEvent() );
718 throw new NHXFormatException( "error in NHX formatted data: :D=Y or :D=N or :D=?" );
721 else if ( s.startsWith( NHXtags.SUPPORT ) ) {
722 PhylogenyMethods.setConfidence( node_to_annotate, doubleValue( s.substring( 2 ) ) );
724 else if ( s.startsWith( NHXtags.TAXONOMY_ID ) ) {
725 if ( !node_to_annotate.getNodeData().isHasTaxonomy() ) {
726 node_to_annotate.getNodeData().setTaxonomy( new Taxonomy() );
728 node_to_annotate.getNodeData().getTaxonomy().setIdentifier( new Identifier( s.substring( 2 ) ) );
730 else if ( s.startsWith( NHXtags.PARENT_BRANCH_WIDTH ) ) {
731 PhylogenyMethods.setBranchWidthValue( node_to_annotate, Integer.parseInt( s.substring( 2 ) ) );
733 else if ( s.startsWith( NHXtags.COLOR ) ) {
734 final Color c = NHXParser.stringToColor( s.substring( 2 ) );
736 PhylogenyMethods.setBranchColorValue( node_to_annotate, c );
739 else if ( s.startsWith( NHXtags.CUSTOM_DATA_ON_NODE ) ) {
740 if ( !node_to_annotate.getNodeData().isHasProperties() ) {
741 node_to_annotate.getNodeData().setProperties( new PropertiesMap() );
743 node_to_annotate.getNodeData().getProperties().addProperty( Property.createFromNhxString( s ) );
745 else if ( s.startsWith( NHXtags.DOMAIN_STRUCTURE ) ) {
746 if ( !node_to_annotate.getNodeData().isHasSequence() ) {
747 node_to_annotate.getNodeData().setSequence( new Sequence() );
749 node_to_annotate.getNodeData().getSequence()
750 .setDomainArchitecture( new DomainArchitecture( s.substring( 3 ) ) );
752 else if ( s.startsWith( NHXtags.NODE_IDENTIFIER ) ) {
753 node_to_annotate.getNodeData().setNodeIdentifier( new Identifier( s.substring( 3 ) ) );
755 else if ( s.startsWith( NHXtags.SEQUENCE_ACCESSION ) ) {
756 if ( !node_to_annotate.getNodeData().isHasSequence() ) {
757 node_to_annotate.getNodeData().setSequence( new Sequence() );
759 node_to_annotate.getNodeData().getSequence()
760 .setAccession( new Accession( s.substring( 3 ), "?" ) );
762 else if ( s.startsWith( NHXtags.GENE_NAME ) ) {
763 if ( !node_to_annotate.getNodeData().isHasSequence() ) {
764 node_to_annotate.getNodeData().setSequence( new Sequence() );
766 node_to_annotate.getNodeData().getSequence().setName( s.substring( 3 ) );
768 else if ( s.startsWith( NHXtags.GENE_NAME_SYNONYM ) ) {
769 if ( !node_to_annotate.getNodeData().isHasSequence() ) {
770 node_to_annotate.getNodeData().setSequence( new Sequence() );
772 node_to_annotate.getNodeData().getSequence().setName( s.substring( 2 ) );
774 else if ( s.indexOf( '=' ) < 0 ) {
775 if ( node_to_annotate.getDistanceToParent() != PhylogenyNode.DISTANCE_DEFAULT ) {
776 throw new NHXFormatException( "error in NHX formatted data: more than one distance to parent:"
779 node_to_annotate.setDistanceToParent( doubleValue( s ) );
781 } // while ( t.hasMoreTokens() )
787 * Parses String s in the format r.g.b (e.g. "12.34.234" ) into red, green,
788 * and blue and returns the corresponding Color.
790 private static Color stringToColor( final String s ) {
791 final StringTokenizer st = new StringTokenizer( s, "." );
792 if ( st.countTokens() != 3 ) {
793 throw new IllegalArgumentException( "illegal format for color: " + s );
795 final int red = ForesterUtil.limitRangeForColor( Integer.parseInt( st.nextToken() ) );
796 final int green = ForesterUtil.limitRangeForColor( Integer.parseInt( st.nextToken() ) );
797 final int blu = ForesterUtil.limitRangeForColor( Integer.parseInt( st.nextToken() ) );
798 return new Color( red, green, blu );