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.HashSet;
32 import java.util.List;
35 import org.forester.sequence.BasicSequence;
36 import org.forester.sequence.Sequence;
37 import org.forester.sequence.Sequence.TYPE;
38 import org.forester.util.ForesterUtil;
40 public class BasicMsa implements Msa {
42 private final char[][] _data;
43 private final String[] _identifiers;
44 private final TYPE _type;
46 public BasicMsa( final int rows, final int columns, final TYPE type ) {
47 if ( ( rows < 1 ) || ( columns < 1 ) ) {
48 throw new IllegalArgumentException( "basic msa of size zero are illegal" );
50 _data = new char[ rows ][ columns ];
51 _identifiers = new String[ rows ];
55 BasicMsa( final BasicMsa msa ) {
57 _identifiers = msa._identifiers;
62 public List<Sequence> asSequenceList() {
63 final List<Sequence> seqs = new ArrayList<Sequence>();
64 for( int i = 0; i < getNumberOfSequences(); ++i ) {
65 seqs.add( getSequence( i ) );
70 private int determineMaxIdLength() {
72 for( int row = 0; row < _data.length; ++row ) {
73 final int l = _identifiers[ row ].toString().length();
82 public String getIdentifier( final int row ) {
83 return _identifiers[ row ];
87 public int getLength() {
88 return _data[ 0 ].length;
92 public int getNumberOfSequences() {
93 return _identifiers.length;
97 public char getResidueAt( final int row, final int col ) {
98 return _data[ row ][ col ];
102 public Sequence getSequence( final String id ) {
103 for( int i = 0; i < getNumberOfSequences(); ++i ) {
104 if ( getIdentifier( i ).equals( id ) ) {
105 return getSequence( i );
112 public Sequence getSequence( final int row ) {
113 return new BasicSequence( getIdentifier( row ), _data[ row ], getType() );
117 public StringBuffer getSequenceAsString( final int row ) {
118 final StringBuffer sb = new StringBuffer( _data[ 0 ].length );
119 for( int col = 0; col < _data[ 0 ].length; ++col ) {
120 sb.append( getResidueAt( row, col ) );
126 public TYPE getType() {
131 public void setIdentifier( final int row, final String id ) {
132 _identifiers[ row ] = id;
136 public void setResidueAt( final int row, final int col, final char residue ) {
137 _data[ row ][ col ] = residue;
141 public String toString() {
142 final int max = determineMaxIdLength() + 1;
143 final StringBuffer sb = new StringBuffer();
144 for( int row = 0; row < _data.length; ++row ) {
145 sb.append( ForesterUtil.pad( _identifiers[ row ].toString(), max, ' ', false ) );
146 for( int col = 0; col < _data[ 0 ].length; ++col ) {
147 sb.append( getResidueAt( row, col ) );
149 sb.append( ForesterUtil.LINE_SEPARATOR );
151 return sb.toString();
155 public void write( final Writer w ) throws IOException {
156 final int max = determineMaxIdLength() + 1;
157 for( int row = 0; row < _data.length; ++row ) {
158 w.write( ForesterUtil.pad( _identifiers[ row ].toString(), max, ' ', false ).toString() );
159 for( int col = 0; col < _data[ 0 ].length; ++col ) {
160 w.write( getResidueAt( row, col ) );
162 w.write( ForesterUtil.LINE_SEPARATOR );
166 public static Msa createInstance( final List<Sequence> seqs ) {
167 if ( seqs.size() < 1 ) {
168 throw new IllegalArgumentException( "cannot create basic msa from less than one sequence" );
170 final Set<String> ids = new HashSet<String>();
171 final int length = seqs.get( 0 ).getLength();
172 final BasicMsa msa = new BasicMsa( seqs.size(), length, seqs.get( 0 ).getType() );
173 for( int row = 0; row < seqs.size(); ++row ) {
174 final Sequence seq = seqs.get( row );
175 if ( seq.getLength() != length ) {
176 throw new IllegalArgumentException( "illegal attempt to build msa from sequences of unequal length" );
178 if ( seq.getType() != msa.getType() ) {
179 throw new IllegalArgumentException( "illegal attempt to build msa from sequences of different type" );
181 if ( ids.contains( seq.getIdentifier() ) ) {
182 throw new IllegalArgumentException( "illegal attempt to create msa with non-unique identifiers ["
183 + seq.getIdentifier() + "]" );
185 ids.add( seq.getIdentifier() );
186 msa.setIdentifier( row, seq.getIdentifier() );
187 for( int col = 0; col < length; ++col ) {
188 msa._data[ row ][ col ] = seq.getResidueAt( col );
195 public List<Character> getColumnAt( final int col ) {
196 final List<Character> column = new ArrayList<Character>();
197 for( int row = 0; row < getNumberOfSequences(); ++row ) {
198 column.add( getResidueAt( row, col ) );