in progress...
[jalview.git] / forester / java / src / org / forester / clade_analysis / AnalysisSingle.java
1 // $Id:
2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
4 //
5 // Copyright (C) 2017 Christian M. Zmasek
6 // Copyright (C) 2017 J. Craig Venter Institute
7 // All rights reserved
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 //
23 // Contact: phyloxml @ gmail . com
24 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
25 // --------------------
26 // TODO
27 // * Multiple "hits" with different "M" values
28 // * More tests (including multiple children per node), especially on edge cases
29 // * Utilize relevant support values for warnings
30
31 package org.forester.clade_analysis;
32
33 import java.util.ArrayList;
34 import java.util.Collections;
35 import java.util.List;
36
37 import org.forester.phylogeny.Phylogeny;
38 import org.forester.phylogeny.PhylogenyNode;
39 import org.forester.phylogeny.data.Confidence;
40 import org.forester.util.ForesterUtil;
41
42 public final class AnalysisSingle {
43
44     public static ResultSingle execute( final Phylogeny p, final String query, final String separator ) {
45         final PhylogenyNode qnode = p.getNode( query );
46         if ( qnode.isRoot() ) {
47             throw new IllegalStateException( "Unexpected error: Query " + query
48                     + " is root. This should have never happened" );
49         }
50         if ( qnode.getParent().isRoot() ) {
51             throw new IllegalStateException( "Unexpected error: Parent of query " + query
52                     + " is root. This should have never happened" );
53         }
54         PhylogenyNode qnode_p = qnode.getParent();
55         PhylogenyNode qnode_pp = qnode.getParent().getParent();
56         while ( qnode_p.getNumberOfDescendants() == 1 ) {
57             qnode_p = qnode_p.getParent();
58         }
59         while ( qnode_pp.getNumberOfDescendants() == 1 ) {
60             qnode_pp = qnode_pp.getParent();
61         }
62         final List<PhylogenyNode> qnode_ext_nodes = qnode_pp.getAllExternalDescendants();
63         final int lec_ext_nodes = qnode_ext_nodes.size() - 1;
64         final int p_ext_nodes = p.getNumberOfExternalNodes() - 1;
65         final List<String> qnode_ext_nodes_names = new ArrayList<>();
66         for( final PhylogenyNode qnode_ext_node : qnode_ext_nodes ) {
67             String name = qnode_ext_node.getName();
68             if ( ForesterUtil.isEmptyTrimmed( name ) ) {
69                 throw new IllegalArgumentException( "external node(s) with empty names found" );
70             }
71             name = name.trim();
72             if ( !name.equals( query ) ) {
73                 qnode_ext_nodes_names.add( name );
74             }
75         }
76         final String greatest_common_prefix = ForesterUtil.greatestCommonPrefix( qnode_ext_nodes_names, separator );
77         final ResultSingle res = new ResultSingle();
78         if ( greatest_common_prefix.length() < 1 ) {
79             res.addWarning( "No greatest common prefix" );
80             res.setGreatestCommonPrefix( "" );
81         }
82         else {
83             res.setGreatestCommonPrefix( greatest_common_prefix );
84         }
85         if ( qnode_pp.isRoot() ) {
86             res.addWarning( "Least Encompassing Clade is entire tree" );
87         }
88         res.setLeastEncompassingCladeSize( lec_ext_nodes );
89         res.setTreeSize( p_ext_nodes );
90         final String conf = obtainConfidence( qnode_pp );
91         if ( conf != null ) {
92             res.setGreatestCommonCladeSubtreeConfidence( conf );
93         }
94         final String greatest_common_prefix_up[] = analyzeSiblings( qnode_p, qnode_pp, separator );
95         res.setGreatestCommonPrefixUp( greatest_common_prefix_up[ 0 ] );
96         if ( greatest_common_prefix_up[ 1 ] != null ) {
97             res.setGreatestCommonCladeUpSubtreeConfidence( greatest_common_prefix_up[ 1 ] );
98         }
99         final String greatest_common_prefix_down[] = analyzeSiblings( qnode, qnode_p, separator );
100         res.setGreatestCommonPrefixDown( greatest_common_prefix_down[ 0 ] );
101         if ( greatest_common_prefix_down[ 1 ] != null ) {
102             res.setGreatestCommonCladeDownSubtreeConfidence( greatest_common_prefix_down[ 1 ] );
103         }
104         return res;
105     }
106
107     private final static String[] analyzeSiblings( final PhylogenyNode child,
108                                                    final PhylogenyNode parent,
109                                                    final String separator ) {
110         final int child_index = child.getChildNodeIndex();
111         final List<String> ext_nodes_names = new ArrayList<>();
112         final List<PhylogenyNode> descs = parent.getDescendants();
113         String conf = null;
114         for( int i = 0; i < descs.size(); ++i ) {
115             if ( i != child_index ) {
116                 final PhylogenyNode d = descs.get( i );
117                 for( final PhylogenyNode n : d.getAllExternalDescendants() ) {
118                     final String name = n.getName();
119                     if ( ForesterUtil.isEmptyTrimmed( name ) ) {
120                         throw new IllegalArgumentException( "external node(s) with empty names found" );
121                     }
122                     ext_nodes_names.add( name.trim() );
123                 }
124                 if ( descs.size() == 2 ) {
125                     conf = obtainConfidence( d );
126                 }
127             }
128         }
129         final String greatest_common_prefix = ForesterUtil.greatestCommonPrefix( ext_nodes_names, separator );
130         return new String[] { greatest_common_prefix, conf };
131     }
132
133     private final static String obtainConfidence( final PhylogenyNode n ) {
134         if ( ( n.getBranchData().getConfidences() != null ) && ( n.getBranchData().getConfidences().size() > 0 ) ) {
135             final List<Confidence> confidences = n.getBranchData().getConfidences();
136             boolean not_first = false;
137             Collections.sort( confidences );
138             final StringBuilder sb = new StringBuilder();
139             for( final Confidence confidence : confidences ) {
140                 final double value = confidence.getValue();
141                 if ( value != Confidence.CONFIDENCE_DEFAULT_VALUE ) {
142                     if ( not_first ) {
143                         sb.append( " / " );
144                     }
145                     else {
146                         not_first = true;
147                     }
148                     sb.append( ( ForesterUtil.isEmpty( confidence.getType() ) ? "confidence: "
149                             : confidence.getType() + ": " ) + value );
150                 }
151             }
152             return sb.toString();
153         }
154         return null;
155     }
156 }