-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtnrs_stub.pl
More file actions
executable file
·116 lines (91 loc) · 3.3 KB
/
tnrs_stub.pl
File metadata and controls
executable file
·116 lines (91 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/perl
use strict;
use warnings;
#----------------------------------------------------------------------
# imports
#----------------------------------------------------------------------
use CGI;
use JSON;
use Array::Utils qw(:all);
use File::Slurp;
use File::Spec::Functions qw(catfile);
use HelperMethods;
use Getopt::Long;
use Log::Log4perl qw(:easy);
#----------------------------------------------------------------------
# constants
#----------------------------------------------------------------------
use constant USAGE => <<HEREDOC;
Usage 1: $0 <taxa_name_1> [<taxa_name_2>] ...
Usage 2: $0 < taxa-names.txt
Example (Usage 1): $0 'Rattus rattus' 'Mus musculus' 'Homo sapiens' 'Pan paniscus'
INPUT TAXA NAMES FILE:
An input taxa names file contains species names separated by newlines.
Example:
--- BEGIN FILE ---
Rattus rattus
Mus musculus
Homo sapiens
Pan paniscus
--- END FILE ---
OPTIONS:
--help show this help message
HEREDOC
use constant IS_CGI => exists $ENV{'GATEWAY_INTERFACE'};
use constant MOCK_INPUT_FILE => catfile('mock-data', 'input-taxa-names.txt');
use constant MOCK_OUTPUT_FILE => catfile('mock-data', 'tnrs-output.json');
use constant MOCK_POLL_ID => 'abc';
#----------------------------------------------------------------------
# logging
#----------------------------------------------------------------------
# For debugging when owner of this script does not have read access to apache log.
#close STDERR or HelperMethods::fatal($!, IS_CGI, 500);
#open STDERR, '>>/home/ben/temp/cgi.log' or HelperMethods::fatal($!, IS_CGI, 500);
Log::Log4perl::easy_init(IS_CGI ? $WARN : $INFO);
#----------------------------------------------------------------------
# CGI/CLI parameter processing
#----------------------------------------------------------------------
my $help_opt = 0;
my $cgi = CGI->new();
if (IS_CGI) {
@ARGV = split("\n", $cgi->param('query'));
} else {
my $getopt_success = GetOptions('help' => \$help_opt);
die USAGE unless $getopt_success;
die USAGE if $help_opt;
@ARGV = split("\n", join('', <STDIN>)) unless @ARGV;
die USAGE unless @ARGV;
}
my @species = @ARGV;
#----------------------------------------------------------------------
# main
#----------------------------------------------------------------------
my $poll_id = $cgi->param('poll');
my @mock_species = split(/\s*\n\s*/, read_file(MOCK_INPUT_FILE));
my $mock_response = read_file(MOCK_OUTPUT_FILE);
unless (IS_CGI && $poll_id) {
if (array_diff(@species, @mock_species)) {
HelperMethods::fatal(
sprintf('this service only supports a mock query for: \'%s\'', join(' \'', @mock_species)),
IS_CGI,
400
);
}
}
if (IS_CGI) {
if ($poll_id) {
unless ($poll_id eq MOCK_POLL_ID) {
HelperMethods::fatal("no pending request for polling id $poll_id", IS_CGI, 400);
}
print $cgi->header(-status => 200, -type => 'application/json');
print $mock_response;
exit 0;
} else {
HelperMethods::fatal('request is missing \'query\' param', IS_CGI, 400) unless @species;
my $poll_url = $cgi->url() . '?poll=' . MOCK_POLL_ID;
print $cgi->header(-status => 200, -type => 'application/json');
print "{\"uri\":\"$poll_url\"}";
}
} else {
print $mock_response;
}