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;
35 import java.util.TreeSet;
37 import org.forester.phylogeny.Phylogeny;
38 import org.forester.phylogeny.PhylogenyMethods;
39 import org.forester.phylogeny.PhylogenyNode;
40 import org.forester.phylogeny.data.Event;
41 import org.forester.phylogeny.data.Taxonomy;
42 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
43 import org.forester.util.ForesterUtil;
46 * Implements our algorithm for speciation - duplication inference (SDI). <p>
47 * The initialization is accomplished by: </p> <ul> <li>method
48 * "linkExtNodesOfG()" of class SDI: setting the links for the external nodes of
49 * the gene tree <li>"preorderReID(int)" from class Phylogeny: numbering of
50 * nodes of the species tree in preorder <li>the optional stripping of the
51 * species tree is accomplished by method "stripTree(Phylogeny,Phylogeny)" of
52 * class Phylogeny </ul> <p> The recursion part is accomplished by this class'
53 * method "geneTreePostOrderTraversal(PhylogenyNode)". <p> Requires JDK 1.5 or
56 * @see SDI#linkNodesOfG()
58 * @see Phylogeny#preorderReID(int)
61 * PhylogenyMethods#taxonomyBasedDeletionOfExternalNodes(Phylogeny,Phylogeny)
63 * @see #geneTreePostOrderTraversal(PhylogenyNode)
65 * @author Christian M. Zmasek
67 public class GSDI extends SDI {
69 private final boolean _most_parsimonious_duplication_model;
70 protected int _speciation_or_duplication_events_sum;
71 protected int _speciations_sum;
72 private final List<PhylogenyNode> _stripped_gene_tree_nodes;
73 private final List<PhylogenyNode> _stripped_species_tree_nodes;
74 private final Set<PhylogenyNode> _mapped_species_tree_nodes;
75 private TaxonomyComparisonBase _tax_comp_base;
76 private final SortedSet<String> _scientific_names_mapped_to_reduced_specificity;
78 public GSDI( final Phylogeny gene_tree,
79 final Phylogeny species_tree,
80 final boolean most_parsimonious_duplication_model,
81 final boolean strip_gene_tree,
82 final boolean strip_species_tree ) throws SDIException {
83 super( gene_tree, species_tree );
84 _speciation_or_duplication_events_sum = 0;
86 _most_parsimonious_duplication_model = most_parsimonious_duplication_model;
87 _duplications_sum = 0;
88 _stripped_gene_tree_nodes = new ArrayList<PhylogenyNode>();
89 _stripped_species_tree_nodes = new ArrayList<PhylogenyNode>();
90 _mapped_species_tree_nodes = new HashSet<PhylogenyNode>();
91 _scientific_names_mapped_to_reduced_specificity = new TreeSet<String>();
92 linkNodesOfG( null, strip_gene_tree, strip_species_tree );
93 PhylogenyMethods.preOrderReId( getSpeciesTree() );
94 geneTreePostOrderTraversal();
98 protected GSDI( final Phylogeny gene_tree, final Phylogeny species_tree, final boolean strip_gene_tree )
100 super( gene_tree, species_tree );
101 _speciation_or_duplication_events_sum = -1;
102 _speciations_sum = 0;
103 _most_parsimonious_duplication_model = true;
104 _duplications_sum = 0;
105 _stripped_gene_tree_nodes = new ArrayList<PhylogenyNode>();
106 _stripped_species_tree_nodes = new ArrayList<PhylogenyNode>();
107 _mapped_species_tree_nodes = new HashSet<PhylogenyNode>();
108 _scientific_names_mapped_to_reduced_specificity = new TreeSet<String>();
111 // s is the node on the species tree g maps to.
112 private final void determineEvent( final PhylogenyNode s, final PhylogenyNode g ) {
113 boolean oyako = false;
114 if ( ( g.getChildNode1().getLink() == s ) || ( g.getChildNode2().getLink() == s ) ) {
117 if ( g.getLink().getNumberOfDescendants() == 2 ) {
119 g.getNodeData().setEvent( createDuplicationEvent() );
122 g.getNodeData().setEvent( createSpeciationEvent() );
127 final Set<PhylogenyNode> set = new HashSet<PhylogenyNode>();
128 for( PhylogenyNode n : g.getChildNode1().getAllExternalDescendants() ) {
130 while ( n.getParent() != s ) {
138 boolean multiple = false;
139 for( PhylogenyNode n : g.getChildNode2().getAllExternalDescendants() ) {
141 while ( n.getParent() != s ) {
147 if ( set.contains( n ) ) {
153 g.getNodeData().setEvent( createDuplicationEvent() );
156 if ( _most_parsimonious_duplication_model ) {
157 g.getNodeData().setEvent( createSpeciationEvent() );
160 g.getNodeData().setEvent( createSingleSpeciationOrDuplicationEvent() );
165 g.getNodeData().setEvent( createSpeciationEvent() );
171 * Traverses the subtree of PhylogenyNode g in postorder, calculating the
172 * mapping function M, and determines which nodes represent speciation
173 * events and which ones duplication events.
175 * Preconditions: Mapping M for external nodes must have been calculated and
176 * the species tree must be labeled in preorder.
180 final void geneTreePostOrderTraversal() {
181 for( final PhylogenyNodeIterator it = getGeneTree().iteratorPostorder(); it.hasNext(); ) {
182 final PhylogenyNode g = it.next();
183 if ( g.isInternal() ) {
184 PhylogenyNode s1 = g.getChildNode1().getLink();
185 PhylogenyNode s2 = g.getChildNode2().getLink();
187 if ( s1.getId() > s2.getId() ) {
195 determineEvent( s1, g );
200 private final Event createDuplicationEvent() {
201 final Event event = Event.createSingleDuplicationEvent();
206 private final Event createSingleSpeciationOrDuplicationEvent() {
207 final Event event = Event.createSingleSpeciationOrDuplicationEvent();
208 ++_speciation_or_duplication_events_sum;
212 private final Event createSpeciationEvent() {
213 final Event event = Event.createSingleSpeciationEvent();
218 public final int getSpeciationOrDuplicationEventsSum() {
219 return _speciation_or_duplication_events_sum;
222 public final int getSpeciationsSum() {
223 return _speciations_sum;
227 * This allows for linking of internal nodes of the species tree (as opposed
228 * to just external nodes, as in the method it overrides.
229 * If TaxonomyComparisonBase is null, it will try to determine it.
230 * @throws SDIException
233 final void linkNodesOfG( final TaxonomyComparisonBase tax_comp_base,
234 final boolean strip_gene_tree,
235 final boolean strip_species_tree ) throws SDIException {
236 final Map<String, PhylogenyNode> species_to_node_map = new HashMap<String, PhylogenyNode>();
237 final List<PhylogenyNode> species_tree_ext_nodes = new ArrayList<PhylogenyNode>();
238 if ( tax_comp_base == null ) {
239 _tax_comp_base = determineTaxonomyComparisonBase( _gene_tree );
242 _tax_comp_base = tax_comp_base;
244 // Stringyfied taxonomy is the key, node is the value.
245 for( final PhylogenyNodeIterator iter = _species_tree.iteratorExternalForward(); iter.hasNext(); ) {
246 final PhylogenyNode s = iter.next();
247 species_tree_ext_nodes.add( s );
248 if ( s.getNodeData().isHasTaxonomy() ) {
249 final String tax_str = taxonomyToString( s, _tax_comp_base );
250 if ( !ForesterUtil.isEmpty( tax_str ) ) {
251 if ( species_to_node_map.containsKey( tax_str ) ) {
252 throw new SDIException( "taxonomy \"" + s + "\" is not unique in species tree" );
254 species_to_node_map.put( tax_str, s );
258 // Retrieve the reference to the node with a matching stringyfied taxonomy.
259 for( final PhylogenyNodeIterator iter = _gene_tree.iteratorExternalForward(); iter.hasNext(); ) {
260 final PhylogenyNode g = iter.next();
261 if ( !g.getNodeData().isHasTaxonomy() ) {
262 if ( strip_gene_tree ) {
263 _stripped_gene_tree_nodes.add( g );
266 throw new SDIException( "gene tree node \"" + g + "\" has no taxonomic data" );
270 final String tax_str = taxonomyToString( g, _tax_comp_base );
271 if ( ForesterUtil.isEmpty( tax_str ) ) {
272 if ( strip_gene_tree ) {
273 _stripped_gene_tree_nodes.add( g );
276 throw new SDIException( "gene tree node \"" + g + "\" has no appropriate taxonomic data" );
280 PhylogenyNode s = species_to_node_map.get( tax_str );
281 if ( ( _tax_comp_base == TaxonomyComparisonBase.SCIENTIFIC_NAME ) && ( s == null )
282 && ( ForesterUtil.countChars( tax_str, ' ' ) > 1 ) ) {
283 s = tryMapByRemovingOverlySpecificData( species_to_node_map, tax_str );
286 if ( strip_gene_tree ) {
287 _stripped_gene_tree_nodes.add( g );
290 throw new SDIException( "taxonomy \"" + g.getNodeData().getTaxonomy()
291 + "\" not present in species tree" );
296 _mapped_species_tree_nodes.add( s );
301 if ( strip_gene_tree ) {
303 if ( getGeneTree().isEmpty() || ( getGeneTree().getNumberOfExternalNodes() < 2 ) ) {
304 throw new SDIException( "species could not be mapped between gene tree and species tree" );
307 if ( strip_species_tree ) {
308 stripSpeciesTree( species_tree_ext_nodes );
312 private final PhylogenyNode tryMapByRemovingOverlySpecificData( final Map<String, PhylogenyNode> species_to_node_map,
313 final String tax_str ) {
314 PhylogenyNode s = tryMapByRemovingOverlySpecificData( species_to_node_map, tax_str, " (" );
316 if ( ForesterUtil.countChars( tax_str, ' ' ) == 2 ) {
317 final String new_tax_str = tax_str.substring( 0, tax_str.lastIndexOf( ' ' ) ).trim();
318 s = species_to_node_map.get( new_tax_str );
320 addScientificNamesMappedToReducedSpecificity( tax_str, new_tax_str );
325 for( final String t : new String[] { " subspecies ", " strain ", " variety ", " varietas ", " subvariety ",
326 " form ", " subform ", " cultivar ", " section ", " subsection " } ) {
327 s = tryMapByRemovingOverlySpecificData( species_to_node_map, tax_str, t );
336 private final PhylogenyNode tryMapByRemovingOverlySpecificData( final Map<String, PhylogenyNode> species_to_node_map,
337 final String tax_str,
338 final String term ) {
339 final int i = tax_str.indexOf( term );
341 final String new_tax_str = tax_str.substring( 0, i ).trim();
342 final PhylogenyNode s = species_to_node_map.get( new_tax_str );
344 addScientificNamesMappedToReducedSpecificity( tax_str, new_tax_str );
351 private final void addScientificNamesMappedToReducedSpecificity( final String s1, final String s2 ) {
352 _scientific_names_mapped_to_reduced_specificity.add( s1 + " -> " + s2 );
355 public final SortedSet<String> getReMappedScientificNamesFromGeneTree() {
356 return _scientific_names_mapped_to_reduced_specificity;
359 public TaxonomyComparisonBase getTaxCompBase() {
360 return _tax_comp_base;
363 private void stripSpeciesTree( final List<PhylogenyNode> species_tree_ext_nodes ) {
364 for( final PhylogenyNode s : species_tree_ext_nodes ) {
365 if ( !_mapped_species_tree_nodes.contains( s ) ) {
366 _species_tree.deleteSubtree( s, true );
367 _stripped_species_tree_nodes.add( s );
370 _species_tree.clearHashIdToNodeMap();
371 _species_tree.externalNodesHaveChanged();
374 public List<PhylogenyNode> getStrippedSpeciesTreeNodes() {
375 return _stripped_species_tree_nodes;
378 private void stripGeneTree() {
379 for( final PhylogenyNode g : _stripped_gene_tree_nodes ) {
380 _gene_tree.deleteSubtree( g, true );
382 _gene_tree.clearHashIdToNodeMap();
383 _gene_tree.externalNodesHaveChanged();
386 public Set<PhylogenyNode> getMappedExternalSpeciesTreeNodes() {
387 return _mapped_species_tree_nodes;
390 public static TaxonomyComparisonBase determineTaxonomyComparisonBase( final Phylogeny gene_tree ) {
391 int with_id_count = 0;
392 int with_code_count = 0;
393 int with_sn_count = 0;
395 for( final PhylogenyNodeIterator iter = gene_tree.iteratorExternalForward(); iter.hasNext(); ) {
396 final PhylogenyNode g = iter.next();
397 if ( g.getNodeData().isHasTaxonomy() ) {
398 final Taxonomy tax = g.getNodeData().getTaxonomy();
399 if ( ( tax.getIdentifier() != null ) && !ForesterUtil.isEmpty( tax.getIdentifier().getValue() ) ) {
400 if ( ++with_id_count > max ) {
404 if ( !ForesterUtil.isEmpty( tax.getTaxonomyCode() ) ) {
405 if ( ++with_code_count > max ) {
406 max = with_code_count;
409 if ( !ForesterUtil.isEmpty( tax.getScientificName() ) ) {
410 if ( ++with_sn_count > max ) {
417 throw new IllegalArgumentException( "gene tree has no taxonomic data" );
419 else if ( max == 1 ) {
420 throw new IllegalArgumentException( "gene tree has only one node with taxonomic data" );
422 else if ( max == with_id_count ) {
423 return SDI.TaxonomyComparisonBase.ID;
425 else if ( max == with_sn_count ) {
426 return SDI.TaxonomyComparisonBase.SCIENTIFIC_NAME;
429 return SDI.TaxonomyComparisonBase.CODE;
433 public List<PhylogenyNode> getStrippedExternalGeneTreeNodes() {
434 return _stripped_gene_tree_nodes;
438 public final String toString() {
439 final StringBuffer sb = new StringBuffer();
440 sb.append( "Most parsimonious duplication model: " + _most_parsimonious_duplication_model );
441 sb.append( ForesterUtil.getLineSeparator() );
442 sb.append( "Speciations sum : " + getSpeciationsSum() );
443 sb.append( ForesterUtil.getLineSeparator() );
444 sb.append( "Duplications sum : " + getDuplicationsSum() );
445 sb.append( ForesterUtil.getLineSeparator() );
446 if ( !_most_parsimonious_duplication_model ) {
447 sb.append( "Speciation or duplications sum : " + getSpeciationOrDuplicationEventsSum() );
448 sb.append( ForesterUtil.getLineSeparator() );
450 sb.append( "mapping cost L : " + computeMappingCostL() );
451 return sb.toString();