6d39b6119393915f27543091df994cf287253c75
[jalview.git] / forester / java / src / org / forester / surfacing / DomainLengths.java
1 // $Id:
2 //
3 // FORESTER -- software libraries and applications
4 // for evolutionary biology research and applications.
5 //
6 // Copyright (C) 2008-2010 Christian M. Zmasek
7 // Copyright (C) 2008-2010 Burnham Institute for Medical Research
8 // All rights reserved
9 //
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Lesser General Public
12 // License as published by the Free Software Foundation; either
13 // version 2.1 of the License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 //
24 // Contact: phylosoft @ gmail . com
25 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
26
27 package org.forester.surfacing;
28
29 import java.util.ArrayList;
30 import java.util.List;
31 import java.util.SortedMap;
32 import java.util.TreeMap;
33
34 import org.forester.species.Species;
35 import org.forester.util.BasicDescriptiveStatistics;
36 import org.forester.util.DescriptiveStatistics;
37
38 public class DomainLengths {
39
40     final String                                    _domain_id;
41     final SortedMap<Species, DescriptiveStatistics> _length_statistics;
42
43     public DomainLengths( final String domain_id ) {
44         _domain_id = domain_id;
45         _length_statistics = new TreeMap<Species, DescriptiveStatistics>();
46     }
47
48     public void addLength( final Species species, final int domain_length ) {
49         if ( !getLengthStatistics().containsKey( species ) ) {
50             addLengthStatistics( species, new BasicDescriptiveStatistics() );
51         }
52         getLengthStatistic( species ).addValue( domain_length );
53     }
54
55     /**
56      * Returns descriptive statistics based on the arithmetic means
57      * for each species.
58      *
59      *
60      * @return
61      */
62     public DescriptiveStatistics calculateMeanBasedStatistics() {
63         final DescriptiveStatistics stats = new BasicDescriptiveStatistics();
64         for( final DescriptiveStatistics s : getLengthStatisticsList() ) {
65             stats.addValue( s.arithmeticMean() );
66         }
67         return stats;
68     }
69
70     /**
71      *
72      * Note. This is not technically a Z-score since the distribution
73      * of means is unknown (and not normal).
74      *
75      * @param species
76      * @return
77      */
78     public double calculateZScoreForSpecies( final Species species ) {
79         final double species_mean = getLengthStatistic( species ).arithmeticMean();
80         final DescriptiveStatistics domain_stats = calculateMeanBasedStatistics();
81         final double population_sd = domain_stats.sampleStandardDeviation();
82         final double population_mean = domain_stats.arithmeticMean();
83         return ( species_mean - population_mean ) / population_sd;
84     }
85
86     public String getDomainId() {
87         return _domain_id;
88     }
89
90     public DescriptiveStatistics getLengthStatistic( final Species species ) {
91         return getLengthStatistics().get( species );
92     }
93
94     public List<DescriptiveStatistics> getLengthStatisticsList() {
95         final List<DescriptiveStatistics> list = new ArrayList<DescriptiveStatistics>();
96         for( final DescriptiveStatistics stats : _length_statistics.values() ) {
97             list.add( stats );
98         }
99         return list;
100     }
101
102     public List<Species> getMeanBasedOutlierSpecies( final double z_score_limit ) {
103         final List<Species> species = new ArrayList<Species>();
104         if ( getSpeciesList().size() > 1 ) {
105             for( final Species s : getSpeciesList() ) {
106                 final double z = calculateZScoreForSpecies( s );
107                 if ( z_score_limit < 0 ) {
108                     if ( z <= z_score_limit ) {
109                         species.add( s );
110                     }
111                 }
112                 else if ( z_score_limit > 0 ) {
113                     if ( z >= z_score_limit ) {
114                         species.add( s );
115                     }
116                 }
117             }
118         }
119         return species;
120     }
121
122     public List<Species> getSpeciesList() {
123         final List<Species> list = new ArrayList<Species>();
124         for( final Species s : _length_statistics.keySet() ) {
125             list.add( s );
126         }
127         return list;
128     }
129
130     public boolean isHasLengthStatistic( final Species species ) {
131         return getLengthStatistics().containsKey( species );
132     }
133
134     private void addLengthStatistics( final Species species, final DescriptiveStatistics length_statistic ) {
135         if ( getLengthStatistics().containsKey( species ) ) {
136             throw new IllegalArgumentException( "length statistics for [" + species.getSpeciesId() + "] already added" );
137         }
138         getLengthStatistics().put( species, length_statistic );
139     }
140
141     private SortedMap<Species, DescriptiveStatistics> getLengthStatistics() {
142         return _length_statistics;
143     }
144 }