2 # function2gene, is part of the function2gene 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>.
19 function2gene - Call out to each of the search modules to search for
24 function2gene --keywords keywords.txt --results gene_search_results
27 --keywords newline delineated list of keywords to search for
28 --results directory to store results in
29 --database databases to search
30 --restart-at mode to start searching at
31 --debug, -d debugging level (Default 0)
32 --help, -h display this help
33 --man, -m display manual
41 A file which contains a newline delinated list of keywords to search
42 for. Can be specified multiple times. Lines starting with # or ; are
47 Directory in which to store results; also stores the current state of
52 Databases to search, can be specified multiple times. [Defaults to
53 NCBI, GeneCards and Harvester, the only currently supported
58 If you need to restart the process at a particular state (which has
59 already been completed) specify this option.
63 Debug verbosity. (Default 0)
67 Display brief useage information.
77 # Search all databases for transferrin
78 echo 'transferrin' > keywords.txt
79 function2gene --keywords keywords.txt --results keyword_results
87 use Storable qw(thaw freeze);
88 use File::Basename qw(basename dirname);
91 my %options = (databases => [],
99 GetOptions(\%options,'keywords=s@','databases=s@',
100 'restart_at|restart-at=s','results=s',
101 'debug|d+','help|h|?','man|m');
103 pod2usage() if $options{help};
104 pod2usage({verbose=>2}) if $options{man};
106 my $base_dir = dirname(abs_path($0));
110 $ERRORS.="restart-at must be one of get, parse or combine\n" if
111 exists $options{restart_at} and $options{restart_at} !~ /^(?:get|parse|combine)$/;
113 $ERRORS.="unknown database(s)" if
114 @{$options{databases}} and
115 grep {$_ !~ /^(?:ncbi|genecard|harvester)$/i} @{$options{databases}};
117 if (not length $options{results}) {
118 $ERRORS.="results directory not specified";
120 elsif (not -d $options{results} or not -w $options{results}) {
121 $ERRORS.="results directory $options{results} does not exist or is not writeable";
124 pod2usage($ERRORS) if length $ERRORS;
126 if (not @{$options{databases}}) {
127 $options{databases} = [qw(ncbi genecard harvester)]
130 $DEBUG = $options{debug};
132 # There are three states for our engine
137 # first, check to see if the state in the result directory exists
141 $options{keywords} = [map {abs_path($_)} @{$options{keywords}}];
143 chdir $options{results} or die "Unable to chdir to $options{results}";
145 if (-e "do_it_all_state") {
146 ADVISE("Using existing state information");
147 my $state_fh = IO::File->new("do_it_all_state",'r') or die
148 "Unable to open state file for reading: $!";
150 my $state_file = <$state_fh>;
151 %state = %{thaw($state_file)} or die "Unable to thaw state file";
154 ADVISE("Starting new run");
155 %state = (keywords => [],
156 databases => [map {lc($_)} @{$options{databases}}],
166 if (@{$options{keywords}}) {
169 @old_keywords{@{$state{keywords}}} = (1) x @{$state{keywords}};
170 for my $keyword_file (@{$options{keywords}}) {
171 my $keyword_fh = IO::File->new($keyword_file,'r') or die
172 "Unable to open $keyword_file for reading: $!";
173 while (<$keyword_fh>) {
177 if (not $old_keywords{$_}) {
178 DEBUG("Adding new keyword '$_'");
179 push @new_keywords, $_;
182 DEBUG("Not adding duplicate keyword '$_'");
186 push @{$state{keywords}},@new_keywords;
189 if (exists $options{restart_at} and length $options{restart_at}) {
190 if (lc($options{restart_at}) eq 'get') {
191 delete $state{done_keywords}{get};
192 delete $state{done_keywords}{parse};
193 delete $state{done_keywords}{combine};
195 elsif (lc($options{restart_at}) eq 'parse') {
196 delete $state{done_keywords}{parse};
197 delete $state{done_keywords}{combine};
199 elsif (lc($options{restart_at}) eq 'combine') {
200 delete $state{done_keywords}{combine};
204 # now we need to figure out what has to happen
205 # for each keyword, we check to see if we've got results, parsed
206 # results, and combined it. If not, we queue up those actions.
208 my %actions = (combine => 0,
213 if (not @{$state{keywords}}) {
214 ADVISE("There are no keywords specified");
217 for my $keyword (@{$state{keywords}}) {
218 for my $database (@{$state{databases}}) {
219 if (not exists $state{done_keywords}{get}{$database}{$keyword}) {
220 push @{$actions{get}{$database}}, $keyword;
221 delete $state{done_keywords}{parse}{$database}{$keyword} if
222 exists $state{done_keywords}{parse}{$database}{$keyword};
223 delete $state{done_keywords}{combine}{$database}{$keyword} if
224 exists $state{done_keywords}{combine}{$database}{$keyword};
226 if (not exists $state{done_keywords}{parse}{$database}{$keyword}) {
227 push @{$actions{parse}{$database}},$keyword;
228 delete $state{done_keywords}{combine}{$database}{$keyword} if
229 exists $state{done_keywords}{combine}{$database}{$keyword};
231 if (not exists $state{done_keywords}{combine}{$database}{$keyword}) {
232 $actions{combine} = 1;
238 for my $state (qw(get parse)) {
240 for my $database (keys %{$actions{$state}}) {
241 next unless @{$actions{$state}{$database}};
242 $databases{$database}{queue} = Thread::Queue->new
243 or die "Unable to create new thread queue";
244 $databases{$database}{thread} = threads->create(\&handle_action,$state,$database,$databases{$database}{queue})
245 or die "Unable to create new thread";
246 $databases{$database}{queue}->enqueue(@{$actions{$state}{$database}});
247 $databases{$database}{queue}->enqueue(undef);
250 for my $database (keys %databases) {
251 my ($actioned_keywords,$failed_keywords) = @{$databases{$database}{thread}->join||[]};
252 if (not defined $failed_keywords) {
253 ADVISE("Something bad happened during '$state' of '$database'");
256 elsif (@{$failed_keywords}) {
257 ADVISE("These keywords failed during '$state' of '$database':",@{$failed_keywords});
260 @{$state{done_keywords}{$state}{$database}}{@{$actioned_keywords}} = (1) x @{$actioned_keywords};
261 delete @{$state{done_keywords}{$state}{$database}}{@{$failed_keywords}};
265 WARN("Stoping, as there are errors");
270 if ($actions{combine}) {
272 # deal with combining results
273 my @parsed_results = map { my $db = $_;
275 "parsed_results_${db}_${_}.txt"
276 } keys %{$state{done_keywords}{parse}{$db}}
277 } keys %{$state{done_keywords}{parse}};
279 write_command_to_file('combined_results.txt',
280 "$base_dir/combine_results",
283 for my $result (@parsed_results) {
284 s/^parsed_results_//;
286 my ($db,$keyword) = split /_/, $_, 2;
287 $state{done_keywords}{combined}{$db}{$keyword} = 1;
290 ADVISE("Finished; results in $options{results}/combined_results");
293 ADVISE('Nothing to do. [Perhaps you wanted --restart-at?]');
297 my ($state,$database,$queue) = @_;
299 my $actioned_keywords = [];
300 my $failed_keywords = [];
301 DEBUG("Beginning to handle actions for state '$state' database '$database'");
302 while ($keyword = $queue->dequeue) {
303 DEBUG("Handling state '$state' database '$database' keyword '$keyword'");
304 # handle the action, baybee
305 if ($state eq 'get') {
308 open($command_fh,'|-',
309 "$base_dir/get_${database}_results",
310 ) or die "unable to execute '$base_dir/get_${database}_results'";
311 print {$command_fh} "$keyword\n" or die "unable to print $keyword to 'get_${database}_results'";
312 close($command_fh) or die "Unable to close filehandle";
314 die "get_${database}_results with keyword $keyword failed with error code ".($?>>8);
319 push @{$failed_keywords}, $keyword;
323 elsif ($state eq 'parse') {
325 write_command_to_file("parsed_results_${database}_${keyword}.txt",
326 "$base_dir/parse_${database}_results",
332 WARN("parse_${database}_results failed with $@");
333 push @{$failed_keywords}, $keyword;
338 die "I don't know how to handle state $state";
340 ADVISE("$state results from '$database' for '$keyword'");
341 push @{$actioned_keywords},$keyword;
343 return [$actioned_keywords,$failed_keywords];
348 my $state_fh = IO::File->new("do_it_all_state",'w') or die
349 "Unable to open state file for writing: $!";
350 print {$state_fh} freeze($state) or die "Unable to freeze state file";
351 close $state_fh or die "Unable to close state file: $!";
354 sub write_command_to_file{
355 my ($file,@command) = @_;
356 my $fh = IO::File->new($file,'w') or
357 die "Unable to open $file for writing: $!";
359 open($command_fh,'-|',
361 ) or die "Unable to execute $command[0] $!";
362 print {$fh} <$command_fh>;
364 close $command_fh or die "$command[0] failed with ".($?>>8);
369 print STDOUT map {($_,qq(\n))} @_;
373 print STDERR map {($_,qq(\n))} @_;
378 print STDERR map {($_,qq(\n))} @_;