304199444bdbe94771761bd4abe93594a4b98327
[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
29 public final class DeleteableMsa extends BasicMsa {
30
31     private int                      _length                 = 0;
32     private int                      _mapped_col_positions[] = null;
33     private int                      _mapped_row_positions[] = null;
34     private int                      _seqs                   = 0;
35     private HashMap<String, Integer> _seq_id_to_row_map      = null;
36
37     public DeleteableMsa( final BasicMsa msa ) {
38         super( msa );
39         _mapped_col_positions = new int[ msa.getLength() ];
40         _mapped_row_positions = new int[ msa.getNumberOfSequences() ];
41         for( int i = 0; i < _mapped_col_positions.length; ++i ) {
42             _mapped_col_positions[ i ] = i;
43         }
44         for( int i = 0; i < _mapped_row_positions.length; ++i ) {
45             _mapped_row_positions[ i ] = i;
46         }
47         _seq_id_to_row_map = new HashMap<String, Integer>();
48         for( int row = 0; row < msa.getNumberOfSequences(); ++row ) {
49             _seq_id_to_row_map.put( msa.getIdentifier( row ), row );
50         }
51         _length = msa.getLength();
52         _seqs = msa.getNumberOfSequences();
53     }
54
55     public void deleteColumn( final int col ) {
56         if ( col >= _length || col < 0 ) {
57             throw new IllegalArgumentException( "column " + col + " is out of range" );
58         }
59         for( int c = col; c < _length - 1; ++c ) {
60             _mapped_col_positions[ c ] = _mapped_col_positions[ c + 1 ];
61         }
62         --_length;
63     }
64
65     public void deleteRow( final int row ) {
66         if ( row >= _seqs || row < 0 ) {
67             throw new IllegalArgumentException( "row " + row + " is out of range" );
68         }
69         for( int r = row; r < _seqs - 1; ++r ) {
70             _mapped_row_positions[ r ] = _mapped_row_positions[ r + 1 ];
71         }
72         --_seqs;
73     }
74
75     public void deleteRow( final String id ) {
76         int row = -1;
77         for( int r = 0; r < getNumberOfSequences(); ++r ) {
78             if ( getIdentifier( r ).equals( id ) ) {
79                 row = r;
80                 break;
81             }
82         }
83         if ( row < 0 ) {
84             throw new IllegalArgumentException( "id [" + id + "] not found" );
85         }
86         deleteRow( row );
87     }
88
89     @Override
90     public String getIdentifier( final int row ) {
91         return super.getIdentifier( _mapped_row_positions[ row ] );
92     }
93
94     @Override
95     public int getLength() {
96         return _length;
97     }
98
99     @Override
100     public int getNumberOfSequences() {
101         return _seqs;
102     }
103
104     @Override
105     public char getResidueAt( final int row, final int col ) {
106         return super.getResidueAt( _mapped_row_positions[ row ], _mapped_col_positions[ col ] );
107     }
108
109     @Override
110     public void setIdentifier( final int row, final String id ) {
111         super.setIdentifier( _mapped_row_positions[ row ], id );
112     }
113
114     @Override
115     public void setResidueAt( final int row, final int col, final char residue ) {
116         super.setResidueAt( _mapped_row_positions[ row ], _mapped_col_positions[ col ], residue );
117     }
118 }