JAL-2872 Undid inference disabling (done in Jalview instead)
[jalview.git] / forester / java / src / org / forester / evoinference / matrix / character / BasicCharacterStateMatrix.java
index 30b5f9b..573bab0 100644 (file)
 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 //
 // Contact: phylosoft @ gmail . com
-// WWW: www.phylosoft.org/forester
+// WWW: https://sites.google.com/site/cmzmasek/home/software/forester
 
 package org.forester.evoinference.matrix.character;
 
 import java.io.IOException;
+import java.io.StringWriter;
 import java.io.Writer;
 import java.util.HashMap;
 import java.util.List;
@@ -102,7 +103,7 @@ public class BasicCharacterStateMatrix<S> implements CharacterStateMatrix<S> {
     @Override
     public CharacterStateMatrix<S> copy() {
         final CharacterStateMatrix<S> new_matrix = new BasicCharacterStateMatrix<S>( getNumberOfIdentifiers(),
-                                                                                     getNumberOfCharacters() );
+                getNumberOfCharacters() );
         for( int character = 0; character < getNumberOfCharacters(); ++character ) {
             if ( getCharacter( character ) != null ) {
                 new_matrix.setCharacter( character, getCharacter( character ) );
@@ -120,6 +121,7 @@ public class BasicCharacterStateMatrix<S> implements CharacterStateMatrix<S> {
     }
 
     @Override
+    @SuppressWarnings("unchecked")
     public boolean equals( final Object o ) {
         if ( this == o ) {
             return true;
@@ -129,7 +131,7 @@ public class BasicCharacterStateMatrix<S> implements CharacterStateMatrix<S> {
         }
         else if ( o.getClass() != this.getClass() ) {
             throw new IllegalArgumentException( "attempt to check character state matrix to " + o + " [" + o.getClass()
-                    + "]" );
+                                                + "]" );
         }
         else {
             final CharacterStateMatrix<S> other = ( CharacterStateMatrix<S> ) o;
@@ -216,6 +218,7 @@ public class BasicCharacterStateMatrix<S> implements CharacterStateMatrix<S> {
     }
 
     @Override
+    @SuppressWarnings("unchecked")
     public S getState( final int identifier_index, final int character_index ) {
         return ( S ) _states[ identifier_index ][ character_index ];
     }
@@ -247,7 +250,7 @@ public class BasicCharacterStateMatrix<S> implements CharacterStateMatrix<S> {
     @Override
     public CharacterStateMatrix<S> pivot() {
         final CharacterStateMatrix<S> new_matrix = new BasicCharacterStateMatrix<S>( getNumberOfCharacters(),
-                                                                                     getNumberOfIdentifiers() );
+                getNumberOfIdentifiers() );
         for( int character = 0; character < getNumberOfCharacters(); ++character ) {
             if ( getCharacter( character ) != null ) {
                 new_matrix.setIdentifier( character, getCharacter( character ) );
@@ -311,6 +314,19 @@ public class BasicCharacterStateMatrix<S> implements CharacterStateMatrix<S> {
         }
         setState( _identifier_index_map.get( identifier ), _character_index_map.get( character ), state );
     }
+    
+    
+    public String toString() {
+        StringWriter w = new StringWriter();
+        try {
+            toForester( w );
+        }
+        catch ( IOException e ) {
+            // TODO Auto-generated catch block
+            e.printStackTrace();
+        }
+        return w.toString();
+    }
 
     private void toForester( final Writer writer ) throws IOException {
         final int longest = getLengthOfLongestState() + 5;
@@ -326,7 +342,7 @@ public class BasicCharacterStateMatrix<S> implements CharacterStateMatrix<S> {
             final String c = getCharacter( character );
             writer.write( c != null ? ForesterUtil.pad( c, longest, ' ', false ).toString() : ForesterUtil
                     .pad( "", longest, ' ', false ).toString() );
-            if ( character < getNumberOfCharacters() - 1 ) {
+            if ( character < ( getNumberOfCharacters() - 1 ) ) {
                 writer.write( ' ' );
             }
         }
@@ -340,11 +356,11 @@ public class BasicCharacterStateMatrix<S> implements CharacterStateMatrix<S> {
                 final S state = getState( identifier, character );
                 writer.write( state != null ? ForesterUtil.pad( state.toString(), longest, ' ', false ).toString()
                         : ForesterUtil.pad( "", longest, ' ', false ).toString() );
-                if ( character < getNumberOfCharacters() - 1 ) {
+                if ( character < ( getNumberOfCharacters() - 1 ) ) {
                     writer.write( ' ' );
                 }
             }
-            if ( identifier < getNumberOfIdentifiers() - 1 ) {
+            if ( identifier < ( getNumberOfIdentifiers() - 1 ) ) {
                 writer.write( ForesterUtil.LINE_SEPARATOR );
             }
         }
@@ -384,21 +400,18 @@ public class BasicCharacterStateMatrix<S> implements CharacterStateMatrix<S> {
                 final String state = getState( identifier, character ).toString();
                 writer.write( state != null ? ForesterUtil.pad( state, pad, ' ', false ).toString() : ForesterUtil
                         .pad( "", pad, ' ', false ).toString() );
-                if ( character < getNumberOfCharacters() - 1 ) {
+                if ( character < ( getNumberOfCharacters() - 1 ) ) {
                     writer.write( ' ' );
                     writer.write( ' ' );
                 }
             }
-            if ( identifier < getNumberOfIdentifiers() - 1 ) {
+            if ( identifier < ( getNumberOfIdentifiers() - 1 ) ) {
                 writer.write( ForesterUtil.LINE_SEPARATOR );
             }
         }
     }
 
-    //TODO
-    //to format for microarray-style clustering
-    // states are ints in this case
-    //TODO
+   
     @Override
     public void toWriter( final Writer writer ) throws IOException {
         toForester( writer );
@@ -424,7 +437,7 @@ public class BasicCharacterStateMatrix<S> implements CharacterStateMatrix<S> {
     public void writeNexusBinaryChractersBlock( final Writer w ) throws IOException {
         //BEGIN CHARACTERS;
         // DIMENSIONS NCHAR=x;
-        //BEGIN CHARSTATELABELS 
+        //BEGIN CHARSTATELABELS
         // 1 bcl,
         // 2 tir,
         //END;
@@ -472,7 +485,7 @@ public class BasicCharacterStateMatrix<S> implements CharacterStateMatrix<S> {
             w.write( "  " + ( i + 1 ) + " '" );
             w.write( getCharacter( i ) );
             w.write( "'" );
-            if ( i < getNumberOfCharacters() - 1 ) {
+            if ( i < ( getNumberOfCharacters() - 1 ) ) {
                 w.write( "," );
                 w.write( ForesterUtil.LINE_SEPARATOR );
             }
@@ -505,7 +518,7 @@ public class BasicCharacterStateMatrix<S> implements CharacterStateMatrix<S> {
                 }
                 w.write( state.toString() );
             }
-            if ( identifier < getNumberOfIdentifiers() - 1 ) {
+            if ( identifier < ( getNumberOfIdentifiers() - 1 ) ) {
                 w.write( ForesterUtil.LINE_SEPARATOR );
             }
         }