-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathannotation.psgi
90 lines (78 loc) · 1.86 KB
/
annotation.psgi
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
#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
use Plack::Request;
use JSON;
use FindBin qw($Bin);
use lib "$Bin";
use Text::TogoAnnotator;
use utf8;
use Data::Dumper;
my $sysroot = "$Bin";
our ($opt_t, $opt_m) = (0.6, 5);
Text::TogoAnnotator->init($opt_t, 30, $opt_m, 3, $sysroot, "dict_cyanobacteria_20151120_with_cyanobase.txt.gz","dict_cyanobacteria_curated.txt");
sub match{
my @queries = @_;
my @out = ();
foreach my $q (@queries){
my $r = Text::TogoAnnotator->retrieve($q);
my $json = JSON->new->utf8(0)->encode($r);
push @out, $json;
}
return "[".join(",",@out)."]";
}
sub file2queries {
my $path = shift;
my @queries = ();
open(my $LIST, $path);
while(<$LIST>){
chomp;
next if /^#/;
push @queries, $_;
}
close($LIST);
return @queries;
}
sub ddbjfile2queries {
my $path = shift;
my @queries = ();
open(my $LIST, $path);
while(<$LIST>){
chomp;
my @a = split "\t",$_;
if($a[3] eq 'product'){
push @queries, $a[4];
}
}
return @queries;
}
my $app = sub {
my $env = shift;
my $req = Plack::Request->new($env);
my @queries = ();
if ($req->param('query')){
@queries = $req->param('query');
}elsif( $req->upload('list')){
my $upload = $req->upload('list');
@queries = file2queries($upload->path);
}elsif( my $upload = $req->upload('ddbj')){
@queries = ddbjfile2queries($upload->path);
}
# TogoAnnotatorの処理
Text::TogoAnnotator->openDicts;
my $body = match(@queries);
Text::TogoAnnotator->closeDicts;
# my $res = $req->new_response(200);
# $res->content_type('application/json');
# my $length = length($body);
# $res->content_length($length);
# $res->body([$body]);
# return $res->finilize;
return [
'200',
[ 'Content-Type' => 'application/json' ],
[ $body ], # or IO::Handle-like object
];
};
$app;