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