2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
5 // Copyright (C) 2017 Christian M. Zmasek
6 // Copyright (C) 2017 J. Craig Venter Institute
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.
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.
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
23 // Contact: phyloxml @ gmail . com
24 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
25 // --------------------
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
31 package org.forester.clade_analysis;
33 import java.util.ArrayList;
34 import java.util.List;
36 import org.forester.phylogeny.Phylogeny;
37 import org.forester.phylogeny.PhylogenyNode;
38 import org.forester.phylogeny.data.Confidence;
39 import org.forester.util.ForesterUtil;
41 public final class Analysis {
43 public static Result execute( final Phylogeny p, final String query, final String separator ) {
44 final PhylogenyNode qnode = p.getNode( query );
45 if ( qnode.isRoot() ) {
46 throw new IllegalStateException( "Unexpected error: Query " + query
47 + " is root. This should have never happened" );
49 if ( qnode.getParent().isRoot() ) {
50 throw new IllegalStateException( "Unexpected error: Parent of query " + query
51 + " is root. This should have never happened" );
53 PhylogenyNode qnode_p = qnode.getParent();
54 PhylogenyNode qnode_pp = qnode.getParent().getParent();
55 while ( qnode_p.getNumberOfDescendants() == 1 ) {
56 qnode_p = qnode_p.getParent();
58 while ( qnode_pp.getNumberOfDescendants() == 1 ) {
59 qnode_pp = qnode_pp.getParent();
61 final List<PhylogenyNode> qnode_ext_nodes = qnode_pp.getAllExternalDescendants();
62 final int lec_ext_nodes = qnode_ext_nodes.size() - 1;
63 final int p_ext_nodes = p.getNumberOfExternalNodes() - 1;
64 final List<String> qnode_ext_nodes_names = new ArrayList<>();
65 for( final PhylogenyNode qnode_ext_node : qnode_ext_nodes ) {
66 String name = qnode_ext_node.getName();
67 if ( ForesterUtil.isEmptyTrimmed( name ) ) {
68 throw new IllegalArgumentException( "external node(s) with empty names found" );
71 if ( !name.equals( query ) ) {
72 qnode_ext_nodes_names.add( name );
75 final String greatest_common_prefix = ForesterUtil.greatestCommonPrefix( qnode_ext_nodes_names, separator );
76 final Result res = new Result();
77 if ( greatest_common_prefix.length() < 1 ) {
78 res.addWarning( "No greatest common prefix" );
79 res.setGreatestCommonPrefix( "" );
82 res.setGreatestCommonPrefix( greatest_common_prefix );
84 if ( qnode_pp.isRoot() ) {
85 res.addWarning( "Least Encompassing Clade is entire tree" );
87 res.setLeastEncompassingCladeSize( lec_ext_nodes );
88 res.setTreeSize( p_ext_nodes );
89 if ( qnode_pp.getBranchData().getConfidences() != null
90 && qnode_pp.getBranchData().getConfidences().size() > 0 ) {
91 final Confidence conf = qnode_pp.getBranchData().getConfidence( 0 );
93 res.setGreatestCommonCladeConfidence( conf.getValue()
94 + ( ForesterUtil.isEmpty( conf.getType() ) ? "" : " [" + conf.getType() + "]" ) );
97 final String greatest_common_prefix_a = analyzeSiblings( qnode_p, qnode_pp, separator );
98 res.setGreatestCommonPrefixUp( greatest_common_prefix_a );
99 final String greatest_common_prefix_b = analyzeSiblings( qnode, qnode_p, separator );
100 res.setGreatestCommonPrefixDown( greatest_common_prefix_b );
104 private final static String analyzeSiblings( final PhylogenyNode child,
105 final PhylogenyNode parent,
106 final String separator ) {
107 final int child_index = child.getChildNodeIndex();
108 final List<String> ext_nodes_names = new ArrayList<>();
109 final List<PhylogenyNode> descs = parent.getDescendants();
110 for( int i = 0; i < descs.size(); ++i ) {
111 if ( i != child_index ) {
112 final PhylogenyNode d = descs.get( i );
113 for( final PhylogenyNode n : d.getAllExternalDescendants() ) {
114 final String name = n.getName();
115 if ( ForesterUtil.isEmptyTrimmed( name ) ) {
116 throw new IllegalArgumentException( "external node(s) with empty names found" );
118 ext_nodes_names.add( name.trim() );
122 final String greatest_common_prefix = ForesterUtil.greatestCommonPrefix( ext_nodes_names, separator );
123 return greatest_common_prefix;