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
7 // Copyright (C) 2000-2001 Washington University School of Medicine
8 // and Howard Hughes Medical Institute
11 // This library is free software; you can redistribute it and/or
12 // modify it under the terms of the GNU Lesser General Public
13 // License as published by the Free Software Foundation; either
14 // version 2.1 of the License, or (at your option) any later version.
16 // This library is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 // Lesser General Public License for more details.
21 // You should have received a copy of the GNU Lesser General Public
22 // License along with this library; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25 // Contact: phylosoft @ gmail . com
26 // WWW: www.phylosoft.org/forester
28 package org.forester.rio;
31 import java.io.FileNotFoundException;
32 import java.io.IOException;
33 import java.util.ArrayList;
34 import java.util.Collections;
35 import java.util.HashMap;
36 import java.util.HashSet;
37 import java.util.List;
40 import org.forester.datastructures.IntMatrix;
41 import org.forester.io.parsers.PhylogenyParser;
42 import org.forester.io.parsers.nhx.NHXParser;
43 import org.forester.io.parsers.util.ParserUtils;
44 import org.forester.phylogeny.Phylogeny;
45 import org.forester.phylogeny.PhylogenyMethods;
46 import org.forester.phylogeny.PhylogenyNode;
47 import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
48 import org.forester.phylogeny.factories.PhylogenyFactory;
49 import org.forester.sdi.GSDIR;
50 import org.forester.sdi.SDIException;
51 import org.forester.sdi.SDIR;
52 import org.forester.sdi.SDIutil.ALGORITHM;
53 import org.forester.sdi.SDIutil.TaxonomyComparisonBase;
54 import org.forester.util.BasicDescriptiveStatistics;
55 import org.forester.util.ForesterUtil;
57 public final class RIO {
59 private final static boolean ROOT_BY_MINIMIZING_SUM_OF_DUPS = true;
60 private final static boolean ROOT_BY_MINIMIZING_TREE_HEIGHT = true;
61 private Phylogeny[] _analyzed_gene_trees;
62 private List<PhylogenyNode> _removed_gene_tree_nodes;
63 private int _ext_nodes;
64 private TaxonomyComparisonBase _gsdir_tax_comp_base;
65 private StringBuilder _log;
66 private boolean _produce_log;
67 private boolean _verbose;
69 public RIO( final File gene_trees_file,
70 final Phylogeny species_tree,
71 final ALGORITHM algorithm,
72 final boolean produce_log,
73 final boolean verbose ) throws IOException, SDIException, RIOException {
74 init( produce_log, verbose );
75 inferOrthologs( gene_trees_file, species_tree, algorithm );
78 public RIO( final Phylogeny[] gene_trees,
79 final Phylogeny species_tree,
80 final ALGORITHM algorithm,
81 final boolean produce_log,
82 final boolean verbose ) throws IOException, SDIException, RIOException {
83 init( produce_log, verbose );
84 inferOrthologs( gene_trees, species_tree, algorithm );
87 public final Phylogeny[] getAnalyzedGeneTrees() {
88 return _analyzed_gene_trees;
92 * Returns the numbers of number of ext nodes in gene trees analyzed (after
95 * @return number of ext nodes in gene trees analyzed (after stripping)
97 public final int getExtNodesOfAnalyzedGeneTrees() {
101 public final TaxonomyComparisonBase getGSDIRtaxCompBase() {
102 return _gsdir_tax_comp_base;
105 public final StringBuilder getLog() {
109 public final List<PhylogenyNode> getRemovedGeneTreeNodes() {
110 return _removed_gene_tree_nodes;
113 private final void inferOrthologs( final File gene_trees_file,
114 final Phylogeny species_tree,
115 final ALGORITHM algorithm ) throws SDIException, RIOException,
116 FileNotFoundException, IOException {
117 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
118 final PhylogenyParser p = ParserUtils.createParserDependingOnFileType( gene_trees_file, true );
119 if ( p instanceof NHXParser ) {
120 final NHXParser nhx = ( NHXParser ) p;
121 nhx.setReplaceUnderscores( false );
122 nhx.setIgnoreQuotes( true );
123 nhx.setTaxonomyExtraction( NHXParser.TAXONOMY_EXTRACTION.YES );
125 final Phylogeny[] gene_trees = factory.create( gene_trees_file, p );
126 inferOrthologs( gene_trees, species_tree, algorithm );
129 private final void inferOrthologs( final Phylogeny[] gene_trees,
130 final Phylogeny species_tree,
131 final ALGORITHM algorithm ) throws SDIException, RIOException,
132 FileNotFoundException, IOException {
133 if ( algorithm == ALGORITHM.SDIR ) {
134 // Removes from species_tree all species not found in gene_tree.
135 PhylogenyMethods.taxonomyBasedDeletionOfExternalNodes( gene_trees[ 0 ], species_tree );
136 if ( species_tree.isEmpty() ) {
137 throw new RIOException( "failed to establish species based mapping between gene and species trees" );
140 if ( _produce_log ) {
141 _log = new StringBuilder();
144 _analyzed_gene_trees = new Phylogeny[ gene_trees.length ];
145 int gene_tree_ext_nodes = 0;
147 System.out.println();
149 for( int i = 0; i < gene_trees.length; ++i ) {
150 final Phylogeny gt = gene_trees[ i ];
152 ForesterUtil.updateProgress( ( double ) i / gene_trees.length );
155 gene_tree_ext_nodes = gt.getNumberOfExternalNodes();
157 else if ( gene_tree_ext_nodes != gt.getNumberOfExternalNodes() ) {
158 throw new RIOException( "gene tree #" + ( i + 1 ) + " has a different number of external nodes ("
159 + gt.getNumberOfExternalNodes() + ") than the preceding gene trees (" + gene_tree_ext_nodes
162 if ( algorithm == ALGORITHM.SDIR ) {
163 // Removes from gene_tree all species not found in species_tree.
164 PhylogenyMethods.taxonomyBasedDeletionOfExternalNodes( species_tree, gt );
165 if ( gt.isEmpty() ) {
166 throw new RIOException( "failed to establish species based mapping between gene and species trees" );
169 _analyzed_gene_trees[ i ] = performOrthologInference( gt, species_tree, algorithm, i );
172 System.out.println();
173 System.out.println();
177 private final void init( final boolean produce_log, final boolean verbose ) {
178 _produce_log = produce_log;
182 _gsdir_tax_comp_base = null;
183 _analyzed_gene_trees = null;
184 _removed_gene_tree_nodes = null;
187 private final Phylogeny performOrthologInference( final Phylogeny gene_tree,
188 final Phylogeny species_tree,
189 final ALGORITHM algorithm,
190 final int i ) throws SDIException, RIOException {
191 final Phylogeny assigned_tree;
192 switch ( algorithm ) {
194 final SDIR sdir = new SDIR();
195 assigned_tree = sdir.infer( gene_tree,
198 RIO.ROOT_BY_MINIMIZING_SUM_OF_DUPS,
199 RIO.ROOT_BY_MINIMIZING_TREE_HEIGHT,
205 final GSDIR gsdir = new GSDIR( gene_tree, species_tree, true, i == 0 );
206 final List<Phylogeny> assigned_trees = gsdir.getMinDuplicationsSumGeneTrees();
208 _removed_gene_tree_nodes = gsdir.getStrippedExternalGeneTreeNodes();
209 for( final PhylogenyNode r : _removed_gene_tree_nodes ) {
210 if ( !r.getNodeData().isHasTaxonomy() ) {
211 throw new RIOException( "node with no (appropriate) taxonomic information found in gene tree #1: "
216 final List<Integer> shortests = GSDIR.getIndexesOfShortestTree( assigned_trees );
217 assigned_tree = assigned_trees.get( shortests.get( 0 ) );
218 if ( _produce_log ) {
219 writeStatsToLog( i, gsdir, shortests );
221 _gsdir_tax_comp_base = gsdir.getTaxCompBase();
225 throw new IllegalArgumentException( "illegal algorithm: " + algorithm );
229 _ext_nodes = assigned_tree.getNumberOfExternalNodes();
231 else if ( _ext_nodes != assigned_tree.getNumberOfExternalNodes() ) {
232 throw new RIOException( "after stripping gene tree #" + ( i + 1 )
233 + " has a different number of external nodes (" + assigned_tree.getNumberOfExternalNodes()
234 + ") than the preceding gene trees (" + _ext_nodes + ")" );
236 return assigned_tree;
239 private void writeLogSubHeader() {
242 _log.append( "with minimal number of duplications" );
244 _log.append( "root placements" );
245 _log.append( "\t[" );
246 _log.append( "min" );
248 _log.append( "max" );
249 _log.append( "]\t<" );
250 _log.append( "shortest" );
252 _log.append( ForesterUtil.LINE_SEPARATOR );
255 private final void writeStatsToLog( final int i, final GSDIR gsdir, final List<Integer> shortests ) {
256 final BasicDescriptiveStatistics stats = gsdir.getDuplicationsSumStats();
259 _log.append( gsdir.getMinDuplicationsSumGeneTrees().size() );
261 _log.append( stats.getN() );
262 _log.append( "\t[" );
263 _log.append( ( int ) stats.getMin() );
265 _log.append( ( int ) stats.getMax() );
266 _log.append( "]\t<" );
267 _log.append( shortests.size() );
269 _log.append( ForesterUtil.LINE_SEPARATOR );
272 public final static IntMatrix calculateOrthologTable( final Phylogeny[] analyzed_gene_trees, final boolean sort )
273 throws RIOException {
274 final List<String> labels = new ArrayList<String>();
275 final Set<String> labels_set = new HashSet<String>();
277 for( final PhylogenyNode n : analyzed_gene_trees[ 0 ].getExternalNodes() ) {
278 if ( n.getNodeData().isHasSequence() && !ForesterUtil.isEmpty( n.getNodeData().getSequence().getName() ) ) {
279 label = n.getNodeData().getSequence().getName();
281 else if ( n.getNodeData().isHasSequence()
282 && !ForesterUtil.isEmpty( n.getNodeData().getSequence().getSymbol() ) ) {
283 label = n.getNodeData().getSequence().getSymbol();
285 else if ( !ForesterUtil.isEmpty( n.getName() ) ) {
289 throw new RIOException( "node " + n + " has no appropriate label" );
291 if ( labels_set.contains( label ) ) {
292 throw new RIOException( "label " + label + " is not unique" );
294 labels_set.add( label );
298 Collections.sort( labels );
300 final IntMatrix m = new IntMatrix( labels );
302 for( final Phylogeny gt : analyzed_gene_trees ) {
304 PhylogenyMethods.preOrderReId( gt );
305 final HashMap<String, PhylogenyNode> map = PhylogenyMethods.createNameToExtNodeMap( gt );
306 for( int x = 0; x < m.size(); ++x ) {
307 final String mx = m.getLabel( x );
308 final PhylogenyNode nx = map.get( mx );
310 throw new RIOException( "node \"" + mx + "\" not present in gene tree #" + counter );
314 for( int y = 0; y < m.size(); ++y ) {
315 my = m.getLabel( y );
318 throw new RIOException( "node \"" + my + "\" not present in gene tree #" + counter );
320 if ( !PhylogenyMethods.calculateLCAonTreeWithIdsInPreOrder( nx, ny ).isDuplication() ) {
321 m.inreaseByOne( x, y );