-
Notifications
You must be signed in to change notification settings - Fork 23
/
emboss_dotpath.pl
1084 lines (877 loc) · 33.2 KB
/
emboss_dotpath.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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env perl
=head1 NAME
emboss_dotpath.pl
=head1 DESCRIPTION
EMBOSS dotpath (REST) web service Perl client using L<LWP>.
Tested with:
=over
=item *
L<LWP> 6.35, L<XML::Simple> 2.25 and Perl 5.22.0 (MacOS 10.13.6)
=back
For further information see:
=over
=item *
L<https://www.ebi.ac.uk/Tools/webservices/>
=back
=head1 LICENSE
Copyright 2012-2024 EMBL - European Bioinformatics Institute
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Perl Client Automatically generated with:
https://github.com/ebi-jdispatcher/webservice-clients-generator
=cut
# ======================================================================
# Enable Perl warnings
use strict;
use warnings;
# Load libraries
use English;
use LWP;
use XML::Simple;
use Getopt::Long qw(:config no_ignore_case bundling);
use File::Basename;
use Data::Dumper;
use Time::HiRes qw(usleep);
# Base URL for service
my $baseUrl = 'https://www.ebi.ac.uk/Tools/services/rest/emboss_dotpath';
my $version = '2024-07-12 11:05';
# Set interval for checking status
my $checkInterval = 3;
# Set maximum number of 'ERROR' status calls to call job failed.
my $maxErrorStatusCount = 3;
# Output level
my $outputLevel = 1;
# Process command-line options
my $numOpts = scalar(@ARGV);
my %params = (
'debugLevel' => 0,
'maxJobs' => 1
);
# Default parameter values (should get these from the service)
GetOptions(
# Tool specific options
'stype=s' => \$params{'stype'}, # Defines the type of the sequences to be aligned
'asequence=s' => \$params{'asequence'}, # A free text (raw) list of sequences is simply a block of characters representing several DNA/RNA or Protein sequences. A sequence can be in GCG, FASTA, EMBL (Nucleotide only), GenBank, PIR, NBRF, PHYLIP or UniProtKB/Swiss-Prot (Protein only) format. Partially formatted sequences are not accepted. Adding a return to the end of the sequence may help certain applications understand the input. Note that directly using data from word processors may yield unpredictable results as hidden/control characters may be present.
'bsequence=s' => \$params{'bsequence'}, # A free text (raw) list of sequences is simply a block of characters representing several DNA/RNA or Protein sequences. A sequence can be in GCG, FASTA, EMBL (Nucleotide only), GenBank, PIR, NBRF, PHYLIP or UniProtKB/Swiss-Prot (Protein only) format. Partially formatted sequences are not accepted. Adding a return to the end of the sequence may help certain applications understand the input. Note that directly using data from word processors may yield unpredictable results as hidden/control characters may be present.
'wordsize=i' => \$params{'wordsize'}, # Word size
'overlaps' => \$params{'overlaps'}, # Displays the overlapping matches (in red) as well as the minimal set of non-overlapping matches.
'boxit' => \$params{'boxit'}, # Draw a box around dotplot.
# Generic options
'email=s' => \$params{'email'}, # User e-mail address
'title=s' => \$params{'title'}, # Job title
'outfile=s' => \$params{'outfile'}, # Output file name
'outformat=s' => \$params{'outformat'}, # Output file type
'jobid=s' => \$params{'jobid'}, # JobId
'help|h' => \$params{'help'}, # Usage help
'asyncjob' => \$params{'asyncjob'}, # Asynchronous submission
'polljob' => \$params{'polljob'}, # Get results
'pollFreq=f' => \$params{'pollFreq'}, # Poll Frequency
'resultTypes' => \$params{'resultTypes'}, # Get result types
'status' => \$params{'status'}, # Get status
'params' => \$params{'params'}, # List input parameters
'paramDetail=s' => \$params{'paramDetail'}, # Get details for parameter
'verbose' => \$params{'verbose'}, # Increase output level
'version' => \$params{'version'}, # Prints out the version of the Client and exit.
'quiet' => \$params{'quiet'}, # Decrease output level
'debugLevel=i' => \$params{'debugLevel'}, # Debugging level
'baseUrl=s' => \$baseUrl, # Base URL for service.
);
if ($params{'verbose'}) {$outputLevel++}
if ($params{'quiet'}) {$outputLevel--}
if ($params{'pollFreq'}) {$checkInterval = $params{'pollFreq'} * 1000 * 1000}
if ($params{'baseUrl'}) {$baseUrl = $params{'baseUrl'}}
# Debug mode: LWP version
&print_debug_message('MAIN', 'LWP::VERSION: ' . $LWP::VERSION,
1);
# Debug mode: print the input parameters
&print_debug_message('MAIN', "params:\n" . Dumper(\%params), 11);
# LWP UserAgent for making HTTP calls (initialised when required).
my $ua;
# Get the script filename for use in usage messages
my $scriptName = basename($0, ());
# Print usage and exit if requested
if ($params{'help'} || $numOpts == 0) {
&usage();
exit(0);
}
# Debug mode: show the base URL
&print_debug_message('MAIN', 'baseUrl: ' . $baseUrl, 1);
if (
!(
$params{'polljob'}
|| $params{'resultTypes'}
|| $params{'status'}
|| $params{'params'}
|| $params{'paramDetail'}
)
&& !((defined($ARGV[0]) && defined($ARGV[1])) ||
(defined($params{'asequence'}) && defined($params{'bsequence'})))
) {
# Bad argument combination, so print error message and usage
print STDERR 'Error: bad option combination', "\n";
&usage();
exit(1);
}
# Get parameters list
elsif ($params{'params'}) {
&print_tool_params();
}
# Get parameter details
elsif ($params{'paramDetail'}) {
&print_param_details($params{'paramDetail'});
}
# Print Client version
elsif ($params{'version'}) {
print STDOUT 'Revision: ' . $version, "\n";
exit(1);
}
# Job status
elsif ($params{'status'} && defined($params{'jobid'})) {
&print_job_status($params{'jobid'});
}
# Result types
elsif ($params{'resultTypes'} && defined($params{'jobid'})) {
&print_result_types($params{'jobid'});
}
# Poll job and get results
elsif ($params{'polljob'} && defined($params{'jobid'})) {
&get_results($params{'jobid'});
}
# Submit a job
else {
# Multiple input sequence mode, assume fasta format.
if (defined($params{'multifasta'}) && $params{'multifasta'}) {
&multi_submit_job();
}
# Entry identifier list file.
elsif ((defined($params{'sequence'}) && $params{'sequence'} =~ m/^\@/)
|| (defined($ARGV[0]) && $ARGV[0] =~ m/^\@/)) {
my $list_filename = $params{'sequence'} || $ARGV[0];
$list_filename =~ s/^\@//;
&list_file_submit_job($list_filename);
}
# Default: single sequence/identifier.
else {
# Warn for invalid batch only option use.
if (defined($params{'useSeqId'}) && $params{'useSeqId'}) {
print STDERR "Warning: --useSeqId option ignored.\n";
delete $params{'useSeqId'};
}
if (defined($params{'maxJobs'}) && $params{'maxJobs'} > 1) {
print STDERR "Warning: --maxJobs option ignored.\n";
$params{'maxJobs'} = 1;
}
# Load the sequence data and submit.
&submit_job(&load_data());
}
}
=head1 FUNCTIONS
=cut
### Wrappers for REST resources ###
=head2 rest_user_agent()
Get a LWP UserAgent to use to perform REST requests.
my $ua = &rest_user_agent();
=cut
sub rest_user_agent() {
print_debug_message('rest_user_agent', 'Begin', 21);
# Create an LWP UserAgent for making HTTP calls.
my $ua = LWP::UserAgent->new();
# Set 'User-Agent' HTTP header to identifiy the client.
my $revisionNumber = 0;
$revisionNumber = "Revision: " . $version;
$ua->agent("EBI-Sample-Client/$revisionNumber ($scriptName; $OSNAME) " . $ua->agent());
# Configure HTTP proxy support from environment.
$ua->env_proxy;
print_debug_message('rest_user_agent', 'End', 21);
return $ua;
}
=head2 rest_error()
Check a REST response for an error condition. An error is mapped to a die.
&rest_error($response, $content_data);
=cut
sub rest_error() {
print_debug_message('rest_error', 'Begin', 21);
my $response = shift;
my $contentdata;
if (scalar(@_) > 0) {
$contentdata = shift;
}
if (!defined($contentdata) || $contentdata eq '') {
$contentdata = $response->content();
}
# Check for HTTP error codes
if ($response->is_error) {
my $error_message = '';
# HTML response.
if ($contentdata =~ m/<h1>([^<]+)<\/h1>/) {
$error_message = $1;
}
# XML response.
elsif ($contentdata =~ m/<description>([^<]+)<\/description>/) {
$error_message = $1;
}
# die 'http status: ' . $response->code . ' ' . $response->message . ' ' . $error_message;
}
print_debug_message('rest_error', 'End', 21);
}
=head2 rest_request()
Perform a REST request (HTTP GET).
my $response_str = &rest_request($url);
=cut
sub rest_request {
print_debug_message('rest_request', 'Begin', 11);
my $requestUrl = shift;
print_debug_message('rest_request', 'URL: ' . $requestUrl, 11);
# Get an LWP UserAgent.
$ua = &rest_user_agent() unless defined($ua);
# Available HTTP compression methods.
my $can_accept;
eval {
$can_accept = HTTP::Message::decodable();
};
$can_accept = '' unless defined($can_accept);
# Perform the request
my $response = $ua->get($requestUrl,
'Accept-Encoding' => $can_accept, # HTTP compression.
);
print_debug_message('rest_request', 'HTTP status: ' . $response->code,
11);
print_debug_message('rest_request',
'response length: ' . length($response->content()), 11);
print_debug_message('rest_request',
'request:' . "\n" . $response->request()->as_string(), 32);
print_debug_message('rest_request',
'response: ' . "\n" . $response->as_string(), 32);
# Unpack possibly compressed response.
my $retVal;
if (defined($can_accept) && $can_accept ne '') {
$retVal = $response->decoded_content();
}
# If unable to decode use orginal content.
$retVal = $response->content() unless defined($retVal);
# Check for an error.
&rest_error($response, $retVal);
print_debug_message('rest_request', 'retVal: ' . $retVal, 12);
print_debug_message('rest_request', 'End', 11);
# Return the response data
return $retVal;
}
=head2 rest_get_parameters()
Get list of tool parameter names.
my (@param_list) = &rest_get_parameters();
=cut
sub rest_get_parameters {
print_debug_message('rest_get_parameters', 'Begin', 1);
my $url = $baseUrl . '/parameters/';
my $param_list_xml_str = rest_request($url);
my $param_list_xml = XMLin($param_list_xml_str);
my (@param_list) = @{$param_list_xml->{'id'}};
print_debug_message('rest_get_parameters', 'End', 1);
return(@param_list);
}
=head2 rest_get_parameter_details()
Get details of a tool parameter.
my $paramDetail = &rest_get_parameter_details($param_name);
=cut
sub rest_get_parameter_details {
print_debug_message('rest_get_parameter_details', 'Begin', 1);
my $parameterId = shift;
print_debug_message('rest_get_parameter_details',
'parameterId: ' . $parameterId, 1);
my $url = $baseUrl . '/parameterdetails/' . $parameterId;
my $param_detail_xml_str = rest_request($url);
my $param_detail_xml = XMLin($param_detail_xml_str);
print_debug_message('rest_get_parameter_details', 'End', 1);
return($param_detail_xml);
}
=head2 rest_run()
Submit a job.
my $job_id = &rest_run($email, $title, \%params );
=cut
sub rest_run {
print_debug_message('rest_run', 'Begin', 1);
my $email = shift;
my $title = shift;
my $params = shift;
$email = '' if (!$email);
print_debug_message('rest_run', 'email: ' . $email, 1);
if (defined($title)) {
print_debug_message('rest_run', 'title: ' . $title, 1);
}
print_debug_message('rest_run', 'params: ' . Dumper($params), 1);
# Get an LWP UserAgent.
$ua = &rest_user_agent() unless defined($ua);
# Clean up parameters
my (%tmp_params) = %{$params};
$tmp_params{'email'} = $email;
$tmp_params{'title'} = $title;
foreach my $param_name (keys(%tmp_params)) {
if (!defined($tmp_params{$param_name})) {
delete $tmp_params{$param_name};
}
}
# Submit the job as a POST
my $url = $baseUrl . '/run';
my $response = $ua->post($url, \%tmp_params);
print_debug_message('rest_run', 'HTTP status: ' . $response->code, 11);
print_debug_message('rest_run',
'request:' . "\n" . $response->request()->as_string(), 11);
print_debug_message('rest_run',
'response: ' . length($response->as_string()) . "\n" . $response->as_string(), 11);
# Check for an error.
&rest_error($response);
# The job id is returned
my $job_id = $response->content();
print_debug_message('rest_run', 'End', 1);
return $job_id;
}
=head2 rest_get_status()
Check the status of a job.
my $status = &rest_get_status($job_id);
=cut
sub rest_get_status {
print_debug_message('rest_get_status', 'Begin', 1);
my $job_id = shift;
print_debug_message('rest_get_status', 'jobid: ' . $job_id, 2);
my $status_str = 'UNKNOWN';
my $url = $baseUrl . '/status/' . $job_id;
$status_str = &rest_request($url);
print_debug_message('rest_get_status', 'status_str: ' . $status_str, 2);
print_debug_message('rest_get_status', 'End', 1);
return $status_str;
}
=head2 rest_get_result_types()
Get list of result types for finished job.
my (@result_types) = &rest_get_result_types($job_id);
=cut
sub rest_get_result_types {
print_debug_message('rest_get_result_types', 'Begin', 1);
my $job_id = shift;
print_debug_message('rest_get_result_types', 'jobid: ' . $job_id, 2);
my (@resultTypes);
my $url = $baseUrl . '/resulttypes/' . $job_id;
my $result_type_list_xml_str = &rest_request($url);
my $result_type_list_xml = XMLin($result_type_list_xml_str);
(@resultTypes) = @{$result_type_list_xml->{'type'}};
print_debug_message('rest_get_result_types',
scalar(@resultTypes) . ' result types', 2);
print_debug_message('rest_get_result_types', 'End', 1);
return(@resultTypes);
}
=head2 rest_get_result()
Get result data of a specified type for a finished job.
my $result = rest_get_result($job_id, $result_type);
=cut
sub rest_get_result {
print_debug_message('rest_get_result', 'Begin', 1);
my $job_id = shift;
my $type = shift;
print_debug_message('rest_get_result', 'jobid: ' . $job_id, 1);
print_debug_message('rest_get_result', 'type: ' . $type, 1);
my $url = $baseUrl . '/result/' . $job_id . '/' . $type;
my $result = &rest_request($url);
print_debug_message('rest_get_result', length($result) . ' characters',
1);
print_debug_message('rest_get_result', 'End', 1);
return $result;
}
### Service actions and utility functions ###
=head2 print_debug_message()
Print debug message at specified debug level.
&print_debug_message($method_name, $message, $level);
=cut
sub print_debug_message {
my $function_name = shift;
my $message = shift;
my $level = shift;
if ($level <= $params{'debugLevel'}) {
print STDERR '[', $function_name, '()] ', $message, "\n";
}
}
=head2 print_tool_params()
Print list of tool parameters.
&print_tool_params();
=cut
sub print_tool_params {
print_debug_message('print_tool_params', 'Begin', 1);
my (@param_list) = &rest_get_parameters();
foreach my $param (sort (@param_list)) {
print $param, "\n";
}
print_debug_message('print_tool_params', 'End', 1);
}
=head2 print_param_details()
Print details of a tool parameter.
&print_param_details($param_name);
=cut
sub print_param_details {
print_debug_message('print_param_details', 'Begin', 1);
my $paramName = shift;
print_debug_message('print_param_details', 'paramName: ' . $paramName, 2);
my $paramDetail = &rest_get_parameter_details($paramName);
print $paramDetail->{'name'}, "\t", $paramDetail->{'type'}, "\n";
print $paramDetail->{'description'}, "\n";
if (defined($paramDetail->{'values'}->{'value'})) {
if (ref($paramDetail->{'values'}->{'value'}) eq 'ARRAY') {
foreach my $value (@{$paramDetail->{'values'}->{'value'}}) {
&print_param_value($value);
}
}
else {
&print_param_value($paramDetail->{'values'}->{'value'});
}
}
print_debug_message('print_param_details', 'End', 1);
}
=head2 print_param_value()
Print details of a tool parameter value.
&print_param_details($param_value);
Used by print_param_details() to handle both singluar and array values.
=cut
sub print_param_value {
my $value = shift;
print $value->{'value'};
if ($value->{'defaultValue'} eq 'true') {
print "\t", 'default';
}
print "\n";
print "\t", $value->{'label'}, "\n";
if (defined($value->{'properties'})) {
foreach
my $key (sort ( keys(%{$value->{'properties'}{'property'}}) )) {
if (ref($value->{'properties'}{'property'}{$key}) eq 'HASH'
&& defined($value->{'properties'}{'property'}{$key}{'value'})
) {
print "\t", $key, "\t",
$value->{'properties'}{'property'}{$key}{'value'}, "\n";
}
else {
print "\t", $value->{'properties'}{'property'}{'key'},
"\t", $value->{'properties'}{'property'}{'value'}, "\n";
last;
}
}
}
}
=head2 print_job_status()
Print status of a job.
&print_job_status($job_id);
=cut
sub print_job_status {
print_debug_message('print_job_status', 'Begin', 1);
my $jobid = shift;
print_debug_message('print_job_status', 'jobid: ' . $jobid, 1);
if ($outputLevel > 0) {
print STDERR 'Getting status for job ', $jobid, "\n";
}
my $result = &rest_get_status($jobid);
print "$result\n";
if ($result eq 'FINISHED' && $outputLevel > 0) {
print STDERR "To get results: perl $scriptName --polljob --jobid " . $jobid
. "\n";
}
print_debug_message('print_job_status', 'End', 1);
}
=head2 print_result_types()
Print available result types for a job.
&print_result_types($job_id);
=cut
sub print_result_types {
print_debug_message('result_types', 'Begin', 1);
my $jobid = shift;
print_debug_message('result_types', 'jobid: ' . $jobid, 1);
if ($outputLevel > 0) {
print STDERR 'Getting result types for job ', $jobid, "\n";
}
my $status = &rest_get_status($jobid);
if ($status eq 'QUEUED' || $status eq 'RUNNING') {
print STDERR 'Error: Job status is ', $status,
'. To get result types the job must be finished.', "\n";
}
else {
my (@resultTypes) = &rest_get_result_types($jobid);
if ($outputLevel > 0) {
print STDOUT 'Available result types:', "\n";
}
foreach my $resultType (@resultTypes) {
print STDOUT $resultType->{'identifier'}, "\n";
if (defined($resultType->{'label'})) {
print STDOUT "\t", $resultType->{'label'}, "\n";
}
if (defined($resultType->{'description'})) {
print STDOUT "\t", $resultType->{'description'}, "\n";
}
if (defined($resultType->{'mediaType'})) {
print STDOUT "\t", $resultType->{'mediaType'}, "\n";
}
if (defined($resultType->{'fileSuffix'})) {
print STDOUT "\t", $resultType->{'fileSuffix'}, "\n";
}
}
if ($status eq 'FINISHED' && $outputLevel > 0) {
print STDERR "\n", 'To get results:', "\n",
" perl $scriptName --polljob --jobid " . $params{'jobid'} . "\n",
" perl $scriptName --polljob --outformat <type> --jobid "
. $params{'jobid'} . "\n";
}
}
print_debug_message('result_types', 'End', 1);
}
=head2 submit_job()
Submit a job to the service.
&submit_job($seq);
=cut
sub submit_job {
print_debug_message('submit_job', 'Begin', 1);
# Set input sequence
$params{'asequence'} = shift;
$params{'bsequence'} = shift;
# Load parameters
&load_params();
# Submit the job
my $jobid = &rest_run($params{'email'}, $params{'title'}, \%params);
# Asynchronous submission.
if (defined($params{'asyncjob'})) {
print STDOUT $jobid, "\n";
if ($outputLevel > 0) {
print STDERR
"To check status: perl $scriptName --status --jobid $jobid\n";
}
}
# Simulate synchronous submission serial mode.
else {
if ($outputLevel > 0) {
print STDERR "JobId: $jobid\n";
} else {
print STDERR "$jobid\n";
}
usleep($checkInterval);
# Get results.
&get_results($jobid);
}
print_debug_message('submit_job', 'End', 1);
return $jobid;
}
=head2 load_data()
Load sequence data from file or option specified on the command-line.
&load_data();
=cut
sub load_data {
print_debug_message('load_data', 'Begin', 1);
my @retSeq = ();
# First sequence
if (defined($ARGV[0])) { # Bare option
if (-f $ARGV[0] || $ARGV[0] eq '-') { # File
$retSeq[0] = &read_file($ARGV[0]);
}
else { # DB:ID or raw sequence
$retSeq[0] = $ARGV[0];
}
}
if ($params{'asequence'}) { # Via --sequence
if (-f $params{'asequence'} || $params{'asequence'} eq '-') { # File
$retSeq[0] = &read_file($params{'asequence'});
}
else { # DB:ID or sequence
$retSeq[0] = $params{'asequence'};
}
}
# Second sequence
if (defined($ARGV[1])) { # Bare option
if (-f $ARGV[1] || $ARGV[1] eq '-') { # File
$retSeq[1] = &read_file($ARGV[1]);
}
else { # DB:ID or raw sequence
$retSeq[1] = $ARGV[1];
}
}
if ($params{'bsequence'}) { # Via --sequence
if (-f $params{'bsequence'} || $params{'bsequence'} eq '-') { # File
$retSeq[1] = &read_file($params{'bsequence'});
}
else { # DB:ID or sequence
$retSeq[1] = $params{'bsequence'};
}
}
print_debug_message('load_data', 'End', 1);
return @retSeq;
}
=head2 load_params()
Load job parameters from command-line options.
&load_params();
=cut
sub load_params {
print_debug_message('load_params', 'Begin', 1);
# Pass default values and fix bools (without default value)
if (!$params{'overlaps'}) {
$params{'overlaps'} = 'false'
}
if (!$params{'boxit'}) {
$params{'boxit'} = 'true'
}
print_debug_message('load_params', 'End', 1);
}
=head2 client_poll()
Client-side job polling.
&client_poll($job_id);
=cut
sub client_poll {
print_debug_message('client_poll', 'Begin', 1);
my $jobid = shift;
my $status = 'QUEUED';
# Check status and wait if not finished. Terminate if three attempts get "ERROR".
my $errorCount = 0;
while ($status eq 'RUNNING'
|| $status eq 'QUEUED'
|| ($status eq 'ERROR' && $errorCount < 2)) {
$status = rest_get_status($jobid);
print STDERR "$status\n" if ($outputLevel > 0);
if ($status eq 'ERROR') {
$errorCount++;
}
elsif ($errorCount > 0) {
$errorCount--;
}
if ($status eq 'RUNNING'
|| $status eq 'QUEUED'
|| $status eq 'ERROR') {
# Wait before polling again.
usleep($checkInterval);
}
}
print_debug_message('client_poll', 'End', 1);
return $status;
}
=head2 get_results()
Get the results for a job identifier.
&get_results($job_id);
=cut
sub get_results {
print_debug_message('get_results', 'Begin', 1);
my $jobid = shift;
print_debug_message('get_results', 'jobid: ' . $jobid, 1);
my $output_basename = $jobid;
# Verbose
if ($outputLevel > 1) {
print 'Getting results for job ', $jobid, "\n";
}
# Check status, and wait if not finished
client_poll($jobid);
# Default output file names use JobId, however the name can be specified...
if (defined($params{'outfile'})) {
$output_basename = $params{'outfile'};
}
# Use JobId if output file name is not defined
else {
unless (defined($params{'outfile'})) {
$params{'outfile'} = $jobid;
$output_basename = $jobid;
}
}
# Get list of data types
my (@resultTypes) = rest_get_result_types($jobid);
# Get the data and write it to a file
if (defined($params{'outformat'})) {
# Specified data type
# check to see if there are multiple formats (comma separated)
my $sep = ",";
my (@multResultTypes);
if ($params{'outformat'} =~ /$sep/) {
@multResultTypes = split(',', $params{'outformat'});
}
else {
$multResultTypes[0] = $params{'outformat'};
}
# check if the provided formats are recognised
foreach my $inputType (@multResultTypes) {
my $expectation = 0;
foreach my $resultType (@resultTypes) {
if ($resultType->{'identifier'} eq $inputType && $expectation eq 0) {
$expectation = 1;
}
}
if ($expectation ne 1) {
die 'Error: unknown result format "' . $inputType . '"';
}
}
# if so get the files
my $selResultType;
foreach my $resultType (@resultTypes) {
if (grep {$_ eq $resultType->{'identifier'}} @multResultTypes) {
$selResultType = $resultType;
my $result = rest_get_result($jobid, $selResultType->{'identifier'});
if (defined($params{'outfile'}) && $params{'outfile'} eq '-') {
write_file($params{'outfile'}, $result);
}
else {
write_file(
$output_basename . '.'
. $selResultType->{'identifier'} . '.'
. $selResultType->{'fileSuffix'},
$result
);
}
}
}
}
else { # Data types available
# Write a file for each output type
for my $resultType (@resultTypes) {
if ($outputLevel > 1) {
print STDERR 'Getting ', $resultType->{'identifier'}, "\n";
}
my $result = rest_get_result($jobid, $resultType->{'identifier'});
if (defined($params{'outfile'}) && $params{'outfile'} eq '-') {
write_file($params{'outfile'}, $result);
}
else {
write_file(
$output_basename . '.'
. $resultType->{'identifier'} . '.'
. $resultType->{'fileSuffix'},
$result
);
}
}
}
print_debug_message('get_results', 'End', 1);
}
=head2 read_file()
Read a file into a scalar. The special filename '-' can be used to read from
standard input (STDIN).
my $data = &read_file($filename);
=cut
sub read_file {
print_debug_message('read_file', 'Begin', 1);
my $filename = shift;
print_debug_message('read_file', 'filename: ' . $filename, 2);
my ($content, $buffer);
if ($filename eq '-') {
while (sysread(STDIN, $buffer, 1024)) {
$content .= $buffer;
}
}
else {
# File
open(my $FILE, '<', $filename)
or die "Error: unable to open input file $filename ($!)";
while (sysread($FILE, $buffer, 1024)) {
$content .= $buffer;
}
close($FILE);
}
print_debug_message('read_file', 'End', 1);
return $content;
}
=head2 write_file()
Write data to a file. The special filename '-' can be used to write to
standard output (STDOUT).
&write_file($filename, $data);
=cut
sub write_file {
print_debug_message('write_file', 'Begin', 1);
my ($filename, $data) = @_;
print_debug_message('write_file', 'filename: ' . $filename, 2);
if ($outputLevel > 0) {
print STDERR 'Creating result file: ' . $filename . "\n";
}
if ($filename eq '-') {
print STDOUT $data;
}
else {
open(my $FILE, '>', $filename)
or die "Error: unable to open output file $filename ($!)";
syswrite($FILE, $data);
close($FILE);
}
print_debug_message('write_file', 'End', 1);
}
=head2 usage()
Print program usage message.
&usage();
=cut
sub usage {
print STDERR <<EOF
EMBL-EBI EMBOSS dotpath Perl Client:
Sequence statistics and plots with dotpath.
[Required (for job submission)]