clean up
[jalview.git] / forester / java / src / org / forester / msa / MsaTools.java
1 // $Id:
2 // forester -- software libraries and applications
3 // for genomics and evolutionary biology research.
4 //
5 // Copyright (C) 2010 Christian M Zmasek
6 // Copyright (C) 2010 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 // Contact: phylosoft @ gmail . com
24 // WWW: www.phylosoft.org/forester
25
26 package org.forester.msa;
27
28 import java.util.ArrayList;
29 import java.util.List;
30
31 import org.forester.sequence.BasicSequence;
32 import org.forester.sequence.Sequence;
33 import org.forester.util.BasicDescriptiveStatistics;
34 import org.forester.util.DescriptiveStatistics;
35
36 public final class MsaTools {
37
38     private ArrayList<String> _ignored_seqs_ids;
39
40     synchronized public ArrayList<String> getIgnoredSequenceIds() {
41         return _ignored_seqs_ids;
42     }
43
44     synchronized public static MsaTools createInstance() {
45         return new MsaTools();
46     }
47
48     private MsaTools() {
49         init();
50     }
51
52     synchronized private void init() {
53         _ignored_seqs_ids = new ArrayList<String>();
54     }
55
56     @Override
57     public Object clone() {
58         throw new NoSuchMethodError();
59     }
60
61     public static int calcGapSumPerColumn( final Msa msa, final int col ) {
62         int gap_rows = 0;
63         for( int j = 0; j < msa.getNumberOfSequences(); ++j ) {
64             if ( msa.getResidueAt( j, col ) == Sequence.GAP ) {
65                 gap_rows++;
66             }
67         }
68         return gap_rows;
69     }
70
71     synchronized public Msa removeGapColumns( final double max_allowed_gap_ratio,
72                                               final int min_allowed_length,
73                                               final Msa msa ) {
74         init();
75         if ( ( max_allowed_gap_ratio < 0 ) || ( max_allowed_gap_ratio > 1 ) ) {
76             throw new IllegalArgumentException( "max allowed gap ration is out of range: " + max_allowed_gap_ratio );
77         }
78         final boolean ignore_too_short_seqs = min_allowed_length > 0;
79         final boolean[] delete_cols = new boolean[ msa.getLength() ];
80         int new_length = 0;
81         for( int col = 0; col < msa.getLength(); ++col ) {
82             delete_cols[ col ] = ( ( double ) calcGapSumPerColumn( msa, col ) / msa.getNumberOfSequences() ) > max_allowed_gap_ratio;
83             if ( !delete_cols[ col ] ) {
84                 ++new_length;
85             }
86         }
87         final List<Sequence> seqs = new ArrayList<Sequence>( msa.getNumberOfSequences() );
88         for( int row = 0; row < msa.getNumberOfSequences(); ++row ) {
89             final char[] mol_seq = new char[ new_length ];
90             int new_col = 0;
91             int non_gap_cols_sum = 0;
92             for( int col = 0; col < msa.getLength(); ++col ) {
93                 if ( !delete_cols[ col ] ) {
94                     final char residue = msa.getResidueAt( row, col );
95                     mol_seq[ new_col++ ] = ( residue );
96                     if ( residue != Sequence.GAP ) {
97                         ++non_gap_cols_sum;
98                     }
99                 }
100             }
101             if ( ignore_too_short_seqs ) {
102                 if ( non_gap_cols_sum >= min_allowed_length ) {
103                     seqs.add( new BasicSequence( msa.getIdentifier( row ), mol_seq, msa.getType() ) );
104                 }
105                 else {
106                     _ignored_seqs_ids.add( msa.getIdentifier( row ).toString() );
107                 }
108             }
109             else {
110                 seqs.add( new BasicSequence( msa.getIdentifier( row ), mol_seq, msa.getType() ) );
111             }
112         }
113         if ( seqs.size() < 1 ) {
114             return null;
115         }
116         return BasicMsa.createInstance( seqs );
117     }
118
119     public static DescriptiveStatistics calcBasicGapinessStatistics( final Msa msa ) {
120         final DescriptiveStatistics stats = new BasicDescriptiveStatistics();
121         for( int i = 0; i < msa.getLength(); ++i ) {
122             stats.addValue( ( double ) calcGapSumPerColumn( msa, i ) / msa.getNumberOfSequences() );
123         }
124         return stats;
125     }
126 }