2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
5 // Copyright (C) 2014 Christian M. Zmasek
6 // Copyright (C) 2014 Sanford-Burnham Medical Research Institute
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.
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.
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
23 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
25 package org.forester.msa;
27 import java.util.HashMap;
28 import java.util.List;
30 import org.forester.sequence.BasicSequence;
31 import org.forester.sequence.Sequence;
33 public final class DeleteableMsa extends BasicMsa {
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;
41 private DeleteableMsa( final BasicMsa 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;
48 for( int i = 0; i < _mapped_row_positions.length; ++i ) {
49 _mapped_row_positions[ i ] = i;
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 );
55 _length = msa.getLength();
56 _seqs = msa.getNumberOfSequences();
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 );
63 for( int col = getLength() - 1; col >= 0; --col ) {
64 final boolean delete = ( ( double ) MsaMethods.calcGapSumPerColumn( this, col ) / getNumberOfSequences() ) > max_allowed_gap_ratio;
71 final public void deleteGapOnlyColumns() {
72 for( int col = getLength() - 1; col >= 0; --col ) {
73 if ( MsaMethods.calcGapSumPerColumn( this, col ) == getNumberOfSequences() ) {
79 final public void deleteRow( final String id ) {
81 for( int r = 0; r < getNumberOfSequences(); ++r ) {
82 if ( getIdentifier( r ).equals( id ) ) {
88 throw new IllegalArgumentException( "id [" + id + "] not found" );
94 final public String getIdentifier( final int row ) {
95 return super.getIdentifier( _mapped_row_positions[ row ] );
99 final public int getLength() {
104 final public int getNumberOfSequences() {
109 final public char getResidueAt( final int row, final int col ) {
110 return super.getResidueAt( _mapped_row_positions[ row ], _mapped_col_positions[ col ] );
114 public Sequence getSequence( final int row ) {
115 return new BasicSequence( getIdentifier( row ), getSequenceAsString( row ).toString(), getType() );
119 final public void setIdentifier( final int row, final String id ) {
120 super.setIdentifier( _mapped_row_positions[ row ], id );
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 );
128 final private void deleteColumn( final int col ) {
129 if ( ( col >= _length ) || ( col < 0 ) ) {
130 throw new IllegalArgumentException( "column " + col + " is out of range" );
132 for( int c = col; c < _length - 1; ++c ) {
133 _mapped_col_positions[ c ] = _mapped_col_positions[ c + 1 ];
138 final private void deleteRow( final int row ) {
139 if ( ( row >= _seqs ) || ( row < 0 ) ) {
140 throw new IllegalArgumentException( "row " + row + " is out of range" );
142 for( int r = row; r < _seqs - 1; ++r ) {
143 _mapped_row_positions[ r ] = _mapped_row_positions[ r + 1 ];
148 public final static DeleteableMsa createInstance( final List<Sequence> seqs ) {
149 return new DeleteableMsa( ( BasicMsa ) BasicMsa.createInstance( seqs ) );
152 public final static DeleteableMsa createInstance( final Msa msa ) {
153 return new DeleteableMsa( ( BasicMsa ) msa );