3 // FORESTER -- software libraries and applications
4 // for evolutionary biology research and applications.
6 // Copyright (C) 2008-2009 Christian M. Zmasek
7 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
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.
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.
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
24 // Contact: phylosoft @ gmail . com
25 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
27 package org.forester.pccx;
29 import java.awt.Color;
30 import java.util.List;
32 import java.util.SortedMap;
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;
42 * @author Christian M. Zmasek
44 public class ExternalNodeBasedCoverageMethod implements CoverageCalculationMethod {
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 );
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" );
61 BranchCountingBasedScoringMethod scoring_method;
63 scoring_method = ( BranchCountingBasedScoringMethod ) ( Class.forName( my_options.getScoringMethod() ) )
66 catch ( final Exception e ) {
67 throw new IllegalArgumentException( "could not create scoring method class \""
68 + my_options.getScoringMethod() + "\"" );
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,
77 normalization_factor );
78 normalized_score_stats.addValue( raw_score * normalization_factor );
79 raw_score_stats.addValue( raw_score );
81 return new ExternalNodeBasedCoverage( normalized_score_stats, raw_score_stats.arithmeticMean(), options );
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,
95 phylogeny.getNode( ( String ) element ),
98 if ( annotate_phylogeny ) {
99 colorizePhylogenyAccordingToCoverage( external_node_scores, phylogeny, normalization_factor );
102 for( final Object element : external_node_scores.values() ) {
103 score += ( ( Double ) element ).doubleValue();
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 );
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,
127 ExternalNodeBasedCoverageMethod.MINIMAL_COV_COLOR,
128 ExternalNodeBasedCoverageMethod.MAXIMAL_COV_COLOR,
129 ExternalNodeBasedCoverageMethod.MEAN_COVERAGE_COLOR ) );
131 PhylogenyMethods.postorderBranchColorAveragingExternalNodeBased( phylogeny );