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.Writer;
30 import java.util.ArrayList;
31 import java.util.HashSet;
32 import java.util.List;
35 import org.forester.io.writers.SequenceWriter;
36 import org.forester.io.writers.SequenceWriter.SEQ_FORMAT;
37 import org.forester.sequence.BasicSequence;
38 import org.forester.sequence.Sequence;
39 import org.forester.sequence.Sequence.TYPE;
40 import org.forester.util.ForesterUtil;
42 public class BasicMsa implements Msa {
44 private final char[][] _data;
45 private final String[] _identifiers;
46 private final TYPE _type;
48 public BasicMsa( final int rows, final int columns, final TYPE type ) {
49 if ( ( rows < 1 ) || ( columns < 1 ) ) {
50 throw new IllegalArgumentException( "basic msa of size zero are illegal" );
52 _data = new char[ rows ][ columns ];
53 _identifiers = new String[ rows ];
57 BasicMsa( final BasicMsa msa ) {
59 _identifiers = msa._identifiers;
64 public List<Sequence> asSequenceList() {
65 final List<Sequence> seqs = new ArrayList<Sequence>();
66 for( int i = 0; i < getNumberOfSequences(); ++i ) {
67 seqs.add( getSequence( i ) );
72 private int determineMaxIdLength() {
74 for( int row = 0; row < getNumberOfSequences(); ++row ) {
75 final int l = getIdentifier(row).length();
84 public String getIdentifier( final int row ) {
85 return _identifiers[ row ];
89 public int getLength() {
90 return _data[ 0 ].length;
94 public int getNumberOfSequences() {
95 return _identifiers.length;
99 public char getResidueAt( final int row, final int col ) {
100 return _data[ row ][ col ];
104 public Sequence getSequence( final String id ) {
105 for( int i = 0; i < getNumberOfSequences(); ++i ) {
106 if ( getIdentifier( i ).equals( id ) ) {
107 return getSequence( i );
114 public Sequence getSequence( final int row ) {
115 return new BasicSequence( getIdentifier( row ), getSequenceAsArray( row ), getType() );
119 public StringBuffer getSequenceAsString( final int row ) {
120 final StringBuffer sb = new StringBuffer(getLength() );
121 for( int col = 0; col < getLength(); ++col ) {
122 sb.append( getResidueAt( row, col ) );
128 public char[] getSequenceAsArray( final int row ) {
133 public TYPE getType() {
138 public void setIdentifier( final int row, final String id ) {
139 _identifiers[ row ] = id;
143 public void setResidueAt( final int row, final int col, final char residue ) {
144 _data[ row ][ col ] = residue;
148 public String toString() {
149 final int max = determineMaxIdLength() + 1;
150 final StringBuffer sb = new StringBuffer();
151 for( int row = 0; row < getNumberOfSequences(); ++row ) {
152 sb.append( ForesterUtil.pad( getIdentifier( row ).toString(), max, ' ', false ) );
153 for( int col = 0; col < getLength(); ++col ) {
154 sb.append( getResidueAt( row, col ) );
156 sb.append( ForesterUtil.LINE_SEPARATOR );
158 return sb.toString();
162 public void write( final Writer w, final MSA_FORMAT format ) throws IOException {
171 throw new RuntimeException( "unknown format " + format );
175 private void writeToFasta( final Writer w ) throws IOException {
176 SequenceWriter.writeSeqs( asSequenceList(), w, SEQ_FORMAT.FASTA, 100 );
179 private void writeToPhylip( final Writer w ) throws IOException {
180 final int max = determineMaxIdLength() + 1;
181 for( int row = 0; row < getNumberOfSequences(); ++row ) {
182 w.write( ForesterUtil.pad( getIdentifier( row ), max, ' ', false ).toString() );
183 for( int col = 0; col < getLength(); ++col ) {
184 w.write( getResidueAt( row, col ) );
186 w.write( ForesterUtil.LINE_SEPARATOR );
190 public static Msa createInstance( final List<Sequence> seqs ) {
191 if ( seqs.size() < 1 ) {
192 throw new IllegalArgumentException( "cannot create basic msa from less than one sequence" );
194 final Set<String> ids = new HashSet<String>();
195 final int length = seqs.get( 0 ).getLength();
196 final BasicMsa msa = new BasicMsa( seqs.size(), length, seqs.get( 0 ).getType() );
197 for( int row = 0; row < seqs.size(); ++row ) {
198 final Sequence seq = seqs.get( row );
200 // int x = length - seq.getLength();
203 // for( int i = 0; i < x; i++ ) {
206 // seq = BasicSequence.createAaSequence( seq.getIdentifier(), seq.getMolecularSequenceAsString() + a );
209 // seq = BasicSequence.createAaSequence( seq.getIdentifier(), seq.getMolecularSequenceAsString()
210 // .substring( 0, length ) );
213 if ( seq.getLength() != length ) {
214 throw new IllegalArgumentException( "illegal attempt to build msa from sequences of unequal length ["
215 + seq.getIdentifier() + "]" );
217 if ( seq.getType() != msa.getType() ) {
218 throw new IllegalArgumentException( "illegal attempt to build msa from sequences of different type ["
219 + seq.getIdentifier() + "]" );
221 if ( ids.contains( seq.getIdentifier() ) ) {
222 throw new IllegalArgumentException( "illegal attempt to create msa with non-unique identifiers ["
223 + seq.getIdentifier() + "]" );
225 ids.add( seq.getIdentifier() );
226 msa.setIdentifier( row, seq.getIdentifier() );
227 for( int col = 0; col < length; ++col ) {
228 msa._data[ row ][ col ] = seq.getResidueAt( col );
235 public List<Character> getColumnAt( final int col ) {
236 final List<Character> column = new ArrayList<Character>();
237 for( int row = 0; row < getNumberOfSequences(); ++row ) {
238 column.add( getResidueAt( row, col ) );
244 public boolean isGapAt( final int row, final int col ) {
245 return getResidueAt( row, col ) == Sequence.GAP;