-
Notifications
You must be signed in to change notification settings - Fork 23
/
HFCuS-client.pl
executable file
·233 lines (189 loc) · 5.88 KB
/
HFCuS-client.pl
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
#!/usr/bin/perl
use strict;
use warnings;
use lib 'lib';
use Getopt::Long;
use File::Basename;
# load the API package
use WWW::SwaggerClient::ApiFactory;
# load the models used
use WWW::SwaggerClient::Object::HFCurationResponse;
use WWW::SwaggerClient::Object::HFCurationRequest;
use WWW::SwaggerClient::Object::License;
# for displaying the API response data
use Data::Dumper;
#--- options and arguments:
my $opt_help;
my $opt_verbose = 0;
my $action = "";
my $submission_id;
my $base = "http://phycus.b12x.org:8080";
# my $base = "http://hfcus.b12x.org:8080";
my $opt_local;
my $prg = basename($0);
if (
!&GetOptions(
"help" => \$opt_help,
"local" => \$opt_local,
"verbose!" => \$opt_verbose, # -verbose, -noverbose
"action=s" => \$action,
"sub=s" => \$submission_id,
)
)
{
print "\n$prg: wrong option\n";
exit 1;
}
if ($opt_help) {
&help;
exit 0;
}
if($opt_local) {
$base = "http://localhost:8080";
}
print "Connecting to $base\n";
print "$prg: $action\n" if $opt_verbose;
if ( $action !~ /(get_sub|get_all|push|del_sub)/i ) {
print "\n$prg: wrong action $action\n";
exit 1;
}
if ( $action =~ /get_sub/i && !$submission_id ) {
print "\n$prg: Submission ID missing for action $action\n";
exit 1;
}
if ( $action =~ /del_sub/i && !$submission_id ) {
print "\n$prg: Submission ID missing for action $action\n";
exit 1;
}
my $api_instance = WWW::SwaggerClient::DefaultApi->new();
$api_instance->{api_client}->{config}{base_url} = $base;
$api_instance->{api_client}->{ua}->env_proxy;
print Dumper($api_instance) if $opt_verbose;
if ( $action eq "get_sub" ) {
&get_haplotypes( $api_instance, $submission_id );
}
elsif ( $action eq "del_sub" ) {
&delete_submission( $api_instance, $submission_id );
}
elsif ( $action eq "push" ) {
my $file;
if ( @ARGV == 1 ) {
$file = $ARGV[0];
}
else {
print "\n$prg: hfr file missing for action $action\n";
exit 1;
}
&push_haplotypes( $api_instance, $file );
}
elsif ( $action eq "get_all" ) {
&get_all_submissions($api_instance);
}
else {
print "\n$prg: wrong action $action\n";
exit 1;
}
exit 0;
# end of main
sub push_haplotypes {
my ( $api_instance, $file ) = @_;
my $HFCurReq = WWW::SwaggerClient::Object::HFCurationRequest->new();
my $HapFreqData =
WWW::SwaggerClient::Object::HaplotypeFrequencyData->new();
my $PopData = WWW::SwaggerClient::Object::PopulationData->new();
my $Lic = WWW::SwaggerClient::Object::License->new();
my $ResInfo = WWW::SwaggerClient::Object::ResolutionInfo->new();
print Dumper($HapFreqData) if $opt_verbose;
$Lic->{type_of_license} = "CC0";
$ResInfo->{resolution} = "4-Field";
$HapFreqData->{license} = $Lic;
$HapFreqData->{resolution_data}->[0] = $ResInfo;
open( HFR, $file ) || die "$prg: Cannot open file $file: $!";
my $i = 0;
while (<HFR>) {
chomp;
my ( $hap, $hf ) = split /,/;
my $HapFreq = WWW::SwaggerClient::Object::HaplotypeFrequency->new();
$HapFreq->{haplotype_string} = $hap;
$HapFreq->{frequency} = $hf;
$HapFreqData->{haplotype_frequency_list}->[$i] = $HapFreq;
$i++;
}
close HFR;
# Use /population to create a new Population
# See Population-client.pl
# Use /cohort to create a new Cohort
# See Cohort-client.pl
$HFCurReq->{population_id} = 1;
$HFCurReq->{cohort_id} = 1;
$HFCurReq->{haplotype_frequency_data} = $HapFreqData;
my $response = $api_instance->hfc_post( hf_curation_request => $HFCurReq );
print "$0: Submission $response->{submission_id}\n";
}
sub delete_submission {
my ( $api_instance, $submission_id ) = @_;
eval {
my $response = $api_instance->hfc_submission_id_delete(
submission_id => $submission_id );
print "Submission ID: " . $submission_id . " deleted.\n";
};
if ($@) {
warn
"Exception when calling DefaultApi->hfc_submission_id_delete: $@\n";
}
}
sub get_haplotypes {
my ( $api_instance, $submission_id ) = @_;
eval {
my $haplotypes = $api_instance->hfc_submission_id_haplotypes_get(
submission_id => $submission_id );
print Dumper($haplotypes);
};
if ($@) {
warn
"Exception when calling DefaultApi->hfc_submission_id_haplotypes_get: $@\n";
}
} # end sub get_haplotypes
sub get_all_submissions {
my ($api_instance) = @_;
eval {
my $result = $api_instance->hfc_get();
print Dumper($result);
};
if ($@) {
warn "Exception when calling DefaultApi->hfc_get: $@\n";
}
} # end of get_all_submissions
#=============================================================================
sub help {
#-----------------------------------------------------------------------------
# Called by: MAIN
# Purpose: show help
# Arguments: none
# Returns: nothing
# ToDo:
# Remarks:
#-----------------------------------------------------------------------------
my $def_v = $opt_verbose ? "-- verbose" : "-- noverbose";
print << "__END_OF_HELP__";
Name
$prg
Purpose
Client for HFCuS
Syntax
$prg [--help]
$prg [--verbose] --action=[get_sub|get_all|push|del_sub]
Options and arguments
--help show this help
--[no]verbose set verbose mode; default: $def_v
--action get_sub: getting HF set by submission ID -sub <submission_id>
push: push HF set <file>
get_all: get all submissions
del_sub: deleting HF set by submission ID -sub <submission_id>
--local connects to localhost:8080 as the server instead of http://phycus.b12x.org:8080
The action push requires a file with HF data as argument.
__END_OF_HELP__
return;
} # end sub help()
#=============================================================================
# end of file