clean up
[jalview.git] / forester / java / src / org / forester / io / parsers / nexus / NexusCharactersParser.java
1 // $Id:
2 //
3 // FORESTER -- software libraries and applications
4 // for evolutionary biology research and applications.
5 //
6 // Copyright (C) 2008-2009 Christian M. Zmasek
7 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
8 // All rights reserved
9 //
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.
14 //
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.
19 //
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
23 //
24 // Contact: phylosoft @ gmail . com
25 // WWW: www.phylosoft.org/
26
27 package org.forester.io.parsers.nexus;
28
29 import java.io.BufferedReader;
30 import java.io.IOException;
31 import java.util.ArrayList;
32 import java.util.List;
33
34 import org.forester.io.parsers.util.ParserUtils;
35 import org.forester.io.parsers.util.PhylogenyParserException;
36 import org.forester.util.ForesterUtil;
37
38 public class NexusCharactersParser {
39
40     final private static String charstatelabels = NexusConstants.CHARSTATELABELS.toLowerCase();
41     private Object              _nexus_source;
42     private String[]            _char_state_labels;
43
44     public String[] getCharStateLabels() {
45         return _char_state_labels;
46     }
47
48     private Object getNexusSource() {
49         return _nexus_source;
50     }
51
52     public void parse() throws IOException {
53         reset();
54         final BufferedReader reader = ParserUtils.createReader( getNexusSource() );
55         String line;
56         boolean in_charstatelabels = false;
57         final List<String> labels_list = new ArrayList<String>();
58         int counter = 1;
59         while ( ( line = reader.readLine() ) != null ) {
60             line = line.trim();
61             if ( ( line.length() > 0 ) && !line.startsWith( "#" ) && !line.startsWith( ">" ) ) {
62                 if ( line.toLowerCase().startsWith( charstatelabels ) ) {
63                     in_charstatelabels = true;
64                 }
65                 else if ( in_charstatelabels ) {
66                     String label = line;
67                     if ( label.indexOf( ' ' ) > 0 ) {
68                         final String[] s = label.split( "\\s+" );
69                         label = s[ 1 ];
70                         int count = -1;
71                         try {
72                             count = Integer.parseInt( s[ 0 ] );
73                         }
74                         catch ( final NumberFormatException ex ) {
75                             throw new NexusFormatException( "failed to parse character label number from: " + line );
76                         }
77                         if ( count != counter ) {
78                             throw new NexusFormatException( "character label numbers are not in order, current line: "
79                                     + line );
80                         }
81                     }
82                     ++counter;
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" );
87                         }
88                         labels_list.add( label );
89                     }
90                 }
91                 if ( line.endsWith( ";" ) ) {
92                     in_charstatelabels = false;
93                 }
94             }
95         }
96         setCharStateLabels( new String[ labels_list.size() ] );
97         int i = 0;
98         for( final String label : labels_list ) {
99             getCharStateLabels()[ i++ ] = label;
100         }
101     }
102
103     private void reset() {
104         setCharStateLabels( new String[ 0 ] );
105     }
106
107     private void setCharStateLabels( final String[] char_state_labels ) {
108         _char_state_labels = char_state_labels;
109     }
110
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." );
114         }
115         _nexus_source = nexus_source;
116     }
117 }