// Copyright (C) 2010 Christian M Zmasek
// Copyright (C) 2010 Sanford-Burnham Medical Research Institute
// All rights reserved
// Copyright (C) 2010 Christian M Zmasek
// Copyright (C) 2010 Sanford-Burnham Medical Research Institute
// All rights reserved
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
//
// Contact: phylosoft @ gmail . com
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
//
// Contact: phylosoft @ gmail . com
import org.forester.sequence.Sequence;
import org.forester.sequence.Sequence.TYPE;
import org.forester.util.ForesterUtil;
import org.forester.sequence.Sequence;
import org.forester.sequence.Sequence.TYPE;
import org.forester.util.ForesterUtil;
private final TYPE _type;
public BasicMsa( final int rows, final int columns, final TYPE type ) {
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 ];
throw new IllegalArgumentException( "basic msa of size zero are illegal" );
}
_data = new char[ rows ][ columns ];
+ 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 ) {
public StringBuffer getSequenceAsString( final int row ) {
final StringBuffer sb = new StringBuffer( _data[ 0 ].length );
for( int col = 0; col < _data[ 0 ].length; ++col ) {
public void setResidueAt( final int row, final int col, final char residue ) {
_data[ row ][ col ] = residue;
}
public void setResidueAt( final int row, final int col, final char residue ) {
_data[ row ][ col ] = residue;
}
- public void write( final Writer w ) throws IOException {
+ @Override
+ 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() );
final int max = determineMaxIdLength() + 1;
for( int row = 0; row < _data.length; ++row ) {
w.write( ForesterUtil.pad( _identifiers[ row ].toString(), max, ' ', false ).toString() );
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 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 ) {
if ( seq.getType() != msa.getType() ) {
throw new IllegalArgumentException( "illegal attempt to build msa from sequences of different type" );
}
if ( seq.getType() != msa.getType() ) {
throw new IllegalArgumentException( "illegal attempt to build msa from sequences of different type" );
}
+ 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 );
msa.setIdentifier( row, seq.getIdentifier() );
for( int col = 0; col < length; ++col ) {
msa._data[ row ][ col ] = seq.getResidueAt( col );
+
+ @Override
+ public List<Character> getColumnAt( final int col ) {
+ final List<Character> column = new ArrayList<Character>();
+ for( int row = 0; row < getNumberOfSequences(); ++row ) {
+ column.add( getResidueAt( row, col ) );
+ }
+ return column;
+ }