bda642466b2b6aaecd05b3a487f2def2e46697b0
[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: https://sites.google.com/site/cmzmasek/home/software/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.io.writers.SequenceWriter;
36 import org.forester.io.writers.SequenceWriter.SEQ_FORMAT;
37 import org.forester.sequence.BasicSequence;
38 import org.forester.sequence.Sequence;
39 import org.forester.sequence.Sequence.TYPE;
40 import org.forester.util.ForesterUtil;
41
42 public class BasicMsa implements Msa {
43
44     private final char[][] _data;
45     private final String[] _identifiers;
46     private final TYPE     _type;
47
48     public BasicMsa( final int rows, final int columns, final TYPE type ) {
49         if ( ( rows < 1 ) || ( columns < 1 ) ) {
50             throw new IllegalArgumentException( "basic msa of size zero are illegal" );
51         }
52         _data = new char[ rows ][ columns ];
53         _identifiers = new String[ rows ];
54         _type = type;
55     }
56
57     BasicMsa( final BasicMsa msa ) {
58         _data = msa._data;
59         _identifiers = msa._identifiers;
60         _type = msa._type;
61     }
62
63     @Override
64     public List<Sequence> asSequenceList() {
65         final List<Sequence> seqs = new ArrayList<Sequence>();
66         for( int i = 0; i < getNumberOfSequences(); ++i ) {
67             seqs.add( getSequence( i ) );
68         }
69         return seqs;
70     }
71
72     private int determineMaxIdLength() {
73         int max = 0;
74         for( int row = 0; row < _data.length; ++row ) {
75             final int l = _identifiers[ row ].toString().length();
76             if ( l > max ) {
77                 max = l;
78             }
79         }
80         return max;
81     }
82
83     @Override
84     public String getIdentifier( final int row ) {
85         return _identifiers[ row ];
86     }
87
88     @Override
89     public int getLength() {
90         return _data[ 0 ].length;
91     }
92
93     @Override
94     public int getNumberOfSequences() {
95         return _identifiers.length;
96     }
97
98     @Override
99     public char getResidueAt( final int row, final int col ) {
100         return _data[ row ][ col ];
101     }
102
103     @Override
104     public Sequence getSequence( final String id ) {
105         for( int i = 0; i < getNumberOfSequences(); ++i ) {
106             if ( getIdentifier( i ).equals( id ) ) {
107                 return getSequence( i );
108             }
109         }
110         return null;
111     }
112
113     @Override
114     public Sequence getSequence( final int row ) {
115         return new BasicSequence( getIdentifier( row ), _data[ row ], getType() );
116     }
117
118     @Override
119     public StringBuffer getSequenceAsString( final int row ) {
120         final StringBuffer sb = new StringBuffer( _data[ 0 ].length );
121         for( int col = 0; col < _data[ 0 ].length; ++col ) {
122             sb.append( getResidueAt( row, col ) );
123         }
124         return sb;
125     }
126
127     @Override
128     public TYPE getType() {
129         return _type;
130     }
131
132     @Override
133     public void setIdentifier( final int row, final String id ) {
134         _identifiers[ row ] = id;
135     }
136
137     @Override
138     public void setResidueAt( final int row, final int col, final char residue ) {
139         _data[ row ][ col ] = residue;
140     }
141
142     @Override
143     public String toString() {
144         final int max = determineMaxIdLength() + 1;
145         final StringBuffer sb = new StringBuffer();
146         for( int row = 0; row < _data.length; ++row ) {
147             sb.append( ForesterUtil.pad( _identifiers[ row ].toString(), max, ' ', false ) );
148             for( int col = 0; col < _data[ 0 ].length; ++col ) {
149                 sb.append( getResidueAt( row, col ) );
150             }
151             sb.append( ForesterUtil.LINE_SEPARATOR );
152         }
153         return sb.toString();
154     }
155
156     @Override
157     public void write( final Writer w, final MSA_FORMAT format ) throws IOException {
158         switch ( format ) {
159             case PHYLIP:
160                 writeToPhylip( w );
161                 break;
162             case FASTA:
163                 writeToFasta( w );
164                 break;
165             default:
166                 throw new RuntimeException( "unknown format " + format );
167         }
168     }
169
170     private void writeToFasta( final Writer w ) throws IOException {
171         SequenceWriter.writeSeqs( asSequenceList(), w, SEQ_FORMAT.FASTA, 100 );
172     }
173
174     private void writeToPhylip( final Writer w ) throws IOException {
175         final int max = determineMaxIdLength() + 1;
176         for( int row = 0; row < _data.length; ++row ) {
177             w.write( ForesterUtil.pad( _identifiers[ row ].toString(), max, ' ', false ).toString() );
178             for( int col = 0; col < _data[ 0 ].length; ++col ) {
179                 w.write( getResidueAt( row, col ) );
180             }
181             w.write( ForesterUtil.LINE_SEPARATOR );
182         }
183     }
184
185     public static Msa createInstance( final List<Sequence> seqs ) {
186         if ( seqs.size() < 1 ) {
187             throw new IllegalArgumentException( "cannot create basic msa from less than one sequence" );
188         }
189         final Set<String> ids = new HashSet<String>();
190         final int length = seqs.get( 0 ).getLength();
191         final BasicMsa msa = new BasicMsa( seqs.size(), length, seqs.get( 0 ).getType() );
192         for( int row = 0; row < seqs.size(); ++row ) {
193             final Sequence seq = seqs.get( row );
194             if ( seq.getLength() != length ) {
195                 throw new IllegalArgumentException( "illegal attempt to build msa from sequences of unequal length ["
196                         + seq.getIdentifier() + "]" );
197             }
198             if ( seq.getType() != msa.getType() ) {
199                 throw new IllegalArgumentException( "illegal attempt to build msa from sequences of different type ["
200                         + seq.getIdentifier() + "]" );
201             }
202             if ( ids.contains( seq.getIdentifier() ) ) {
203                 throw new IllegalArgumentException( "illegal attempt to create msa with non-unique identifiers ["
204                         + seq.getIdentifier() + "]" );
205             }
206             ids.add( seq.getIdentifier() );
207             msa.setIdentifier( row, seq.getIdentifier() );
208             for( int col = 0; col < length; ++col ) {
209                 msa._data[ row ][ col ] = seq.getResidueAt( col );
210             }
211         }
212         return msa;
213     }
214
215     @Override
216     public List<Character> getColumnAt( final int col ) {
217         final List<Character> column = new ArrayList<Character>();
218         for( int row = 0; row < getNumberOfSequences(); ++row ) {
219             column.add( getResidueAt( row, col ) );
220         }
221         return column;
222     }
223
224     @Override
225     public boolean isGapAt( final int row, final int col ) {
226         return getResidueAt( row, col ) == Sequence.GAP;
227     }
228 }