fa5265df404affae78791b4d4fd8dbd8d91d9776
[jalview.git] / forester / java / src / org / forester / sdi / GSDI.java
1 // $Id:
2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
4 //
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
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: phylosoft @ gmail . com
24 // WWW: www.phylosoft.org/forester
25
26 package org.forester.sdi;
27
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.HashSet;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.Set;
34 import java.util.SortedSet;
35 import java.util.TreeSet;
36
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;
44
45 /*
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
54  * greater.
55  * 
56  * @see SDI#linkNodesOfG()
57  * 
58  * @see Phylogeny#preorderReID(int)
59  * 
60  * @see
61  * PhylogenyMethods#taxonomyBasedDeletionOfExternalNodes(Phylogeny,Phylogeny)
62  * 
63  * @see #geneTreePostOrderTraversal(PhylogenyNode)
64  * 
65  * @author Christian M. Zmasek
66  */
67 public class GSDI extends SDI {
68
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;
77
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;
85         _speciations_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();
95     }
96
97     // Used by GSDIR
98     protected GSDI( final Phylogeny gene_tree, final Phylogeny species_tree, final boolean strip_gene_tree )
99             throws SDIException {
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>();
109     }
110
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 ) ) {
115             oyako = true;
116         }
117         if ( g.getLink().getNumberOfDescendants() == 2 ) {
118             if ( oyako ) {
119                 g.getNodeData().setEvent( createDuplicationEvent() );
120             }
121             else {
122                 g.getNodeData().setEvent( createSpeciationEvent() );
123             }
124         }
125         else {
126             if ( oyako ) {
127                 final Set<PhylogenyNode> set = new HashSet<PhylogenyNode>();
128                 for( PhylogenyNode n : g.getChildNode1().getAllExternalDescendants() ) {
129                     n = n.getLink();
130                     while ( n.getParent() != s ) {
131                         n = n.getParent();
132                         if ( n.isRoot() ) {
133                             break;
134                         }
135                     }
136                     set.add( n );
137                 }
138                 boolean multiple = false;
139                 for( PhylogenyNode n : g.getChildNode2().getAllExternalDescendants() ) {
140                     n = n.getLink();
141                     while ( n.getParent() != s ) {
142                         n = n.getParent();
143                         if ( n.isRoot() ) {
144                             break;
145                         }
146                     }
147                     if ( set.contains( n ) ) {
148                         multiple = true;
149                         break;
150                     }
151                 }
152                 if ( multiple ) {
153                     g.getNodeData().setEvent( createDuplicationEvent() );
154                 }
155                 else {
156                     if ( _most_parsimonious_duplication_model ) {
157                         g.getNodeData().setEvent( createSpeciationEvent() );
158                     }
159                     else {
160                         g.getNodeData().setEvent( createSingleSpeciationOrDuplicationEvent() );
161                     }
162                 }
163             }
164             else {
165                 g.getNodeData().setEvent( createSpeciationEvent() );
166             }
167         }
168     }
169
170     /**
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.
174      * <p>
175      * Preconditions: Mapping M for external nodes must have been calculated and
176      * the species tree must be labeled in preorder.
177      * <p>
178      * 
179      */
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();
186                 while ( s1 != s2 ) {
187                     if ( s1.getId() > s2.getId() ) {
188                         s1 = s1.getParent();
189                     }
190                     else {
191                         s2 = s2.getParent();
192                     }
193                 }
194                 g.setLink( s1 );
195                 determineEvent( s1, g );
196             }
197         }
198     }
199
200     private final Event createDuplicationEvent() {
201         final Event event = Event.createSingleDuplicationEvent();
202         ++_duplications_sum;
203         return event;
204     }
205
206     private final Event createSingleSpeciationOrDuplicationEvent() {
207         final Event event = Event.createSingleSpeciationOrDuplicationEvent();
208         ++_speciation_or_duplication_events_sum;
209         return event;
210     }
211
212     private final Event createSpeciationEvent() {
213         final Event event = Event.createSingleSpeciationEvent();
214         ++_speciations_sum;
215         return event;
216     }
217
218     public final int getSpeciationOrDuplicationEventsSum() {
219         return _speciation_or_duplication_events_sum;
220     }
221
222     public final int getSpeciationsSum() {
223         return _speciations_sum;
224     }
225
226     /**
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 
231      * 
232      */
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 );
240         }
241         else {
242             _tax_comp_base = tax_comp_base;
243         }
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" );
253                     }
254                     species_to_node_map.put( tax_str, s );
255                 }
256             }
257         }
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 );
264                 }
265                 else {
266                     throw new SDIException( "gene tree node \"" + g + "\" has no taxonomic data" );
267                 }
268             }
269             else {
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 );
274                     }
275                     else {
276                         throw new SDIException( "gene tree node \"" + g + "\" has no appropriate taxonomic data" );
277                     }
278                 }
279                 else {
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 );
284                     }
285                     if ( s == null ) {
286                         if ( strip_gene_tree ) {
287                             _stripped_gene_tree_nodes.add( g );
288                         }
289                         else {
290                             throw new SDIException( "taxonomy \"" + g.getNodeData().getTaxonomy()
291                                     + "\" not present in species tree" );
292                         }
293                     }
294                     else {
295                         g.setLink( s );
296                         _mapped_species_tree_nodes.add( s );
297                     }
298                 }
299             }
300         } // for loop
301         if ( strip_gene_tree ) {
302             stripGeneTree();
303             if ( getGeneTree().isEmpty() || ( getGeneTree().getNumberOfExternalNodes() < 2 ) ) {
304                 throw new SDIException( "species could not be mapped between gene tree and species tree" );
305             }
306         }
307         if ( strip_species_tree ) {
308             stripSpeciesTree( species_tree_ext_nodes );
309         }
310     }
311
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, " (" );
315         if ( s == null ) {
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 );
319                 if ( s != null ) {
320                     addScientificNamesMappedToReducedSpecificity( tax_str, new_tax_str );
321                 }
322             }
323         }
324         if ( s == null ) {
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 );
328                 if ( s != null ) {
329                     break;
330                 }
331             }
332         }
333         return s;
334     }
335
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 );
340         if ( i > 4 ) {
341             final String new_tax_str = tax_str.substring( 0, i ).trim();
342             final PhylogenyNode s = species_to_node_map.get( new_tax_str );
343             if ( s != null ) {
344                 addScientificNamesMappedToReducedSpecificity( tax_str, new_tax_str );
345             }
346             return s;
347         }
348         return null;
349     }
350
351     private final void addScientificNamesMappedToReducedSpecificity( final String s1, final String s2 ) {
352         _scientific_names_mapped_to_reduced_specificity.add( s1 + " -> " + s2 );
353     }
354
355     public final SortedSet<String> getReMappedScientificNamesFromGeneTree() {
356         return _scientific_names_mapped_to_reduced_specificity;
357     }
358
359     public TaxonomyComparisonBase getTaxCompBase() {
360         return _tax_comp_base;
361     }
362
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 );
368             }
369         }
370         _species_tree.clearHashIdToNodeMap();
371         _species_tree.externalNodesHaveChanged();
372     }
373
374     public List<PhylogenyNode> getStrippedSpeciesTreeNodes() {
375         return _stripped_species_tree_nodes;
376     }
377
378     private void stripGeneTree() {
379         for( final PhylogenyNode g : _stripped_gene_tree_nodes ) {
380             _gene_tree.deleteSubtree( g, true );
381         }
382         _gene_tree.clearHashIdToNodeMap();
383         _gene_tree.externalNodesHaveChanged();
384     }
385
386     public Set<PhylogenyNode> getMappedExternalSpeciesTreeNodes() {
387         return _mapped_species_tree_nodes;
388     }
389
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;
394         int max = 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 ) {
401                         max = with_id_count;
402                     }
403                 }
404                 if ( !ForesterUtil.isEmpty( tax.getTaxonomyCode() ) ) {
405                     if ( ++with_code_count > max ) {
406                         max = with_code_count;
407                     }
408                 }
409                 if ( !ForesterUtil.isEmpty( tax.getScientificName() ) ) {
410                     if ( ++with_sn_count > max ) {
411                         max = with_sn_count;
412                     }
413                 }
414             }
415         }
416         if ( max == 0 ) {
417             throw new IllegalArgumentException( "gene tree has no taxonomic data" );
418         }
419         else if ( max == 1 ) {
420             throw new IllegalArgumentException( "gene tree has only one node with taxonomic data" );
421         }
422         else if ( max == with_id_count ) {
423             return SDI.TaxonomyComparisonBase.ID;
424         }
425         else if ( max == with_sn_count ) {
426             return SDI.TaxonomyComparisonBase.SCIENTIFIC_NAME;
427         }
428         else {
429             return SDI.TaxonomyComparisonBase.CODE;
430         }
431     }
432
433     public List<PhylogenyNode> getStrippedExternalGeneTreeNodes() {
434         return _stripped_gene_tree_nodes;
435     }
436
437     @Override
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() );
449         }
450         sb.append( "mapping cost L                     : " + computeMappingCostL() );
451         return sb.toString();
452     }
453 }