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.ForesterUtil;
38 public class NexusCharactersParser {
40 final private static String charstatelabels = NexusConstants.CHARSTATELABELS.toLowerCase();
41 private Object _nexus_source;
42 private String[] _char_state_labels;
44 public String[] getCharStateLabels() {
45 return _char_state_labels;
48 private Object getNexusSource() {
52 public void parse() throws IOException {
54 final BufferedReader reader = ParserUtils.createReader( getNexusSource() );
56 boolean in_charstatelabels = false;
57 final List<String> labels_list = new ArrayList<String>();
59 while ( ( line = reader.readLine() ) != null ) {
61 if ( ( line.length() > 0 ) && !line.startsWith( "#" ) && !line.startsWith( ">" ) ) {
62 if ( line.toLowerCase().startsWith( charstatelabels ) ) {
63 in_charstatelabels = true;
65 else if ( in_charstatelabels ) {
67 if ( label.indexOf( ' ' ) > 0 ) {
68 final String[] s = label.split( "\\s+" );
72 count = Integer.parseInt( s[ 0 ] );
74 catch ( final NumberFormatException ex ) {
75 throw new NexusFormatException( "failed to parse character label number from: " + line );
77 if ( count != counter ) {
78 throw new NexusFormatException( "character label numbers are not in order, current line: "
83 label = label.replaceAll( "[\\s;\"',]+", "" );
84 if ( !ForesterUtil.isEmpty( label ) ) {
85 if ( labels_list.contains( label ) ) {
86 throw new NexusFormatException( "character label [" + label + "] is not unique" );
88 labels_list.add( label );
91 if ( line.endsWith( ";" ) ) {
92 in_charstatelabels = false;
96 setCharStateLabels( new String[ labels_list.size() ] );
98 for( final String label : labels_list ) {
99 getCharStateLabels()[ i++ ] = label;
103 private void reset() {
104 setCharStateLabels( new String[ 0 ] );
107 private void setCharStateLabels( final String[] char_state_labels ) {
108 _char_state_labels = char_state_labels;
111 public void setSource( final Object nexus_source ) throws PhylogenyParserException, IOException {
112 if ( nexus_source == null ) {
113 throw new PhylogenyParserException( getClass() + ": attempt to parse null object." );
115 _nexus_source = nexus_source;