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 TYPE _type;
49 public BasicMsa( final int rows, final int columns, final TYPE type ) {
50 if ( ( rows < 1 ) || ( columns < 1 ) ) {
51 throw new IllegalArgumentException( "basic msa of size zero are illegal" );
53 _data = new char[ rows ][ columns ];
54 _identifiers = new String[ rows ];
58 BasicMsa( final BasicMsa msa ) {
60 _identifiers = msa._identifiers;
65 public List<Sequence> asSequenceList() {
66 final List<Sequence> seqs = new ArrayList<Sequence>();
67 for( int i = 0; i < getNumberOfSequences(); ++i ) {
68 seqs.add( getSequence( i ) );
73 private int determineMaxIdLength() {
75 for( int row = 0; row < getNumberOfSequences(); ++row ) {
76 final int l = getIdentifier( row ).length();
85 public String getIdentifier( final int row ) {
86 return _identifiers[ row ];
90 public int getLength() {
91 return _data[ 0 ].length;
95 public int getNumberOfSequences() {
96 return _identifiers.length;
100 public char getResidueAt( final int row, final int col ) {
101 return _data[ row ][ col ];
105 public Sequence getSequence( final String id ) {
106 for( int i = 0; i < getNumberOfSequences(); ++i ) {
107 if ( getIdentifier( i ).equals( id ) ) {
108 return getSequence( i );
115 public Sequence getSequence( final int row ) {
116 return new BasicSequence( getIdentifier( row ), _data[ row ], getType() );
120 public StringBuffer getSequenceAsString( final int row ) {
121 final StringBuffer sb = new StringBuffer( getLength() );
122 for( int col = 0; col < getLength(); ++col ) {
123 sb.append( getResidueAt( row, col ) );
129 public TYPE getType() {
134 public void setIdentifier( final int row, final String id ) {
135 _identifiers[ row ] = id;
139 public void setResidueAt( final int row, final int col, final char residue ) {
140 _data[ row ][ col ] = residue;
144 public String toString() {
145 final Writer w = new StringWriter();
147 write( w, MSA_FORMAT.PHYLIP );
149 catch ( final IOException e ) {
156 public void write( final Writer w, final MSA_FORMAT format ) throws IOException {
165 throw new RuntimeException( "unknown format " + format );
169 private void writeToFasta( final Writer w ) throws IOException {
170 SequenceWriter.writeSeqs( asSequenceList(), w, SEQ_FORMAT.FASTA, 100 );
173 private void writeToPhylip( final Writer w ) throws IOException {
174 final int max = determineMaxIdLength() + 1;
175 for( int row = 0; row < getNumberOfSequences(); ++row ) {
176 w.write( ForesterUtil.pad( getIdentifier( row ), max, ' ', false ).toString() );
177 for( int col = 0; col < getLength(); ++col ) {
178 w.write( getResidueAt( row, col ) );
180 w.write( ForesterUtil.LINE_SEPARATOR );
184 public static Msa createInstance( final List<Sequence> seqs ) {
185 if ( seqs.size() < 1 ) {
186 throw new IllegalArgumentException( "cannot create basic msa from less than one sequence" );
188 final Set<String> ids = new HashSet<String>();
189 final int length = seqs.get( 0 ).getLength();
190 final BasicMsa msa = new BasicMsa( seqs.size(), length, seqs.get( 0 ).getType() );
191 for( int row = 0; row < seqs.size(); ++row ) {
192 final Sequence seq = seqs.get( row );
193 if ( seq.getLength() != length ) {
194 throw new IllegalArgumentException( "illegal attempt to build msa from sequences of unequal length ["
195 + seq.getIdentifier() + "]" );
197 if ( seq.getType() != msa.getType() ) {
198 throw new IllegalArgumentException( "illegal attempt to build msa from sequences of different type ["
199 + seq.getIdentifier() + "]" );
201 if ( ids.contains( seq.getIdentifier() ) ) {
202 throw new IllegalArgumentException( "illegal attempt to create msa with non-unique identifiers ["
203 + seq.getIdentifier() + "]" );
205 ids.add( seq.getIdentifier() );
206 msa.setIdentifier( row, seq.getIdentifier() );
207 for( int col = 0; col < length; ++col ) {
208 msa._data[ row ][ col ] = seq.getResidueAt( col );
215 public List<Character> getColumnAt( final int col ) {
216 final List<Character> column = new ArrayList<Character>();
217 for( int row = 0; row < getNumberOfSequences(); ++row ) {
218 column.add( getResidueAt( row, col ) );
224 public boolean isGapAt( final int row, final int col ) {
225 return getResidueAt( row, col ) == Sequence.GAP;