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;
35 import org.forester.phylogeny.Phylogeny;
36 import org.forester.phylogeny.PhylogenyNode;
37 import org.forester.phylogeny.data.Event;
38 import org.forester.phylogeny.data.Taxonomy;
39 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
40 import org.forester.util.ForesterUtil;
43 * Implements our algorithm for speciation - duplication inference (SDI). <p>
44 * The initialization is accomplished by: </p> <ul> <li>method
45 * "linkExtNodesOfG()" of class SDI: setting the links for the external nodes of
46 * the gene tree <li>"preorderReID(int)" from class Phylogeny: numbering of
47 * nodes of the species tree in preorder <li>the optional stripping of the
48 * species tree is accomplished by method "stripTree(Phylogeny,Phylogeny)" of
49 * class Phylogeny </ul> <p> The recursion part is accomplished by this class'
50 * method "geneTreePostOrderTraversal(PhylogenyNode)". <p> Requires JDK 1.5 or
53 * @see SDI#linkNodesOfG()
55 * @see Phylogeny#preorderReID(int)
58 * PhylogenyMethods#taxonomyBasedDeletionOfExternalNodes(Phylogeny,Phylogeny)
60 * @see #geneTreePostOrderTraversal(PhylogenyNode)
62 * @author Christian M. Zmasek
64 public final class GSDI extends SDI {
66 private final boolean _most_parsimonious_duplication_model;
67 private final boolean _strip_gene_tree;
68 private final boolean _strip_species_tree;
69 private int _speciation_or_duplication_events_sum;
70 private int _speciations_sum;
71 private final List<PhylogenyNode> _stripped_gene_tree_nodes;
72 private final List<PhylogenyNode> _stripped_species_tree_nodes;
73 private final Set<PhylogenyNode> _mapped_species_tree_nodes;
74 private TaxonomyComparisonBase _tax_comp_base;
76 public GSDI( final Phylogeny gene_tree,
77 final Phylogeny species_tree,
78 final boolean most_parsimonious_duplication_model,
79 final boolean strip_gene_tree,
80 final boolean strip_species_tree ) throws SDIException {
81 super( gene_tree, species_tree );
82 _speciation_or_duplication_events_sum = 0;
84 _most_parsimonious_duplication_model = most_parsimonious_duplication_model;
85 _duplications_sum = 0;
86 _strip_gene_tree = strip_gene_tree;
87 _strip_species_tree = strip_species_tree;
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 getSpeciesTree().preOrderReId();
93 geneTreePostOrderTraversal();
96 GSDI( final Phylogeny gene_tree, final Phylogeny species_tree, final boolean most_parsimonious_duplication_model )
98 this( gene_tree, species_tree, most_parsimonious_duplication_model, false, false );
101 // s is the node on the species tree g maps to.
102 private final void determineEvent( final PhylogenyNode s, final PhylogenyNode g ) {
103 boolean oyako = false;
104 if ( ( g.getChildNode1().getLink() == s ) || ( g.getChildNode2().getLink() == s ) ) {
107 if ( g.getLink().getNumberOfDescendants() == 2 ) {
109 g.getNodeData().setEvent( createDuplicationEvent() );
112 g.getNodeData().setEvent( createSpeciationEvent() );
117 final Set<PhylogenyNode> set = new HashSet<PhylogenyNode>();
118 for( PhylogenyNode n : g.getChildNode1().getAllExternalDescendants() ) {
120 while ( n.getParent() != s ) {
128 boolean multiple = false;
129 for( PhylogenyNode n : g.getChildNode2().getAllExternalDescendants() ) {
131 while ( n.getParent() != s ) {
137 if ( set.contains( n ) ) {
143 g.getNodeData().setEvent( createDuplicationEvent() );
146 if ( _most_parsimonious_duplication_model ) {
147 g.getNodeData().setEvent( createSpeciationEvent() );
150 g.getNodeData().setEvent( createSingleSpeciationOrDuplicationEvent() );
155 g.getNodeData().setEvent( createSpeciationEvent() );
161 * Traverses the subtree of PhylogenyNode g in postorder, calculating the
162 * mapping function M, and determines which nodes represent speciation
163 * events and which ones duplication events.
165 * Preconditions: Mapping M for external nodes must have been calculated and
166 * the species tree must be labeled in preorder.
170 final void geneTreePostOrderTraversal() {
171 for( final PhylogenyNodeIterator it = getGeneTree().iteratorPostorder(); it.hasNext(); ) {
172 final PhylogenyNode g = it.next();
173 if ( g.isInternal() ) {
174 PhylogenyNode s1 = g.getChildNode1().getLink();
175 PhylogenyNode s2 = g.getChildNode2().getLink();
177 if ( s1.getId() > s2.getId() ) {
185 determineEvent( s1, g );
190 private final Event createDuplicationEvent() {
191 final Event event = Event.createSingleDuplicationEvent();
196 private final Event createSingleSpeciationOrDuplicationEvent() {
197 final Event event = Event.createSingleSpeciationOrDuplicationEvent();
198 ++_speciation_or_duplication_events_sum;
202 private final Event createSpeciationEvent() {
203 final Event event = Event.createSingleSpeciationEvent();
208 public final int getSpeciationOrDuplicationEventsSum() {
209 return _speciation_or_duplication_events_sum;
212 public final int getSpeciationsSum() {
213 return _speciations_sum;
217 * This allows for linking of internal nodes of the species tree (as opposed
218 * to just external nodes, as in the method it overrides.
219 * @throws SDIException
223 final void linkNodesOfG() throws SDIException {
224 final Map<String, PhylogenyNode> species_to_node_map = new HashMap<String, PhylogenyNode>();
225 final List<PhylogenyNode> species_tree_ext_nodes = new ArrayList<PhylogenyNode>();
226 _tax_comp_base = determineTaxonomyComparisonBase( _gene_tree );
227 // System.out.println( "comp base is: " + tax_comp_base );
228 // Stringyfied taxonomy is the key, node is the value.
229 for( final PhylogenyNodeIterator iter = _species_tree.iteratorExternalForward(); iter.hasNext(); ) {
230 final PhylogenyNode s = iter.next();
231 species_tree_ext_nodes.add( s );
232 final String tax_str = taxonomyToString( s, _tax_comp_base );
233 if ( !ForesterUtil.isEmpty( tax_str ) ) {
234 if ( species_to_node_map.containsKey( tax_str ) ) {
235 throw new SDIException( "taxonomy \"" + s + "\" is not unique in species tree" );
237 species_to_node_map.put( tax_str, s );
240 // Retrieve the reference to the node with a matching stringyfied taxonomy.
241 for( final PhylogenyNodeIterator iter = _gene_tree.iteratorExternalForward(); iter.hasNext(); ) {
242 final PhylogenyNode g = iter.next();
243 if ( !g.getNodeData().isHasTaxonomy() ) {
244 if ( _strip_gene_tree ) {
245 _stripped_gene_tree_nodes.add( g );
248 throw new SDIException( "gene tree node \"" + g + "\" has no taxonomic data" );
252 final String tax_str = taxonomyToString( g, _tax_comp_base );
253 if ( ForesterUtil.isEmpty( tax_str ) ) {
254 if ( _strip_gene_tree ) {
255 _stripped_gene_tree_nodes.add( g );
258 throw new SDIException( "gene tree node \"" + g + "\" has no appropriate taxonomic data" );
262 final PhylogenyNode s = species_to_node_map.get( tax_str );
264 if ( _strip_gene_tree ) {
265 _stripped_gene_tree_nodes.add( g );
268 throw new SDIException( "taxonomy \"" + g.getNodeData().getTaxonomy()
269 + "\" not present in species tree" );
274 _mapped_species_tree_nodes.add( s );
275 // System.out.println( "setting link of " + g + " to " + s );
280 if ( _strip_gene_tree ) {
283 if ( _strip_species_tree ) {
284 stripSpeciesTree( species_tree_ext_nodes );
288 public TaxonomyComparisonBase getTaxCompBase() {
289 return _tax_comp_base;
292 private void stripSpeciesTree( final List<PhylogenyNode> species_tree_ext_nodes ) {
293 for( final PhylogenyNode s : species_tree_ext_nodes ) {
294 if ( !_mapped_species_tree_nodes.contains( s ) ) {
295 _species_tree.deleteSubtree( s, true );
296 _stripped_species_tree_nodes.add( s );
301 public List<PhylogenyNode> getStrippedSpeciesTreeNodes() {
302 return _stripped_species_tree_nodes;
305 private void stripGeneTree() {
306 for( final PhylogenyNode g : _stripped_gene_tree_nodes ) {
307 _gene_tree.deleteSubtree( g, true );
311 public Set<PhylogenyNode> getMappedExternalSpeciesTreeNodes() {
312 return _mapped_species_tree_nodes;
315 public static TaxonomyComparisonBase determineTaxonomyComparisonBase( final Phylogeny gene_tree ) {
316 int with_id_count = 0;
317 int with_code_count = 0;
318 int with_sn_count = 0;
320 for( final PhylogenyNodeIterator iter = gene_tree.iteratorExternalForward(); iter.hasNext(); ) {
321 final PhylogenyNode g = iter.next();
322 if ( g.getNodeData().isHasTaxonomy() ) {
323 final Taxonomy tax = g.getNodeData().getTaxonomy();
324 if ( ( tax.getIdentifier() != null ) && !ForesterUtil.isEmpty( tax.getIdentifier().getValue() ) ) {
325 if ( ++with_id_count > max ) {
329 if ( !ForesterUtil.isEmpty( tax.getTaxonomyCode() ) ) {
330 if ( ++with_code_count > max ) {
331 max = with_code_count;
334 if ( !ForesterUtil.isEmpty( tax.getScientificName() ) ) {
335 if ( ++with_sn_count > max ) {
342 throw new IllegalArgumentException( "gene tree has no taxonomic data" );
344 else if ( max == 1 ) {
345 throw new IllegalArgumentException( "gene tree has only one node with taxonomic data" );
347 else if ( max == with_sn_count ) {
348 return SDI.TaxonomyComparisonBase.SCIENTIFIC_NAME;
350 else if ( max == with_id_count ) {
351 return SDI.TaxonomyComparisonBase.ID;
354 return SDI.TaxonomyComparisonBase.CODE;
358 public List<PhylogenyNode> getStrippedExternalGeneTreeNodes() {
359 return _stripped_gene_tree_nodes;
363 public final String toString() {
364 final StringBuffer sb = new StringBuffer();
365 sb.append( "Most parsimonious duplication model: " + _most_parsimonious_duplication_model );
366 sb.append( ForesterUtil.getLineSeparator() );
367 sb.append( "Speciations sum : " + getSpeciationsSum() );
368 sb.append( ForesterUtil.getLineSeparator() );
369 sb.append( "Duplications sum : " + getDuplicationsSum() );
370 sb.append( ForesterUtil.getLineSeparator() );
371 if ( !_most_parsimonious_duplication_model ) {
372 sb.append( "Speciation or duplications sum : " + getSpeciationOrDuplicationEventsSum() );
373 sb.append( ForesterUtil.getLineSeparator() );
375 sb.append( "mapping cost L : " + computeMappingCostL() );
376 return sb.toString();