in progress
[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.ForesterConstants;
37 import org.forester.util.ForesterUtil;
38
39 public class NexusCharactersParser {
40
41      final private static String charstatelabels = NexusConstants.CHARSTATELABELS.toLowerCase();
42     private Object              _nexus_source;
43     private String[]            _char_state_labels;
44
45     public String[] getCharStateLabels() {
46         return _char_state_labels;
47     }
48
49     private Object getNexusSource() {
50         return _nexus_source;
51     }
52
53     public void parse() throws IOException {
54         reset();
55         final BufferedReader reader = ParserUtils.createReader( getNexusSource(), ForesterConstants.UTF_8 );
56         String line;
57         boolean in_charstatelabels = false;
58         final List<String> labels_list = new ArrayList<String>();
59         int counter = 1;
60         while ( ( line = reader.readLine() ) != null ) {
61             line = line.trim();
62             if ( ( line.length() > 0 ) && !line.startsWith( "#" ) && !line.startsWith( ">" ) ) {
63                 if ( line.toLowerCase().startsWith( charstatelabels ) ) {
64                     in_charstatelabels = true;
65                 }
66                 else if ( in_charstatelabels ) {
67                     String label = line;
68                     if ( label.indexOf( ' ' ) > 0 ) {
69                         final String[] s = label.split( "\\s+" );
70                         label = s[ 1 ];
71                         int count = -1;
72                         try {
73                             count = Integer.parseInt( s[ 0 ] );
74                         }
75                         catch ( final NumberFormatException ex ) {
76                             throw new NexusFormatException( "failed to parse character label number from: " + line );
77                         }
78                         if ( count != counter ) {
79                             throw new NexusFormatException( "character label numbers are not in order, current line: "
80                                     + line );
81                         }
82                     }
83                     ++counter;
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" );
88                         }
89                         labels_list.add( label );
90                     }
91                 }
92                 if ( line.endsWith( ";" ) ) {
93                     in_charstatelabels = false;
94                 }
95             }
96         }
97         setCharStateLabels( new String[ labels_list.size() ] );
98         int i = 0;
99         for( final String label : labels_list ) {
100             getCharStateLabels()[ i++ ] = label;
101         }
102     }
103
104     private void reset() {
105         setCharStateLabels( new String[ 0 ] );
106     }
107
108     private void setCharStateLabels( final String[] char_state_labels ) {
109         _char_state_labels = char_state_labels;
110     }
111
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." );
115         }
116         _nexus_source = nexus_source;
117     }
118 }