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.evoinference.matrix.character.BasicCharacterStateMatrix;
35 import org.forester.evoinference.matrix.character.CharacterStateMatrix;
36 import org.forester.evoinference.matrix.character.CharacterStateMatrix.BinaryStates;
37 import org.forester.io.parsers.util.ParserUtils;
38 import org.forester.io.parsers.util.PhylogenyParserException;
40 public class PaupLogParser {
42 private static final String DATA_MATRIX_AND_RECONSTRUCTED_STATES_FOR_INTERNAL_NODES = "data matrix and reconstructed states for internal nodes";
43 private Object _nexus_source;
45 private Object getNexusSource() {
49 public CharacterStateMatrix<BinaryStates> parse() throws IOException {
50 final BufferedReader reader = ParserUtils.createReader( getNexusSource() );
52 boolean saw_line = false;
53 int identifier_index = 0;
54 boolean first_block = true;
55 boolean saw_data_matrix_line = false;
56 final List<String> identifiers = new ArrayList<String>();
57 final List<List<BinaryStates>> states = new ArrayList<List<BinaryStates>>();
59 while ( ( ( line = reader.readLine() ) != null ) && !done ) {
61 if ( ( line.length() > 0 ) && !line.startsWith( "#" ) && !line.startsWith( ">" ) ) {
62 if ( ( ( identifier_index > 0 ) && line.startsWith( "Tree " ) )
63 || line.startsWith( "Character change list" ) ) {
67 if ( line.toLowerCase().startsWith( DATA_MATRIX_AND_RECONSTRUCTED_STATES_FOR_INTERNAL_NODES ) ) {
69 saw_data_matrix_line = true;
71 if ( first_block && ( line.indexOf( "continued" ) > 0 ) ) {
75 if ( saw_data_matrix_line && line.startsWith( "----------" ) ) {
78 else if ( saw_line && ( line.indexOf( ' ' ) > 0 ) ) {
79 final String[] s = line.split( "\\s+" );
80 if ( s.length != 2 ) {
81 throw new NexusFormatException( "unexpected format at line: " + line );
83 final String identifier = s[ 0 ];
84 final String row = s[ 1 ];
86 if ( identifiers.contains( identifier ) ) {
87 throw new NexusFormatException( "identifier [" + identifier + "] is not unique in line: "
90 identifiers.add( identifier );
91 states.add( new ArrayList<BinaryStates>() );
94 if ( !identifiers.contains( identifier ) ) {
95 throw new NexusFormatException( "new identifier [" + identifier + "] at line: " + line );
98 for( int c = 0; c < row.length(); ++c ) {
99 final char ch = row.charAt( c );
101 states.get( identifier_index ).add( BinaryStates.ABSENT );
103 else if ( ch == '1' ) {
104 states.get( identifier_index ).add( BinaryStates.PRESENT );
107 throw new NexusFormatException( "unknown character state [" + ch + "] at line: " + line );
114 final CharacterStateMatrix<BinaryStates> matrix = new BasicCharacterStateMatrix<BinaryStates>( states );
116 for( final String identifier : identifiers ) {
117 matrix.setIdentifier( i++, identifier );
122 public void setSource( final Object nexus_source ) throws PhylogenyParserException, IOException {
123 if ( nexus_source == null ) {
124 throw new PhylogenyParserException( getClass() + ": attempt to parse null object." );
126 _nexus_source = nexus_source;