| 1 | #!/usr/bin/env perl | ||
| 2 | use strictures 1; | ||
| 3 | use utf8; | ||
| 4 | use 5.018; | ||
| 5 | |||
| 6 | =head1 NAME | ||
| 7 | |||
| 8 | Bio::WebService::LANL::SequenceLocator::Server - A JSON web API for LANL's HIV sequence locator | ||
| 9 | |||
| 10 | =head1 SYNOPSIS | ||
| 11 | |||
| 12 | After installation: | ||
| 13 | |||
| 14 | plackup `perldoc -l Bio::WebService::LANL::SequenceLocator::Server` | ||
| 15 | |||
| 16 | Or from a git checkout or tarball: | ||
| 17 | |||
| 18 | plackup # uses app.psgi | ||
| 19 | |||
| 20 | Or as a L<Server::Starter> managed service: | ||
| 21 | |||
| 22 | examples/service start | ||
| 23 | |||
| 24 | =head1 DESCRIPTION | ||
| 25 | |||
| 26 | This server powers | ||
| 27 | L<a simple, JSON-based web API|http://indra.mullins.microbiol.washington.edu/locate-sequence/> | ||
| 28 | for the L<LANL's HIV sequence locator|http://www.hiv.lanl.gov/content/sequence/LOCATE/locate.html> | ||
| 29 | using L<Bio::WebService::LANL::SequenceLocator>. | ||
| 30 | |||
| 31 | =head1 ENVIRONMENT | ||
| 32 | |||
| 33 | =head2 SERVER_ADMIN | ||
| 34 | |||
| 35 | Set the SERVER_ADMIN environment variable before starting the server to provide | ||
| 36 | a contact address in requests to LANL and server error messages in API | ||
| 37 | responses. | ||
| 38 | |||
| 39 | =head1 INSTALLATION | ||
| 40 | |||
| 41 | The prerequisites for this server are optional and are probably not installed | ||
| 42 | by default on your computer when you install this distribution. From a git | ||
| 43 | checkout or tarball, you can install the necessary modules with | ||
| 44 | L<cpanm|App::cpanminus>: | ||
| 45 | |||
| 46 | cpanm --with-all-features --installdeps . | ||
| 47 | |||
| 48 | =cut | ||
| 49 | |||
| 50 | package Bio::WebService::LANL::SequenceLocator::Server; | ||
| 51 | use Web::Simple; | ||
| 52 | |||
| 53 | use Bio::WebService::LANL::SequenceLocator; | ||
| 54 | use File::Share qw< dist_file >; | ||
| 55 | use JSON qw< encode_json >; | ||
| 56 | use Text::CSV; | ||
| 57 | use Plack::App::File; | ||
| 58 | use Path::Tiny; | ||
| 59 | use IO::String; | ||
| 60 | |||
| 61 | has contact => ( | ||
| 62 | is => 'ro', | ||
| 63 | default => sub { $ENV{SERVER_ADMIN} || '[no address provided]' }, | ||
| 64 | ); | ||
| 65 | |||
| 66 | has locator => ( | ||
| 67 | is => 'ro', | ||
| 68 | isa => sub { | ||
| 69 | die "Attribute 'locator' is not a Bio::WebService::LANL::SequenceLocator" | ||
| 70 | unless $_[0]->isa("Bio::WebService::LANL::SequenceLocator"); | ||
| 71 | }, | ||
| 72 | lazy => 1, | ||
| 73 | builder => sub { | ||
| 74 | Bio::WebService::LANL::SequenceLocator->new( | ||
| 75 | agent_string => join " ", "via", __PACKAGE__, $_[0]->contact | ||
| 76 | ) | ||
| 77 | }, | ||
| 78 | ); | ||
| 79 | |||
| 80 | has about_page => ( | ||
| 81 | is => 'ro', | ||
| 82 | lazy => 1, | ||
| 83 | builder => sub { dist_file('Bio-WebService-LANL-SequenceLocator', 'about.html') }, | ||
| 84 | ); | ||
| 85 | |||
| 86 | has formats => ( | ||
| 87 | is => 'ro', | ||
| 88 | default => sub { [qw( json csv )] }, | ||
| 89 | ); | ||
| 90 | |||
| 91 | sub dispatch_request { | ||
| 92 | sub (POST + /within/hiv) { | ||
| 93 | sub (%base~&format~) { | ||
| 94 | my ($self, $base, $format) = @_; | ||
| 95 | $format ||= 'json'; | ||
| 96 | $format = lc $format; | ||
| 97 | return error(406 => "format '$format' is not supported; try one of " . join(", ", @{$self->formats})) | ||
| 98 | unless grep { $format eq $_ } @{$self->formats}; | ||
| 99 | |||
| 100 | sub (%fasta=) { | ||
| 101 | my ($self, $fasta) = @_; | ||
| 102 | return $self->locate_sequences_from_fasta($fasta, $base, $format); | ||
| 103 | }, | ||
| 104 | sub (*fasta=) { | ||
| 105 | my ($self, $fasta) = @_; | ||
| 106 | return error(422 => $fasta->reason) | ||
| 107 | unless $fasta->is_upload; | ||
| 108 | return $self->locate_sequences_from_fasta(path($fasta->path)->slurp, $base, $format); | ||
| 109 | }, | ||
| 110 | sub (%@sequence~) { | ||
| 111 | my ($self, $sequences) = @_; | ||
| 112 | return $self->locate_sequences($sequences, $base, $format); | ||
| 113 | }, | ||
| 114 | }, | ||
| 115 | }, | ||
| 116 | sub (GET + /within/hiv) { | ||
| 117 | error( 405 => "You must make location requests using POST." ) | ||
| 118 | }, | ||
| 119 | sub (GET + /) { | ||
| 120 | state $about = Plack::App::File->new(file => $_[0]->about_page); | ||
| 121 | $about; | ||
| 122 | }, | ||
| 123 | } | ||
| 124 | |||
| 125 | sub locate_sequences_from_fasta { | ||
| 126 | my $self = shift; | ||
| 127 | my $fasta = shift; | ||
| 128 | my $sequences = $self->read_fasta(\$fasta) | ||
| 129 | or return error( 415 => "Couldn't parse FASTA; invalid formating?" ); | ||
| 130 | return $self->locate_sequences($sequences, @_); | ||
| 131 | } | ||
| 132 | |||
| 133 | sub locate_sequences { | ||
| 134 | my ($self, $sequences, $base, $format) = @_; | ||
| 135 | |||
| 136 | return error(422 => 'At least one value for "sequence" is needed.') | ||
| 137 | unless $sequences and @$sequences; | ||
| 138 | |||
| 139 | my $results = $self->locator->find($sequences, base => $base) | ||
| 140 | or return error(503 => "Backend request to LANL failed, sorry! Contact @{[ $self->contact ]} if the problem persists."); | ||
| 141 | |||
| 142 | return $self->format_results($results, $format); | ||
| 143 | } | ||
| 144 | |||
| 145 | sub format_results { | ||
| 146 | my ($self, $results, $format) = @_; | ||
| 147 | |||
| 148 | my $formatter = $self->can("as_$format") | ||
| 149 | or return error(500 => "Unknown format '$format'"); | ||
| 150 | |||
| 151 | return $formatter->($self, $results); | ||
| 152 | } | ||
| 153 | |||
| 154 | sub as_json { | ||
| 155 | my ($self, $results) = @_; | ||
| 156 | my $json = eval { encode_json($results) }; | ||
| 157 | if ($@ or not $json) { | ||
| 158 | warn $@ ? "Error encoding JSON response: $@\n" | ||
| 159 | : "Failed to encode JSON response, but no error?!\n"; | ||
| 160 | return error(500 => "Error encoding results to JSON. Contact @{[ $self->contact ]}"); | ||
| 161 | } | ||
| 162 | |||
| 163 | return [ | ||
| 164 | 200, | ||
| 165 | [ 'Content-type' => 'application/json' ], | ||
| 166 | [ $json, "\n" ], | ||
| 167 | ]; | ||
| 168 | } | ||
| 169 | |||
| 170 | sub as_csv { | ||
| 171 | my ($self, $results) = @_; | ||
| 172 | my $csv = IO::String->new; | ||
| 173 | my $write = sub { | ||
| 174 | state $csv_writer = Text::CSV->new({ binary => 1 }); | ||
| 175 | $csv_writer->print($csv, @_); | ||
| 176 | $csv->print("\n"); | ||
| 177 | }; | ||
| 178 | |||
| 179 | my @fields = qw( query_sequence base_type reverse_complement genome_start genome_end | ||
| 180 | polyprotein start end region_names similarity_to_hxb2 alignment hxb2_sequence ); | ||
| 181 | $write->(\@fields); | ||
| 182 | |||
| 183 | for my $query (@$results) { | ||
| 184 | # Trim leading/trailing whitespace | ||
| 185 | $query->{alignment} =~ s/^\n//gm; | ||
| 186 | $query->{alignment} =~ s/^\s*$//gm; | ||
| 187 | chomp $query->{alignment}; | ||
| 188 | |||
| 189 | $query->{region_names} = join " ", @{$query->{region_names}}; | ||
| 190 | |||
| 191 | $write->([ @$query{@fields} ]); | ||
| 192 | } | ||
| 193 | $csv->seek(0, 0); | ||
| 194 | |||
| 195 | return [ | ||
| 196 | 200, | ||
| 197 | [ 'Content-type' => 'text/csv', | ||
| 198 | 'Content-disposition' => 'inline; filename="located.csv"' ], | ||
| 199 | $csv, | ||
| 200 | ]; | ||
| 201 | } | ||
| 202 | |||
| 203 | sub read_fasta { | ||
| 204 | my ($self, $fasta) = @_; | ||
| 205 | |||
| 206 | # XXX TODO: preserve sequence names and use them in output? | ||
| 207 | my (@sequences) = map { chomp; $_ } | ||
| 208 | split /^>.*\R/m, $$fasta; | ||
| 209 | |||
| 210 | # Remove any leading garbage before the first description line (usually | ||
| 211 | # just the empty string) | ||
| 212 | shift @sequences; | ||
| 213 | return \@sequences; | ||
| 214 | } | ||
| 215 | |||
| 216 | sub error { | ||
| 217 | return [ | ||
| 218 | shift, | ||
| 219 | [ 'Content-type' => 'text/plain' ], | ||
| 220 | [ join(" ", @_), "\n" ] | ||
| 221 | ]; | ||
| 222 | } | ||
| 223 | |||
| 224 | __PACKAGE__->run_if_script; | ||
| 225 | |||
| 226 | =head1 AUTHOR | ||
| 227 | |||
| 228 | Thomas Sibley E<lt>trsibley@uw.eduE<gt> | ||
| 229 | |||
| 230 | =head1 COPYRIGHT | ||
| 231 | |||
| 232 | Copyright 2014 by the Mullins Lab, Department of Microbiology, University of | ||
| 233 | Washington. | ||
| 234 | |||
| 235 | =head1 LICENSE | ||
| 236 | |||
| 237 | Licensed under the same terms as Perl 5 itself. | ||
| 238 | |||
| 239 | =cut |