2b14bf4c56157d838e29aaeaa9a111668833ef76
[jalview.git] / forester / java / src / org / forester / pccx / BranchLengthBasedScoringMethod.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 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 BranchLengthBasedScoringMethod extends BranchCountingBasedScoringMethod {
38
39     public static final double MIN_ALLOWED_BL_VALUE = 0.001;
40
41     @Override
42     double calculateScoreContributionPerExternalNode( final PhylogenyNode external_node,
43                                                       final PhylogenyNode current_node ) {
44         double score_contribution = 0.0;
45         if ( current_node == external_node ) {
46             score_contribution = external_node.getDistanceToParent();
47             // This, of course, is completely /ad hoc/.
48         }
49         else {
50             score_contribution = ModelingUtils.calculateBranchLengthSum( external_node, current_node );
51         }
52         return 1.0 / ( score_contribution > BranchLengthBasedScoringMethod.MIN_ALLOWED_BL_VALUE ? score_contribution
53                 : BranchLengthBasedScoringMethod.MIN_ALLOWED_BL_VALUE );
54     }
55
56     @Override
57     public String getDesciption() {
58         return "sum of 1/branch-length-sum [for self: 1/branch-length] [min branch length: "
59                 + BranchLengthBasedScoringMethod.MIN_ALLOWED_BL_VALUE + "]";
60     }
61
62     @Override
63     public double getNormalizationFactor( final Phylogeny phylogeny ) {
64         double s = 0.0;
65         double d = 0.0;
66         for( final PhylogenyNodeIterator iter = phylogeny.iteratorExternalForward(); iter.hasNext(); ) {
67             d = iter.next().getDistanceToParent();
68             s += ( 1.0 / ( d > BranchLengthBasedScoringMethod.MIN_ALLOWED_BL_VALUE ? d
69                     : BranchLengthBasedScoringMethod.MIN_ALLOWED_BL_VALUE ) );
70         }
71         return 1.0 / s;
72     }
73 }