17f72bb170f884e3c771306a46656f71f60e5d19
[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.HashMap;
28 import java.util.List;
29
30 import org.forester.sequence.BasicSequence;
31 import org.forester.sequence.Sequence;
32
33 public final class DeleteableMsa extends BasicMsa {
34
35     private int                      _length                 = 0;
36     private int                      _mapped_col_positions[] = null;
37     private int                      _mapped_row_positions[] = null;
38     private HashMap<String, Integer> _seq_id_to_row_map      = null;
39     private int                      _seqs                   = 0;
40
41     private DeleteableMsa( final BasicMsa msa ) {
42         super( msa );
43         _mapped_col_positions = new int[ msa.getLength() ];
44         _mapped_row_positions = new int[ msa.getNumberOfSequences() ];
45         for( int i = 0; i < _mapped_col_positions.length; ++i ) {
46             _mapped_col_positions[ i ] = i;
47         }
48         for( int i = 0; i < _mapped_row_positions.length; ++i ) {
49             _mapped_row_positions[ i ] = i;
50         }
51         _seq_id_to_row_map = new HashMap<String, Integer>();
52         for( int row = 0; row < msa.getNumberOfSequences(); ++row ) {
53             _seq_id_to_row_map.put( msa.getIdentifier( row ), row );
54         }
55         _length = msa.getLength();
56         _seqs = msa.getNumberOfSequences();
57     }
58
59     final public void deleteGapColumns( final double max_allowed_gap_ratio ) {
60         if ( ( max_allowed_gap_ratio < 0 ) || ( max_allowed_gap_ratio > 1 ) ) {
61             throw new IllegalArgumentException( "max allowed gap ration is out of range: " + max_allowed_gap_ratio );
62         }
63         for( int col = getLength() - 1; col >= 0; --col ) {
64             final boolean delete = ( ( double ) MsaMethods.calcGapSumPerColumn( this, col ) / getNumberOfSequences() ) > max_allowed_gap_ratio;
65             if ( delete ) {
66                 deleteColumn( col );
67             }
68         }
69     }
70
71     final public void deleteGapOnlyColumns() {
72         for( int col = getLength() - 1; col >= 0; --col ) {
73             if ( MsaMethods.calcGapSumPerColumn( this, col ) == getNumberOfSequences() ) {
74                 deleteColumn( col );
75             }
76         }
77     }
78
79     final public void deleteRow( final String id ) {
80         int row = -1;
81         for( int r = 0; r < getNumberOfSequences(); ++r ) {
82             if ( getIdentifier( r ).equals( id ) ) {
83                 row = r;
84                 break;
85             }
86         }
87         if ( row < 0 ) {
88             throw new IllegalArgumentException( "id [" + id + "] not found" );
89         }
90         deleteRow( row );
91     }
92
93     @Override
94     final public String getIdentifier( final int row ) {
95         return super.getIdentifier( _mapped_row_positions[ row ] );
96     }
97
98     @Override
99     final public int getLength() {
100         return _length;
101     }
102
103     @Override
104     final public int getNumberOfSequences() {
105         return _seqs;
106     }
107
108     @Override
109     final public char getResidueAt( final int row, final int col ) {
110         return super.getResidueAt( _mapped_row_positions[ row ], _mapped_col_positions[ col ] );
111     }
112
113     @Override
114     public Sequence getSequence( final int row ) {
115         return new BasicSequence( getIdentifier( row ), getSequenceAsString( row ).toString(), getType() );
116     }
117
118     @Override
119     final public void setIdentifier( final int row, final String id ) {
120         super.setIdentifier( _mapped_row_positions[ row ], id );
121     }
122
123     @Override
124     final public void setResidueAt( final int row, final int col, final char residue ) {
125         super.setResidueAt( _mapped_row_positions[ row ], _mapped_col_positions[ col ], residue );
126     }
127
128     final private void deleteColumn( final int col ) {
129         if ( ( col >= _length ) || ( col < 0 ) ) {
130             throw new IllegalArgumentException( "column " + col + " is out of range" );
131         }
132         for( int c = col; c < _length - 1; ++c ) {
133             _mapped_col_positions[ c ] = _mapped_col_positions[ c + 1 ];
134         }
135         --_length;
136     }
137
138     final private void deleteRow( final int row ) {
139         if ( ( row >= _seqs ) || ( row < 0 ) ) {
140             throw new IllegalArgumentException( "row " + row + " is out of range" );
141         }
142         for( int r = row; r < _seqs - 1; ++r ) {
143             _mapped_row_positions[ r ] = _mapped_row_positions[ r + 1 ];
144         }
145         --_seqs;
146     }
147
148     public final static DeleteableMsa createInstance( final List<Sequence> seqs ) {
149         return new DeleteableMsa( ( BasicMsa ) BasicMsa.createInstance( seqs ) );
150     }
151
152     public final static DeleteableMsa createInstance( final Msa msa ) {
153         return new DeleteableMsa( ( BasicMsa ) msa );
154     }
155 }