// 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.msa;
import java.io.IOException;
+import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
+import java.util.HashSet;
import java.util.List;
+import java.util.Set;
+import org.forester.io.writers.SequenceWriter;
+import org.forester.io.writers.SequenceWriter.SEQ_FORMAT;
+import org.forester.sequence.BasicSequence;
import org.forester.sequence.Sequence;
import org.forester.sequence.Sequence.TYPE;
import org.forester.util.ForesterUtil;
public class BasicMsa implements Msa {
private final char[][] _data;
- private final Object[] _identifiers;
+ private final String[] _identifiers;
private final TYPE _type;
public BasicMsa( final int rows, final int columns, final TYPE type ) {
throw new IllegalArgumentException( "basic msa of size zero are illegal" );
}
_data = new char[ rows ][ columns ];
- _identifiers = new Object[ rows ];
+ _identifiers = new String[ rows ];
_type = type;
}
_type = msa._type;
}
+ @Override
+ public List<Sequence> asSequenceList() {
+ final List<Sequence> seqs = new ArrayList<Sequence>();
+ for( int i = 0; i < getNumberOfSequences(); ++i ) {
+ seqs.add( getSequence( i ) );
+ }
+ return seqs;
+ }
+
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;
}
}
@Override
- public Object getIdentifier( final int row ) {
+ public String getIdentifier( final int row ) {
return _identifiers[ row ];
}
}
@Override
+ public Sequence getSequence( final String id ) {
+ for( int i = 0; i < getNumberOfSequences(); ++i ) {
+ if ( getIdentifier( i ).equals( id ) ) {
+ return getSequence( i );
+ }
+ }
+ return null;
+ }
+
+ @Override
+ public Sequence getSequence( final int row ) {
+ return new BasicSequence( getIdentifier( row ), _data[ row ], getType() );
+ }
+
+ @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;
}
@Override
- public void setIdentifier( final int row, final Object id ) {
+ public void setIdentifier( final int row, final String id ) {
_identifiers[ row ] = id;
}
@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
- public void write( final Writer w ) throws IOException {
+ public void write( final Writer w, final MSA_FORMAT format ) throws IOException {
+ switch ( format ) {
+ case PHYLIP:
+ writeToPhylip( w );
+ break;
+ case FASTA:
+ writeToFasta( w );
+ break;
+ default:
+ throw new RuntimeException( "unknown format " + format );
+ }
+ }
+
+ private void writeToFasta( final Writer w ) throws IOException {
+ SequenceWriter.writeSeqs( asSequenceList(), w, SEQ_FORMAT.FASTA, 100 );
+ }
+
+ 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 );
if ( seqs.size() < 1 ) {
throw new IllegalArgumentException( "cannot create basic msa from less than one sequence" );
}
+ final Set<String> ids = new HashSet<String>();
final int length = seqs.get( 0 ).getLength();
final BasicMsa msa = new BasicMsa( seqs.size(), length, seqs.get( 0 ).getType() );
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 ["
+ + seq.getIdentifier() + "]" );
}
+ ids.add( seq.getIdentifier() );
msa.setIdentifier( row, seq.getIdentifier() );
for( int col = 0; col < length; ++col ) {
msa._data[ row ][ col ] = seq.getResidueAt( col );
}
return column;
}
+
+ @Override
+ public boolean isGapAt( final int row, final int col ) {
+ return getResidueAt( row, col ) == Sequence.GAP;
+ }
}