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