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.List;
29 import org.forester.sequence.BasicSequence;
30 import org.forester.sequence.Sequence;
32 public final class DeleteableMsa extends BasicMsa {
34 private int _length = 0;
35 private int _mapped_col_positions[] = null;
36 private int _mapped_row_positions[] = null;
37 private int _seqs = 0;
39 private DeleteableMsa( final BasicMsa 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;
46 for( int i = 0; i < _mapped_row_positions.length; ++i ) {
47 _mapped_row_positions[ i ] = i;
49 _length = msa.getLength();
50 _seqs = msa.getNumberOfSequences();
53 public short determineMaxIdLength() {
55 for( int row = 0; row < getNumberOfSequences(); ++row ) {
56 final short l = ( short ) getIdentifier( row ).length();
64 final public void deleteGapColumns( final double max_allowed_gap_ratio ) {
65 if ( ( max_allowed_gap_ratio < 0 ) || ( max_allowed_gap_ratio > 1 ) ) {
66 throw new IllegalArgumentException( "max allowed gap ration is out of range: " + max_allowed_gap_ratio );
68 for( int col = getLength() - 1; col >= 0; --col ) {
69 final boolean delete = ( ( double ) MsaMethods.calcGapSumPerColumn( this, col ) / getNumberOfSequences() ) > max_allowed_gap_ratio;
76 final public void deleteGapOnlyColumns() {
77 for( int col = getLength() - 1; col >= 0; --col ) {
78 if ( isAllGap( col ) ) {
84 final public Sequence deleteRow( final String id ) {
86 for( int r = 0; r < getNumberOfSequences(); ++r ) {
87 if ( getIdentifier( r ).equals( id ) ) {
93 throw new IllegalArgumentException( "id [" + id + "] not found" );
95 final Sequence s = getSequence( row );
101 final public String getIdentifier( final int row ) {
103 return super.getIdentifier( _mapped_row_positions[ row ] );
107 final public int getLength() {
112 final public int getNumberOfSequences() {
117 final public char getResidueAt( final int row, final int col ) {
120 return super.getResidueAt( _mapped_row_positions[ row ], _mapped_col_positions[ col ] );
124 public Sequence getSequence( final int row ) {
126 return new BasicSequence( getIdentifier( row ), getSequenceAsString( row ).toString(), getType() );
129 final public boolean isAllGap( final int col ) {
130 final int m_col = _mapped_col_positions[ col ];
131 for( int j = 0; j < getNumberOfSequences(); ++j ) {
132 if ( super.getResidueAt( _mapped_row_positions[ j ], m_col ) != Sequence.GAP ) {
140 final public void setIdentifier( final int row, final String id ) {
142 super.setIdentifier( _mapped_row_positions[ row ], id );
146 final public void setResidueAt( final int row, final int col, final char residue ) {
149 super.setResidueAt( _mapped_row_positions[ row ], _mapped_col_positions[ col ], residue );
152 final private void checkColumn( final int col ) {
153 if ( ( col >= _length ) || ( col < 0 ) ) {
154 throw new IllegalArgumentException( "column " + col + " is out of range" );
158 final private void checkRow( final int row ) {
159 if ( ( row >= _seqs ) || ( row < 0 ) ) {
160 throw new IllegalArgumentException( "row " + row + " is out of range" );
164 final private void deleteColumn( final int col ) {
166 for( int c = col; c < _length - 1; ++c ) {
167 _mapped_col_positions[ c ] = _mapped_col_positions[ c + 1 ];
172 final private void deleteRow( final int row ) {
174 for( int r = row; r < _seqs - 1; ++r ) {
175 _mapped_row_positions[ r ] = _mapped_row_positions[ r + 1 ];
180 public final static DeleteableMsa createInstance( final List<Sequence> seqs ) {
181 return new DeleteableMsa( ( BasicMsa ) BasicMsa.createInstance( seqs ) );
184 public final static DeleteableMsa createInstance( final Msa msa ) {
185 return new DeleteableMsa( ( BasicMsa ) msa );