in progress
[jalview.git] / forester / archive / perl / mt.pl
1 #!/usr/bin/perl -W
2
3 # mt.pl
4 # -----
5 #
6 # Copyright (C) 2003 Christian M. Zmasek
7 # All rights reserved
8 #
9 # Author: Christian M. Zmasek
10 #         zmasek@genetics.wustl.edu
11 #         http://www.genetics.wustl.edu/eddy/people/zmasek/
12 #
13 # Version: 1.000
14 # Created on: 09/05/03
15 # Last modified: 09/05/03
16 #
17 #
18 #
19 # Calculates trees based on all alignments/files in a given directory using
20 # makeTree.pl.
21 #
22 #
23
24 use strict;
25 use FindBin;
26 use lib $FindBin::Bin;
27 use rio_module2;
28
29
30 my $PREPROCESSING_COMMAND = "";
31 my $PERFORM_PREPROCESSING = 0;
32
33 my $POSTPROCESSING_COMMAND = "/nfs/dm3/homedir1/czmasek/RIO1.24/perl/extractSpecies.pl";
34 my $PERFORM_POSTPROCESSING = 1;
35
36
37 my $MY_TEMP_DIR           = $TEMP_DIR_DEFAULT;   # $TEMP_DIR_DEFAULT is inherited 
38                                                  # from rio_module.pm 
39
40
41
42
43 my $options               = ""; # Options for makeTree.pl, see makeTree.pl.
44                                
45
46 my $suffix                = "";
47 my $use_suffixes          = 0;
48 my $input_dir             = "";
49 my $output_dir            = "";
50
51 my $i                     = 0;
52 my $filename              = "";
53 my @filenames             = ();
54
55
56
57
58
59
60 # Analyzes the options:
61 # ---------------------
62
63 unless ( @ARGV == 3 || @ARGV == 4 ) {
64     &printUsage();
65 }
66
67 $options    = $ARGV[ 0 ]; 
68 $input_dir  = $ARGV[ 1 ];
69 $output_dir = $ARGV[ 2 ];
70
71 if ( @ARGV == 3 ) {
72     $use_suffixes = 0;
73 }
74 elsif ( @ARGV == 4 ) {
75     $use_suffixes = 1;
76     $suffix = $ARGV[ 3 ];
77 }
78
79
80 $input_dir   = &addSlashAtEndIfNotPresent( $input_dir ); 
81 $output_dir  = &addSlashAtEndIfNotPresent( $output_dir );
82 $MY_TEMP_DIR = &addSlashAtEndIfNotPresent( $MY_TEMP_DIR );
83
84
85
86
87 # This adds a "-" before the options for makeTree:
88 # ------------------------------------------------
89 unless ( $options =~ /^-/ ) {
90     $options = "-".$options;
91 }
92
93
94
95
96
97 # This creates the temp file:
98 # --------------------------
99
100 my $time = time;
101 my $ii   = 0;
102
103 my $temp_file = $MY_TEMP_DIR."mt".$time.$ii;
104
105 while ( -e $temp_file ) {
106     $ii++;
107     $temp_file = $MY_TEMP_DIR."mt".$time.$ii;
108 }
109
110
111
112 opendir( DIR, $input_dir ) || error( "Cannot open directory \"$input_dir\": $!" );
113
114 $i = 0;
115
116 while( defined( $filename = readdir( DIR ) ) ) {
117     if ( $filename =~ /^\.\.?$/ ) {
118         next;
119     }
120     if ( $use_suffixes == 1 && $filename !~ /$suffix$/ ) {
121         next;
122     }
123    
124     $filenames[ $i ] = $filename;
125     $i++;
126 }
127
128 close( DIR );
129
130 $i = 0;
131
132 FOREACH: foreach $filename ( @filenames ) {
133
134     # If the corresponding tree seems to already exists, do next one.
135     if ( -e "$output_dir$filename.nhx" ) {
136         next FOREACH;
137     }
138
139     print "\n\n\n\n";
140     print "MT.PL\n";
141     print "working on: $filename\n";
142     
143     print "[tree calculation $i]\n";
144     print "=====================================================================\n\n\n";
145
146
147     unlink( "$output_dir$filename.aln",
148             "$output_dir$filename.log",
149             "$output_dir$filename.nbd"  );
150
151     print( "MT.PL: executing:\n" );
152     
153     my $inputfile = $input_dir.$filename;
154     
155     my $outputfilename = "";
156     
157     if ( $use_suffixes == 1 ) {
158         $outputfilename = $output_dir . $filename;
159         $outputfilename =~ s/$suffix$//;
160         $outputfilename =~ s/\.$//;
161         $outputfilename .= ".nhx";
162     }
163     else {
164         $outputfilename = $output_dir . $filename . ".nhx";
165     }
166     
167     
168  
169     if ( $PERFORM_PREPROCESSING == 1 ) {
170          my $pre_command = "$PREPROCESSING_COMMAND";
171     
172          print( "$pre_command\n" );
173          system( $pre_command ) && &error( "Could not execute \"$pre_command\"" );
174     }
175  
176     $MAKETREE = "/nfs/dm3/homedir1/czmasek/RIO1.24/perl/makeTree2.pl"; # <<<<<<<<<<<<<<<<<<<<<<<-------------------~~~~~~~~~~~~~~~~~~~~~~~
177     
178     my $command = "$MAKETREE $options $inputfile $outputfilename";
179     
180     print( "$command\n" );
181     system( $command ) && &error( "Could not execute \"$command\"" );
182    
183    
184    
185     if ( $PERFORM_POSTPROCESSING == 1 ) {
186          my $post_command = "$POSTPROCESSING_COMMAND $outputfilename";
187     
188          print( "$post_command\n" );
189          system( $post_command ) && &error( "Could not execute \"$post_command\"" );
190     }
191  
192  
193    
194     $i++;
195
196 }
197
198
199
200 print( "\n\n\nMT.PL: Done!\n" );
201
202 exit( 0 );
203
204
205
206
207
208
209 sub error{
210
211     my $text = $_[ 0 ];
212
213     print( "\nxt.pl: ERROR:\n" );
214     print( "$text\n\n" );
215
216     exit( -1 );
217
218 }
219
220
221
222
223 sub printUsage {
224     print "\n";
225     print " mt.pl\n";
226     print " _____\n";
227     print " \n";
228     print " Copyright (C) 2003 Christian M. Zmasek\n";
229     print " All rights reserved\n";
230     print "\n";
231     print " Author: Christian M. Zmasek\n";
232     print " zmasek\@genetics.wustl.edu\n";
233     print " http://www.genetics.wustl.edu/eddy/forester/\n";
234     print "\n";
235     print "\n";
236     print " Purpose\n";
237     print " -------\n";
238     print "\n";
239     print " Tree construction using makeTree.pl on all alignments/files\n";
240     print " in a given directory.\n"; 
241     print "\n";
242     print "\n";
243     print " Usage\n";
244     print " -----\n";
245     print "\n"; 
246     print "   mt.pl <options for makeTree.pl> <input directory: aligments> <output\n";
247     print "         directory> [suffix for alignments to be used in input directory]\n";
248     print "\n";
249     print "   If a suffix is given, it will be removed for the output files.\n";
250     print "\n";
251     print "\n";
252     print " Example\n";
253     print " -------\n";
254     print "\n";
255     print "   \"mt.pl NS21UTRB100DX alignments/ trees/ .aln\"\n";
256     print "\n";
257     print "\n";
258     print "\n";
259     exit( -1 );
260
261 }