inprogress
[jalview.git] / forester / java / src / org / forester / msa / BasicMsa.java
index 5fc2590..4c00a30 100644 (file)
@@ -26,6 +26,7 @@
 package org.forester.msa;
 
 import java.io.IOException;
+import java.io.StringWriter;
 import java.io.Writer;
 import java.util.ArrayList;
 import java.util.HashSet;
@@ -71,8 +72,8 @@ public class BasicMsa implements Msa {
 
     private int determineMaxIdLength() {
         int max = 0;
-        for( int row = 0; row < _data.length; ++row ) {
-            final int l = _identifiers[ row ].toString().length();
+        for( int row = 0; row < getNumberOfSequences(); ++row ) {
+            final int l = getIdentifier( row ).length();
             if ( l > max ) {
                 max = l;
             }
@@ -117,8 +118,8 @@ public class BasicMsa implements Msa {
 
     @Override
     public StringBuffer getSequenceAsString( final int row ) {
-        final StringBuffer sb = new StringBuffer( _data[ 0 ].length );
-        for( int col = 0; col < _data[ 0 ].length; ++col ) {
+        final StringBuffer sb = new StringBuffer( getLength() );
+        for( int col = 0; col < getLength(); ++col ) {
             sb.append( getResidueAt( row, col ) );
         }
         return sb;
@@ -141,16 +142,14 @@ public class BasicMsa implements Msa {
 
     @Override
     public String toString() {
-        final int max = determineMaxIdLength() + 1;
-        final StringBuffer sb = new StringBuffer();
-        for( int row = 0; row < _data.length; ++row ) {
-            sb.append( ForesterUtil.pad( _identifiers[ row ].toString(), max, ' ', false ) );
-            for( int col = 0; col < _data[ 0 ].length; ++col ) {
-                sb.append( getResidueAt( row, col ) );
-            }
-            sb.append( ForesterUtil.LINE_SEPARATOR );
+        final Writer w = new StringWriter();
+        try {
+            write( w, MSA_FORMAT.PHYLIP );
         }
-        return sb.toString();
+        catch ( final IOException e ) {
+            e.printStackTrace();
+        }
+        return w.toString();
     }
 
     @Override
@@ -173,9 +172,9 @@ public class BasicMsa implements Msa {
 
     private void writeToPhylip( final Writer w ) throws IOException {
         final int max = determineMaxIdLength() + 1;
-        for( int row = 0; row < _data.length; ++row ) {
-            w.write( ForesterUtil.pad( _identifiers[ row ].toString(), max, ' ', false ).toString() );
-            for( int col = 0; col < _data[ 0 ].length; ++col ) {
+        for( int row = 0; row < getNumberOfSequences(); ++row ) {
+            w.write( ForesterUtil.pad( getIdentifier( row ), max, ' ', false ).toString() );
+            for( int col = 0; col < getLength(); ++col ) {
                 w.write( getResidueAt( row, col ) );
             }
             w.write( ForesterUtil.LINE_SEPARATOR );
@@ -192,10 +191,12 @@ public class BasicMsa implements Msa {
         for( int row = 0; row < seqs.size(); ++row ) {
             final Sequence seq = seqs.get( row );
             if ( seq.getLength() != length ) {
-                throw new IllegalArgumentException( "illegal attempt to build msa from sequences of unequal length" );
+                throw new IllegalArgumentException( "illegal attempt to build msa from sequences of unequal length ["
+                        + seq.getIdentifier() + "]" );
             }
             if ( seq.getType() != msa.getType() ) {
-                throw new IllegalArgumentException( "illegal attempt to build msa from sequences of different type" );
+                throw new IllegalArgumentException( "illegal attempt to build msa from sequences of different type ["
+                        + seq.getIdentifier() + "]" );
             }
             if ( ids.contains( seq.getIdentifier() ) ) {
                 throw new IllegalArgumentException( "illegal attempt to create msa with non-unique identifiers ["
@@ -218,4 +219,9 @@ public class BasicMsa implements Msa {
         }
         return column;
     }
+
+    @Override
+    public boolean isGapAt( final int row, final int col ) {
+        return getResidueAt( row, col ) == Sequence.GAP;
+    }
 }