3 // FORESTER -- software libraries and applications
4 // for evolutionary biology research and applications.
6 // Copyright (C) 2008-2009 Christian M. Zmasek
7 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Lesser General Public
12 // License as published by the Free Software Foundation; either
13 // version 2.1 of the License, or (at your option) any later version.
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 // Contact: phylosoft @ gmail . com
25 // WWW: www.phylosoft.org/
27 package org.forester.io.parsers.nexus;
29 import java.io.BufferedReader;
30 import java.io.IOException;
31 import java.util.ArrayList;
32 import java.util.List;
34 import org.forester.io.parsers.util.ParserUtils;
35 import org.forester.io.parsers.util.PhylogenyParserException;
36 import org.forester.util.ForesterConstants;
37 import org.forester.util.ForesterUtil;
39 public class NexusCharactersParser {
41 final private static String charstatelabels = NexusConstants.CHARSTATELABELS.toLowerCase();
42 private Object _nexus_source;
43 private String[] _char_state_labels;
45 public String[] getCharStateLabels() {
46 return _char_state_labels;
49 private Object getNexusSource() {
53 public void parse() throws IOException {
55 final BufferedReader reader = ParserUtils.createReader( getNexusSource(), ForesterConstants.UTF_8 );
57 boolean in_charstatelabels = false;
58 final List<String> labels_list = new ArrayList<String>();
60 while ( ( line = reader.readLine() ) != null ) {
62 if ( ( line.length() > 0 ) && !line.startsWith( "#" ) && !line.startsWith( ">" ) ) {
63 if ( line.toLowerCase().startsWith( charstatelabels ) ) {
64 in_charstatelabels = true;
66 else if ( in_charstatelabels ) {
68 if ( label.indexOf( ' ' ) > 0 ) {
69 final String[] s = label.split( "\\s+" );
73 count = Integer.parseInt( s[ 0 ] );
75 catch ( final NumberFormatException ex ) {
76 throw new NexusFormatException( "failed to parse character label number from: " + line );
78 if ( count != counter ) {
79 throw new NexusFormatException( "character label numbers are not in order, current line: "
84 label = label.replaceAll( "[\\s;\"',]+", "" );
85 if ( !ForesterUtil.isEmpty( label ) ) {
86 if ( labels_list.contains( label ) ) {
87 throw new NexusFormatException( "character label [" + label + "] is not unique" );
89 labels_list.add( label );
92 if ( line.endsWith( ";" ) ) {
93 in_charstatelabels = false;
97 setCharStateLabels( new String[ labels_list.size() ] );
99 for( final String label : labels_list ) {
100 getCharStateLabels()[ i++ ] = label;
104 private void reset() {
105 setCharStateLabels( new String[ 0 ] );
108 private void setCharStateLabels( final String[] char_state_labels ) {
109 _char_state_labels = char_state_labels;
112 public void setSource( final Object nexus_source ) throws PhylogenyParserException, IOException {
113 if ( nexus_source == null ) {
114 throw new PhylogenyParserException( getClass() + ": attempt to parse null object." );
116 _nexus_source = nexus_source;