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 < _data.length; ++row ) {
75 final int l = _identifiers[ row ].toString().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 ), _data[ row ], getType() );
119 public StringBuffer getSequenceAsString( final int row ) {
120 final StringBuffer sb = new StringBuffer( _data[ 0 ].length );
121 for( int col = 0; col < _data[ 0 ].length; ++col ) {
122 sb.append( getResidueAt( row, col ) );
128 public TYPE getType() {
133 public void setIdentifier( final int row, final String id ) {
134 _identifiers[ row ] = id;
138 public void setResidueAt( final int row, final int col, final char residue ) {
139 _data[ row ][ col ] = residue;
143 public String toString() {
144 final int max = determineMaxIdLength() + 1;
145 final StringBuffer sb = new StringBuffer();
146 for( int row = 0; row < _data.length; ++row ) {
147 sb.append( ForesterUtil.pad( _identifiers[ row ].toString(), max, ' ', false ) );
148 for( int col = 0; col < _data[ 0 ].length; ++col ) {
149 sb.append( getResidueAt( row, col ) );
151 sb.append( ForesterUtil.LINE_SEPARATOR );
153 return sb.toString();
157 public void write( final Writer w, final MSA_FORMAT format ) throws IOException {
166 throw new RuntimeException( "unknown format " + format );
170 private void writeToFasta( final Writer w ) throws IOException {
171 SequenceWriter.writeSeqs( asSequenceList(), w, SEQ_FORMAT.FASTA, 100 );
174 private void writeToPhylip( final Writer w ) throws IOException {
175 final int max = determineMaxIdLength() + 1;
176 for( int row = 0; row < _data.length; ++row ) {
177 w.write( ForesterUtil.pad( _identifiers[ row ].toString(), max, ' ', false ).toString() );
178 for( int col = 0; col < _data[ 0 ].length; ++col ) {
179 w.write( getResidueAt( row, col ) );
181 w.write( ForesterUtil.LINE_SEPARATOR );
185 public static Msa createInstance( final List<Sequence> seqs ) {
186 if ( seqs.size() < 1 ) {
187 throw new IllegalArgumentException( "cannot create basic msa from less than one sequence" );
189 final Set<String> ids = new HashSet<String>();
190 final int length = seqs.get( 0 ).getLength();
191 final BasicMsa msa = new BasicMsa( seqs.size(), length, seqs.get( 0 ).getType() );
192 for( int row = 0; row < seqs.size(); ++row ) {
193 final Sequence seq = seqs.get( row );
194 if ( seq.getLength() != length ) {
195 throw new IllegalArgumentException( "illegal attempt to build msa from sequences of unequal length" );
197 if ( seq.getType() != msa.getType() ) {
198 throw new IllegalArgumentException( "illegal attempt to build msa from sequences of different type" );
200 if ( ids.contains( seq.getIdentifier() ) ) {
201 throw new IllegalArgumentException( "illegal attempt to create msa with non-unique identifiers ["
202 + seq.getIdentifier() + "]" );
204 ids.add( seq.getIdentifier() );
205 msa.setIdentifier( row, seq.getIdentifier() );
206 for( int col = 0; col < length; ++col ) {
207 msa._data[ row ][ col ] = seq.getResidueAt( col );
214 public List<Character> getColumnAt( final int col ) {
215 final List<Character> column = new ArrayList<Character>();
216 for( int row = 0; row < getNumberOfSequences(); ++row ) {
217 column.add( getResidueAt( row, col ) );