moved to: https://sites.google.com/site/cmzmasek/home/software/forester
[jalview.git] / forester / java / src / org / forester / evoinference / matrix / character / CharacterStateMatrix.java
1 // $Id:
2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
4 //
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
7 // All rights reserved
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 //
23 // Contact: phylosoft @ gmail . com
24 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
25
26 package org.forester.evoinference.matrix.character;
27
28 import java.io.IOException;
29 import java.io.Writer;
30
31 public interface CharacterStateMatrix<S> {
32
33     public boolean containsCharacter( final String character );
34
35     public boolean containsIdentifier( final String identifier );
36
37     public CharacterStateMatrix<S> copy();
38
39     public String getCharacter( int character_index );
40
41     public int getCharacterIndex( final String character );
42
43     public String getIdentifier( int identifier_index );
44
45     public int getIdentifierIndex( final String identifier );
46
47     public int getNumberOfCharacters();
48
49     public int getNumberOfIdentifiers();
50
51     public S getState( final int identifier_index, final int character_index );
52
53     public S getState( final String identifier, final int character_index );
54
55     public S getState( final String identifier, final String character );
56
57     public boolean isEmpty();
58
59     public CharacterStateMatrix<S> pivot();
60
61     public void setCharacter( int character_index, final String character );
62
63     public void setIdentifier( int identifier_index, final String identifier );
64
65     public void setState( int identifier_index, int character_index, final S state );
66
67     public void setState( final String identifier, int character_index, final S state );
68
69     public void setState( final String identifier, final String character, final S state );
70
71     public void toWriter( final Writer writer ) throws IOException;
72
73     public void toWriter( final Writer writer, final Format format ) throws IOException;
74
75     /**
76      * It is crucial that the order
77      * ABSENT, UNKNOWN, PRESENT not be changes since
78      * this determines the sort order.
79      *
80      */
81     static public enum BinaryStates {
82         ABSENT, UNKNOWN, PRESENT;
83
84         public char toChar() {
85             switch ( this ) {
86                 case PRESENT:
87                     return '1';
88                 case ABSENT:
89                     return '0';
90                 case UNKNOWN:
91                     return '?';
92             }
93             throw new RuntimeException( "unknown state: " + this );
94         }
95
96         @Override
97         public String toString() {
98             switch ( this ) {
99                 case PRESENT:
100                     return "1";
101                 case ABSENT:
102                     return "0";
103                 case UNKNOWN:
104                     return "?";
105             }
106             throw new RuntimeException( "unknown state: " + this );
107         }
108     }
109
110     public static enum Format {
111         PHYLIP, FORESTER, NEXUS_BINARY
112     }
113
114     static public enum GainLossStates {
115         GAIN, LOSS, UNCHANGED_PRESENT, UNCHANGED_ABSENT, UNKNOWN;
116
117         @Override
118         public String toString() {
119             switch ( this ) {
120                 case GAIN:
121                     return "+";
122                 case LOSS:
123                     return "-";
124                 case UNCHANGED_PRESENT:
125                     return "X";
126                 case UNCHANGED_ABSENT:
127                     return ".";
128                 case UNKNOWN:
129                     return "?";
130             }
131             throw new AssertionError( "unknown state: " + this );
132         }
133     }
134
135     static public enum NucleotideStates {
136         A, C, G, T, UNKNOWN;
137     }
138 }