in progress
[jalview.git] / forester / java / src / org / forester / archaeopteryx / TreePanelUtil.java
1
2 package org.forester.archaeopteryx;
3
4 import java.awt.Color;
5 import java.awt.Component;
6 import java.io.UnsupportedEncodingException;
7 import java.net.URLEncoder;
8 import java.util.ArrayList;
9 import java.util.Collections;
10 import java.util.HashMap;
11 import java.util.HashSet;
12 import java.util.List;
13 import java.util.Map;
14 import java.util.Map.Entry;
15 import java.util.Set;
16 import java.util.SortedMap;
17 import java.util.SortedSet;
18 import java.util.TreeMap;
19
20 import javax.swing.JOptionPane;
21
22 import org.forester.analysis.TaxonomyDataManager;
23 import org.forester.phylogeny.Phylogeny;
24 import org.forester.phylogeny.PhylogenyMethods;
25 import org.forester.phylogeny.PhylogenyNode;
26 import org.forester.phylogeny.data.Accession;
27 import org.forester.phylogeny.data.Annotation;
28 import org.forester.phylogeny.data.BranchColor;
29 import org.forester.phylogeny.data.NodeDataField;
30 import org.forester.phylogeny.data.Sequence;
31 import org.forester.phylogeny.data.Taxonomy;
32 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
33 import org.forester.phylogeny.iterators.PreorderTreeIterator;
34 import org.forester.util.ForesterConstants;
35 import org.forester.util.ForesterUtil;
36 import org.forester.util.SequenceAccessionTools;
37 import org.forester.util.StringInt;
38 import org.forester.ws.seqdb.UniProtTaxonomy;
39
40 public class TreePanelUtil {
41
42     public final static String createUriForSeqWeb( final PhylogenyNode node,
43                                                    final Configuration conf,
44                                                    final TreePanel tp ) {
45         String uri_str = null;
46         final String upkb = SequenceAccessionTools.obtainUniProtAccessorFromDataFields( node );
47         if ( !ForesterUtil.isEmpty( upkb ) ) {
48             try {
49                 uri_str = ForesterUtil.UNIPROT_KB + URLEncoder.encode( upkb, ForesterConstants.UTF_8 );
50             }
51             catch ( final UnsupportedEncodingException e ) {
52                 AptxUtil.showErrorMessage( tp, e.toString() );
53                 e.printStackTrace();
54             }
55         }
56         if ( ForesterUtil.isEmpty( uri_str ) ) {
57             final String v = SequenceAccessionTools.obtainGenbankAccessorFromDataFields( node );
58             if ( !ForesterUtil.isEmpty( v ) ) {
59                 try {
60                     if ( SequenceAccessionTools.isProteinDbQuery( v ) ) {
61                         uri_str = ForesterUtil.NCBI_PROTEIN + URLEncoder.encode( v, ForesterConstants.UTF_8 );
62                     }
63                     else {
64                         uri_str = ForesterUtil.NCBI_NUCCORE + URLEncoder.encode( v, ForesterConstants.UTF_8 );
65                     }
66                 }
67                 catch ( final UnsupportedEncodingException e ) {
68                     AptxUtil.showErrorMessage( tp, e.toString() );
69                     e.printStackTrace();
70                 }
71             }
72         }
73         if ( ForesterUtil.isEmpty( uri_str ) ) {
74             final String v = SequenceAccessionTools.obtainRefSeqAccessorFromDataFields( node );
75             if ( !ForesterUtil.isEmpty( v ) ) {
76                 try {
77                     if ( SequenceAccessionTools.isProteinDbQuery( v ) ) {
78                         uri_str = ForesterUtil.NCBI_PROTEIN + URLEncoder.encode( v, ForesterConstants.UTF_8 );
79                     }
80                     else {
81                         uri_str = ForesterUtil.NCBI_NUCCORE + URLEncoder.encode( v, ForesterConstants.UTF_8 );
82                     }
83                 }
84                 catch ( final UnsupportedEncodingException e ) {
85                     AptxUtil.showErrorMessage( tp, e.toString() );
86                     e.printStackTrace();
87                 }
88             }
89         }
90         if ( ForesterUtil.isEmpty( uri_str ) ) {
91             final String v = SequenceAccessionTools.obtainGiNumberFromDataFields( node );
92             if ( !ForesterUtil.isEmpty( v ) ) {
93                 try {
94                     uri_str = ForesterUtil.NCBI_GI + URLEncoder.encode( v, ForesterConstants.UTF_8 );
95                 }
96                 catch ( final UnsupportedEncodingException e ) {
97                     AptxUtil.showErrorMessage( tp, e.toString() );
98                     e.printStackTrace();
99                 }
100             }
101         }
102         return uri_str;
103     }
104
105     public static List<String> createUrisForPdbWeb( final PhylogenyNode node,
106                                                     final List<Accession> pdb_accs,
107                                                     final Configuration configuration,
108                                                     final TreePanel treePanel ) {
109         final List<String> uris = new ArrayList<String>();
110         if ( !ForesterUtil.isEmpty( pdb_accs ) ) {
111             for( final Accession pdb_acc : pdb_accs ) {
112                 if ( !ForesterUtil.isEmpty( pdb_acc.getValue() ) ) {
113                     uris.add( ForesterUtil.PDB + pdb_acc.getValue() );
114                 }
115             }
116         }
117         return uris;
118     }
119
120     /**
121      * Returns the set of distinct taxonomies of
122      * all external nodes of node.
123      * If at least one the external nodes has no taxonomy,
124      * null is returned.
125      *
126      */
127     public static Set<Taxonomy> obtainDistinctTaxonomies( final PhylogenyNode node ) {
128         final List<PhylogenyNode> descs = node.getAllExternalDescendants();
129         final Set<Taxonomy> tax_set = new HashSet<Taxonomy>();
130         for( final PhylogenyNode n : descs ) {
131             if ( !n.getNodeData().isHasTaxonomy() || n.getNodeData().getTaxonomy().isEmpty() ) {
132                 return null;
133             }
134             tax_set.add( n.getNodeData().getTaxonomy() );
135         }
136         return tax_set;
137     }
138
139     public final static void showExtDescNodeDataUserSelectedHelper( final ControlPanel cp,
140                                                                     final PhylogenyNode node,
141                                                                     final List<String> data ) {
142         final StringBuilder sb = new StringBuilder();
143         if ( cp.isShowNodeNames() && !ForesterUtil.isEmpty( node.getName() ) ) {
144             TreePanelUtil.showExtDescNodeDataUserSelectedHelperHelper( node.getName(), sb );
145         }
146         if ( cp.isShowSeqNames() && node.getNodeData().isHasSequence()
147                 && !ForesterUtil.isEmpty( node.getNodeData().getSequence().getName() ) ) {
148             TreePanelUtil.showExtDescNodeDataUserSelectedHelperHelper( node.getNodeData().getSequence().getName(), sb );
149         }
150         if ( cp.isShowSeqSymbols() && node.getNodeData().isHasSequence()
151                 && !ForesterUtil.isEmpty( node.getNodeData().getSequence().getSymbol() ) ) {
152             TreePanelUtil
153                     .showExtDescNodeDataUserSelectedHelperHelper( node.getNodeData().getSequence().getSymbol(), sb );
154         }
155         if ( cp.isShowGeneNames() && node.getNodeData().isHasSequence()
156                 && !ForesterUtil.isEmpty( node.getNodeData().getSequence().getGeneName() ) ) {
157             TreePanelUtil.showExtDescNodeDataUserSelectedHelperHelper( node.getNodeData().getSequence().getGeneName(),
158                                                                        sb );
159         }
160         if ( cp.isShowSequenceAcc() && node.getNodeData().isHasSequence()
161                 && ( node.getNodeData().getSequence().getAccession() != null )
162                 && !ForesterUtil.isEmpty( node.getNodeData().getSequence().getAccession().toString() ) ) {
163             TreePanelUtil.showExtDescNodeDataUserSelectedHelperHelper( node.getNodeData().getSequence().getAccession()
164                     .toString(), sb );
165         }
166         if ( cp.isShowTaxonomyCode() && node.getNodeData().isHasTaxonomy()
167                 && !ForesterUtil.isEmpty( node.getNodeData().getTaxonomy().getTaxonomyCode() ) ) {
168             TreePanelUtil.showExtDescNodeDataUserSelectedHelperHelper( node.getNodeData().getTaxonomy()
169                     .getTaxonomyCode(), sb );
170         }
171         if ( cp.isShowTaxonomyScientificNames() && node.getNodeData().isHasTaxonomy()
172                 && !ForesterUtil.isEmpty( node.getNodeData().getTaxonomy().getScientificName() ) ) {
173             TreePanelUtil.showExtDescNodeDataUserSelectedHelperHelper( node.getNodeData().getTaxonomy()
174                     .getScientificName(), sb );
175         }
176         if ( cp.isShowTaxonomyCommonNames() && node.getNodeData().isHasTaxonomy()
177                 && !ForesterUtil.isEmpty( node.getNodeData().getTaxonomy().getCommonName() ) ) {
178             TreePanelUtil
179                     .showExtDescNodeDataUserSelectedHelperHelper( node.getNodeData().getTaxonomy().getCommonName(), sb );
180         }
181         //        if ( ( cp.isShowSeqNames() || cp.isShowSeqSymbols() || cp.isShowSequenceAcc() )
182         //                && node.getNodeData().isHasSequence()
183         //                && !ForesterUtil.isEmpty( node.getNodeData().getSequence().getMolecularSequence() ) ) {
184         //            TreePanelUtil.showExtDescNodeDataUserSelectedHelperHelper( node.getNodeData().getSequence()
185         //                    .getMolecularSequence(), sb );
186         //        }
187         final String s = sb.toString().trim();
188         if ( !ForesterUtil.isEmpty( s ) ) {
189             data.add( s );
190         }
191     }
192
193     public final static void showExtDescNodeDataUserSelectedHelperHelper( final String s, final StringBuilder sb ) {
194         if ( sb.length() > 0 ) {
195             sb.append( "\t" );
196         }
197         sb.append( s );
198     }
199
200     final public static void showInformationMessage( final Component parent, final String title, final String msg ) {
201         JOptionPane.showMessageDialog( parent, msg, title, JOptionPane.INFORMATION_MESSAGE );
202     }
203
204     final static void collapseSpeciesSpecificSubtrees( final Phylogeny phy ) {
205         boolean inferred = false;
206         for( final PhylogenyNodeIterator it = phy.iteratorPreorder(); it.hasNext(); ) {
207             final PhylogenyNode n = it.next();
208             if ( !n.isExternal() && !n.isCollapse() && ( n.getNumberOfDescendants() > 1 ) ) {
209                 final Set<Taxonomy> taxs = TreePanelUtil.obtainDistinctTaxonomies( n );
210                 if ( ( taxs != null ) && ( taxs.size() == 1 ) ) {
211                     TreePanelUtil.collapseSubtree( n, true );
212                     if ( !n.getNodeData().isHasTaxonomy() ) {
213                         n.getNodeData().setTaxonomy( ( Taxonomy ) n.getAllExternalDescendants().get( 0 ).getNodeData()
214                                 .getTaxonomy().copy() );
215                     }
216                     inferred = true;
217                 }
218                 else {
219                     n.setCollapse( false );
220                 }
221             }
222         }
223         if ( inferred ) {
224             phy.setRerootable( false );
225         }
226     }
227
228     final static void collapseSubtree( final PhylogenyNode node, final boolean collapse ) {
229         node.setCollapse( collapse );
230         if ( node.isExternal() ) {
231             return;
232         }
233         final PhylogenyNodeIterator it = new PreorderTreeIterator( node );
234         while ( it.hasNext() ) {
235             it.next().setCollapse( collapse );
236         }
237     }
238
239     static void colorizeSubtree( final PhylogenyNode node, final BranchColor c ) {
240         node.getBranchData().setBranchColor( c );
241         final List<PhylogenyNode> descs = PhylogenyMethods.getAllDescendants( node );
242         for( final PhylogenyNode desc : descs ) {
243             desc.getBranchData().setBranchColor( c );
244         }
245     }
246
247     final static void colorPhylogenyAccordingToConfidenceValues( final Phylogeny tree, final TreePanel tree_panel ) {
248         double max_conf = 0.0;
249         for( final PhylogenyNodeIterator it = tree.iteratorPreorder(); it.hasNext(); ) {
250             final PhylogenyNode n = it.next();
251             n.getBranchData().setBranchColor( null );
252             if ( n.getBranchData().isHasConfidences() ) {
253                 final double conf = PhylogenyMethods.getConfidenceValue( n );
254                 if ( conf > max_conf ) {
255                     max_conf = conf;
256                 }
257             }
258         }
259         if ( max_conf > 0.0 ) {
260             final Color bg = tree_panel.getTreeColorSet().getBackgroundColor();
261             final Color br = tree_panel.getTreeColorSet().getBranchColor();
262             for( final PhylogenyNodeIterator it = tree.iteratorPreorder(); it.hasNext(); ) {
263                 final PhylogenyNode n = it.next();
264                 if ( n.getBranchData().isHasConfidences() ) {
265                     final double conf = PhylogenyMethods.getConfidenceValue( n );
266                     final BranchColor c = new BranchColor( ForesterUtil.calcColor( conf, 0.0, max_conf, bg, br ) );
267                     TreePanelUtil.colorizeSubtree( n, c );
268                 }
269             }
270         }
271     }
272
273     final static void colorPhylogenyAccordingToExternalTaxonomy( final Phylogeny tree, final TreePanel tree_panel ) {
274         for( final PhylogenyNodeIterator it = tree.iteratorPreorder(); it.hasNext(); ) {
275             it.next().getBranchData().setBranchColor( null );
276         }
277         for( final PhylogenyNodeIterator it = tree.iteratorPreorder(); it.hasNext(); ) {
278             final PhylogenyNode n = it.next();
279             if ( !n.getBranchData().isHasBranchColor() ) {
280                 final Taxonomy tax = PhylogenyMethods.getExternalDescendantsTaxonomy( n );
281                 if ( tax != null ) {
282                     n.getBranchData().setBranchColor( new BranchColor( tree_panel.calculateTaxonomyBasedColor( tax ) ) );
283                     final List<PhylogenyNode> descs = PhylogenyMethods.getAllDescendants( n );
284                     for( final PhylogenyNode desc : descs ) {
285                         desc.getBranchData()
286                                 .setBranchColor( new BranchColor( tree_panel.calculateTaxonomyBasedColor( tax ) ) );
287                     }
288                 }
289             }
290         }
291     }
292
293     final static int colorPhylogenyAccordingToRanks( final Phylogeny tree, final String rank, final TreePanel tree_panel ) {
294         final Map<String, Color> true_lineage_to_color_map = new HashMap<String, Color>();
295         int colorizations = 0;
296         for( final PhylogenyNodeIterator it = tree.iteratorPostorder(); it.hasNext(); ) {
297             final PhylogenyNode n = it.next();
298             if ( n.getNodeData().isHasTaxonomy()
299                     && ( !ForesterUtil.isEmpty( n.getNodeData().getTaxonomy().getScientificName() )
300                             || !ForesterUtil.isEmpty( n.getNodeData().getTaxonomy().getCommonName() ) || !ForesterUtil
301                                 .isEmpty( n.getNodeData().getTaxonomy().getTaxonomyCode() ) ) ) {
302                 if ( !ForesterUtil.isEmpty( n.getNodeData().getTaxonomy().getRank() )
303                         && n.getNodeData().getTaxonomy().getRank().equalsIgnoreCase( rank ) ) {
304                     final BranchColor c = new BranchColor( tree_panel.calculateTaxonomyBasedColor( n.getNodeData()
305                             .getTaxonomy() ) );
306                     TreePanelUtil.colorizeSubtree( n, c );
307                     ++colorizations;
308                     if ( !ForesterUtil.isEmpty( n.getNodeData().getTaxonomy().getScientificName() ) ) {
309                         true_lineage_to_color_map.put( n.getNodeData().getTaxonomy().getScientificName(), c.getValue() );
310                     }
311                 }
312             }
313         }
314         for( final PhylogenyNodeIterator it = tree.iteratorPostorder(); it.hasNext(); ) {
315             final PhylogenyNode node = it.next();
316             if ( ( node.getBranchData().getBranchColor() == null ) && node.getNodeData().isHasTaxonomy()
317                     && !ForesterUtil.isEmpty( node.getNodeData().getTaxonomy().getLineage() ) ) {
318                 boolean success = false;
319                 if ( !true_lineage_to_color_map.isEmpty() ) {
320                     for( final String lin : node.getNodeData().getTaxonomy().getLineage() ) {
321                         if ( true_lineage_to_color_map.containsKey( lin ) ) {
322                             TreePanelUtil
323                                     .colorizeSubtree( node, new BranchColor( true_lineage_to_color_map.get( lin ) ) );
324                             ++colorizations;
325                             success = true;
326                             break;
327                         }
328                     }
329                 }
330                 if ( !success ) {
331                     final Map<String, String> lineage_to_rank_map = MainPanel.getLineageToRankMap();
332                     for( final String lin : node.getNodeData().getTaxonomy().getLineage() ) {
333                         final Taxonomy temp_tax = new Taxonomy();
334                         temp_tax.setScientificName( lin );
335                         if ( lineage_to_rank_map.containsKey( lin )
336                                 && !ForesterUtil.isEmpty( lineage_to_rank_map.get( lin ) )
337                                 && lineage_to_rank_map.get( lin ).equalsIgnoreCase( rank ) ) {
338                             final BranchColor c = new BranchColor( tree_panel.calculateTaxonomyBasedColor( temp_tax ) );
339                             TreePanelUtil.colorizeSubtree( node, c );
340                             ++colorizations;
341                             true_lineage_to_color_map.put( lin, c.getValue() );
342                             break;
343                         }
344                         else {
345                             UniProtTaxonomy up = null;
346                             try {
347                                 up = TaxonomyDataManager.obtainUniProtTaxonomy( temp_tax, null, null );
348                             }
349                             catch ( final Exception e ) {
350                                 e.printStackTrace();
351                             }
352                             if ( ( up != null ) && !ForesterUtil.isEmpty( up.getRank() ) ) {
353                                 lineage_to_rank_map.put( lin, up.getRank() );
354                                 if ( up.getRank().equalsIgnoreCase( rank ) ) {
355                                     final BranchColor c = new BranchColor( tree_panel.calculateTaxonomyBasedColor( temp_tax ) );
356                                     TreePanelUtil.colorizeSubtree( node, c );
357                                     ++colorizations;
358                                     true_lineage_to_color_map.put( lin, c.getValue() );
359                                     break;
360                                 }
361                             }
362                         }
363                     }
364                 }
365             }
366         }
367         return colorizations;
368     }
369
370     final static String createAnnotationString( final SortedSet<Annotation> annotations, final boolean show_ref_sources ) {
371         final SortedMap<String, List<Annotation>> m = new TreeMap<String, List<Annotation>>();
372         for( final Annotation an : annotations ) {
373             final String ref_source = ForesterUtil.isEmpty( an.getRefSource() ) ? "?" : an.getRefSource();
374             if ( !m.containsKey( ref_source ) ) {
375                 m.put( ref_source, new ArrayList<Annotation>() );
376             }
377             m.get( ref_source ).add( an );
378         }
379         final StringBuilder sb = new StringBuilder();
380         for( final Entry<String, List<Annotation>> e : m.entrySet() ) {
381             final String ref_source = e.getKey();
382             final List<Annotation> ans = e.getValue();
383             if ( m.size() > 1 ) {
384                 sb.append( "[" );
385             }
386             if ( show_ref_sources && !ref_source.equals( "?" ) ) {
387                 sb.append( ref_source );
388                 sb.append( ": " );
389             }
390             for( int i = 0; i < ans.size(); ++i ) {
391                 final Annotation an = ans.get( i );
392                 if ( !ForesterUtil.isEmpty( an.getRefValue() ) ) {
393                     sb.append( an.getRefValue() );
394                     sb.append( " " );
395                 }
396                 if ( !ForesterUtil.isEmpty( an.getDesc() ) ) {
397                     sb.append( an.getDesc() );
398                 }
399                 if ( sb.charAt( sb.length() - 1 ) == ' ' ) {
400                     sb.deleteCharAt( sb.length() - 1 );
401                 }
402                 if ( i < ( ans.size() - 1 ) ) {
403                     sb.append( ", " );
404                 }
405             }
406             if ( m.size() > 1 ) {
407                 sb.append( "] " );
408             }
409         }
410         return sb.toString();
411     }
412
413     final static String getPartAfterColon( final String s ) {
414         final int i = s.indexOf( ':' );
415         if ( ( i < 1 ) || ( i == ( s.length() - 1 ) ) ) {
416             return s;
417         }
418         return s.substring( i + 1, s.length() );
419     }
420
421     final static boolean isHasAssignedEvent( final PhylogenyNode node ) {
422         if ( !node.getNodeData().isHasEvent() ) {
423             return false;
424         }
425         if ( ( node.getNodeData().getEvent() ).isUnassigned() ) {
426             return false;
427         }
428         return true;
429     }
430
431     final static boolean isSequenceEmpty( final Sequence seq ) {
432         return ( seq.getAccession() == null ) && ForesterUtil.isEmpty( seq.getName() )
433                 && ForesterUtil.isEmpty( seq.getGeneName() ) && ForesterUtil.isEmpty( seq.getSymbol() );
434     }
435
436     final static boolean isTaxonomyEmpty( final Taxonomy tax ) {
437         return ( ( tax.getIdentifier() == null ) && ForesterUtil.isEmpty( tax.getTaxonomyCode() )
438                 && ForesterUtil.isEmpty( tax.getCommonName() ) && ForesterUtil.isEmpty( tax.getScientificName() ) && tax
439                 .getSynonyms().isEmpty() );
440     }
441
442     static final int nodeDataIntoStringBuffer( final List<String> data, final Options optz, final StringBuilder sb ) {
443         final SortedMap<String, Integer> map = new TreeMap<String, Integer>();
444         int size = 0;
445         if ( ( optz.getExtDescNodeDataToReturn() != NodeDataField.SEQUENCE_MOL_SEQ_FASTA )
446                 && ( optz.getExtDescNodeDataToReturn() != NodeDataField.GO_TERM_IDS ) ) {
447             for( final String d : data ) {
448                 if ( !ForesterUtil.isEmpty( d ) ) {
449                     if ( map.containsKey( d ) ) {
450                         map.put( d, map.get( d ) + 1 );
451                     }
452                     else {
453                         map.put( d, 1 );
454                     }
455                 }
456             }
457             if ( ( optz.getExtDescNodeDataToReturn() == NodeDataField.DOMAINS_ALL )
458                     || ( optz.getExtDescNodeDataToReturn() == NodeDataField.DOMAINS_COLLAPSED_PER_PROTEIN )
459                     || ( optz.getExtDescNodeDataToReturn() == NodeDataField.SEQ_ANNOTATIONS ) ) {
460                 final ArrayList<StringInt> sis = new ArrayList<StringInt>();
461                 for( final Entry<String, Integer> e : map.entrySet() ) {
462                     sis.add( new StringInt( e.getKey(), e.getValue() ) );
463                 }
464                 Collections.sort( sis, new StringInt.DescendingIntComparator() );
465                 for( final StringInt si : sis ) {
466                     sb.append( si.getString() );
467                     sb.append( "\t" );
468                     sb.append( si.getInt() );
469                     sb.append( ForesterUtil.LINE_SEPARATOR );
470                 }
471             }
472             else {
473                 for( final Entry<String, Integer> e : map.entrySet() ) {
474                     final String v = e.getKey();
475                     final Object c = e.getValue();
476                     sb.append( v );
477                     sb.append( "\t" );
478                     sb.append( c );
479                     sb.append( ForesterUtil.LINE_SEPARATOR );
480                 }
481             }
482             size = map.size();
483         }
484         else {
485             for( final String d : data ) {
486                 if ( !ForesterUtil.isEmpty( d ) ) {
487                     sb.append( d );
488                     sb.append( ForesterUtil.LINE_SEPARATOR );
489                 }
490             }
491             size = data.size();
492         }
493         return size;
494     }
495
496     final static String pdbAccToString( final List<Accession> accs, final int i ) {
497         if ( ForesterUtil.isEmpty( accs.get( i ).getComment() ) ) {
498             return accs.get( i ).getValue();
499         }
500         return accs.get( i ).getValue() + " (" + accs.get( i ).getComment().toLowerCase() + ")";
501     }
502
503     final static Phylogeny subTree( final PhylogenyNode new_root, final Phylogeny source_phy ) {
504         final Phylogeny new_phy = new Phylogeny();
505         new_phy.setRooted( true );
506         new_phy.setName( source_phy.getName() );
507         new_phy.setDescription( source_phy.getDescription() );
508         new_phy.setType( source_phy.getType() );
509         new_phy.setDistanceUnit( source_phy.getDistanceUnit() );
510         new_phy.setConfidence( source_phy.getConfidence() );
511         new_phy.setIdentifier( source_phy.getIdentifier() );
512         new_phy.setRoot( new_root.copyNodeDataShallow() );
513         int i = 0;
514         for( final PhylogenyNode n : new_root.getDescendants() ) {
515             new_phy.getRoot().setChildNode( i++, n );
516         }
517         return new_phy;
518     }
519 }