7ba40cb857b83d64acf3baadc42e9be5f659836f
[jalview.git] / forester / java / src / org / forester / pccx / LogBranchLengthBasedScoringMethod.java
1 // $Id:
2 // cmzmasek 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 org.forester.phylogeny.Phylogeny;
30 import org.forester.phylogeny.PhylogenyNode;
31 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
32
33 /*
34  * 
35  * @author Christian M. Zmasek
36  */
37 public class LogBranchLengthBasedScoringMethod extends BranchCountingBasedScoringMethod {
38
39     public static final double MIN_ALLOWED_BL_VALUE = 0.0001;
40     public static final double MAX_ALLOWED_BL_VALUE = 1.0;
41
42     @Override
43     double calculateScoreContributionPerExternalNode( final PhylogenyNode external_node,
44                                                       final PhylogenyNode current_node ) {
45         double score_contribution = 0.0;
46         if ( current_node == external_node ) {
47             score_contribution = external_node.getDistanceToParent();
48             // This, of course, is completely /ad hoc/.
49         }
50         else {
51             score_contribution = ModelingUtils.calculateBranchLengthSum( external_node, current_node );
52         }
53         if ( score_contribution > LogBranchLengthBasedScoringMethod.MAX_ALLOWED_BL_VALUE ) {
54             score_contribution = LogBranchLengthBasedScoringMethod.MAX_ALLOWED_BL_VALUE;
55         }
56         else if ( score_contribution < LogBranchLengthBasedScoringMethod.MIN_ALLOWED_BL_VALUE ) {
57             score_contribution = LogBranchLengthBasedScoringMethod.MIN_ALLOWED_BL_VALUE;
58         }
59         return ( -Math.log( score_contribution ) );
60     }
61
62     @Override
63     public String getDesciption() {
64         return "sum of -ln(branch-length-sum) [for self: -ln(branch-length)] [min branch length: "
65                 + LogBranchLengthBasedScoringMethod.MIN_ALLOWED_BL_VALUE + ", max branch length: "
66                 + LogBranchLengthBasedScoringMethod.MAX_ALLOWED_BL_VALUE + "]";
67     }
68
69     @Override
70     public double getNormalizationFactor( final Phylogeny phylogeny ) {
71         double s = 0.0;
72         double d = 0.0;
73         for( final PhylogenyNodeIterator iter = phylogeny.iteratorExternalForward(); iter.hasNext(); ) {
74             d = iter.next().getDistanceToParent();
75             if ( d > LogBranchLengthBasedScoringMethod.MAX_ALLOWED_BL_VALUE ) {
76                 d = LogBranchLengthBasedScoringMethod.MAX_ALLOWED_BL_VALUE;
77             }
78             else if ( d < LogBranchLengthBasedScoringMethod.MIN_ALLOWED_BL_VALUE ) {
79                 d = LogBranchLengthBasedScoringMethod.MIN_ALLOWED_BL_VALUE;
80             }
81             s += ( -Math.log( d ) );
82         }
83         return 1 / s;
84     }
85 }