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