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: www.phylosoft.org/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.List;
33 import org.forester.sequence.Sequence;
34 import org.forester.sequence.Sequence.TYPE;
35 import org.forester.util.ForesterUtil;
37 public class BasicMsa implements Msa {
39 private final char[][] _data;
40 private final Object[] _identifiers;
41 private final TYPE _type;
43 public BasicMsa( final int rows, final int columns, final TYPE type ) {
44 if ( ( rows < 1 ) || ( columns < 1 ) ) {
45 throw new IllegalArgumentException( "basic msa of size zero are illegal" );
47 _data = new char[ rows ][ columns ];
48 _identifiers = new Object[ rows ];
52 BasicMsa( final BasicMsa msa ) {
54 _identifiers = msa._identifiers;
58 private int determineMaxIdLength() {
60 for( int row = 0; row < _data.length; ++row ) {
61 final int l = _identifiers[ row ].toString().length();
70 public Object getIdentifier( final int row ) {
71 return _identifiers[ row ];
75 public int getLength() {
76 return _data[ 0 ].length;
80 public int getNumberOfSequences() {
81 return _identifiers.length;
85 public char getResidueAt( final int row, final int col ) {
86 return _data[ row ][ col ];
90 public StringBuffer getSequenceAsString( final int row ) {
91 final StringBuffer sb = new StringBuffer( _data[ 0 ].length );
92 for( int col = 0; col < _data[ 0 ].length; ++col ) {
93 sb.append( getResidueAt( row, col ) );
99 public TYPE getType() {
104 public void setIdentifier( final int row, final Object id ) {
105 _identifiers[ row ] = id;
109 public void setResidueAt( final int row, final int col, final char residue ) {
110 _data[ row ][ col ] = residue;
114 public String toString() {
115 final int max = determineMaxIdLength() + 1;
116 final StringBuffer sb = new StringBuffer();
117 for( int row = 0; row < _data.length; ++row ) {
118 sb.append( ForesterUtil.pad( _identifiers[ row ].toString(), max, ' ', false ) );
119 for( int col = 0; col < _data[ 0 ].length; ++col ) {
120 sb.append( getResidueAt( row, col ) );
122 sb.append( ForesterUtil.LINE_SEPARATOR );
124 return sb.toString();
128 public void write( final Writer w ) throws IOException {
129 final int max = determineMaxIdLength() + 1;
130 for( int row = 0; row < _data.length; ++row ) {
131 w.write( ForesterUtil.pad( _identifiers[ row ].toString(), max, ' ', false ).toString() );
132 for( int col = 0; col < _data[ 0 ].length; ++col ) {
133 w.write( getResidueAt( row, col ) );
135 w.write( ForesterUtil.LINE_SEPARATOR );
139 public static Msa createInstance( final List<Sequence> seqs ) {
140 if ( seqs.size() < 1 ) {
141 throw new IllegalArgumentException( "cannot create basic msa from less than one sequence" );
143 final int length = seqs.get( 0 ).getLength();
144 final BasicMsa msa = new BasicMsa( seqs.size(), length, seqs.get( 0 ).getType() );
145 for( int row = 0; row < seqs.size(); ++row ) {
146 final Sequence seq = seqs.get( row );
147 if ( seq.getLength() != length ) {
148 throw new IllegalArgumentException( "illegal attempt to build msa from sequences of unequal length" );
150 if ( seq.getType() != msa.getType() ) {
151 throw new IllegalArgumentException( "illegal attempt to build msa from sequences of different type" );
153 msa.setIdentifier( row, seq.getIdentifier() );
154 for( int col = 0; col < length; ++col ) {
155 msa._data[ row ][ col ] = seq.getResidueAt( col );
162 public List<Character> getColumnAt( final int col ) {
163 final List<Character> column = new ArrayList<Character>();
164 for( int row = 0; row < getNumberOfSequences(); ++row ) {
165 column.add( getResidueAt( row, col ) );