inprogress
[jalview.git] / forester / java / src / org / forester / msa / DeleteableMsa.java
1 // / $Id:
2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
4 //
5 // Copyright (C) 2014 Christian M. Zmasek
6 // Copyright (C) 2014 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 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
24
25 package org.forester.msa;
26
27 import java.util.List;
28
29 import org.forester.sequence.BasicSequence;
30 import org.forester.sequence.Sequence;
31
32 public final class DeleteableMsa extends BasicMsa {
33
34     private int _length                 = 0;
35     private int _mapped_col_positions[] = null;
36     private int _mapped_row_positions[] = null;
37     private int _seqs                   = 0;
38
39     private DeleteableMsa( final BasicMsa msa ) {
40         super( msa );
41         _mapped_col_positions = new int[ msa.getLength() ];
42         _mapped_row_positions = new int[ msa.getNumberOfSequences() ];
43         for( int i = 0; i < _mapped_col_positions.length; ++i ) {
44             _mapped_col_positions[ i ] = i;
45         }
46         for( int i = 0; i < _mapped_row_positions.length; ++i ) {
47             _mapped_row_positions[ i ] = i;
48         }
49         _length = msa.getLength();
50         _seqs = msa.getNumberOfSequences();
51     }
52
53     public short determineMaxIdLength() {
54         short max = 0;
55         for( int row = 0; row < getNumberOfSequences(); ++row ) {
56             final short l = ( short ) getIdentifier( row ).length();
57             if ( l > max ) {
58                 max = l;
59             }
60         }
61         return max;
62     }
63
64     final public void deleteGapColumns( final double max_allowed_gap_ratio ) {
65         if ( ( max_allowed_gap_ratio < 0 ) || ( max_allowed_gap_ratio > 1 ) ) {
66             throw new IllegalArgumentException( "max allowed gap ration is out of range: " + max_allowed_gap_ratio );
67         }
68         for( int col = getLength() - 1; col >= 0; --col ) {
69             final boolean delete = ( ( double ) MsaMethods.calcGapSumPerColumn( this, col ) / getNumberOfSequences() ) > max_allowed_gap_ratio;
70             if ( delete ) {
71                 deleteColumn( col );
72             }
73         }
74     }
75
76     final public void deleteGapOnlyColumns() {
77         for( int col = getLength() - 1; col >= 0; --col ) {
78             if ( isAllGap( col ) ) {
79                 deleteColumn( col );
80             }
81         }
82     }
83
84     final public Sequence deleteRow( final String id, final boolean return_removed_seq ) {
85         int row = -1;
86         for( int r = 0; r < getNumberOfSequences(); ++r ) {
87             if ( getIdentifier( r ).equals( id ) ) {
88                 row = r;
89                 break;
90             }
91         }
92         if ( row < 0 ) {
93             throw new IllegalArgumentException( "id [" + id + "] not found" );
94         }
95         Sequence s = null;
96         StringBuilder sb = null;
97         if ( return_removed_seq ) {
98             s = getSequence( row );
99             final char[] x = s.getMolecularSequence();
100             sb = new StringBuilder( x.length );
101             for( int i = 0; i < x.length; ++i ) {
102                 if ( x[ i ] != Sequence.GAP ) {
103                     sb.append( x[ i ] );
104                 }
105             }
106         }
107         deleteRow( row );
108         if ( return_removed_seq ) {
109             return new BasicSequence( new String( s.getIdentifier() ), sb.toString(), s.getType() );
110         }
111         else {
112             return null;
113         }
114     }
115
116     @Override
117     final public String getIdentifier( final int row ) {
118         checkRow( row );
119         return super.getIdentifier( _mapped_row_positions[ row ] );
120     }
121
122     @Override
123     final public int getLength() {
124         return _length;
125     }
126
127     @Override
128     final public int getNumberOfSequences() {
129         return _seqs;
130     }
131
132     @Override
133     final public char getResidueAt( final int row, final int col ) {
134         checkRow( row );
135         checkColumn( col );
136         return super.getResidueAt( _mapped_row_positions[ row ], _mapped_col_positions[ col ] );
137     }
138
139     @Override
140     public Sequence getSequence( final int row ) {
141         checkRow( row );
142         return new BasicSequence( getIdentifier( row ), getSequenceAsString( row ).toString(), getType() );
143     }
144
145     final public boolean isAllGap( final int col ) {
146         final int m_col = _mapped_col_positions[ col ];
147         for( int j = 0; j < getNumberOfSequences(); ++j ) {
148             if ( super.getResidueAt( _mapped_row_positions[ j ], m_col ) != Sequence.GAP ) {
149                 return false;
150             }
151         }
152         return true;
153     }
154
155     @Override
156     final public void setIdentifier( final int row, final String id ) {
157         checkRow( row );
158         super.setIdentifier( _mapped_row_positions[ row ], id );
159     }
160
161     @Override
162     final public void setResidueAt( final int row, final int col, final char residue ) {
163         checkRow( row );
164         checkColumn( col );
165         super.setResidueAt( _mapped_row_positions[ row ], _mapped_col_positions[ col ], residue );
166     }
167
168     final private void checkColumn( final int col ) {
169         if ( ( col >= _length ) || ( col < 0 ) ) {
170             throw new IllegalArgumentException( "column " + col + " is out of range" );
171         }
172     }
173
174     final private void checkRow( final int row ) {
175         if ( ( row >= _seqs ) || ( row < 0 ) ) {
176             throw new IllegalArgumentException( "row " + row + " is out of range" );
177         }
178     }
179
180     final private void deleteColumn( final int col ) {
181         checkColumn( col );
182         for( int c = col; c < _length - 1; ++c ) {
183             _mapped_col_positions[ c ] = _mapped_col_positions[ c + 1 ];
184         }
185         --_length;
186     }
187
188     final private void deleteRow( final int row ) {
189         checkRow( row );
190         for( int r = row; r < _seqs - 1; ++r ) {
191             _mapped_row_positions[ r ] = _mapped_row_positions[ r + 1 ];
192         }
193         --_seqs;
194     }
195
196     public final static DeleteableMsa createInstance( final List<Sequence> seqs ) {
197         return new DeleteableMsa( ( BasicMsa ) BasicMsa.createInstance( seqs ) );
198     }
199
200     public final static DeleteableMsa createInstance( final Msa msa ) {
201         return new DeleteableMsa( ( BasicMsa ) msa );
202     }
203 }