-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtreestore_stub.pl
More file actions
executable file
·102 lines (81 loc) · 3.46 KB
/
treestore_stub.pl
File metadata and controls
executable file
·102 lines (81 loc) · 3.46 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
#!/usr/bin/perl
use strict;
use warnings;
#----------------------------------------------------------------------
# imports
#----------------------------------------------------------------------
use CGI;
use JSON;
use File::Slurp;
use Array::Utils qw(:all);
use File::Spec::Functions qw(catfile);
use HelperMethods;
use Data::DPath 'dpath';
use Data::Dumper;
use Getopt::Long;
use Log::Log4perl qw(:easy);
#----------------------------------------------------------------------
# constants
#----------------------------------------------------------------------
use constant USAGE => <<HEREDOC;
Usage 1: $0 <taxa_uri_1> [<taxa_uri_2>] ...
Usage 2: $0 < input-taxa-uris.txt
Example (Usage 1): $0 ' 'http://www.ncbi.nlm.nih.gov/Taxonomy/Browser/wwwtax.cgi?id=9606'
INPUT TAXA URIS FILE
An input taxa URIs file contains taxa URIs separated by newlines.
Example:
--- BEGIN FILE ---
http://www.itis.gov/servlet/SingleRpt/SingleRpt?search_topic=TSN&search_value=573081
http://www.ncbi.nlm.nih.gov/Taxonomy/Browser/wwwtax.cgi?id=9597
http://www.itis.gov/servlet/SingleRpt/SingleRpt?search_topic=TSN&search_value=180092
http://www.ncbi.nlm.nih.gov/Taxonomy/Browser/wwwtax.cgi?id=9606
http://www.itis.gov/servlet/SingleRpt/SingleRpt?search_topic=TSN&search_value=180362
http://www.ncbi.nlm.nih.gov/Taxonomy/Browser/wwwtax.cgi?id=10117
http://www.itis.gov/servlet/SingleRpt/SingleRpt?search_topic=TSN&search_value=180366
http://www.ncbi.nlm.nih.gov/Taxonomy/Browser/wwwtax.cgi?id=10090
--- END FILE ---
HEREDOC
use constant IS_CGI => exists $ENV{'GATEWAY_INTERFACE'};
use constant TNRS_OUTPUT_FILE => catfile('mock-data', 'tnrs-output.json');
use constant TNRS_OUTPUT_URIS_DPATH_FILE => catfile('mock-data', 'tnrs-output-uris.dpath');
use constant TREESTORE_OUTPUT_FILE => catfile('mock-data', 'treestore-output.json');
#----------------------------------------------------------------------
# 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 = $cgi->param('taxa_uris');
} 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 @taxa_uris = @ARGV;
#----------------------------------------------------------------------
# main
#----------------------------------------------------------------------
# extract taxa URIs from TNRS output
my $tnrs_output = read_file(TNRS_OUTPUT_FILE);
my $tnrs_data = JSON->new()->decode($tnrs_output);
my $uri_path = read_file(TNRS_OUTPUT_URIS_DPATH_FILE);
my @mock_input = dpath($uri_path)->match($tnrs_data);
my $mock_output = read_file(TREESTORE_OUTPUT_FILE);
if (array_diff(@taxa_uris, @mock_input)) {
HelperMethods::fatal(
sprintf("this service only supports a mock query for: (\n\t%s\n)", join(",\n\t", @mock_input)),
IS_CGI,
400
);
}
print $cgi->header(-status => 200, -type => 'application/json') if IS_CGI;
print $mock_output;