| 1 |
package WWW::Search::PubMed; |
| 2 |
|
| 3 |
=head1 NAME |
| 4 |
|
| 5 |
WWW::Search::PubMed - Search the NCBI PubMed abstract database. |
| 6 |
|
| 7 |
=head1 SYNOPSIS |
| 8 |
|
| 9 |
use WWW::Search; |
| 10 |
my $s = new WWW::Search ('PubMed'); |
| 11 |
$s->native_query( 'ACGT' ); |
| 12 |
while (my $r = $s->next_result) { |
| 13 |
print $r->title . "\n"; |
| 14 |
print $r->description . "\n"; |
| 15 |
} |
| 16 |
|
| 17 |
=head1 DESCRIPTION |
| 18 |
|
| 19 |
WWW::Search::PubMed provides a WWW::Search backend for searching the |
| 20 |
NCBI/PubMed abstracts database. |
| 21 |
|
| 22 |
=head1 VERSION |
| 23 |
|
| 24 |
This document describes WWW::Search::PubMed version 1.004, |
| 25 |
released 31 October 2007. |
| 26 |
|
| 27 |
=head1 REQUIRES |
| 28 |
|
| 29 |
L<WWW::Search|WWW::Search> |
| 30 |
L<XML::DOM|XML::DOM> |
| 31 |
|
| 32 |
=cut |
| 33 |
|
| 34 |
our($VERSION) = '1.004'; |
| 35 |
|
| 36 |
use strict; |
| 37 |
use warnings; |
| 38 |
|
| 39 |
require WWW::Search; |
| 40 |
require WWW::SearchResult; |
| 41 |
use WWW::Search::PubMed::Result; |
| 42 |
use base qw(WWW::Search); |
| 43 |
|
| 44 |
use XML::DOM; |
| 45 |
our $debug = 0; |
| 46 |
|
| 47 |
use constant ARTICLES_PER_REQUEST => 20; |
| 48 |
use constant QUERY_ARTICLE_LIST_URI => 'http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&retmax=500'; # term=ACTG |
| 49 |
use constant QUERY_ARTICLE_INFO_URI => 'http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=pubmed'; # &id=12167276&retmode=xml |
| 50 |
|
| 51 |
=begin private |
| 52 |
|
| 53 |
=item C<< native_setup_search ( $query, $options ) >> |
| 54 |
|
| 55 |
Sets up the NCBI search using the supplied C<$query> string. |
| 56 |
|
| 57 |
=end private |
| 58 |
|
| 59 |
=cut |
| 60 |
|
| 61 |
sub native_setup_search { |
| 62 |
my $self = shift; |
| 63 |
my $query = shift; |
| 64 |
my $options = shift; |
| 65 |
|
| 66 |
$self->user_agent( "WWW::Search::PubMed/${VERSION} libwww-perl/${LWP::VERSION}; <http://kasei.us/code/pubmed/>" ); |
| 67 |
|
| 68 |
my $ua = $self->user_agent(); |
| 69 |
my $url = QUERY_ARTICLE_LIST_URI . '&term=' . WWW::Search::escape_query($query); |
| 70 |
my $response = $ua->get( $url ); |
| 71 |
my $success = $response->is_success; |
| 72 |
if ($success) { |
| 73 |
my $parser = new XML::DOM::Parser; |
| 74 |
my $content = $response->content; |
| 75 |
$self->{'_xml_parser'} = $parser; |
| 76 |
my $doc = $parser->parse( $content ); |
| 77 |
|
| 78 |
$self->{'_count'} = eval { ($doc->getElementsByTagName('Count')->item(0)->getChildNodes)[0]->getNodeValue() } || 0; |
| 79 |
|
| 80 |
my @articles; |
| 81 |
my $ids = $doc->getElementsByTagName('Id'); |
| 82 |
my $n = $ids->getLength; |
| 83 |
foreach my $i (0 .. $n - 1) { |
| 84 |
my $node = $ids->item( $i ); |
| 85 |
my @children = $node->getChildNodes(); |
| 86 |
push(@articles, + $children[0]->getNodeValue() ); |
| 87 |
} |
| 88 |
$self->{'_article_ids'} = \@articles; |
| 89 |
} else { |
| 90 |
return undef; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
=begin private |
| 95 |
|
| 96 |
=item C<< native_retrieve_some >> |
| 97 |
|
| 98 |
Requests search results from NCBI, adding the results to the WWW::Search object's cache. |
| 99 |
|
| 100 |
=end private |
| 101 |
|
| 102 |
=cut |
| 103 |
|
| 104 |
sub native_retrieve_some { |
| 105 |
my $self = shift; |
| 106 |
|
| 107 |
return undef unless scalar (@{ $self->{'_article_ids'} || [] }); |
| 108 |
my $ua = $self->user_agent(); |
| 109 |
my $url = QUERY_ARTICLE_INFO_URI . '&id=' . join(',', splice(@{ $self->{'_article_ids'} },0,ARTICLES_PER_REQUEST)) . '&retmode=xml'; |
| 110 |
warn 'Fetching URL: ' . $url if ($debug); |
| 111 |
my $response = $ua->get( $url ); |
| 112 |
if ($response->is_success) { |
| 113 |
my $content = $response->content; |
| 114 |
if ($debug) { |
| 115 |
open (my $fh, ">/tmp/pubmed.article.info"); |
| 116 |
print { $fh } $content; |
| 117 |
close($fh); |
| 118 |
warn "Saved response in /tmp/pubmed.article.info\n"; |
| 119 |
} |
| 120 |
my $doc = $self->{'_xml_parser'}->parse( $content ); |
| 121 |
my $articles = $doc->getElementsByTagName('PubmedArticle'); |
| 122 |
my $n = $articles->getLength; |
| 123 |
warn "$n articles found\n" if ($debug); |
| 124 |
my $count = 0; |
| 125 |
foreach my $i (0 .. $n - 1) { |
| 126 |
my $article = $articles->item( $i ); |
| 127 |
my $id = ($article->getElementsByTagName('PMID')->item(0)->getChildNodes)[0]->getNodeValue(); |
| 128 |
warn "$id\n" if ($debug); |
| 129 |
my $title = ($article->getElementsByTagName('ArticleTitle')->item(0)->getChildNodes)[0]->getNodeValue(); |
| 130 |
warn "\t$title\n" if ($debug); |
| 131 |
my $url = 'http://www.ncbi.nlm.nih.gov:80/entrez/query.fcgi?cmd=Retrieve&db=PubMed&list_uids=' . $id . '&dopt=Abstract'; |
| 132 |
my @authors; |
| 133 |
my $authornodes = $article->getElementsByTagName('Author'); |
| 134 |
my $n = $authornodes->getLength; |
| 135 |
foreach my $i (0 .. $n - 1) { |
| 136 |
my ($author, $fname, $lname); |
| 137 |
eval { |
| 138 |
$author = $authornodes->item($i); |
| 139 |
$lname = ($author->getElementsByTagName('LastName')->item(0)->getChildNodes)[0]->getNodeValue(); |
| 140 |
$fname = substr( ($author->getElementsByTagName('ForeName')->item(0)->getChildNodes)[0]->getNodeValue(), 0, 1) . '.'; |
| 141 |
}; |
| 142 |
if ($@) { |
| 143 |
warn $@ if ($debug); |
| 144 |
next unless ($lname); |
| 145 |
} else { |
| 146 |
push(@authors, join(' ', $lname, $fname)); |
| 147 |
} |
| 148 |
} |
| 149 |
my $author = join(', ', @authors); |
| 150 |
warn "\t$author\n" if ($debug); |
| 151 |
|
| 152 |
my $journal = $self->get_text_node( $article, 'MedlineTA' ); |
| 153 |
my $page = $self->get_text_node( $article, 'MedlinePgn' ); |
| 154 |
my $volume = $self->get_text_node( $article, 'Volume' ); |
| 155 |
my $issue = $self->get_text_node( $article, 'Issue' ); |
| 156 |
my $pmid = $self->get_text_node( $article, 'PMID' ); |
| 157 |
my $abstract = $self->get_text_node( $article, 'AbstractText' ); |
| 158 |
|
| 159 |
my @date; |
| 160 |
{ |
| 161 |
my $date = $article->getElementsByTagName('PubDate')->item(0); |
| 162 |
push(@date, $self->get_text_node( $date, 'Year' )); |
| 163 |
push(@date, $self->get_text_node( $date, 'Month' )); |
| 164 |
push(@date, $self->get_text_node( $date, 'Day' )); |
| 165 |
} |
| 166 |
|
| 167 |
my $hit = new WWW::Search::PubMed::Result; |
| 168 |
|
| 169 |
my $source = ''; |
| 170 |
my $date = join(' ', grep defined, @date); |
| 171 |
$hit->date( $date ); |
| 172 |
$hit->year( $date[0] ) if (defined($date[0])); |
| 173 |
$hit->month( $date[1] ) if (defined($date[1])); |
| 174 |
$hit->day( $date[2] ) if (defined($date[2])); |
| 175 |
|
| 176 |
$source = "${journal}. " |
| 177 |
. ($date ? "${date}; " : '') |
| 178 |
. ($volume ? "${volume}" : '') |
| 179 |
. ($issue ? "(${issue})" : '') |
| 180 |
. ($page ? ":$page" : ''); |
| 181 |
$source = "(${source})" if ($source); |
| 182 |
warn "\t$source\n" if ($debug); |
| 183 |
|
| 184 |
$hit->add_url( $url ); |
| 185 |
$hit->title( $title ); |
| 186 |
|
| 187 |
$hit->pmid( $pmid ); |
| 188 |
$hit->abstract( $abstract ) if ($abstract); |
| 189 |
|
| 190 |
my $desc = join(' ', grep {$_} ($author, $source)); |
| 191 |
$hit->description( $desc ); |
| 192 |
push( @{ $self->{'cache'} }, $hit ); |
| 193 |
$count++; |
| 194 |
warn "$count : $title\n" if ($debug); |
| 195 |
} |
| 196 |
return $count; |
| 197 |
} else { |
| 198 |
warn "Uh-oh." . $response->error_as_HTML(); |
| 199 |
return undef; |
| 200 |
} |
| 201 |
|
| 202 |
} |
| 203 |
|
| 204 |
=begin private |
| 205 |
|
| 206 |
=item C<< get_text_node ( $node, $name ) |
| 207 |
|
| 208 |
Returns the text contained in the named descendent of the XML $node. |
| 209 |
|
| 210 |
=end private |
| 211 |
|
| 212 |
=cut |
| 213 |
|
| 214 |
sub get_text_node { |
| 215 |
my $self = shift; |
| 216 |
my $node = shift; |
| 217 |
my $name = shift; |
| 218 |
my $text = eval { ($node->getElementsByTagName($name)->item(0)->getChildNodes)[0]->getNodeValue() }; |
| 219 |
if ($@) { |
| 220 |
warn "XML[$name]: $@" if ($debug); |
| 221 |
return undef; |
| 222 |
} else { |
| 223 |
warn "XML[$name]: $text\n" if ($debug); |
| 224 |
return $text; |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
1; |
| 229 |
|
| 230 |
__END__ |
| 231 |
|
| 232 |
=head1 SEE ALSO |
| 233 |
|
| 234 |
L<WWW::Search::PubMed::Result> |
| 235 |
L<http://www.ncbi.nlm.nih.gov:80/entrez/query/static/overview.html> |
| 236 |
L<http://eutils.ncbi.nlm.nih.gov/entrez/query/static/esearch_help.html> |
| 237 |
L<http://eutils.ncbi.nlm.nih.gov/entrez/query/static/efetchlit_help.html> |
| 238 |
|
| 239 |
=head1 COPYRIGHT |
| 240 |
|
| 241 |
Copyright (c) 2003-2007 Gregory Todd Williams. All rights reserved. This |
| 242 |
program is free software; you can redistribute it and/or modify it under |
| 243 |
the same terms as Perl itself. |
| 244 |
|
| 245 |
=head1 AUTHOR |
| 246 |
|
| 247 |
Gregory Todd Williams C<< <[email protected]> >> |
| 248 |
|
| 249 |
=cut |