b9a15738056982413b4205a966b60b3a9a5bae53
[jalview.git] / forester / java / src / org / forester / application / rio.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 // Copyright (C) 2000-2001 Washington University School of Medicine
8 // and Howard Hughes Medical Institute
9 // All rights reserved
10 //
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.
15 //
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.
20 //
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
24 //
25 // Contact: phylosoft @ gmail . com
26 // WWW: www.phylosoft.org/forester
27
28 package org.forester.application;
29
30 import java.io.File;
31 import java.io.FileWriter;
32 import java.io.PrintWriter;
33
34 import org.forester.io.parsers.phyloxml.PhyloXmlParser;
35 import org.forester.phylogeny.Phylogeny;
36 import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
37 import org.forester.phylogeny.factories.PhylogenyFactory;
38 import org.forester.sdi.RIO;
39 import org.forester.util.ForesterUtil;
40
41 public class rio {
42
43     final static private String  PRG_NAME    = "RIO";
44     final static private String  PRG_VERSION = "2.03 ALPHA";
45     final static private String  PRG_DATE    = "2010.01.15";
46     final static private String  E_MAIL      = "czmasek@burnham.org";
47     final static private String  WWW         = "www.phylosoft.org/forester/";
48     final static private boolean TIME        = true;
49     final static private boolean VERBOSE     = true;
50
51     private final static void errorInCommandLine() {
52         System.out.println( "\nrio: Error in command line.\n" );
53         printHelp();
54         System.exit( -1 );
55     }
56
57     public static void main( final String[] args ) {
58         ForesterUtil.printProgramInformation( PRG_NAME, PRG_VERSION, PRG_DATE, E_MAIL, WWW );
59         File species_tree_file = null;
60         File multiple_trees_file = null;
61         File outfile = null;
62         String seq_name = "";
63         String arg = "";
64         boolean output_ultraparalogs = false;
65         double t_orthologs = 0.0;
66         double t_orthologs_dc = 0.0;
67         double threshold_ultra_paralogs = 0.0;
68         int sort = 13;
69         Phylogeny species_tree = null;
70         RIO rio_instance = null;
71         PrintWriter out = null;
72         long time = 0;
73         if ( args.length < 2 ) {
74             printHelp();
75             System.exit( 0 );
76         }
77         else if ( ( args.length < 3 ) || ( args.length > 18 ) ) {
78             errorInCommandLine();
79         }
80         for( final String arg2 : args ) {
81             if ( arg2.trim().charAt( 0 ) != 'p' ) {
82                 if ( arg2.trim().length() < 3 ) {
83                     errorInCommandLine();
84                 }
85                 else {
86                     arg = arg2.trim().substring( 2 );
87                 }
88             }
89             try {
90                 switch ( arg2.trim().charAt( 0 ) ) {
91                     case 'M':
92                         multiple_trees_file = new File( arg );
93                         break;
94                     case 'N':
95                         seq_name = arg;
96                         break;
97                     case 'S':
98                         species_tree_file = new File( arg );
99                         break;
100                     case 'O':
101                         outfile = new File( arg );
102                         break;
103                     case 'p':
104                         output_ultraparalogs = true;
105                         break;
106                     case 'P':
107                         sort = Integer.parseInt( arg );
108                         if ( ( sort < 0 ) || ( sort > 17 ) ) {
109                             errorInCommandLine();
110                         }
111                         break;
112                     case 'L':
113                         t_orthologs = Double.parseDouble( arg );
114                         break;
115                     case 'v':
116                         threshold_ultra_paralogs = Double.parseDouble( arg );
117                         break;
118                     default:
119                         errorInCommandLine();
120                 }
121             }
122             catch ( final Exception e ) {
123                 errorInCommandLine();
124             }
125         }
126         if ( ( seq_name == "" ) || ( species_tree_file == null ) || ( multiple_trees_file == null )
127                 || ( outfile == null ) ) {
128             errorInCommandLine();
129         }
130         if ( ( sort < 0 ) || ( sort > 2 ) ) {
131             errorInCommandLine();
132         }
133         if ( VERBOSE ) {
134             System.out.println( "\nMultiple trees file:                          " + multiple_trees_file );
135             System.out.println( "Seq name:                                     " + seq_name );
136             System.out.println( "Species tree file:                            " + species_tree_file );
137             System.out.println( "Outfile:                                      " + outfile );
138             System.out.println( "Sort:                                         " + sort );
139             System.out.println( "Threshold orthologs:                          " + t_orthologs );
140             System.out.println( "Threshold orthologs for distance calc.:       " + t_orthologs_dc );
141             if ( output_ultraparalogs ) {
142                 System.out.println( "Threshold ultra paralogs:                     " + threshold_ultra_paralogs );
143             }
144         }
145         if ( TIME && VERBOSE ) {
146             time = System.currentTimeMillis();
147         }
148         try {
149             final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
150             species_tree = factory.create( species_tree_file, new PhyloXmlParser() )[ 0 ];
151         }
152         catch ( final Exception e ) {
153             e.printStackTrace();
154             System.exit( -1 );
155         }
156         if ( !species_tree.isRooted() ) {
157             ForesterUtil.printErrorMessage( PRG_NAME, "Species tree is not rooted" );
158             System.exit( -1 );
159         }
160         if ( !species_tree.isCompletelyBinary() ) {
161             ForesterUtil.printErrorMessage( PRG_NAME, "Species tree is not completely binary" );
162             System.exit( -1 );
163         }
164         rio_instance = new RIO();
165         final StringBuffer output = new StringBuffer();
166         try {
167             rio_instance.inferOrthologs( multiple_trees_file, species_tree.copy(), seq_name );
168             output.append( rio_instance.inferredOrthologsToString( seq_name, sort, t_orthologs ) );
169             if ( output_ultraparalogs ) {
170                 output.append( "\n\nUltra paralogs:\n" );
171                 output.append( rio_instance.inferredUltraParalogsToString( seq_name, threshold_ultra_paralogs ) );
172             }
173             output.append( "\n\nSort priority: " + RIO.getOrder( sort ) );
174             output.append( "\nExt nodes    : " + rio_instance.getExtNodesOfAnalyzedGeneTrees() );
175             output.append( "\nSamples      : " + rio_instance.getNumberOfSamples() + "\n" );
176             out = new PrintWriter( new FileWriter( outfile ), true );
177         }
178         catch ( final Exception e ) {
179             ForesterUtil.printErrorMessage( PRG_NAME, e.getLocalizedMessage() );
180             e.printStackTrace();
181             System.exit( -1 );
182         }
183         out.println( output );
184         out.close();
185         ForesterUtil.programMessage( PRG_NAME, "wrote results to \"" + outfile + "\"" );
186         if ( TIME && VERBOSE ) {
187             time = System.currentTimeMillis() - time;
188             ForesterUtil.programMessage( PRG_NAME, "time: " + time + "ms" );
189         }
190         ForesterUtil.programMessage( PRG_NAME, "OK." );
191         System.exit( 0 );
192     }
193
194     private final static void printHelp() {
195         System.out.println( "M= (String) Multiple gene tree file (mandatory)" );
196         System.out.println( "N= (String) Query sequence name (mandatory)" );
197         System.out.println( "S= (String) Species tree file (mandatory)" );
198         System.out.println( "O= (String) Output file name -- overwritten without warning! (mandatory)" );
199         System.out.println( "P= (int)    Sort priority" );
200         System.out.println( "L= (double) Threshold orthologs for output" );
201         System.out.println( " Sort priority (\"P=\"):" );
202         System.out.println( RIO.getOrderHelp().toString() );
203         System.out.println();
204         System.out
205                 .println( " Example: \"rio M=gene_trees.xml N=bcl2_NEMVE S=species_tree.xml D=distances P=13 p O=out\"" );
206         System.out.println();
207     }
208 }