5994bb9a3dbc8fe08baccec6a3a356f029c580d7
[jalview.git] / forester / java / src / org / forester / pccx / ExternalNodeBasedCoverageMethod.java
1 // $Id:
2 // Exp $
3 // FORESTER -- software libraries and applications
4 // for evolutionary biology research and applications.
5 //
6 // Copyright (C) 2008-2009 Christian M. Zmasek
7 // Copyright (C) 2008-2009 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: www.phylosoft.org/forester
26
27 package org.forester.pccx;
28
29 import java.awt.Color;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.SortedMap;
33
34 import org.forester.phylogeny.Phylogeny;
35 import org.forester.phylogeny.PhylogenyMethods;
36 import org.forester.phylogeny.PhylogenyNode;
37 import org.forester.util.BasicDescriptiveStatistics;
38 import org.forester.util.DescriptiveStatistics;
39 import org.forester.util.ForesterUtil;
40
41 /*
42  * @author Christian M. Zmasek
43  */
44 public class ExternalNodeBasedCoverageMethod implements CoverageCalculationMethod {
45
46     private static final Color MEAN_COVERAGE_COLOR = new Color( 0, 0, 0 );
47     private static final Color MAXIMAL_COV_COLOR   = new Color( 0, 255, 0 );
48     private static final Color MINIMAL_COV_COLOR   = new Color( 255, 0, 0 );
49
50     public Coverage calculateCoverage( final List<Phylogeny> phylogenies,
51                                        final List<String> names,
52                                        final CoverageCalculationOptions options,
53                                        final boolean annotate_phylogenies ) {
54         final DescriptiveStatistics normalized_score_stats = new BasicDescriptiveStatistics();
55         final DescriptiveStatistics raw_score_stats = new BasicDescriptiveStatistics();
56         final ExternalNodeBasedCoverageMethodOptions my_options = ( ExternalNodeBasedCoverageMethodOptions ) options;
57         if ( ( my_options == null ) || ForesterUtil.isEmpty( my_options.getScoringMethod() ) ) {
58             throw new IllegalArgumentException( "options for external node based coverage method appear to not have been set" );
59         }
60         BranchCountingBasedScoringMethod scoring_method;
61         try {
62             scoring_method = ( BranchCountingBasedScoringMethod ) ( Class.forName( my_options.getScoringMethod() ) )
63                     .newInstance();
64         }
65         catch ( final Exception e ) {
66             throw new IllegalArgumentException( "could not create scoring method class \""
67                     + my_options.getScoringMethod() + "\"" );
68         }
69         final double normalization_factor = scoring_method.getNormalizationFactor( phylogenies.get( 0 ) );
70         for( final Object element : phylogenies ) {
71             final double raw_score = calculateCoverage( ( Phylogeny ) element,
72                                                         names,
73                                                         options,
74                                                         scoring_method,
75                                                         annotate_phylogenies,
76                                                         normalization_factor );
77             normalized_score_stats.addValue( raw_score * normalization_factor );
78             raw_score_stats.addValue( raw_score );
79         }
80         return new ExternalNodeBasedCoverage( normalized_score_stats, raw_score_stats.arithmeticMean(), options );
81     }
82
83     private double calculateCoverage( final Phylogeny phylogeny,
84                                       final List<String> names,
85                                       final CoverageCalculationOptions options,
86                                       final BranchCountingBasedScoringMethod scoring_method,
87                                       final boolean annotate_phylogeny,
88                                       final double normalization_factor ) {
89         final SortedMap<PhylogenyNode, Double> external_node_scores = ModelingUtils
90                 .setUpExternalCoverageHashMap( phylogeny );
91         for( final Object element : names ) {
92             scoring_method.calculateScoreForExternalNode( external_node_scores, phylogeny, phylogeny
93                     .getNode( ( String ) element ), options );
94         }
95         if ( annotate_phylogeny ) {
96             colorizePhylogenyAccordingToCoverage( external_node_scores, phylogeny, normalization_factor );
97         }
98         double score = 0.0;
99         for( final Object element : external_node_scores.values() ) {
100             score += ( ( Double ) element ).doubleValue();
101         }
102         return score;
103     }
104
105     private void colorizePhylogenyAccordingToCoverage( final SortedMap<PhylogenyNode, Double> external_node_scores,
106                                                        final Phylogeny phylogeny,
107                                                        final double normalization_factor ) {
108         final DescriptiveStatistics ds = new BasicDescriptiveStatistics();
109         for( final Object element : external_node_scores.entrySet() ) {
110             ds.addValue( ( Double ) ( ( Map.Entry ) element ).getValue() * normalization_factor );
111         }
112         final double min = ds.getMin();
113         final double max = ds.getMax();
114         final double median = ds.median();
115         for( final Object element2 : external_node_scores.entrySet() ) {
116             final Map.Entry element = ( Map.Entry ) element2;
117             final PhylogenyNode node = ( PhylogenyNode ) element.getKey();
118             final double normalized_value = ( Double ) element.getValue() * normalization_factor;
119             PhylogenyMethods.setBranchColorValue( node, ForesterUtil
120                     .calcColor( normalized_value,
121                                 min,
122                                 max,
123                                 median,
124                                 ExternalNodeBasedCoverageMethod.MINIMAL_COV_COLOR,
125                                 ExternalNodeBasedCoverageMethod.MAXIMAL_COV_COLOR,
126                                 ExternalNodeBasedCoverageMethod.MEAN_COVERAGE_COLOR ) );
127         }
128         PhylogenyMethods.postorderBranchColorAveragingExternalNodeBased( phylogeny );
129     }
130 }