05bf07416834f41900ad0122cdb0c10c7b9932b2
[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 ) {
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         final Sequence s = getSequence( row );
96         deleteRow( row );
97         return s;
98     }
99
100     @Override
101     final public String getIdentifier( final int row ) {
102         checkRow( row );
103         return super.getIdentifier( _mapped_row_positions[ row ] );
104     }
105
106     @Override
107     final public int getLength() {
108         return _length;
109     }
110
111     @Override
112     final public int getNumberOfSequences() {
113         return _seqs;
114     }
115
116     @Override
117     final public char getResidueAt( final int row, final int col ) {
118         checkRow( row );
119         checkColumn( col );
120         return super.getResidueAt( _mapped_row_positions[ row ], _mapped_col_positions[ col ] );
121     }
122
123     @Override
124     public Sequence getSequence( final int row ) {
125         checkRow( row );
126         return new BasicSequence( getIdentifier( row ), getSequenceAsString( row ).toString(), getType() );
127     }
128
129     final public boolean isAllGap( final int col ) {
130         final int m_col = _mapped_col_positions[ col ];
131         for( int j = 0; j < getNumberOfSequences(); ++j ) {
132             if ( super.getResidueAt( _mapped_row_positions[ j ], m_col ) != Sequence.GAP ) {
133                 return false;
134             }
135         }
136         return true;
137     }
138
139     @Override
140     final public void setIdentifier( final int row, final String id ) {
141         checkRow( row );
142         super.setIdentifier( _mapped_row_positions[ row ], id );
143     }
144
145     @Override
146     final public void setResidueAt( final int row, final int col, final char residue ) {
147         checkRow( row );
148         checkColumn( col );
149         super.setResidueAt( _mapped_row_positions[ row ], _mapped_col_positions[ col ], residue );
150     }
151
152     final private void checkColumn( final int col ) {
153         if ( ( col >= _length ) || ( col < 0 ) ) {
154             throw new IllegalArgumentException( "column " + col + " is out of range" );
155         }
156     }
157
158     final private void checkRow( final int row ) {
159         if ( ( row >= _seqs ) || ( row < 0 ) ) {
160             throw new IllegalArgumentException( "row " + row + " is out of range" );
161         }
162     }
163
164     final private void deleteColumn( final int col ) {
165         checkColumn( col );
166         for( int c = col; c < _length - 1; ++c ) {
167             _mapped_col_positions[ c ] = _mapped_col_positions[ c + 1 ];
168         }
169         --_length;
170     }
171
172     final private void deleteRow( final int row ) {
173         checkRow( row );
174         for( int r = row; r < _seqs - 1; ++r ) {
175             _mapped_row_positions[ r ] = _mapped_row_positions[ r + 1 ];
176         }
177         --_seqs;
178     }
179
180     public final static DeleteableMsa createInstance( final List<Sequence> seqs ) {
181         return new DeleteableMsa( ( BasicMsa ) BasicMsa.createInstance( seqs ) );
182     }
183
184     public final static DeleteableMsa createInstance( final Msa msa ) {
185         return new DeleteableMsa( ( BasicMsa ) msa );
186     }
187 }