This repository has been archived by the owner on May 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbeaconresponse.cgi
executable file
·231 lines (175 loc) · 8.7 KB
/
beaconresponse.cgi
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
#!/usr/bin/perl
# Progenetix & arrayMap site scripts
# Beacon+ server implementation based on arraymap.org | progenetix.org data
# © 2000-2019 Michael Baudis: [email protected]
use strict;
use CGI::Carp qw(fatalsToBrowser);
use CGI qw(param);
use Data::Dumper;
use JSON;
use MongoDB;
use MongoDB::MongoClient;
$MongoDB::Cursor::timeout = 36000;
use BeaconPlus::ConfigLoader;
use BeaconPlus::QueryParameters;
use BeaconPlus::QueryExecution;
use BeaconPlus::DataExporter;
=podmd
The "beaconresponse.cgi" script is a server backend for the Beacon protocol,
specifically adapted to connect to _MongoDB_ database backends that adhere to
the core GA4GH model.
#### Example Queries
* <https://beacon.progenetix.org/cgi-bin/beaconresponse.cgi?datasetIds=arraymap&datasetIds=progenetix&referenceName=9&assemblyId=GRCh38&variantType=DEL&startMin=19500000&startMax=21975098&endMin=21967753&endMax=24500000&referenceBases=N&filters=NCIT:C3224>
- focal deletions in the CDKN2A locus, filtered for "NCIT:C3224" (Melanoma)
- this query is against 2 datasets (progenetix, arraymap)
- *this query puts some serious strain on the server!*
* <https://beacon.progenetix.org/cgi-bin/beaconresponse.cgi/?datasetIds=arraymap&referenceName=9&assemblyId=GRCh38&variantType=DEL&startMin=19500000&startMax=21975098&endMin=21967753&endMax=24500000&referenceBases=N&filters=icdom-94003>
- as above, but using "icdom-94003", the internal code for ICD-O 3 "9400/3" (Glioblastoma, NOS)
* <https://beacon.progenetix.org/cgi-bin/beaconresponse.cgi?datasetIds=dipg&referenceName=17&assemblyId=GRCh38&startMin=7572826&endMax=7579005&referenceBases=*&alternateBases=N&filters=icdot-C71.7&>
- wildcard range query for allelic variants on chromosome 17, between bases 7572826 and 7579005
- dataset "dipg"
- Since the current specification requires a `alternateBases` parameter, and doesn't include wildcards *but* allows "N", the query will __only__ return variants where the replacement has a length of 1. "NN" leads to 2 etc. This may be changed in future versions of the protocol.
* <https://beacon.progenetix.org/cgi-bin/beaconresponse.cgi?datasetIds=dipg&referenceName=17&assemblyId=GRCh38&start=7577121&referenceBases=G&alternateBases=A&filters=icdot-C71.7&>
- the "traditional" precise BeaconAlleleRequest, as supported by the first Beacon version
- dataset "dipg"
=cut
my $config = BeaconPlus::ConfigLoader->new();
################################################################################
my $datasetR = [];
if (
(! grep{ /ERROR/i } @{ $config->{query_errors}->{variants} })
||
$config->{param}->{overrideErrors}->[0] > 0
) {
foreach (@{ $config->{ dataset_names } }) {
push(@$datasetR, _getDataset($config, $_) );
}
} else {
foreach (@{ $config->{ dataset_names } }) {
push(@$datasetR, { error => join("\n", @{ $config->{query_errors}->{variants} }) } );
}
print "Status: 400 Bad request\n".'Content-type: application/json'."\n\n".JSON::XS->new->encode($datasetR)."\n";
exit;
}
if (! -t STDIN) { print 'Status: 200'."\n".'Content-type: application/json'."\n\n" }
my $bcExists = \0;
foreach (@$datasetR) {
if ($_->{variantCount} > 0) {
$bcExists = \1 }
}
my $response = {
beaconId => $config->{id},
# changeDate => $config->{changeDate},
exists => $bcExists,
apiVersion => $config->{apiVersion},
url => $config->{welcomeUrl},
info => {
queryString => $ENV{QUERY_STRING},
version => 'Beacon+ implementation based on the development branch of the ELIXIR Beacon project with custom extensions',
},
};
=podmd
The parameters of the allele request are verified & added to the response.
=cut
my $v_pars_conf = $config->{q_conf}->{scopes}->{variants}->{parameters};
foreach (keys %{ $v_pars_conf }) {
if ($config->{param}->{$_}->[0] =~ /$v_pars_conf->{$_}->{pattern}/) {
$response->{alleleRequest}->{$_} = $config->{param}->{$_}->[0];
if ($v_pars_conf->{$_}->{type} eq 'integer') {
$response->{alleleRequest}->{$_} *= 1 }
}
}
if (! $config->{param}->{includeDatasetResponses}->[0]) {
$datasetR = [] }
elsif ($config->{param}->{includeDatasetResponses}->[0] eq 'HIT') {
$datasetR = [ grep{ $_->{variantCount} > 0 } @$datasetR ] }
elsif ($config->{param}->{includeDatasetResponses}->[0] eq 'MISS') {
$datasetR = [ grep{ $_->{variantCount} < 1 } @$datasetR ] }
if (@$datasetR > 0) {
$response->{datasetAlleleResponses} = $datasetR }
print JSON::XS->new->pretty( 0 )->encode($response)."\n";
exit;
################################################################################
################################################################################
# SUBs #########################################################################
################################################################################
################################################################################
sub _getDataset {
=pod
=cut
my $config = shift;
my $dataset = shift;
# Handover:
my $handover = [];
# custom handling of distinct variants
my $varResp = [];
my $varDistNo = 0;
=podmd
#### Beacon Query Execution
Different types of query are run, if parameters for them exist.
The current concept is:
- run queries on the different primary object types (individual, biosample, callset, variant)
- provide the object-level counts as output (and store the ids for handover procedures)
- aggregate on biosamples
* Variant query
* Callset query
* Biosample query
* Individual query
The details of the query execution can be found in the documentation for the
[__BeaconPlus::QueryExecution.pm__](/doc/+generated-doc-BeaconPlus-QueryExecution/) module.
=cut
my $counts = {
frequency => 0,
exists => \0,
};
my $prefetch = BeaconPlus::QueryExecution->new($config, $dataset);
$prefetch->execute_aggregate_query();
##############################################################################
my $exporter = BeaconPlus::DataExporter->new($config, $prefetch);
my $ucscStart = $config->{param}->{start}->[0] =~ /^\d+?/ ? $config->{param}->{start}->[0] : $config->{param}->{startMin}->[0];
$ucscStart += 1;
my $ucscEnd;
if ($config->{param}->{end}->[0] =~ /^\d+?/) {
$ucscEnd = $config->{param}->{end}->[0] }
elsif ($config->{param}->{endMax}->[0] =~ /^\d+?/) {
$ucscEnd = $config->{param}->{endMax}->[0] }
if ($ucscEnd < $ucscStart) {
$ucscEnd = $ucscStart }
$exporter->{handover_types}->{UCSClink}->{link_post} = '&ucscChro='.$config->{param}->{referenceName}->[0].'&ucscStart='.$ucscStart.'&ucscEnd='.$ucscEnd;
$exporter->create_handover_exporter();
$varResp = $prefetch->{handover}->{'variants::digest'}->{target_values};
##############################################################################
if ($prefetch->{counts}->{variants_match_count} > 0) { $counts->{'exists'} = \1 }
if ($prefetch->{counts}->{biosamples_base_count} > 0) {
$counts->{frequency} = sprintf "%.4f", $prefetch->{counts}->{biosamples_match_count} / $prefetch->{counts}->{biosamples_base_count} }
if (! $counts->{"exists"}) { $handover = [] }
my $dsResp = {
datasetId => $dataset,
"exists" => $counts->{"exists"},
accessType => 'PUBLIC',
error => $prefetch->{error},
frequency => $counts->{frequency} * 1,
variantCount => $prefetch->{counts}->{variants_match_count} * 1,
varResp => $varResp,
callCount => $prefetch->{counts}->{callsets_match_count} * 1,
sampleCount => $prefetch->{counts}->{biosamples_match_count} * 1,
note => q{},
externalUrl => $config->{url},
datasetHandover => $exporter->{handover},
info => {
description => 'The query was against database "'.$dataset.'", variant collection "'.$config->{collection_names}->{variant_collection}.'". '.$prefetch->{counts}->{callsets_match_count}.' matched callsets for '.$prefetch->{counts}->{variants_match_count}.' distinct variants. Out of '.$prefetch->{counts}->{biosamples_base_count}.' biosamples in the database, '.$prefetch->{counts}->{biosamples_query_match_count}.' matched the biosample query; of those, '.$prefetch->{counts}->{biosamples_match_count}.' had the variant.',
distinctVarCount => $prefetch->{handover}->{'variants::digest'}->{target_count},
},
};
my $v_pars_conf = $config->{q_conf}->{scopes}->{variants}->{parameters};
foreach (keys %{ $v_pars_conf }) {
# $response->{alleleRequest}->{$_} = \0;
if ($config->{param}->{$_}->[0] =~ /$v_pars_conf->{$_}->{pattern}/) {
$dsResp->{$_} = $config->{param}->{$_}->[0];
if ($v_pars_conf->{$_}->{type} eq 'integer') {
$dsResp->{$_} *= 1 }
}
}
return $dsResp;
}
1;