in progress
[jalview.git] / forester / java / src / org / forester / msa / BasicMsa.java
1 // $Id:
2 // forester -- software libraries and applications
3 // for genomics and evolutionary biology research.
4 //
5 // Copyright (C) 2010 Christian M Zmasek
6 // Copyright (C) 2010 Sanford-Burnham Medical Research Institute
7 // All rights reserved
8 //
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.
13 //
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.
18 //
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
22 //
23 // Contact: phylosoft @ gmail . com
24 // WWW: www.phylosoft.org/forester
25
26 package org.forester.msa;
27
28 import java.io.IOException;
29 import java.io.Writer;
30 import java.util.ArrayList;
31 import java.util.List;
32
33 import org.forester.sequence.Sequence;
34 import org.forester.sequence.Sequence.TYPE;
35 import org.forester.util.ForesterUtil;
36
37 public class BasicMsa implements Msa {
38
39     private final char[][] _data;
40     private final Object[] _identifiers;
41     private final TYPE     _type;
42
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" );
46         }
47         _data = new char[ rows ][ columns ];
48         _identifiers = new Object[ rows ];
49         _type = type;
50     }
51
52     BasicMsa( final BasicMsa msa ) {
53         _data = msa._data;
54         _identifiers = msa._identifiers;
55         _type = msa._type;
56     }
57
58     private int determineMaxIdLength() {
59         int max = 0;
60         for( int row = 0; row < _data.length; ++row ) {
61             final int l = _identifiers[ row ].toString().length();
62             if ( l > max ) {
63                 max = l;
64             }
65         }
66         return max;
67     }
68
69     @Override
70     public Object getIdentifier( final int row ) {
71         return _identifiers[ row ];
72     }
73
74     @Override
75     public int getLength() {
76         return _data[ 0 ].length;
77     }
78
79     @Override
80     public int getNumberOfSequences() {
81         return _identifiers.length;
82     }
83
84     @Override
85     public char getResidueAt( final int row, final int col ) {
86         return _data[ row ][ col ];
87     }
88
89     @Override
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 ) );
94         }
95         return sb;
96     }
97
98     @Override
99     public TYPE getType() {
100         return _type;
101     }
102
103     @Override
104     public void setIdentifier( final int row, final Object id ) {
105         _identifiers[ row ] = id;
106     }
107
108     @Override
109     public void setResidueAt( final int row, final int col, final char residue ) {
110         _data[ row ][ col ] = residue;
111     }
112
113     @Override
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 ) );
121             }
122             sb.append( ForesterUtil.LINE_SEPARATOR );
123         }
124         return sb.toString();
125     }
126
127     @Override
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 ) );
134             }
135             w.write( ForesterUtil.LINE_SEPARATOR );
136         }
137     }
138
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" );
142         }
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" );
149             }
150             if ( seq.getType() != msa.getType() ) {
151                 throw new IllegalArgumentException( "illegal attempt to build msa from sequences of different type" );
152             }
153             msa.setIdentifier( row, seq.getIdentifier() );
154             for( int col = 0; col < length; ++col ) {
155                 msa._data[ row ][ col ] = seq.getResidueAt( col );
156             }
157         }
158         return msa;
159     }
160
161     @Override
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 ) );
166         }
167         return column;
168     }
169 }