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.HashSet;
32 import java.util.List;
33 import java.util.Set;
34
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;
39
40 public class BasicMsa implements Msa {
41
42     private final char[][] _data;
43     private final String[] _identifiers;
44     private final TYPE     _type;
45
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" );
49         }
50         _data = new char[ rows ][ columns ];
51         _identifiers = new String[ rows ];
52         _type = type;
53     }
54
55     BasicMsa( final BasicMsa msa ) {
56         _data = msa._data;
57         _identifiers = msa._identifiers;
58         _type = msa._type;
59     }
60
61     @Override
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 ) );
66         }
67         return seqs;
68     }
69
70     private int determineMaxIdLength() {
71         int max = 0;
72         for( int row = 0; row < _data.length; ++row ) {
73             final int l = _identifiers[ row ].toString().length();
74             if ( l > max ) {
75                 max = l;
76             }
77         }
78         return max;
79     }
80
81     @Override
82     public String getIdentifier( final int row ) {
83         return _identifiers[ row ];
84     }
85
86     @Override
87     public int getLength() {
88         return _data[ 0 ].length;
89     }
90
91     @Override
92     public int getNumberOfSequences() {
93         return _identifiers.length;
94     }
95
96     @Override
97     public char getResidueAt( final int row, final int col ) {
98         return _data[ row ][ col ];
99     }
100
101     @Override
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 );
106             }
107         }
108         return null;
109     }
110
111     @Override
112     public Sequence getSequence( final int row ) {
113         return new BasicSequence( getIdentifier( row ), _data[ row ], getType() );
114     }
115
116     @Override
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 ) );
121         }
122         return sb;
123     }
124
125     @Override
126     public TYPE getType() {
127         return _type;
128     }
129
130     @Override
131     public void setIdentifier( final int row, final String id ) {
132         _identifiers[ row ] = id;
133     }
134
135     @Override
136     public void setResidueAt( final int row, final int col, final char residue ) {
137         _data[ row ][ col ] = residue;
138     }
139
140     @Override
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 ) );
148             }
149             sb.append( ForesterUtil.LINE_SEPARATOR );
150         }
151         return sb.toString();
152     }
153
154     @Override
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 ) );
161             }
162             w.write( ForesterUtil.LINE_SEPARATOR );
163         }
164     }
165
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" );
169         }
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" );
177             }
178             if ( seq.getType() != msa.getType() ) {
179                 throw new IllegalArgumentException( "illegal attempt to build msa from sequences of different type" );
180             }
181             if ( ids.contains( seq.getIdentifier() ) ) {
182                 throw new IllegalArgumentException( "illegal attempt to create msa with non-unique identifiers ["
183                         + seq.getIdentifier() + "]" );
184             }
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 );
189             }
190         }
191         return msa;
192     }
193
194     @Override
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 ) );
199         }
200         return column;
201     }
202 }