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.List;
31
32 import org.forester.sequence.Sequence;
33 import org.forester.sequence.Sequence.TYPE;
34 import org.forester.util.ForesterUtil;
35
36 public class BasicMsa implements Msa {
37
38     private final char[][] _data;
39     private final Object[] _identifiers;
40     private final TYPE     _type;
41
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" );
45         }
46         _data = new char[ rows ][ columns ];
47         _identifiers = new Object[ rows ];
48         _type = type;
49     }
50
51     BasicMsa( final BasicMsa msa ) {
52         _data = msa._data;
53         _identifiers = msa._identifiers;
54         _type = msa._type;
55     }
56
57     private int determineMaxIdLength() {
58         int max = 0;
59         for( int row = 0; row < _data.length; ++row ) {
60             final int l = _identifiers[ row ].toString().length();
61             if ( l > max ) {
62                 max = l;
63             }
64         }
65         return max;
66     }
67
68     @Override
69     public Object getIdentifier( final int row ) {
70         return _identifiers[ row ];
71     }
72
73     @Override
74     public int getLength() {
75         return _data[ 0 ].length;
76     }
77
78     @Override
79     public int getNumberOfSequences() {
80         return _identifiers.length;
81     }
82
83     @Override
84     public char getResidueAt( final int row, final int col ) {
85         return _data[ row ][ col ];
86     }
87
88     @Override
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 ) );
93         }
94         return sb;
95     }
96
97     @Override
98     public TYPE getType() {
99         return _type;
100     }
101
102     @Override
103     public void setIdentifier( final int row, final Object id ) {
104         _identifiers[ row ] = id;
105     }
106
107     @Override
108     public void setResidueAt( final int row, final int col, final char residue ) {
109         _data[ row ][ col ] = residue;
110     }
111
112     @Override
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 ) );
120             }
121             sb.append( ForesterUtil.LINE_SEPARATOR );
122         }
123         return sb.toString();
124     }
125
126     @Override
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 ) );
133             }
134             w.write( ForesterUtil.LINE_SEPARATOR );
135         }
136     }
137
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" );
141         }
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" );
148             }
149             if ( seq.getType() != msa.getType() ) {
150                 throw new IllegalArgumentException( "illegal attempt to build msa from sequences of different type" );
151             }
152             msa.setIdentifier( row, seq.getIdentifier() );
153             for( int col = 0; col < length; ++col ) {
154                 msa._data[ row ][ col ] = seq.getResidueAt( col );
155             }
156         }
157         return msa;
158     }
159 }