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.sequence.MolecularSequence;
+import org.forester.sequence.MolecularSequence.TYPE;
import org.forester.util.ForesterUtil;
public class BasicMsa implements Msa {
}
@Override
- public List<Sequence> asSequenceList() {
- final List<Sequence> seqs = new ArrayList<Sequence>();
+ public List<MolecularSequence> asSequenceList() {
+ final List<MolecularSequence> seqs = new ArrayList<MolecularSequence>();
for( int i = 0; i < getNumberOfSequences(); ++i ) {
seqs.add( getSequence( i ) );
}
}
@Override
- public Sequence getSequence( final int row ) {
+ public MolecularSequence getSequence( final int row ) {
return new BasicSequence( getIdentifier( row ), _data[ row ], getType() );
}
@Override
- public Sequence getSequence( final String id ) {
+ public MolecularSequence getSequence( final String id ) {
for( int i = 0; i < getNumberOfSequences(); ++i ) {
if ( getIdentifier( i ).equals( id ) ) {
return getSequence( i );
@Override
public boolean isGapAt( final int row, final int col ) {
- return getResidueAt( row, col ) == Sequence.GAP;
+ return getResidueAt( row, col ) == MolecularSequence.GAP;
}
@Override
}
if ( _identifiers_set.contains( id ) ) {
throw new IllegalArgumentException( "illegal attempt to create msa with non-unique identifiers [" + id
- + "]" );
+ + "]" );
}
_identifiers_set.add( id );
_identifiers[ row ] = id;
case FASTA:
writeToFasta( w );
break;
+ case NEXUS:
+ writeToNexus( w );
+ break;
default:
throw new RuntimeException( "unknown format " + format );
}
}
- private int determineMaxIdLength() {
- int max = 0;
+ private short determineMaxIdLength() {
+ short max = 0;
for( int row = 0; row < getNumberOfSequences(); ++row ) {
- final int l = getIdentifier( row ).length();
+ final short l = ( short ) getIdentifier( row ).length();
if ( l > max ) {
max = l;
}
SequenceWriter.writeSeqs( asSequenceList(), w, SEQ_FORMAT.FASTA, 100 );
}
+ private void writeToNexus( final Writer w ) throws IOException {
+ final int max = determineMaxIdLength() + 1;
+ w.write( "Begin Data;" );
+ w.write( ForesterUtil.LINE_SEPARATOR );
+ w.write( " Dimensions NTax=" + getNumberOfSequences() );
+ w.write( " NChar=" + getLength() );
+ w.write( ";" );
+ w.write( ForesterUtil.LINE_SEPARATOR );
+ w.write( " Format DataType=Protein Interleave=No gap=-;" );
+ w.write( ForesterUtil.LINE_SEPARATOR );
+ w.write( " Matrix" );
+ w.write( ForesterUtil.LINE_SEPARATOR );
+ for( int row = 0; row < getNumberOfSequences(); ++row ) {
+ final MolecularSequence seq = getSequence( row );
+ final String s = seq.getMolecularSequenceAsString();
+ w.write( " " );
+ w.write( ForesterUtil.pad( getIdentifier( row ).replace( ' ', '_' ), max, ' ', false ).toString() );
+ w.write( " " );
+ w.write( s );
+ w.write( ForesterUtil.LINE_SEPARATOR );
+ }
+ w.write( " ;" );
+ w.write( ForesterUtil.LINE_SEPARATOR );
+ w.write( "End;" );
+ w.write( ForesterUtil.LINE_SEPARATOR );
+ }
+
private void writeToPhylip( final Writer w ) throws IOException {
final int max = determineMaxIdLength() + 1;
+ w.write( getNumberOfSequences() + " " + getLength() );
+ w.write( ForesterUtil.LINE_SEPARATOR );
for( int row = 0; row < getNumberOfSequences(); ++row ) {
- w.write( ForesterUtil.pad( getIdentifier( row ), max, ' ', false ).toString() );
+ w.write( ForesterUtil.pad( getIdentifier( row ).replace( ' ', '_' ), max, ' ', false ).toString() );
for( int col = 0; col < getLength(); ++col ) {
w.write( getResidueAt( row, col ) );
}
}
}
- public static Msa createInstance( final List<Sequence> seqs ) {
+ public static Msa createInstance( final List<MolecularSequence> seqs ) {
if ( seqs.size() < 1 ) {
throw new IllegalArgumentException( "cannot create msa from less than one sequence" );
}
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 );
+ final MolecularSequence seq = seqs.get( row );
if ( seq.getLength() != length ) {
throw new IllegalArgumentException( "illegal attempt to build msa from sequences of unequal length ["
+ seq.getIdentifier() + "]" );