2 // forester -- software libraries and applications
3 // for genomics and evolutionary biology research.
5 // Copyright (C) 2010 Christian M Zmasek
6 // Copyright (C) 2010 Sanford-Burnham Medical Research Institute
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 // Contact: phylosoft @ gmail . com
24 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
26 package org.forester.msa;
28 import java.io.IOException;
29 import java.io.StringWriter;
30 import java.io.Writer;
31 import java.util.ArrayList;
32 import java.util.HashSet;
33 import java.util.List;
36 import org.forester.io.writers.SequenceWriter;
37 import org.forester.io.writers.SequenceWriter.SEQ_FORMAT;
38 import org.forester.sequence.BasicSequence;
39 import org.forester.sequence.Sequence;
40 import org.forester.sequence.Sequence.TYPE;
41 import org.forester.util.ForesterUtil;
43 public class BasicMsa implements Msa {
45 private final char[][] _data;
46 private final String[] _identifiers;
47 private final Set<String> _identifiers_set;
48 private final TYPE _type;
50 public BasicMsa( final int rows, final int columns, final TYPE type ) {
51 if ( ( rows < 1 ) || ( columns < 1 ) ) {
52 throw new IllegalArgumentException( "basic msa of size zero are illegal" );
54 _data = new char[ rows ][ columns ];
55 _identifiers = new String[ rows ];
56 _identifiers_set = new HashSet<String>();
60 BasicMsa( final BasicMsa msa ) {
62 _identifiers = msa._identifiers;
64 _identifiers_set = msa._identifiers_set;
68 public List<Sequence> asSequenceList() {
69 final List<Sequence> seqs = new ArrayList<Sequence>();
70 for( int i = 0; i < getNumberOfSequences(); ++i ) {
71 seqs.add( getSequence( i ) );
77 public List<Character> getColumnAt( final int col ) {
78 final List<Character> column = new ArrayList<Character>();
79 for( int row = 0; row < getNumberOfSequences(); ++row ) {
80 column.add( getResidueAt( row, col ) );
86 public String getIdentifier( final int row ) {
87 return _identifiers[ row ];
91 public int getLength() {
92 return _data[ 0 ].length;
96 public int getNumberOfSequences() {
97 return _identifiers.length;
101 public char getResidueAt( final int row, final int col ) {
102 return _data[ row ][ col ];
106 public Sequence getSequence( final int row ) {
107 return new BasicSequence( getIdentifier( row ), _data[ row ], getType() );
111 public Sequence getSequence( final String id ) {
112 for( int i = 0; i < getNumberOfSequences(); ++i ) {
113 if ( getIdentifier( i ).equals( id ) ) {
114 return getSequence( i );
121 public StringBuffer getSequenceAsString( final int row ) {
122 final StringBuffer sb = new StringBuffer( getLength() );
123 for( int col = 0; col < getLength(); ++col ) {
124 sb.append( getResidueAt( row, col ) );
130 public TYPE getType() {
135 public boolean isGapAt( final int row, final int col ) {
136 return getResidueAt( row, col ) == Sequence.GAP;
140 public void setIdentifier( final int row, final String id ) {
141 if ( ForesterUtil.isEmpty( id ) ) {
142 throw new IllegalArgumentException( "illegal attempt to create msa with empty identifier" );
144 if ( _identifiers_set.contains( id ) ) {
145 throw new IllegalArgumentException( "illegal attempt to create msa with non-unique identifiers [" + id
148 _identifiers_set.add( id );
149 _identifiers[ row ] = id;
153 public void setResidueAt( final int row, final int col, final char residue ) {
154 _data[ row ][ col ] = residue;
158 public String toString() {
159 final Writer w = new StringWriter();
161 write( w, MSA_FORMAT.PHYLIP );
163 catch ( final IOException e ) {
170 public void write( final Writer w, final MSA_FORMAT format ) throws IOException {
179 throw new RuntimeException( "unknown format " + format );
183 private short determineMaxIdLength() {
185 for( int row = 0; row < getNumberOfSequences(); ++row ) {
186 final short l = ( short ) getIdentifier( row ).length();
194 private void writeToFasta( final Writer w ) throws IOException {
195 SequenceWriter.writeSeqs( asSequenceList(), w, SEQ_FORMAT.FASTA, 100 );
198 private void writeToPhylip( final Writer w ) throws IOException {
199 final int max = determineMaxIdLength() + 1;
200 for( int row = 0; row < getNumberOfSequences(); ++row ) {
201 w.write( ForesterUtil.pad( getIdentifier( row ), max, ' ', false ).toString() );
202 for( int col = 0; col < getLength(); ++col ) {
203 w.write( getResidueAt( row, col ) );
205 w.write( ForesterUtil.LINE_SEPARATOR );
209 public static Msa createInstance( final List<Sequence> seqs ) {
210 if ( seqs.size() < 1 ) {
211 throw new IllegalArgumentException( "cannot create msa from less than one sequence" );
213 final int length = seqs.get( 0 ).getLength();
214 final BasicMsa msa = new BasicMsa( seqs.size(), length, seqs.get( 0 ).getType() );
215 for( int row = 0; row < seqs.size(); ++row ) {
216 final Sequence seq = seqs.get( row );
217 if ( seq.getLength() != length ) {
218 throw new IllegalArgumentException( "illegal attempt to build msa from sequences of unequal length ["
219 + seq.getIdentifier() + "]" );
221 if ( seq.getType() != msa.getType() ) {
222 throw new IllegalArgumentException( "illegal attempt to build msa from sequences of different type ["
223 + seq.getIdentifier() + "]" );
225 msa.setIdentifier( row, seq.getIdentifier() );
226 for( int col = 0; col < length; ++col ) {
227 msa._data[ row ][ col ] = seq.getResidueAt( col );