2 # do_it_all, is part of the gene search suite and is released
3 # under the terms of the GPL version 2, or any later version, at your
4 # option. See the file README and COPYING for more information.
5 # Copyright 2007 by Don Armstrong <don@donarmstrong.com>.
18 do_it_all - Call out to each of the search modules to search for
23 do_it_all --keywords keywords.txt --results gene_search_results
26 --keywords newline delineated list of keywords to search for
27 --results directory to store results in
28 --database databases to search
29 --restart-at mode to start searching at
30 --debug, -d debugging level (Default 0)
31 --help, -h display this help
32 --man, -m display manual
40 A file which contains a newline delinated list of keywords to search
41 for. Can be specified multiple times. Lines starting with # or ; are
46 Directory in which to store results; also stores the current state of
51 Databases to search, can be specified multiple times. [Defaults to
52 NCBI, GeneCards and Harvester, the only currently supported
57 If you need to restart the process at a particular state (which has
58 already been completed) specify this option.
62 Debug verbosity. (Default 0)
66 Display brief useage information.
83 use Storable qw(thaw freeze);
84 use File::Basename qw(basename);
86 my %options = (databases => [],
94 GetOptions(\%options,'keywords=s@','databases=s@',
95 'restart_at|restart-at=s','results=s',
96 'debug|d+','help|h|?','man|m');
98 pod2usage() if $options{help};
99 pod2usage({verbose=>2}) if $options{man};
101 my $base_dir = basename($0);
105 $ERRORS.="restart-at must be one of get, parse or combine\n" if
106 exists $options{restart_at} and $options{restart_at} !~ /^(?:get|parse|combine)$/;
108 $ERRORS.="unknown database(s)" if
109 @{$options{databases}} and
110 grep {$_ !~ /^(?:ncbi|genecards|harvester)$/i} @{$options{databases}};
112 if (not length $options{results}) {
113 $ERRORS.="results directory not specified";
115 elsif (not -d $options{results} or not -w $options{results}) {
116 $ERRORS.="results directory $options{results} does not exist or is not writeable";
119 pod2usage($ERRORS) if length $ERRORS;
121 if (not @{$options{databases}}) {
122 $options{databases} = [qw(ncbi genecards harvester)]
125 $DEBUG = $options{debug};
127 # There are three states for our engine
132 # first, check to see if the state in the result directory exists
136 $options{keywords} = [map {abs_path($_)} @{$options{keywords}}];
138 chdir $options{results} or die "Unable to chdir to $options{results}";
140 if (-e "do_it_all_state") {
141 ADVISE("Using existing state information");
142 my $state_fh = IO::File->new("do_it_all_state",'r') or die
143 "Unable to open state file for reading: $!";
145 my $state_file = <$state_fh>;
146 %state = %{thaw($state_file)} or die "Unable to thaw state file";
149 ADVISE("Starting new run");
150 %state = (keywords => [],
151 databases => [map {lc($_)} @{$options{databases}}],
161 if (@{$options{keywords}}) {
164 @old_keywords{@{$state{keywords}}} = (1) x @{$state{keywords}};
165 for my $keyword_file (@{$options{keywords}}) {
166 my $keyword_fh = IO::File->new($keyword_file,'r') or die
167 "Unable to open $keyword_file for reading: $!";
169 while (<$keyword_fh>) {
172 if (not $old_keywords{$_}) {
173 DEBUG("Adding new keyword '$_'");
174 push @new_keywords, $_;
177 DEBUG("Not adding duplicate keyword '$_'");
183 if (exists $options{restart_at} and length $options{restart_at}) {
184 if (lc($options{restart_at}) eq 'get') {
185 delete $state{done_keywords}{get};
186 delete $state{done_keywords}{parse};
187 delete $state{done_keywords}{combine};
189 elsif (lc($options{restart_at}) eq 'parse') {
190 delete $state{done_keywords}{parse};
191 delete $state{done_keywords}{combine};
193 elsif (lc($options{restart_at}) eq 'combine') {
194 delete $state{done_keywords}{combine};
198 # now we need to figure out what has to happen
199 # for each keyword, we check to see if we've got results, parsed
200 # results, and combined it. If not, we queue up those actions.
202 my %actions = (combine => 0,
207 if (not @{$state{keywords}}) {
208 ADVISE("There are no keywords specified");
211 for my $keyword (@{$state{keywords}}) {
212 for my $database (@{$state{databases}}) {
213 if (not exists $state{done_keywords}{get}{$database}{$keyword}) {
214 push @{$actions{get}{$database}}, $keyword;
215 delete $state{done_keywords}{parse}{$database}{$keyword} if
216 exists $state{done_keywords}{parse}{$database}{$keyword};
217 delete $state{done_keywords}{combine}{$database}{$keyword} if
218 exists $state{done_keywords}{combine}{$database}{$keyword};
220 if (not exists $state{done_keywords}{parse}{$database}{$keyword}) {
221 push @{$actions{parse}{$database}},$keyword;
222 delete $state{done_keywords}{combine}{$database}{$keyword} if
223 exists $state{done_keywords}{combine}{$database}{$keyword};
225 if (not exists $state{done_keywords}{combine}{$database}{$keyword}) {
226 $actions{combine} = 1;
234 for my $state (qw(get parse)) {
236 for my $database (keys %{$actions{$state}}) {
237 next unless @{$actions{$state}{$database}};
238 $databases{$database}{queue} = Thread::Queue->new;
239 $databases{$database}{thread} = threads->new(\&handle_action($state,$database,$databases{database}{queue}));
240 $databases{$database}{queue}->enqueue(@{$actions{$state}{$database}});
241 $databases{$database}{queue}->enqueue(undef);
244 for my $database (keys %databases) {
245 my ($actioned_keywords,$failed_keywords) = $databases{$database}{thread}->join;
246 if (@{$failed_keywords}) {
247 ADVISE("These keywords failed during '$state' of '$database':",@{$failed_keywords});
250 @{$state{done_keywords}{$state}{$database}}{@{$actioned_keywords}} = (1) x @{$actioned_keywords};
251 delete @{$state{done_keywords}{$state}{$database}}{@{$failed_keywords}};
255 WARN("Stoping, as there are errors");
260 if ($actions{combine}) {
262 # deal with combining results
263 my @parsed_results = map { my $db = $_;
265 "parsed_results_${db}_${_}.txt"
266 } keys %{$state{done_keywords}{parse}{$db}}
267 } keys %{$state{done_keywords}{parse}};
269 write_command_to_file('combined_results.txt',
270 "$base_dir/combine_results",
273 for my $result (@parsed_results) {
274 s/^parsed_results_//;
276 my ($db,$keyword) = split /_/, $_, 2;
277 $state{done_keywords}{combined}{$db}{$keyword} = 1;
280 ADVISE("Finished; results in $options{results}/combined_results");
283 ADVISE('Nothing to do. [Perhaps you wanted --restart-at?]');
287 my ($state,$database,$queue) = @_;
289 my $actioned_keywords = ();
290 my $failed_keywords = ();
291 while ($keyword = $queue->dequeue) {
292 # handle the action, baybee
293 if ($state eq 'get') {
295 open($command_fh,'|-',
296 "get_${database}_results",
298 print {$command_fh} "$keyword\n";
301 WARN("get_${database}_results with keyword $keyword failed with error code ".($?>>8));
305 elsif ($state eq 'parse') {
307 write_command_to_file("parsed_results_${database}_${keyword}.txt",
308 "parse_${database}_results",
314 WARN("parse_${database}_results failed with $@");
319 die "I don't know how to handle state $state";
321 ADVISE("$state results from '$database' for '$keyword'");
322 push @{$actioned_keywords},$keyword;
324 return ($actioned_keywords,$failed_keywords);
329 my $state_fh = IO::File->new("do_it_all_state",'w') or die
330 "Unable to open state file for writing: $!";
331 print {$state_fh} freeze($state) or die "Unable to freeze state file";
332 close $state_fh or die "Unable to close state file: $!";
335 sub write_command_to_file{
337 my $fh = IO::File->new($file,'w') or
338 die "Unable to open $file for writing: $!";
340 open($command_fh,'-|',
342 ) or die "Unable to execute $command[0] $!";
343 print {$fh} <$command_fh>;
345 close $command_fh or die "$command[0] failed with ".($?>>8);
350 print STDOUT map {($_,qq(\n))} @_;
354 print STDERR map {($_,qq(\n))} @_;
359 print STDERR map {($_,qq(\n))} @_;