5e73593155c3e43bbce7962b374d308e152c6527
[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: https://sites.google.com/site/cmzmasek/home/software/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     @Override
51     public Coverage calculateCoverage( final List<Phylogeny> phylogenies,
52                                        final List<String> names,
53                                        final CoverageCalculationOptions options,
54                                        final boolean annotate_phylogenies ) {
55         final DescriptiveStatistics normalized_score_stats = new BasicDescriptiveStatistics();
56         final DescriptiveStatistics raw_score_stats = new BasicDescriptiveStatistics();
57         final ExternalNodeBasedCoverageMethodOptions my_options = ( ExternalNodeBasedCoverageMethodOptions ) options;
58         if ( ( my_options == null ) || ForesterUtil.isEmpty( my_options.getScoringMethod() ) ) {
59             throw new IllegalArgumentException( "options for external node based coverage method appear to not have been set" );
60         }
61         BranchCountingBasedScoringMethod scoring_method;
62         try {
63             scoring_method = ( BranchCountingBasedScoringMethod ) ( Class.forName( my_options.getScoringMethod() ) )
64                     .newInstance();
65         }
66         catch ( final Exception e ) {
67             throw new IllegalArgumentException( "could not create scoring method class \""
68                     + my_options.getScoringMethod() + "\"" );
69         }
70         final double normalization_factor = scoring_method.getNormalizationFactor( phylogenies.get( 0 ) );
71         for( final Object element : phylogenies ) {
72             final double raw_score = calculateCoverage( ( Phylogeny ) element,
73                                                         names,
74                                                         options,
75                                                         scoring_method,
76                                                         annotate_phylogenies,
77                                                         normalization_factor );
78             normalized_score_stats.addValue( raw_score * normalization_factor );
79             raw_score_stats.addValue( raw_score );
80         }
81         return new ExternalNodeBasedCoverage( normalized_score_stats, raw_score_stats.arithmeticMean(), options );
82     }
83
84     private double calculateCoverage( final Phylogeny phylogeny,
85                                       final List<String> names,
86                                       final CoverageCalculationOptions options,
87                                       final BranchCountingBasedScoringMethod scoring_method,
88                                       final boolean annotate_phylogeny,
89                                       final double normalization_factor ) {
90         final SortedMap<PhylogenyNode, Double> external_node_scores = ModelingUtils
91                 .setUpExternalCoverageHashMap( phylogeny );
92         for( final Object element : names ) {
93             scoring_method.calculateScoreForExternalNode( external_node_scores,
94                                                           phylogeny,
95                                                           phylogeny.getNode( ( String ) element ),
96                                                           options );
97         }
98         if ( annotate_phylogeny ) {
99             colorizePhylogenyAccordingToCoverage( external_node_scores, phylogeny, normalization_factor );
100         }
101         double score = 0.0;
102         for( final Object element : external_node_scores.values() ) {
103             score += ( ( Double ) element ).doubleValue();
104         }
105         return score;
106     }
107
108     private void colorizePhylogenyAccordingToCoverage( final SortedMap<PhylogenyNode, Double> external_node_scores,
109                                                        final Phylogeny phylogeny,
110                                                        final double normalization_factor ) {
111         final DescriptiveStatistics ds = new BasicDescriptiveStatistics();
112         for( final Object element : external_node_scores.entrySet() ) {
113             ds.addValue( ( Double ) ( ( Map.Entry ) element ).getValue() * normalization_factor );
114         }
115         final double min = ds.getMin();
116         final double max = ds.getMax();
117         final double median = ds.median();
118         for( final Object element2 : external_node_scores.entrySet() ) {
119             final Map.Entry element = ( Map.Entry ) element2;
120             final PhylogenyNode node = ( PhylogenyNode ) element.getKey();
121             final double normalized_value = ( Double ) element.getValue() * normalization_factor;
122             PhylogenyMethods.setBranchColorValue( node, ForesterUtil
123                     .calcColor( normalized_value,
124                                 min,
125                                 max,
126                                 median,
127                                 ExternalNodeBasedCoverageMethod.MINIMAL_COV_COLOR,
128                                 ExternalNodeBasedCoverageMethod.MAXIMAL_COV_COLOR,
129                                 ExternalNodeBasedCoverageMethod.MEAN_COVERAGE_COLOR ) );
130         }
131         PhylogenyMethods.postorderBranchColorAveragingExternalNodeBased( phylogeny );
132     }
133 }