inprogress (not working)
[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     final public void deleteGapColumns( final double max_allowed_gap_ratio ) {
54         if ( ( max_allowed_gap_ratio < 0 ) || ( max_allowed_gap_ratio > 1 ) ) {
55             throw new IllegalArgumentException( "max allowed gap ration is out of range: " + max_allowed_gap_ratio );
56         }
57         for( int col = getLength() - 1; col >= 0; --col ) {
58             final boolean delete = ( ( double ) MsaMethods.calcGapSumPerColumn( this, col ) / getNumberOfSequences() ) > max_allowed_gap_ratio;
59             if ( delete ) {
60                 deleteColumn( col );
61             }
62         }
63     }
64
65     final public void deleteGapOnlyColumns() {
66         for( int col = getLength() - 1; col >= 0; --col ) {
67             if ( MsaMethods.calcGapSumPerColumn( this, col ) == getNumberOfSequences() ) {
68                 deleteColumn( col );
69             }
70         }
71     }
72
73     final public void deleteRow( final String id ) {
74         int row = -1;
75         for( int r = 0; r < getNumberOfSequences(); ++r ) {
76             if ( getIdentifier( r ).equals( id ) ) {
77                 row = r;
78                 break;
79             }
80         }
81         if ( row < 0 ) {
82             throw new IllegalArgumentException( "id [" + id + "] not found" );
83         }
84         deleteRow( row );
85     }
86
87     @Override
88     final public String getIdentifier( final int row ) {
89         checkRow( row );
90         return super.getIdentifier( _mapped_row_positions[ row ] );
91     }
92
93     @Override
94     final public int getLength() {
95         return _length;
96     }
97
98     @Override
99     final public int getNumberOfSequences() {
100         return _seqs;
101     }
102
103     @Override
104     final public char getResidueAt( final int row, final int col ) {
105         checkRow( row );
106         checkColumn( col );
107         return super.getResidueAt( _mapped_row_positions[ row ], _mapped_col_positions[ col ] );
108     }
109
110     @Override
111     public Sequence getSequence( final int row ) {
112         checkRow( row );
113         return new BasicSequence( getIdentifier( row ), getSequenceAsString( row ).toString(), getType() );
114     }
115
116     @Override
117     final public void setIdentifier( final int row, final String id ) {
118         checkRow( row );
119         super.setIdentifier( _mapped_row_positions[ row ], id );
120     }
121
122     @Override
123     final public void setResidueAt( final int row, final int col, final char residue ) {
124         checkRow( row );
125         checkColumn( col );
126         super.setResidueAt( _mapped_row_positions[ row ], _mapped_col_positions[ col ], residue );
127     }
128
129     final private void checkColumn( final int col ) {
130         if ( ( col >= _length ) || ( col < 0 ) ) {
131             throw new IllegalArgumentException( "column " + col + " is out of range" );
132         }
133     }
134
135     final private void checkRow( final int row ) {
136         if ( ( row >= _seqs ) || ( row < 0 ) ) {
137             throw new IllegalArgumentException( "row " + row + " is out of range" );
138         }
139     }
140
141     final private void deleteColumn( final int col ) {
142         checkColumn( col );
143         for( int c = col; c < _length - 1; ++c ) {
144             _mapped_col_positions[ c ] = _mapped_col_positions[ c + 1 ];
145         }
146         --_length;
147     }
148
149     final private void deleteRow( final int row ) {
150         checkRow( row );
151         for( int r = row; r < _seqs - 1; ++r ) {
152             _mapped_row_positions[ r ] = _mapped_row_positions[ r + 1 ];
153         }
154         --_seqs;
155     }
156
157     public final static DeleteableMsa createInstance( final List<Sequence> seqs ) {
158         return new DeleteableMsa( ( BasicMsa ) BasicMsa.createInstance( seqs ) );
159     }
160
161     public final static DeleteableMsa createInstance( final Msa msa ) {
162         return new DeleteableMsa( ( BasicMsa ) msa );
163     }
164 }