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.List;
32 import org.forester.sequence.Sequence;
33 import org.forester.sequence.Sequence.TYPE;
34 import org.forester.util.ForesterUtil;
36 public class BasicMsa implements Msa {
38 private final char[][] _data;
39 private final Object[] _identifiers;
40 private final TYPE _type;
42 public BasicMsa( final int rows, final int columns, final TYPE type ) {
43 if ( ( rows < 1 ) || ( columns < 1 ) ) {
44 throw new IllegalArgumentException( "basic msa of size zero are illegal" );
46 _data = new char[ rows ][ columns ];
47 _identifiers = new Object[ rows ];
51 BasicMsa( final BasicMsa msa ) {
53 _identifiers = msa._identifiers;
57 private int determineMaxIdLength() {
59 for( int row = 0; row < _data.length; ++row ) {
60 final int l = _identifiers[ row ].toString().length();
69 public Object getIdentifier( final int row ) {
70 return _identifiers[ row ];
74 public int getLength() {
75 return _data[ 0 ].length;
79 public int getNumberOfSequences() {
80 return _identifiers.length;
84 public char getResidueAt( final int row, final int col ) {
85 return _data[ row ][ col ];
89 public StringBuffer getSequenceAsString( final int row ) {
90 final StringBuffer sb = new StringBuffer( _data[ 0 ].length );
91 for( int col = 0; col < _data[ 0 ].length; ++col ) {
92 sb.append( getResidueAt( row, col ) );
98 public TYPE getType() {
103 public void setIdentifier( final int row, final Object id ) {
104 _identifiers[ row ] = id;
108 public void setResidueAt( final int row, final int col, final char residue ) {
109 _data[ row ][ col ] = residue;
113 public String toString() {
114 final int max = determineMaxIdLength() + 1;
115 final StringBuffer sb = new StringBuffer();
116 for( int row = 0; row < _data.length; ++row ) {
117 sb.append( ForesterUtil.pad( _identifiers[ row ].toString(), max, ' ', false ) );
118 for( int col = 0; col < _data[ 0 ].length; ++col ) {
119 sb.append( getResidueAt( row, col ) );
121 sb.append( ForesterUtil.LINE_SEPARATOR );
123 return sb.toString();
127 public void write( final Writer w ) throws IOException {
128 final int max = determineMaxIdLength() + 1;
129 for( int row = 0; row < _data.length; ++row ) {
130 w.write( ForesterUtil.pad( _identifiers[ row ].toString(), max, ' ', false ).toString() );
131 for( int col = 0; col < _data[ 0 ].length; ++col ) {
132 w.write( getResidueAt( row, col ) );
134 w.write( ForesterUtil.LINE_SEPARATOR );
138 public static Msa createInstance( final List<Sequence> seqs ) {
139 if ( seqs.size() < 1 ) {
140 throw new IllegalArgumentException( "cannot create basic msa from less than one sequence" );
142 final int length = seqs.get( 0 ).getLength();
143 final BasicMsa msa = new BasicMsa( seqs.size(), length, seqs.get( 0 ).getType() );
144 for( int row = 0; row < seqs.size(); ++row ) {
145 final Sequence seq = seqs.get( row );
146 if ( seq.getLength() != length ) {
147 throw new IllegalArgumentException( "illegal attempt to build msa from sequences of unequal length" );
149 if ( seq.getType() != msa.getType() ) {
150 throw new IllegalArgumentException( "illegal attempt to build msa from sequences of different type" );
152 msa.setIdentifier( row, seq.getIdentifier() );
153 for( int col = 0; col < length; ++col ) {
154 msa._data[ row ][ col ] = seq.getResidueAt( col );