Debian Codegraph @

debian/efetch

raw
1 #!/usr/bin/perl -w
2 # Detect whether to run efetch.acedb or edirect -fetch.
3
4 use strict;
5
6 my $usage = <<EOF;
7 USAGE: efetch [<AceDB efetch options>] [<database>:]<query>
8 -or- efetch [<NCBI Entrez Direct efetch options>]
9
10 per, respectively,
11
12 efetch.acedb -h (or man efetch.acedb)
13 efetch.ncbi -help (or man efetch)
14 EOF
15
16 if ( !@ARGV || ! -x '/usr/bin/efetch.acedb' ) {
17 ExecNCBI();
18 } elsif (grep /--?help/, @ARGV) {
19 print $usage;
20 exit 0;
21 }
22
23 my $acedb_flags = 'afqHprmMbAxw';
24 my $acedb_keys = 'seDdilBn';
25
26 my %ncbi_supported = (
27 'db' => 's',
28 'id' => 's',
29 'input' => 's',
30 'format' => 's',
31 'docsum' => undef,
32 'style' => 's',
33 'mode' => 's',
34 'seq_start' => 'i',
35 'seq_stop' => 'i',
36 'strand' => 's',
37 'forward' => undef,
38 'plus' => undef,
39 'revcomp' => undef,
40 'reverse' => undef,
41 'minus' => undef,
42 'complexity' => 'i',
43 'chr_start' => 'i',
44 'chr_stop' => 'i',
45 'extend' => 'i',
46 'extrafeat' => 'i',
47 'showgaps' => undef,
48 'show-gaps' => undef,
49 'start' => 'i',
50 'stop' => 'i',
51 'raw' => undef,
52 'json' => undef,
53 'external' => undef,
54 'ext' => undef,
55 'email' => 's',
56 'tool' => 's',
57 'help' => undef,
58 'debug' => undef,
59 'oldmode' => undef,
60 'newmode' => undef,
61 'verbose' => undef,
62 'tranquil' => undef,
63 'log' => undef,
64 'immediate' => undef,
65 'express' => undef,
66 'base' => 's',
67 'web' => 's',
68 'step' => 'i',
69 'label' => 's',
70 'timer' => undef,
71 'version' => undef);
72
73 my %ncbi_abbrev = ();
74 {
75 my @names = sort keys %ncbi_supported;
76 unshift @names, '';
77 push @names, '';
78 my $i;
79 my $j;
80 for ($i = 1; $i + 1 < @names; ++$i) {
81 my $name = $names[$i];
82 my $prev_name = $names[$i - 1];
83 my $next_name = $names[$i + 1];
84 for ($j = 1; $j < length($name); ++$j) {
85 my $abbrev = substr($name, 0, $j);
86 if ($abbrev ne substr($prev_name, 0, $j)
87 && $abbrev ne substr($next_name, 0, $j)) {
88 $ncbi_abbrev{$abbrev} = $name;
89 }
90 }
91 }
92 }
93
94 my @acedb_red_flags;
95 my @acedb_yellow_flags;
96 my $acedb_value_expected;
97 my $acedb_query;
98 my @ncbi_red_flags;
99 my @ncbi_yellow_flags;
100 my $ncbi_value_expected;
101
102 for (@ARGV) {
103 if (/^-\d+$/ || !/^-/) {
104 if ( !defined $acedb_value_expected ) {
105 if (defined $acedb_query) {
106 push @acedb_yellow_flags, "$acedb_query $_";
107 }
108 $acedb_query = $_;
109 }
110 if ( !defined $ncbi_value_expected ) {
111 push @ncbi_red_flags, $_;
112 }
113 undef $acedb_value_expected;
114 undef $ncbi_value_expected;
115 } else {
116 my $acedb_type;
117 if (defined $acedb_value_expected) {
118 push @acedb_red_flags, "$acedb_value_expected $_";
119 $acedb_type = 'nominal-value';
120 undef $acedb_value_expected;
121 } elsif (/^-[ho]$/ || /^-[$acedb_flags]*(?:[$acedb_keys].+)?$/) {
122 $acedb_type = 'flag';
123 } elsif (/^-[$acedb_flags]*([$acedb_keys])$/) {
124 $acedb_type = 'key';
125 $acedb_value_expected = "-$1";
126 } else {
127 $acedb_type = 'invalid';
128 push @acedb_red_flags, $_;
129 }
130
131 my $ncbi_type;
132 if (defined $ncbi_value_expected) {
133 push @ncbi_red_flags, "$ncbi_value_expected $_";
134 $ncbi_type = 'nominal-value';
135 undef $ncbi_value_expected;
136 } elsif (/^--?([^=]+)(=.*)?/) {
137 my $base = lc($1);
138 my $has_value = defined $2;
139 if (exists $ncbi_abbrev{$base}) {
140 $base = $ncbi_abbrev{$base};
141 push @ncbi_yellow_flags, $_;
142 }
143 if ( !exists $ncbi_supported{$base}
144 || ($has_value && !defined $ncbi_supported{$base})) {
145 $ncbi_type = 'invalid';
146 push @ncbi_red_flags, $_;
147 } else {
148 if (lc($1) eq $base && $1 =~ /[A-Z]/) {
149 # Worry about capitalization only if otherwise OK
150 push @ncbi_yellow_flags, $_;
151 }
152 if (defined $ncbi_supported{$base} && !$has_value) {
153 $ncbi_type = 'key';
154 $ncbi_value_expected = $_;
155 } else {
156 $ncbi_type = 'flag';
157 }
158 }
159 } else {
160 $ncbi_type = 'invalid';
161 push @ncbi_red_flags, $_;
162 }
163
164 if ($acedb_type =~ /^[fk]/ && $ncbi_type =~ /^[fk]/) {
165 # Could technically be valid for efetch.acedb, but far likelier
166 # to have been intended for edirect -fetch.
167 push @acedb_yellow_flags, $_;
168 }
169 }
170 }
171
172 if ( !defined $acedb_query ) {
173 push @acedb_red_flags, "no query";
174 }
175 if (defined $acedb_value_expected) {
176 push @acedb_red_flags, "$acedb_value_expected with no value";
177 }
178 if (defined $ncbi_value_expected) {
179 push @ncbi_red_flags, "$ncbi_value_expected with no value";
180 }
181
182 my $acedb_red = @acedb_red_flags;
183 my $acedb_ylw = @acedb_yellow_flags;
184 my $ncbi_red = @ncbi_red_flags;
185 my $ncbi_ylw = @ncbi_yellow_flags;
186
187 if ($acedb_red && !$ncbi_red) {
188 # clear-cut, even if there happen to be NCBI yellow flags
189 ExecNCBI();
190 } elsif ($ncbi_red && !$acedb_red) {
191 ExecAceDB();
192 }
193
194 my $acedb_misgivings = "AceDB misgivings ($acedb_red major, $acedb_ylw minor):";
195 my $ncbi_misgivings = "NCBI misgivings ($ncbi_red major, $ncbi_ylw minor):";
196
197 for (@acedb_red_flags) {
198 $acedb_misgivings .= "\n $_ (major)";
199 }
200 for (@acedb_yellow_flags) {
201 $acedb_misgivings .= "\n $_ (minor)";
202 }
203 for (@ncbi_red_flags) {
204 $ncbi_misgivings .= "\n $_ (major)";
205 }
206 for (@ncbi_yellow_flags) {
207 $ncbi_misgivings .= "\n $_ (minor)";
208 }
209
210 if ($ncbi_red > $acedb_red
211 || ($ncbi_red == $acedb_red && $ncbi_ylw > $acedb_ylw)) {
212 if ($acedb_red || $acedb_ylw) {
213 warn <<EOF;
214 Launching AceDB efetch rather than NCBI efetch despite misgivings,
215 due to more severe misgivings about NCBI syntax compatibility. If you
216 meant to run NCBI efetch, please explicitly run it via edirect -fetch.
217 $ncbi_misgivings
218 $acedb_misgivings
219 EOF
220 }
221 ExecAceDB();
222 }
223
224 if ($acedb_red > $ncbi_red
225 || ($acedb_red == $ncbi_red && $acedb_ylw > $ncbi_ylw)) {
226 if ($ncbi_red || $ncbi_ylw) {
227 warn <<EOF;
228 Launching NCBI efetch rather than AceDB efetch despite misgivings,
229 due to more severe misgivings about AceDB syntax compatibility. If you
230 meant to run AceDB efetch, please explicitly run it via efetch.acedb.
231 $acedb_misgivings
232 $ncbi_misgivings
233 EOF
234 }
235 ExecNCBI();
236 }
237
238 if ($acedb_red) {
239 die <<EOF;
240 Usage equally bad for both AceDB efetch and NCBI efetch. Please double
241 check your usage or explicitly run efetch.acedb or edirect -fetch.
242 $acedb_misgivings
243 $ncbi_misgivings
244
245 $usage
246 EOF
247 } elsif ($acedb_ylw) {
248 die <<EOF;
249 Usage equally suspect (but still technically valid, at least to first
250 approximation) for both AceDB efetch and NCBI efetch. Please double
251 check your usage or explicitly run efetch.acedb or edirect -fetch.
252 $acedb_misgivings
253 $ncbi_misgivings
254
255 $usage
256 EOF
257 } else {
258 die <<EOF;
259 Usage apparently valid (at least to first approximation) for both
260 AceDB efetch and NCBI efetch. Please disambiguate your usage or
261 explicitly run efetch.acedb or edirect -fetch.
262 EOF
263 }
264
265 sub ExecAceDB
266 {
267 # exec {'/usr/bin/efetch.acedb'} ('/usr/bin/efetch', @ARGV);
268 exec('/usr/bin/efetch.acedb', @ARGV);
269 }
270 sub ExecNCBI
271 {
272 exec('/usr/bin/efetch.ncbi', @ARGV);
273 }