moved to: https://sites.google.com/site/cmzmasek/home/software/forester
[jalview.git] / forester / java / src / org / forester / io / parsers / nexus / PaupLogParser.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.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;
39
40 public class PaupLogParser {
41
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;
44
45     private Object getNexusSource() {
46         return _nexus_source;
47     }
48
49     public CharacterStateMatrix<BinaryStates> parse() throws IOException {
50         final BufferedReader reader = ParserUtils.createReader( getNexusSource() );
51         String line;
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>>();
58         boolean done = false;
59         while ( ( ( line = reader.readLine() ) != null ) && !done ) {
60             line = line.trim();
61             if ( ( line.length() > 0 ) && !line.startsWith( "#" ) && !line.startsWith( ">" ) ) {
62                 if ( ( ( identifier_index > 0 ) && line.startsWith( "Tree " ) )
63                         || line.startsWith( "Character change list" ) ) {
64                     done = true;
65                     continue;
66                 }
67                 if ( line.toLowerCase().startsWith( DATA_MATRIX_AND_RECONSTRUCTED_STATES_FOR_INTERNAL_NODES ) ) {
68                     saw_line = false;
69                     saw_data_matrix_line = true;
70                     identifier_index = 0;
71                     if ( first_block && ( line.indexOf( "continued" ) > 0 ) ) {
72                         first_block = false;
73                     }
74                 }
75                 if ( saw_data_matrix_line && line.startsWith( "----------" ) ) {
76                     saw_line = true;
77                 }
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 );
82                     }
83                     final String identifier = s[ 0 ];
84                     final String row = s[ 1 ];
85                     if ( first_block ) {
86                         if ( identifiers.contains( identifier ) ) {
87                             throw new NexusFormatException( "identifier [" + identifier + "] is not unique in line: "
88                                     + line );
89                         }
90                         identifiers.add( identifier );
91                         states.add( new ArrayList<BinaryStates>() );
92                     }
93                     else {
94                         if ( !identifiers.contains( identifier ) ) {
95                             throw new NexusFormatException( "new identifier [" + identifier + "] at line: " + line );
96                         }
97                     }
98                     for( int c = 0; c < row.length(); ++c ) {
99                         final char ch = row.charAt( c );
100                         if ( ch == '0' ) {
101                             states.get( identifier_index ).add( BinaryStates.ABSENT );
102                         }
103                         else if ( ch == '1' ) {
104                             states.get( identifier_index ).add( BinaryStates.PRESENT );
105                         }
106                         else {
107                             throw new NexusFormatException( "unknown character state [" + ch + "] at line: " + line );
108                         }
109                     }
110                     ++identifier_index;
111                 }
112             }
113         }
114         final CharacterStateMatrix<BinaryStates> matrix = new BasicCharacterStateMatrix<BinaryStates>( states );
115         int i = 0;
116         for( final String identifier : identifiers ) {
117             matrix.setIdentifier( i++, identifier );
118         }
119         return matrix;
120     }
121
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." );
125         }
126         _nexus_source = nexus_source;
127     }
128 }