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