summaryrefslogtreecommitdiff
path: root/debian/efetch
blob: 1d5577ce62124ac85604b4a03d17a57e3da0d1f3 (plain)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#!/usr/bin/perl -w
# Detect whether to run efetch.acedb or edirect -fetch.

use strict;

my $usage = <<EOF;
USAGE: efetch [<AceDB efetch options>] [<database>:]<query>
 -or-  efetch [<NCBI Entrez Direct efetch options>]

per, respectively,

    efetch.acedb -h (or man efetch.acedb)
    efetch.ncbi -help (or man efetch)
EOF

if ( !@ARGV  ||  ! -x '/usr/bin/efetch.acedb' ) {
    ExecNCBI();
} elsif (grep /--?help/, @ARGV) {
    print $usage;
    exit 0;
}

my $acedb_flags = 'afqHprmMbAxw';
my $acedb_keys  = 'seDdilBn';

my %ncbi_supported = (
    'db'          => 's',
    'id'          => 's',
    'input'       => 's',
    'format'      => 's',
    'docsum'      => undef,
    'style'       => 's',
    'mode'        => 's',
    'seq_start'   => 'i',
    'seq_stop'    => 'i',
    'strand'      => 's',
    'forward'     => undef,
    'plus'        => undef,
    'revcomp'     => undef,
    'reverse'     => undef,
    'minus'       => undef,
    'complexity'  => 'i',
    'chr_start'   => 'i',
    'chr_stop'    => 'i',
    'extend'      => 'i',
    'extrafeat'   => 'i',
    'showgaps'    => undef,
    'show-gaps'   => undef,
    'start'       => 'i',
    'stop'        => 'i',
    'raw'         => undef,
    'json'        => undef,
    'external'    => undef,
    'ext'         => undef,
    'email'       => 's',
    'tool'        => 's',
    'help'        => undef,
    'debug'       => undef,
    'oldmode'     => undef,
    'newmode'     => undef,
    'log'         => undef,
    'immediate'   => undef,
    'express'     => undef,
    'base'        => 's',
    'web'         => 's',
    'step'        => 'i',
    'label'       => 's',
    'timer'       => undef,
    'version'     => undef);

my %ncbi_abbrev = ();
{
    my @names = sort keys %ncbi_supported;
    unshift @names, '';
    push @names, '';
    my $i;
    my $j;
    for ($i = 1;  $i + 1 < @names;  ++$i) {
        my $name      = $names[$i];
        my $prev_name = $names[$i - 1];
        my $next_name = $names[$i + 1];
        for ($j = 1;  $j < length($name);  ++$j) {
            my $abbrev = substr($name, 0, $j);
            if ($abbrev ne substr($prev_name, 0, $j)
                &&  $abbrev ne substr($next_name, 0, $j)) {
                $ncbi_abbrev{$abbrev} = $name;
            }
        }
    }
}

my @acedb_red_flags;
my @acedb_yellow_flags;
my $acedb_value_expected;
my $acedb_query;
my @ncbi_red_flags;
my @ncbi_yellow_flags;
my $ncbi_value_expected;

for (@ARGV) {
    if (/^-\d+$/  ||  !/^-/) {
        if ( !defined $acedb_value_expected ) {
            if (defined $acedb_query) {
                push @acedb_yellow_flags, "$acedb_query $_";
            }
            $acedb_query = $_;
        }
        if ( !defined $ncbi_value_expected ) {
            push @ncbi_red_flags, $_;
        }
        undef $acedb_value_expected;
        undef $ncbi_value_expected;
    } else {
        my $acedb_type;
        if (defined $acedb_value_expected) {
            push @acedb_red_flags, "$acedb_value_expected $_";
            $acedb_type = 'nominal-value';
            undef $acedb_value_expected;
        } elsif (/^-[ho]$/  ||  /^-[$acedb_flags]*(?:[$acedb_keys].+)?$/) {
            $acedb_type = 'flag';
        } elsif (/^-[$acedb_flags]*([$acedb_keys])$/) {
            $acedb_type = 'key';
            $acedb_value_expected = "-$1";
        } else {
            $acedb_type = 'invalid';
            push @acedb_red_flags, $_;
        }

        my $ncbi_type;
        if (defined $ncbi_value_expected) {
            push @ncbi_red_flags, "$ncbi_value_expected $_";
            $ncbi_type = 'nominal-value';
            undef $ncbi_value_expected;
        } elsif (/^--?([^=]+)(=.*)?/) {
            my $base = lc($1);
            my $has_value = defined $2;
            if (exists $ncbi_abbrev{$base}) {
                $base = $ncbi_abbrev{$base};
                push @ncbi_yellow_flags, $_;
            }
            if ( !exists $ncbi_supported{$base}
                 ||  ($has_value  &&  !defined $ncbi_supported{$base})) {
                $ncbi_type = 'invalid';
                push @ncbi_red_flags, $_;
            } else {
                if (lc($1) eq $base  &&  $1 =~ /[A-Z]/) {
                    # Worry about capitalization only if otherwise OK
                    push @ncbi_yellow_flags, $_;
                }
                if (defined $ncbi_supported{$base}  &&  !$has_value) {
                    $ncbi_type = 'key';
                    $ncbi_value_expected = $_;
                } else {
                    $ncbi_type = 'flag';
                }
            }
        } else {
            $ncbi_type = 'invalid';
            push @ncbi_red_flags, $_;
        }

        if ($acedb_type =~ /^[fk]/  &&  $ncbi_type =~ /^[fk]/) {
            # Could technically be valid for efetch.acedb, but far likelier
            # to have been intended for edirect -fetch.
            push @acedb_yellow_flags, $_;
        }
    }
}

if ( !defined $acedb_query ) {
    push @acedb_red_flags, "no query";
}
if (defined $acedb_value_expected) {
    push @acedb_red_flags, "$acedb_value_expected with no value";
}
if (defined $ncbi_value_expected) {
    push @ncbi_red_flags, "$ncbi_value_expected with no value";
}

my $acedb_red  = @acedb_red_flags;
my $acedb_ylw  = @acedb_yellow_flags;
my $ncbi_red   = @ncbi_red_flags;
my $ncbi_ylw   = @ncbi_yellow_flags;

if ($acedb_red  &&  !$ncbi_red) {
    # clear-cut, even if there happen to be NCBI yellow flags
    ExecNCBI();
} elsif ($ncbi_red  &&  !$acedb_red) {
    ExecAceDB();
}

my $acedb_misgivings = "AceDB misgivings ($acedb_red major, $acedb_ylw minor):";
my $ncbi_misgivings  = "NCBI misgivings ($ncbi_red major, $ncbi_ylw minor):";

for (@acedb_red_flags) {
    $acedb_misgivings .= "\n  $_ (major)";
}
for (@acedb_yellow_flags) {
    $acedb_misgivings .= "\n  $_ (minor)";
}
for (@ncbi_red_flags) {
    $ncbi_misgivings .= "\n  $_ (major)";
}
for (@ncbi_yellow_flags) {
    $ncbi_misgivings .= "\n  $_ (minor)";
}

if ($ncbi_red > $acedb_red
    ||  ($ncbi_red == $acedb_red  &&  $ncbi_ylw > $acedb_ylw)) {
    if ($acedb_red  ||  $acedb_ylw) {
        warn <<EOF;
Launching AceDB efetch rather than NCBI efetch despite misgivings,
due to more severe misgivings about NCBI syntax compatibility.  If you
meant to run NCBI efetch, please explicitly run it via edirect -fetch.
$ncbi_misgivings
$acedb_misgivings
EOF
    }
    ExecAceDB();
}

if ($acedb_red > $ncbi_red
    ||  ($acedb_red == $ncbi_red  &&  $acedb_ylw > $ncbi_ylw)) {
    if ($ncbi_red  ||  $ncbi_ylw) {
        warn <<EOF;
Launching NCBI efetch rather than AceDB efetch despite misgivings,
due to more severe misgivings about AceDB syntax compatibility.  If you
meant to run AceDB efetch, please explicitly run it via efetch.acedb.
$acedb_misgivings
$ncbi_misgivings
EOF
    }
    ExecNCBI();
}

if ($acedb_red) {
    die <<EOF;
Usage equally bad for both AceDB efetch and NCBI efetch.  Please double
check your usage or explicitly run efetch.acedb or edirect -fetch.
$acedb_misgivings
$ncbi_misgivings

$usage
EOF
} elsif ($acedb_ylw) {
    die <<EOF;
Usage equally suspect (but still technically valid, at least to first
approximation) for both AceDB efetch and NCBI efetch.  Please double
check your usage or explicitly run efetch.acedb or edirect -fetch.
$acedb_misgivings
$ncbi_misgivings

$usage
EOF
} else {
    die <<EOF;
Usage apparently valid (at least to first approximation) for both
AceDB efetch and NCBI efetch.  Please disambiguate your usage or
explicitly run efetch.acedb or edirect -fetch.
EOF
}

sub ExecAceDB
{
    # exec {'/usr/bin/efetch.acedb'} ('/usr/bin/efetch', @ARGV);
    exec('/usr/bin/efetch.acedb', @ARGV);
}
sub ExecNCBI
{
    exec('/usr/bin/efetch.ncbi', @ARGV);
}