line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catmandu::Importer::EuropePMC; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
81098
|
use Catmandu::Sane; |
|
3
|
|
|
|
|
292607
|
|
|
3
|
|
|
|
|
19
|
|
4
|
3
|
|
|
3
|
|
3100
|
use Catmandu::Importer::XML; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
use Try::Tiny; |
6
|
|
|
|
|
|
|
use Furl; |
7
|
|
|
|
|
|
|
use Moo; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
with 'Catmandu::Importer'; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
use constant BASE_URL => 'http://www.ebi.ac.uk/europepmc/webservices/rest'; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
has base => ( is => 'ro', default => sub { return BASE_URL; } ); |
14
|
|
|
|
|
|
|
has source => ( is => 'ro', default => sub { return "MED"; } ); |
15
|
|
|
|
|
|
|
has module => ( is => 'ro', default => sub { return "search"; } ); |
16
|
|
|
|
|
|
|
has query => ( is => 'ro' ); |
17
|
|
|
|
|
|
|
has pmid => ( is => 'ro' ); |
18
|
|
|
|
|
|
|
has db => ( is => 'ro' ); |
19
|
|
|
|
|
|
|
has page => ( is => 'ro' ); |
20
|
|
|
|
|
|
|
has raw => (is => 'ro'); |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub BUILD { |
23
|
|
|
|
|
|
|
my $self = shift; |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
Catmandu::BadVal->throw("Either 'pmid' or 'query' is required.") |
26
|
|
|
|
|
|
|
unless $self->pmid || $self->query; |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
my %PATH_MAPPING = ( |
31
|
|
|
|
|
|
|
search => 'result', |
32
|
|
|
|
|
|
|
citations => 'citation', |
33
|
|
|
|
|
|
|
references => 'reference', |
34
|
|
|
|
|
|
|
databaseLinks => 'dbCrossReference' |
35
|
|
|
|
|
|
|
); |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub _request { |
38
|
|
|
|
|
|
|
my ( $self, $url ) = @_; |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
my $ua = Furl->new( timeout => 20 ); |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
my $res; |
43
|
|
|
|
|
|
|
try { |
44
|
|
|
|
|
|
|
$res = $ua->get($url); |
45
|
|
|
|
|
|
|
die $res->status_line unless $res->is_success; |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
return $res->content; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
catch { |
50
|
|
|
|
|
|
|
Catmandu::Error->throw("Status code: $res->status_line"); |
51
|
|
|
|
|
|
|
}; |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
sub _parse { |
56
|
|
|
|
|
|
|
my ( $self, $in ) = @_; |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
my $path; |
59
|
|
|
|
|
|
|
($self->raw) ? ($path = '') |
60
|
|
|
|
|
|
|
: ($path = $PATH_MAPPING{ $self->module }); |
61
|
|
|
|
|
|
|
my $xml = Catmandu::Importer::XML->new( file => \$in, path => $path ); |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
return $xml->to_array; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
sub _call { |
67
|
|
|
|
|
|
|
my ($self) = @_; |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
my $url = $self->base; |
70
|
|
|
|
|
|
|
if ( $self->module eq 'search' ) { |
71
|
|
|
|
|
|
|
$url .= '/search/query=' . $self->query; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
else { |
74
|
|
|
|
|
|
|
$url .= '/' . $self->source . '/' . $self->pmid . '/' . $self->module; |
75
|
|
|
|
|
|
|
$url .= '/' . $self->db if $self->db; |
76
|
|
|
|
|
|
|
$url .= '/' . $self->page if $self->page; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
my $res = $self->_request($url); |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
return $res; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
sub _get_record { |
85
|
|
|
|
|
|
|
my ($self) = @_; |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
return $self->_parse( $self->_call ); |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub generator { |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
my ($self) = @_; |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
return sub { |
95
|
|
|
|
|
|
|
state $stack = $self->_get_record; |
96
|
|
|
|
|
|
|
my $rec = pop @$stack; |
97
|
|
|
|
|
|
|
if ($self->raw) { |
98
|
|
|
|
|
|
|
return $rec; |
99
|
|
|
|
|
|
|
} else { |
100
|
|
|
|
|
|
|
$rec->{ $PATH_MAPPING{ $self->module } } |
101
|
|
|
|
|
|
|
? ( return $rec->{ $PATH_MAPPING{ $self->module } } ) |
102
|
|
|
|
|
|
|
: return undef; |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
}; |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
1; |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=head1 NAME |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
Catmandu::Importer::EuropePMC - Package that imports EuropePMC data. |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=head1 API Documentation |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
This module uses the REST service as described at http://www.ebi.ac.uk/europepmc/. |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=head1 SYNOPSIS |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
use Catmandu::Importer::EuropePMC; |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
my %attrs = ( |
123
|
|
|
|
|
|
|
source => 'MED', |
124
|
|
|
|
|
|
|
query => 'malaria', |
125
|
|
|
|
|
|
|
module => 'search', |
126
|
|
|
|
|
|
|
db => 'EMBL', |
127
|
|
|
|
|
|
|
page => '2', |
128
|
|
|
|
|
|
|
); |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
my $importer = Catmandu::Importer::EuropePMC->new(%attrs); |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
my $n = $importer->each(sub { |
133
|
|
|
|
|
|
|
my $hashref = $_[0]; |
134
|
|
|
|
|
|
|
# ... |
135
|
|
|
|
|
|
|
}); |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=head1 OPTIONS |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
=over |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=item * source: default is 'MED' |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
=item * query: either pmid or query is required. |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=item * pmid: either pmid or query is required. |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
=item * module: default is 'search', other possible values are 'databaseLinks', 'citations', 'references' |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=item * db: the name of the database. Use when module is 'databaseLinks'. |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=item * page: the paging parameter |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=item * raw: optional. If true delivers the raw xml object. |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=back |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=head1 SEE ALSO |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
L, L, |
160
|
|
|
|
|
|
|
L |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=cut |