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
/
beacondeliver.cgi
executable file
·225 lines (150 loc) · 6.92 KB
/
beacondeliver.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
#!/usr/bin/perl
# Beacon+ support scripts
# © 2017-2019 Michael Baudis: [email protected]
use strict;
use CGI::Carp qw(fatalsToBrowser);
use CGI qw(param);
use JSON;
use MongoDB;
use MongoDB::MongoClient;
$MongoDB::Cursor::timeout = 120000;
use Data::Dumper;
# local packages
use BeaconPlus::ConfigLoader;
use BeaconPlus::QueryParameters;
use BeaconPlus::QueryExecution;
use lib './PGX';
use PGX;
=podmd
The __BeaconHandover__ is a utility server side application to provide
__h->o__ functionality to the _BeaconPlus_ environment.
The script accepts following parameters:
* `accessid`
- the `_id` of an entry in the `___handover_db___.___handover_coll___`
database, which in the standard implementation stores pointers to the
results from a _BeaconPlus_ query
* `do`
- an action from `handover_types`
* `jsonpretty`
- formatting instruction for JSON style
- defaults to '0', i.e. no line breaks etc.
- values of 1 or y or pretty will provide an formatted return
=cut
my $config = BeaconPlus::ConfigLoader->new();
my $error = q{};
our $handover = {};
my $pretty = $config->{param}->{jsonpretty}->[0];
if ($pretty !~ /^1|y|pretty/) {
$pretty = 0 }
else {
$pretty = 1 }
if (! grep{ /../ } keys %{ $config->{queries}->{handover} }) {
$error = '¡ Wrong or missing access_id parameter !' }
if (! grep{ $config->{param}->{do}->[0] eq $_ } keys %{ $config->{handover_types} }) {
$error = '¡ Wrong or missing "do" parameter !' }
if ($error !~ /\w/) {
$handover = MongoDB::MongoClient->new()->get_database( $config->{handover_db} )->get_collection( $config->{handover_coll} )->find_one( $config->{queries}->{handover} ) }
if (! $handover->{_id} ) {
$error = '¡ No handover object found !' }
if ($error =~ /\w/) {
print 'Content-type: text'."\n\n".$error;
exit;
}
_print_histogram();
_export_callsets();
_export_biosamples_individuals();
_export_variants();
_display_variants_in_UCSC();
exit;
################################################################################
# subs #########################################################################
################################################################################
sub _print_histogram {
if ($config->{param}->{do}->[0] !~ /histo/i) { return }
$config->{param}->{-plotid} = 'histoplot';
$config->{param}->{-text_bottom_left} = $handover->{source_db}.': '.$handover->{target_count}.' samples';
my $pgx = new PGX($config->{param});
$pgx->pgx_open_handover($config, $config->{param}->{accessid}->[0]);
$pgx->pgx_samples_from_handover();
$pgx->pgx_add_frequencymaps( [ { statusmapsets => $pgx->{samples} } ] );
$pgx->return_histoplot_svg();
print 'Content-type: image/svg+xml'."\n\n";
print $pgx->{svg};
exit;
}
################################################################################
sub _print_array {
# TODO ...
if ($config->{param}->{do}->[0] !~ /array/i) { return }
my $plotargs = { map{ $_ => join(',', @{ $config->{param}->{$_} }) } (grep{ /^\-\w+?$/ } keys %{ $config->{param} }) };
$config->{param}->{-plotid} = 'arrayplot';
$config->{param}->{-plottype} = 'array';
my $pgx = new PGX($config->{param});
$pgx->pgx_open_handover($config, $config->{param}->{accessid}->[0]);
$pgx->pgx_samples_from_handover();
$pgx->{parameters}->{text_bottom_left} = $pgx->{samples}->[0]->{id};
$pgx->pgx_add_frequencymaps( [ { statusmapsets => $pgx->{samples} } ] );
$pgx->return_histoplot_svg();
print 'Content-type: image/svg+xml'."\n\n";
print $pgx->{svg};
exit;
}
################################################################################
sub _export_callsets {
if ($config->{param}->{do}->[0] !~ /callsetsdata/i) { return }
print 'Content-type: application/json'."\n\n";
my $datacoll = MongoDB::MongoClient->new()->get_database( $handover->{source_db} )->get_collection( $handover->{target_collection} );
my $dataQuery = { $handover->{target_key} => { '$in' => $handover->{target_values} } };
my $cursor = $datacoll->find( $dataQuery )->fields( { _id => 0, updated => 0, created => 0 } );
print JSON::XS->new->pretty( $pretty )->allow_blessed->convert_blessed->encode([$cursor->all]);
exit;
}
################################################################################
sub _export_biosamples_individuals {
if (! grep{ /^$config->{param}->{do}->[0]$/ } qw(biosamplesdata individualsdata)) { return }
print 'Content-type: application/json'."\n\n";
my $dataconn = MongoDB::MongoClient->new()->get_database( $handover->{source_db} );
my $datacoll = $dataconn->get_collection($handover->{target_collection});
my $cursor = $datacoll->find( { $handover->{target_key} => { '$in' => $handover->{target_values} } } )->fields( { attributes => 0, _id => 0, updated => 0, created => 0 } );
print JSON::XS->new->pretty( $pretty )->allow_blessed->convert_blessed->encode([$cursor->all]);
exit;
}
################################################################################
sub _export_variants {
if ($config->{param}->{do}->[0] !~ /variants/) { return }
print 'Content-type: application/json'."\n\n";
my $dataconn = MongoDB::MongoClient->new()->get_database( $handover->{source_db} );
my $datacoll = $dataconn->get_collection( 'variants' );
my $cursor = {};
my $key = $handover->{target_key};
my $values = $handover->{target_values};
if ($config->{param}->{do}->[0] =~ /callset/i) {
$key = 'callset_id';
my $distincts = $dataconn->run_command([
"distinct"=> 'callsets',
"key" => 'id',
"query" => { _id => { '$in' => $handover->{target_values} } },
]);
$values = $distincts->{values};
}
$cursor = $datacoll->find( { $key => { '$in' => $values } } )->fields( { _id => 0, updated => 0, created => 0 } );
print JSON::XS->new->pretty( $pretty )->allow_blessed->convert_blessed->encode([$cursor->all]);
exit;
}
################################################################################
sub _display_variants_in_UCSC {
print 'Content-type: text/html'."\n\n";
use BeaconPlus::DataExporter;
if ($config->{param}->{do}->[0] !~ /ucsc/i) { return }
write_variants_bedfile($config, $handover);
my $ucscPos = 'chr'.$config->{param}->{ucscChro}->[0].':'.$config->{param}->{ucscStart}->[0];
if ($config->{param}->{ucscEnd}->[0] >= $config->{param}->{ucscStart}->[0]) {
$ucscPos .= '-'. $config->{param}->{ucscEnd}->[0] }
my $UCSCurl = 'http://genome.ucsc.edu/cgi-bin/hgTracks?ignoreCookie=1&org=human&db=hg38&position='.$ucscPos.'&hgt.customText='.$config->{url_base}.'/tmp/'.$handover->{_id}.'.bed';
print '<html>
<meta http-equiv="refresh" content="0; url='.$UCSCurl.'" />
<a href="'.$UCSCurl.'">'.$UCSCurl.'</a></html>'."\n";
exit;
}
################################################################################
1;