blib/lib/Bio/Phylo/CIPRES.pm

filter: Statementstmt 86.6% 13 / 15 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 90.0% 18 / 20 CC 1 SCAR 0.6
CC
1
Cov
87%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package Bio::Phylo::CIPRES;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use XML::Twig;
Subroutine BEGIN: called CC=1 SCAR=2.6
7 use Data::Dumper;
8 use LWP::UserAgent;
9 use YAML qw(LoadFile);
10 use Bio::Phylo::Util::Logger ':simple';
11 use Bio::Phylo::Util::Exceptions 'throw';
12  
13 =head1 NAME
14  
15 Bio::Phylo::CIPRES - Reusable components for CIPRES REST API access
16  
17 =cut
18  
19 # global constants
20 our $AUTOLOAD;
21 use version; our $VERSION = qv("v0.1.2");
22 my $REALM = "Cipres Authentication";
23 my $PORT = 443;
24  
25 sub new {
26 my $class = shift;
27 my @fields = qw[infile tool param outfile url user pass cipres_appkey];
28 my $self = bless { map { $_ => undef } @fields }, $class;
29 my %args = @_;
30 while( my ( $property, $value ) = each %args ) {
31 $self->$property($value);
32 }
33 return $self;
34 }
35  
36 sub run {
37 my $self = shift;
38 my $url = $self->launch_job;
39 while(1) {
40 sleep(60);
41 my $status = $self->check_status($url);
42 if ( $status->{'completed'} eq 'true' ) {
43 return $self->get_results( $status->{'outfiles'} );
44 }
45 }
46 }
47  
48 sub launch_job {
49 my $self = shift;
50 my $ua = $self->ua;
51 my $url = $self->url;
52 my $load = $self->payload;
53 my @head = $self->headers(1);
54 my $res = $ua->post( $url . '/job/' . $self->user, $load, @head );
55 if ( $res->is_success ) {
56
57 # run submission, parse result
58 my $status_url;
59 my $result = $res->decoded_content;
60 DEBUG $result;
61 XML::Twig->new(
62 'twig_handlers' => {
63 'jobstatus/selfUri/url' => sub { $status_url = $_->text }
64 }
65 )->parse($result);
66 INFO "Job launched at $status_url";
67 return $status_url;
68 }
69 else {
70 throw 'NetworkError' => $res->status_line;
71 }
72 }
73  
74 sub check_status {
75 my ( $self, $url ) = @_;
76 INFO "Going to check status for $url";
77 my $ua = $self->ua;
78 my @head = $self->headers(0);
79 my $res = $ua->get( $url, @head );
80 if ( $res->is_success ) {
81
82 # post request, fetch result
83 my ( $status, $outfiles );
84 my $result = $res->decoded_content;
85 DEBUG $result;
86 XML::Twig->new(
87 'twig_handlers' => {
88 'jobstatus/resultsUri/url' => sub { $outfiles = $_->text },
89 'jobstatus/terminalStage' => sub { $status = $_->text }
90 }
91 )->parse($result);
92 my $time = localtime();
93 INFO "[$time] completed: $status";
94 return { 'completed' => $status, 'outfiles' => $outfiles };
95 }
96 else {
97 throw 'NetworkError' => $res->status_line;
98 }
99 }
100  
101 sub get_results {
102 my ( $self, $url ) = @_;
103 my %out = map { $_ => undef } @{ $self->outfile };
104 my $ua = $self->ua;
105 my @head = $self->headers(0);
106 my $res = $ua->get( $url, @head );
107 if ( $res->is_success ) {
108 my $result = $res->decoded_content;
109 DEBUG $result;
110 XML::Twig->new(
111 'twig_handlers' => {
112 'results/jobfiles/jobfile' => sub {
113 my $node = $_;
114 my $name = $node->findvalue('filename');
115 if ( exists $out{ $name } ) {
116 $out{ $name } = $node->findvalue('downloadUri/url');
117 }
118 DEBUG $node->toString;
119 }
120 }
121 )->parse($result);
122 for my $name ( keys %out ) {
123 my $location = $out{$name};
124 $res = $ua->get( $location, @head );
125 if ( $res->is_success ) {
126 $out{$name} = $res->decoded_content;
127 }
128 else {
129 throw 'NetworkError' => $res->status_line;
130 }
131 return %out;
132 }
133 }
134 else {
135 throw 'NetworkError' => $res->status_line;
136 }
137 }
138  
139 sub yml {
140 my ( $self, $yml ) = @_;
141 INFO "Reading YAML file $yml";
142 my $info = LoadFile($yml);
143 DEBUG "Parsed " . Dumper( $info );
144 $self->user( $info->{'CRA_USER'} );
145 $self->pass( $info->{'PASSWORD'} );
146 $self->url( $info->{'URL'} );
147 $self->cipres_appkey( $info->{'KEY'} );
148 }
149  
150 sub ua {
151 my $self = shift;
152 my $host = URI->new( $self->url )->host();
153 my $user = $self->user;
154 my $pass = $self->pass;
155 my $ua = LWP::UserAgent->new;
156 INFO "Instantiating UserAgent $host:$PORT / $REALM / $user:****";
157 $ua->ssl_opts( 'verify_hostname' => 0 );
158 $ua->credentials(
159 $host . ':' . $PORT,
160 $REALM,
161 $user => $pass
162 );
163 return $ua;
164 }
165  
166 sub payload {
167 my $self = shift;
168 INFO "Composing payload for ".$self->tool." with infile ".$self->infile;
169 my $load = [
170 'tool' => $self->tool,
171 'input.infile_' => [ $self->infile ],
172 'metadata.statusEmail' => 'true',
173 %{ $self->param }
174 ];
175 DEBUG Dumper($load);
176 return $load;
177 }
178  
179 sub headers {
180 my ( $self, $form ) = @_;
181 if ( $form ) {
182 INFO "Composing POST / form-data headers";
183 return (
184 'Content_Type' => 'form-data',
185 'cipres-appkey' => $self->cipres_appkey,
186 );
187 }
188 else {
189 INFO "Composing GET headers";
190 return ( 'cipres-appkey' => $self->cipres_appkey );
191 }
192 }
193  
194 sub AUTOLOAD {
195 my ( $self, $arg ) = @_;
196 my $property = $AUTOLOAD;
197 $property =~ s/.*://;
198 if ( exists $self->{$property} ) {
199 if ( $arg ) {
200 $self->{$property} = $arg;
201 return $self;
202 }
203 else {
204 return $self->{$property};
205 }
206 }
207 else {
208 my $template = 'Can\'t locate object method "%s" via package "%s"';
209 croak sprintf $template, $property, __PACKAGE__;
210 }
211 }
212  
213 sub DESTROY {
214 # maybe kill and delete process on server?
215 }
216  
217 1;
↑ Summary