in progress
[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.PhylogenyNode;
39 import org.forester.phylogeny.data.Event;
40 import org.forester.phylogeny.data.Taxonomy;
41 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
42 import org.forester.util.ForesterUtil;
43
44 /*
45  * Implements our algorithm for speciation - duplication inference (SDI). <p>
46  * The initialization is accomplished by: </p> <ul> <li>method
47  * "linkExtNodesOfG()" of class SDI: setting the links for the external nodes of
48  * the gene tree <li>"preorderReID(int)" from class Phylogeny: numbering of
49  * nodes of the species tree in preorder <li>the optional stripping of the
50  * species tree is accomplished by method "stripTree(Phylogeny,Phylogeny)" of
51  * class Phylogeny </ul> <p> The recursion part is accomplished by this class'
52  * method "geneTreePostOrderTraversal(PhylogenyNode)". <p> Requires JDK 1.5 or
53  * greater.
54  * 
55  * @see SDI#linkNodesOfG()
56  * 
57  * @see Phylogeny#preorderReID(int)
58  * 
59  * @see
60  * PhylogenyMethods#taxonomyBasedDeletionOfExternalNodes(Phylogeny,Phylogeny)
61  * 
62  * @see #geneTreePostOrderTraversal(PhylogenyNode)
63  * 
64  * @author Christian M. Zmasek
65  */
66 public final class GSDI extends SDI {
67
68     private final boolean             _most_parsimonious_duplication_model;
69     private final boolean             _strip_gene_tree;
70     private final boolean             _strip_species_tree;
71     private int                       _speciation_or_duplication_events_sum;
72     private int                       _speciations_sum;
73     private final List<PhylogenyNode> _stripped_gene_tree_nodes;
74     private final List<PhylogenyNode> _stripped_species_tree_nodes;
75     private final Set<PhylogenyNode>  _mapped_species_tree_nodes;
76     private TaxonomyComparisonBase    _tax_comp_base;
77     private final SortedSet<String>   _scientific_names_mapped_to_reduced_specificity;
78
79     public GSDI( final Phylogeny gene_tree,
80                  final Phylogeny species_tree,
81                  final boolean most_parsimonious_duplication_model,
82                  final boolean strip_gene_tree,
83                  final boolean strip_species_tree ) throws SDIException {
84         super( gene_tree, species_tree );
85         _speciation_or_duplication_events_sum = 0;
86         _speciations_sum = 0;
87         _most_parsimonious_duplication_model = most_parsimonious_duplication_model;
88         _duplications_sum = 0;
89         _strip_gene_tree = strip_gene_tree;
90         _strip_species_tree = strip_species_tree;
91         _stripped_gene_tree_nodes = new ArrayList<PhylogenyNode>();
92         _stripped_species_tree_nodes = new ArrayList<PhylogenyNode>();
93         _mapped_species_tree_nodes = new HashSet<PhylogenyNode>();
94         _scientific_names_mapped_to_reduced_specificity = new TreeSet<String>();
95         linkNodesOfG();
96         getSpeciesTree().preOrderReId();
97         geneTreePostOrderTraversal();
98     }
99
100     GSDI( final Phylogeny gene_tree, final Phylogeny species_tree, final boolean most_parsimonious_duplication_model )
101             throws SDIException {
102         this( gene_tree, species_tree, most_parsimonious_duplication_model, false, false );
103     }
104
105     // s is the node on the species tree g maps to.
106     private final void determineEvent( final PhylogenyNode s, final PhylogenyNode g ) {
107         boolean oyako = false;
108         if ( ( g.getChildNode1().getLink() == s ) || ( g.getChildNode2().getLink() == s ) ) {
109             oyako = true;
110         }
111         if ( g.getLink().getNumberOfDescendants() == 2 ) {
112             if ( oyako ) {
113                 g.getNodeData().setEvent( createDuplicationEvent() );
114             }
115             else {
116                 g.getNodeData().setEvent( createSpeciationEvent() );
117             }
118         }
119         else {
120             if ( oyako ) {
121                 final Set<PhylogenyNode> set = new HashSet<PhylogenyNode>();
122                 for( PhylogenyNode n : g.getChildNode1().getAllExternalDescendants() ) {
123                     n = n.getLink();
124                     while ( n.getParent() != s ) {
125                         n = n.getParent();
126                         if ( n.isRoot() ) {
127                             break;
128                         }
129                     }
130                     set.add( n );
131                 }
132                 boolean multiple = false;
133                 for( PhylogenyNode n : g.getChildNode2().getAllExternalDescendants() ) {
134                     n = n.getLink();
135                     while ( n.getParent() != s ) {
136                         n = n.getParent();
137                         if ( n.isRoot() ) {
138                             break;
139                         }
140                     }
141                     if ( set.contains( n ) ) {
142                         multiple = true;
143                         break;
144                     }
145                 }
146                 if ( multiple ) {
147                     g.getNodeData().setEvent( createDuplicationEvent() );
148                 }
149                 else {
150                     if ( _most_parsimonious_duplication_model ) {
151                         g.getNodeData().setEvent( createSpeciationEvent() );
152                     }
153                     else {
154                         g.getNodeData().setEvent( createSingleSpeciationOrDuplicationEvent() );
155                     }
156                 }
157             }
158             else {
159                 g.getNodeData().setEvent( createSpeciationEvent() );
160             }
161         }
162     }
163
164     /**
165      * Traverses the subtree of PhylogenyNode g in postorder, calculating the
166      * mapping function M, and determines which nodes represent speciation
167      * events and which ones duplication events.
168      * <p>
169      * Preconditions: Mapping M for external nodes must have been calculated and
170      * the species tree must be labeled in preorder.
171      * <p>
172      * 
173      */
174     final void geneTreePostOrderTraversal() {
175         for( final PhylogenyNodeIterator it = getGeneTree().iteratorPostorder(); it.hasNext(); ) {
176             final PhylogenyNode g = it.next();
177             if ( g.isInternal() ) {
178                 PhylogenyNode s1 = g.getChildNode1().getLink();
179                 PhylogenyNode s2 = g.getChildNode2().getLink();
180                 while ( s1 != s2 ) {
181                     if ( s1.getId() > s2.getId() ) {
182                         s1 = s1.getParent();
183                     }
184                     else {
185                         s2 = s2.getParent();
186                     }
187                 }
188                 g.setLink( s1 );
189                 determineEvent( s1, g );
190             }
191         }
192     }
193
194     private final Event createDuplicationEvent() {
195         final Event event = Event.createSingleDuplicationEvent();
196         ++_duplications_sum;
197         return event;
198     }
199
200     private final Event createSingleSpeciationOrDuplicationEvent() {
201         final Event event = Event.createSingleSpeciationOrDuplicationEvent();
202         ++_speciation_or_duplication_events_sum;
203         return event;
204     }
205
206     private final Event createSpeciationEvent() {
207         final Event event = Event.createSingleSpeciationEvent();
208         ++_speciations_sum;
209         return event;
210     }
211
212     public final int getSpeciationOrDuplicationEventsSum() {
213         return _speciation_or_duplication_events_sum;
214     }
215
216     public final int getSpeciationsSum() {
217         return _speciations_sum;
218     }
219
220     /**
221      * This allows for linking of internal nodes of the species tree (as opposed
222      * to just external nodes, as in the method it overrides.
223      * @throws SDIException 
224      * 
225      */
226     @Override
227     final void linkNodesOfG() throws SDIException {
228         final Map<String, PhylogenyNode> species_to_node_map = new HashMap<String, PhylogenyNode>();
229         final List<PhylogenyNode> species_tree_ext_nodes = new ArrayList<PhylogenyNode>();
230         _tax_comp_base = determineTaxonomyComparisonBase( _gene_tree );
231         // Stringyfied taxonomy is the key, node is the value.
232         for( final PhylogenyNodeIterator iter = _species_tree.iteratorExternalForward(); iter.hasNext(); ) {
233             final PhylogenyNode s = iter.next();
234             species_tree_ext_nodes.add( s );
235             if ( s.getNodeData().isHasTaxonomy() ) {
236                 final String tax_str = taxonomyToString( s, _tax_comp_base );
237                 if ( !ForesterUtil.isEmpty( tax_str ) ) {
238                     if ( species_to_node_map.containsKey( tax_str ) ) {
239                         throw new SDIException( "taxonomy \"" + s + "\" is not unique in species tree" );
240                     }
241                     species_to_node_map.put( tax_str, s );
242                 }
243             }
244         }
245         // Retrieve the reference to the node with a matching stringyfied taxonomy.
246         for( final PhylogenyNodeIterator iter = _gene_tree.iteratorExternalForward(); iter.hasNext(); ) {
247             final PhylogenyNode g = iter.next();
248             if ( !g.getNodeData().isHasTaxonomy() ) {
249                 if ( _strip_gene_tree ) {
250                     _stripped_gene_tree_nodes.add( g );
251                 }
252                 else {
253                     throw new SDIException( "gene tree node \"" + g + "\" has no taxonomic data" );
254                 }
255             }
256             else {
257                 final String tax_str = taxonomyToString( g, _tax_comp_base );
258                 if ( ForesterUtil.isEmpty( tax_str ) ) {
259                     if ( _strip_gene_tree ) {
260                         _stripped_gene_tree_nodes.add( g );
261                     }
262                     else {
263                         throw new SDIException( "gene tree node \"" + g + "\" has no appropriate taxonomic data" );
264                     }
265                 }
266                 else {
267                     PhylogenyNode s = species_to_node_map.get( tax_str );
268                     if ( ( _tax_comp_base == TaxonomyComparisonBase.SCIENTIFIC_NAME ) && ( s == null )
269                             && ( ForesterUtil.countChars( tax_str, ' ' ) > 1 ) ) {
270                         s = tryMapByRemovingOverlySpecificData( species_to_node_map, tax_str );
271                     }
272                     if ( s == null ) {
273                         if ( _strip_gene_tree ) {
274                             _stripped_gene_tree_nodes.add( g );
275                         }
276                         else {
277                             throw new SDIException( "taxonomy \"" + g.getNodeData().getTaxonomy()
278                                     + "\" not present in species tree" );
279                         }
280                     }
281                     else {
282                         g.setLink( s );
283                         _mapped_species_tree_nodes.add( s );
284                     }
285                 }
286             }
287         } // for loop
288         if ( _strip_gene_tree ) {
289             stripGeneTree();
290             if ( getGeneTree().isEmpty() || ( getGeneTree().getNumberOfExternalNodes() < 2 ) ) {
291                 throw new SDIException( "species could not be mapped between gene tree and species tree" );
292             }
293         }
294         if ( _strip_species_tree ) {
295             stripSpeciesTree( species_tree_ext_nodes );
296         }
297     }
298
299     private final PhylogenyNode tryMapByRemovingOverlySpecificData( final Map<String, PhylogenyNode> species_to_node_map,
300                                                                     final String tax_str ) {
301         PhylogenyNode s = tryMapByRemovingOverlySpecificData( species_to_node_map, tax_str, " (" );
302         if ( s == null ) {
303             if ( ForesterUtil.countChars( tax_str, ' ' ) == 2 ) {
304                 final String new_tax_str = tax_str.substring( 0, tax_str.lastIndexOf( ' ' ) ).trim();
305                 s = species_to_node_map.get( new_tax_str );
306                 if ( s != null ) {
307                     addScientificNamesMappedToReducedSpecificity( tax_str, new_tax_str );
308                 }
309             }
310         }
311         if ( s == null ) {
312             for( final String t : new String[] { " subspecies ", " strain ", " variety ", " varietas ", " subvariety ",
313                     " form ", " subform ", " cultivar ", " section ", " subsection " } ) {
314                 s = tryMapByRemovingOverlySpecificData( species_to_node_map, tax_str, t );
315                 if ( s != null ) {
316                     break;
317                 }
318             }
319         }
320         return s;
321     }
322
323     private final PhylogenyNode tryMapByRemovingOverlySpecificData( final Map<String, PhylogenyNode> species_to_node_map,
324                                                                     final String tax_str,
325                                                                     final String term ) {
326         final int i = tax_str.indexOf( term );
327         if ( i > 4 ) {
328             final String new_tax_str = tax_str.substring( 0, i ).trim();
329             final PhylogenyNode s = species_to_node_map.get( new_tax_str );
330             if ( s != null ) {
331                 addScientificNamesMappedToReducedSpecificity( tax_str, new_tax_str );
332             }
333             return s;
334         }
335         return null;
336     }
337
338     private final void addScientificNamesMappedToReducedSpecificity( final String s1, final String s2 ) {
339         _scientific_names_mapped_to_reduced_specificity.add( s1 + " -> " + s2 );
340     }
341
342     public final SortedSet<String> getReMappedScientificNamesFromGeneTree() {
343         return _scientific_names_mapped_to_reduced_specificity;
344     }
345
346     public TaxonomyComparisonBase getTaxCompBase() {
347         return _tax_comp_base;
348     }
349
350     private void stripSpeciesTree( final List<PhylogenyNode> species_tree_ext_nodes ) {
351         for( final PhylogenyNode s : species_tree_ext_nodes ) {
352             if ( !_mapped_species_tree_nodes.contains( s ) ) {
353                 _species_tree.deleteSubtree( s, true );
354                 _stripped_species_tree_nodes.add( s );
355             }
356         }
357         _species_tree.clearHashIdToNodeMap();
358         _species_tree.externalNodesHaveChanged();
359     }
360
361     public List<PhylogenyNode> getStrippedSpeciesTreeNodes() {
362         return _stripped_species_tree_nodes;
363     }
364
365     private void stripGeneTree() {
366         for( final PhylogenyNode g : _stripped_gene_tree_nodes ) {
367             _gene_tree.deleteSubtree( g, true );
368         }
369         _gene_tree.clearHashIdToNodeMap();
370         _gene_tree.externalNodesHaveChanged();
371     }
372
373     public Set<PhylogenyNode> getMappedExternalSpeciesTreeNodes() {
374         return _mapped_species_tree_nodes;
375     }
376
377     public static TaxonomyComparisonBase determineTaxonomyComparisonBase( final Phylogeny gene_tree ) {
378         int with_id_count = 0;
379         int with_code_count = 0;
380         int with_sn_count = 0;
381         int max = 0;
382         for( final PhylogenyNodeIterator iter = gene_tree.iteratorExternalForward(); iter.hasNext(); ) {
383             final PhylogenyNode g = iter.next();
384             if ( g.getNodeData().isHasTaxonomy() ) {
385                 final Taxonomy tax = g.getNodeData().getTaxonomy();
386                 if ( ( tax.getIdentifier() != null ) && !ForesterUtil.isEmpty( tax.getIdentifier().getValue() ) ) {
387                     if ( ++with_id_count > max ) {
388                         max = with_id_count;
389                     }
390                 }
391                 if ( !ForesterUtil.isEmpty( tax.getTaxonomyCode() ) ) {
392                     if ( ++with_code_count > max ) {
393                         max = with_code_count;
394                     }
395                 }
396                 if ( !ForesterUtil.isEmpty( tax.getScientificName() ) ) {
397                     if ( ++with_sn_count > max ) {
398                         max = with_sn_count;
399                     }
400                 }
401             }
402         }
403         if ( max == 0 ) {
404             throw new IllegalArgumentException( "gene tree has no taxonomic data" );
405         }
406         else if ( max == 1 ) {
407             throw new IllegalArgumentException( "gene tree has only one node with taxonomic data" );
408         }
409         else if ( max == with_id_count ) {
410             return SDI.TaxonomyComparisonBase.ID;
411         }
412         else if ( max == with_sn_count ) {
413             return SDI.TaxonomyComparisonBase.SCIENTIFIC_NAME;
414         }
415         else {
416             return SDI.TaxonomyComparisonBase.CODE;
417         }
418     }
419
420     public List<PhylogenyNode> getStrippedExternalGeneTreeNodes() {
421         return _stripped_gene_tree_nodes;
422     }
423
424     @Override
425     public final String toString() {
426         final StringBuffer sb = new StringBuffer();
427         sb.append( "Most parsimonious duplication model: " + _most_parsimonious_duplication_model );
428         sb.append( ForesterUtil.getLineSeparator() );
429         sb.append( "Speciations sum                    : " + getSpeciationsSum() );
430         sb.append( ForesterUtil.getLineSeparator() );
431         sb.append( "Duplications sum                   : " + getDuplicationsSum() );
432         sb.append( ForesterUtil.getLineSeparator() );
433         if ( !_most_parsimonious_duplication_model ) {
434             sb.append( "Speciation or duplications sum     : " + getSpeciationOrDuplicationEventsSum() );
435             sb.append( ForesterUtil.getLineSeparator() );
436         }
437         sb.append( "mapping cost L                     : " + computeMappingCostL() );
438         return sb.toString();
439     }
440 }