in progress
[jalview.git] / forester / java / src / org / forester / msa / MsaMethods.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 import java.util.SortedMap;
31 import java.util.TreeMap;
32
33 import org.forester.sequence.BasicSequence;
34 import org.forester.sequence.Sequence;
35 import org.forester.util.BasicDescriptiveStatistics;
36 import org.forester.util.DescriptiveStatistics;
37
38 public final class MsaMethods {
39
40     private ArrayList<String> _ignored_seqs_ids;
41
42     synchronized public ArrayList<String> getIgnoredSequenceIds() {
43         return _ignored_seqs_ids;
44     }
45
46     synchronized public static MsaMethods createInstance() {
47         return new MsaMethods();
48     }
49
50     private MsaMethods() {
51         init();
52     }
53
54     synchronized private void init() {
55         _ignored_seqs_ids = new ArrayList<String>();
56     }
57
58     @Override
59     public Object clone() {
60         throw new NoSuchMethodError();
61     }
62
63     public static int calcGapSumPerColumn( final Msa msa, final int col ) {
64         int gap_rows = 0;
65         for( int j = 0; j < msa.getNumberOfSequences(); ++j ) {
66             if ( msa.getResidueAt( j, col ) == Sequence.GAP ) {
67                 gap_rows++;
68             }
69         }
70         return gap_rows;
71     }
72
73     synchronized public Msa removeGapColumns( final double max_allowed_gap_ratio,
74                                               final int min_allowed_length,
75                                               final Msa msa ) {
76         init();
77         if ( ( max_allowed_gap_ratio < 0 ) || ( max_allowed_gap_ratio > 1 ) ) {
78             throw new IllegalArgumentException( "max allowed gap ration is out of range: " + max_allowed_gap_ratio );
79         }
80         final boolean ignore_too_short_seqs = min_allowed_length > 0;
81         final boolean[] delete_cols = new boolean[ msa.getLength() ];
82         int new_length = 0;
83         for( int col = 0; col < msa.getLength(); ++col ) {
84             delete_cols[ col ] = ( ( double ) calcGapSumPerColumn( msa, col ) / msa.getNumberOfSequences() ) > max_allowed_gap_ratio;
85             if ( !delete_cols[ col ] ) {
86                 ++new_length;
87             }
88         }
89         final List<Sequence> seqs = new ArrayList<Sequence>( msa.getNumberOfSequences() );
90         for( int row = 0; row < msa.getNumberOfSequences(); ++row ) {
91             final char[] mol_seq = new char[ new_length ];
92             int new_col = 0;
93             int non_gap_cols_sum = 0;
94             for( int col = 0; col < msa.getLength(); ++col ) {
95                 if ( !delete_cols[ col ] ) {
96                     final char residue = msa.getResidueAt( row, col );
97                     mol_seq[ new_col++ ] = ( residue );
98                     if ( residue != Sequence.GAP ) {
99                         ++non_gap_cols_sum;
100                     }
101                 }
102             }
103             if ( ignore_too_short_seqs ) {
104                 if ( non_gap_cols_sum >= min_allowed_length ) {
105                     seqs.add( new BasicSequence( msa.getIdentifier( row ), mol_seq, msa.getType() ) );
106                 }
107                 else {
108                     _ignored_seqs_ids.add( msa.getIdentifier( row ).toString() );
109                 }
110             }
111             else {
112                 seqs.add( new BasicSequence( msa.getIdentifier( row ), mol_seq, msa.getType() ) );
113             }
114         }
115         if ( seqs.size() < 1 ) {
116             return null;
117         }
118         return BasicMsa.createInstance( seqs );
119     }
120
121     public static SortedMap<Character, Integer> calculateResidueDestributionPerColumn( final Msa msa, final int c ) {
122         final SortedMap<Character, Integer> map = new TreeMap<Character, Integer>();
123         for( final Character r : msa.getColumnAt( c ) ) {
124             if ( !map.containsKey( r ) ) {
125                 map.put( r, 1 );
126             }
127             else {
128                 map.put( r, map.get( r ) + 1 );
129             }
130         }
131         return map;
132     }
133
134     public static DescriptiveStatistics calcBasicGapinessStatistics( final Msa msa ) {
135         final DescriptiveStatistics stats = new BasicDescriptiveStatistics();
136         for( int i = 0; i < msa.getLength(); ++i ) {
137             stats.addValue( ( double ) calcGapSumPerColumn( msa, i ) / msa.getNumberOfSequences() );
138         }
139         return stats;
140     }
141 }