2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
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: phylosoft @ gmail . com
24 // WWW: www.phylosoft.org/forester
26 package org.forester.sdi;
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.HashSet;
31 import java.util.List;
34 import java.util.SortedSet;
36 import org.forester.phylogeny.Phylogeny;
37 import org.forester.phylogeny.PhylogenyMethods;
38 import org.forester.phylogeny.PhylogenyNode;
39 import org.forester.phylogeny.data.Event;
40 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
41 import org.forester.sdi.SDIutil.TaxonomyComparisonBase;
42 import org.forester.util.ForesterUtil;
44 public final class GSDI implements GSDII {
46 private final boolean _most_parsimonious_duplication_model;
47 private final int _speciation_or_duplication_events_sum;
48 private final int _speciations_sum;
49 private final int _duplications_sum;
50 private final List<PhylogenyNode> _stripped_gene_tree_nodes;
51 private final List<PhylogenyNode> _stripped_species_tree_nodes;
52 private final Set<PhylogenyNode> _mapped_species_tree_nodes;
53 private final TaxonomyComparisonBase _tax_comp_base;
54 private final SortedSet<String> _scientific_names_mapped_to_reduced_specificity;
56 public GSDI( final Phylogeny gene_tree,
57 final Phylogeny species_tree,
58 final boolean most_parsimonious_duplication_model,
59 final boolean strip_gene_tree,
60 final boolean strip_species_tree ) throws SDIException {
61 _most_parsimonious_duplication_model = most_parsimonious_duplication_model;
62 final NodesLinkingResult nodes_linking_result = linkNodesOfG( gene_tree,
67 _stripped_gene_tree_nodes = nodes_linking_result.getStrippedGeneTreeNodes();
68 _stripped_species_tree_nodes = nodes_linking_result.getStrippedSpeciesTreeNodes();
69 _mapped_species_tree_nodes = nodes_linking_result.getMappedSpeciesTreeNodes();
70 _scientific_names_mapped_to_reduced_specificity = nodes_linking_result
71 .getScientificNamesMappedToReducedSpecificity();
72 _tax_comp_base = nodes_linking_result.getTaxCompBase();
73 PhylogenyMethods.preOrderReId( species_tree );
74 final GSDIsummaryResult gsdi_summary_result = geneTreePostOrderTraversal( gene_tree,
75 _most_parsimonious_duplication_model );
76 _speciation_or_duplication_events_sum = gsdi_summary_result.getSpeciationOrDuplicationEventsSum();
77 _speciations_sum = gsdi_summary_result.getSpeciationsSum();
78 _duplications_sum = gsdi_summary_result.getDuplicationsSum();
81 public int getDuplicationsSum() {
82 return _duplications_sum;
86 public Set<PhylogenyNode> getMappedExternalSpeciesTreeNodes() {
87 return _mapped_species_tree_nodes;
91 public final SortedSet<String> getReMappedScientificNamesFromGeneTree() {
92 return _scientific_names_mapped_to_reduced_specificity;
95 public final int getSpeciationOrDuplicationEventsSum() {
96 return _speciation_or_duplication_events_sum;
100 public final int getSpeciationsSum() {
101 return _speciations_sum;
105 public List<PhylogenyNode> getStrippedExternalGeneTreeNodes() {
106 return _stripped_gene_tree_nodes;
110 public List<PhylogenyNode> getStrippedSpeciesTreeNodes() {
111 return _stripped_species_tree_nodes;
115 public TaxonomyComparisonBase getTaxCompBase() {
116 return _tax_comp_base;
120 public final String toString() {
121 final StringBuffer sb = new StringBuffer();
122 sb.append( "Most parsimonious duplication model: " + _most_parsimonious_duplication_model );
123 sb.append( ForesterUtil.getLineSeparator() );
124 sb.append( "Speciations sum : " + getSpeciationsSum() );
125 sb.append( ForesterUtil.getLineSeparator() );
126 sb.append( "Duplications sum : " + getDuplicationsSum() );
127 sb.append( ForesterUtil.getLineSeparator() );
128 if ( !_most_parsimonious_duplication_model ) {
129 sb.append( "Speciation or duplications sum : " + getSpeciationOrDuplicationEventsSum() );
130 sb.append( ForesterUtil.getLineSeparator() );
132 return sb.toString();
136 * Traverses the subtree of PhylogenyNode g in postorder, calculating the
137 * mapping function M, and determines which nodes represent speciation
138 * events and which ones duplication events.
140 * Preconditions: Mapping M for external nodes must have been calculated and
141 * the species tree must be labeled in preorder.
146 final static GSDIsummaryResult geneTreePostOrderTraversal( final Phylogeny gene_tree,
147 final boolean most_parsimonious_duplication_model ) {
148 final GSDIsummaryResult res = new GSDIsummaryResult();
149 for( final PhylogenyNodeIterator it = gene_tree.iteratorPostorder(); it.hasNext(); ) {
150 final PhylogenyNode g = it.next();
151 if ( g.isInternal() ) {
152 PhylogenyNode s1 = g.getChildNode1().getLink();
153 PhylogenyNode s2 = g.getChildNode2().getLink();
155 if ( s1.getId() > s2.getId() ) {
163 determineEvent( s1, g, most_parsimonious_duplication_model, res );
170 * This allows for linking of internal nodes of the species tree (as opposed
171 * to just external nodes, as in the method it overrides.
172 * If TaxonomyComparisonBase is null, it will try to determine it.
173 * @throws SDIException
176 final static NodesLinkingResult linkNodesOfG( final Phylogeny gene_tree,
177 final Phylogeny species_tree,
178 final TaxonomyComparisonBase tax_comp_base,
179 final boolean strip_gene_tree,
180 final boolean strip_species_tree ) throws SDIException {
181 final Map<String, PhylogenyNode> species_to_node_map = new HashMap<String, PhylogenyNode>();
182 final List<PhylogenyNode> species_tree_ext_nodes = new ArrayList<PhylogenyNode>();
183 final NodesLinkingResult res = new NodesLinkingResult();
184 if ( tax_comp_base == null ) {
185 res.setTaxCompBase( SDIutil.determineTaxonomyComparisonBase( gene_tree ) );
188 res.setTaxCompBase( tax_comp_base );
190 // Stringyfied taxonomy is the key, node is the value.
191 for( final PhylogenyNodeIterator iter = species_tree.iteratorExternalForward(); iter.hasNext(); ) {
192 final PhylogenyNode s = iter.next();
193 species_tree_ext_nodes.add( s );
194 if ( s.getNodeData().isHasTaxonomy() ) {
195 final String tax_str = SDIutil.taxonomyToString( s, res.getTaxCompBase() );
196 if ( !ForesterUtil.isEmpty( tax_str ) ) {
197 if ( species_to_node_map.containsKey( tax_str ) ) {
198 throw new SDIException( "taxonomy \"" + tax_str + "\" is not unique in species tree (using "
199 + res.getTaxCompBase() + " for linking to gene tree)" );
201 species_to_node_map.put( tax_str, s );
205 // Retrieve the reference to the node with a matching stringyfied taxonomy.
206 for( final PhylogenyNodeIterator iter = gene_tree.iteratorExternalForward(); iter.hasNext(); ) {
207 final PhylogenyNode g = iter.next();
208 if ( !g.getNodeData().isHasTaxonomy() ) {
209 if ( strip_gene_tree ) {
210 res.getStrippedGeneTreeNodes().add( g );
213 throw new SDIException( "gene tree node \"" + g + "\" has no taxonomic data" );
217 final String tax_str = SDIutil.taxonomyToString( g, res.getTaxCompBase() );
218 if ( ForesterUtil.isEmpty( tax_str ) ) {
219 if ( strip_gene_tree ) {
220 res.getStrippedGeneTreeNodes().add( g );
223 throw new SDIException( "gene tree node \"" + g + "\" has no appropriate taxonomic data" );
227 PhylogenyNode s = species_to_node_map.get( tax_str );
228 if ( ( res.getTaxCompBase() == TaxonomyComparisonBase.SCIENTIFIC_NAME ) && ( s == null )
229 && ( ForesterUtil.countChars( tax_str, ' ' ) > 1 ) ) {
230 s = tryMapByRemovingOverlySpecificData( species_to_node_map,
232 res.getScientificNamesMappedToReducedSpecificity() );
235 if ( strip_gene_tree ) {
236 res.getStrippedGeneTreeNodes().add( g );
239 throw new SDIException( "taxonomy \"" + g.getNodeData().getTaxonomy()
240 + "\" not present in species tree" );
245 res.getMappedSpeciesTreeNodes().add( s );
250 if ( strip_gene_tree ) {
251 stripTree( gene_tree, res.getStrippedGeneTreeNodes() );
252 if ( gene_tree.isEmpty() || ( gene_tree.getNumberOfExternalNodes() < 2 ) ) {
253 throw new SDIException( "species could not be mapped between gene tree and species tree" );
256 if ( strip_species_tree ) {
257 stripSpeciesTree( species_tree, species_tree_ext_nodes, res );
262 private final static void addScientificNamesMappedToReducedSpecificity( final String s1,
264 final SortedSet<String> scientific_names_mapped_to_reduced_specificity ) {
265 scientific_names_mapped_to_reduced_specificity.add( s1 + " -> " + s2 );
268 private final static void determineEvent( final PhylogenyNode s,
269 final PhylogenyNode g,
270 final boolean most_parsimonious_duplication_model,
271 final GSDIsummaryResult res ) {
272 boolean oyako = false;
273 if ( ( g.getChildNode1().getLink() == s ) || ( g.getChildNode2().getLink() == s ) ) {
276 if ( g.getLink().getNumberOfDescendants() == 2 ) {
278 g.getNodeData().setEvent( Event.createSingleDuplicationEvent() );
279 res.increaseDuplicationsSum();
282 g.getNodeData().setEvent( Event.createSingleSpeciationEvent() );
283 res.increaseSpeciationsSum();
288 final Set<PhylogenyNode> set = new HashSet<PhylogenyNode>();
289 for( PhylogenyNode n : g.getChildNode1().getAllExternalDescendants() ) {
291 while ( n.getParent() != s ) {
299 boolean multiple = false;
300 for( PhylogenyNode n : g.getChildNode2().getAllExternalDescendants() ) {
302 while ( n.getParent() != s ) {
308 if ( set.contains( n ) ) {
314 g.getNodeData().setEvent( Event.createSingleDuplicationEvent() );
315 res.increaseDuplicationsSum();
318 if ( most_parsimonious_duplication_model ) {
319 g.getNodeData().setEvent( Event.createSingleSpeciationEvent() );
320 res.increaseSpeciationsSum();
323 g.getNodeData().setEvent( Event.createSingleSpeciationOrDuplicationEvent() );
324 res.increaseSpeciationOrDuplicationEventsSum();
329 g.getNodeData().setEvent( Event.createSingleSpeciationEvent() );
330 res.increaseSpeciationsSum();
335 private final static void stripSpeciesTree( final Phylogeny species_tree,
336 final List<PhylogenyNode> species_tree_ext_nodes,
337 final NodesLinkingResult res ) {
338 for( final PhylogenyNode s : species_tree_ext_nodes ) {
339 if ( !res.getMappedSpeciesTreeNodes().contains( s ) ) {
340 species_tree.deleteSubtree( s, true );
341 res.getStrippedSpeciesTreeNodes().add( s );
344 species_tree.clearHashIdToNodeMap();
345 species_tree.externalNodesHaveChanged();
348 private final static void stripTree( final Phylogeny phy, final List<PhylogenyNode> strip_nodes ) {
349 for( final PhylogenyNode g : strip_nodes ) {
350 phy.deleteSubtree( g, true );
352 phy.clearHashIdToNodeMap();
353 phy.externalNodesHaveChanged();
356 private final static PhylogenyNode tryMapByRemovingOverlySpecificData( final Map<String, PhylogenyNode> species_to_node_map,
357 final String tax_str,
358 final SortedSet<String> scientific_names_mapped_to_reduced_specificity ) {
359 PhylogenyNode s = tryMapByRemovingOverlySpecificData( species_to_node_map,
362 scientific_names_mapped_to_reduced_specificity );
364 if ( ForesterUtil.countChars( tax_str, ' ' ) == 2 ) {
365 final String new_tax_str = tax_str.substring( 0, tax_str.lastIndexOf( ' ' ) ).trim();
366 s = species_to_node_map.get( new_tax_str );
368 addScientificNamesMappedToReducedSpecificity( tax_str,
370 scientific_names_mapped_to_reduced_specificity );
375 for( final String t : new String[] { " subspecies ", " strain ", " variety ", " varietas ", " subvariety ",
376 " form ", " subform ", " cultivar ", " section ", " subsection " } ) {
377 s = tryMapByRemovingOverlySpecificData( species_to_node_map,
380 scientific_names_mapped_to_reduced_specificity );
389 private final static PhylogenyNode tryMapByRemovingOverlySpecificData( final Map<String, PhylogenyNode> species_to_node_map,
390 final String tax_str,
392 final SortedSet<String> scientific_names_mapped_to_reduced_specificity ) {
393 final int i = tax_str.indexOf( term );
395 final String new_tax_str = tax_str.substring( 0, i ).trim();
396 final PhylogenyNode s = species_to_node_map.get( new_tax_str );
398 addScientificNamesMappedToReducedSpecificity( tax_str,
400 scientific_names_mapped_to_reduced_specificity );