1 # This module is part of Refence, and is released under the terms of
2 # the GPL version 2, or any later version. See the file README and
3 # COPYING for more information.
4 # Copyright 2003 by Don Armstrong <don@donarmstrong.com>.
7 package Reference::Retrieve::PubMed;
11 Reference::Retrieve::PubMed -- Reference Retrieval from PubMed
15 my $reference = Reference::Retrieve::PubMed::get_reference(-pmid=>123456);
16 my @references = Reference::Retrieve::PubMed::get_reference(-query=>'John Smith[AUTHOR] AND 230[Pages]',limit=>50);
21 Uh. Retreives references from pubmed. Yeah.
31 use vars qw($REVISION $DEBUG);
35 use XML::Simple qw(:strict);
40 use Params::Validate qw(:types validate_with);
43 ($REVISION) = q$LastChangedRevision$ =~ /\$LastChangedRevision:\s+([^\s+])/;
44 $DEBUG = 0 unless defined $DEBUG;
52 my $reference = Reference::Retrieve::PubMed::get_reference(-pmid=>123456);
53 my @references = Reference::Retrieve::PubMed::get_reference(-query=>'John Smith[AUTHOR] AND 230[Pages]',-limit=>50);
54 my @references = Reference::Retrieve::PubMed::get_reference(-query=>{author=>'John Smith', pages=>'230'},-limit=>50)
58 Retrives a reference from pubmed
62 In scalar context, effectively assumes -limit=>1 and returns the
63 highest listed reference according to the order, etc. [Probably only
64 usefull with -pmid.] In list context, returns all results (or until it
69 list of arguments to select a reference or collection of references from.
75 my %options = validate_with(params => @_,
76 spec => {pubmed_site => {default => 'http://www.ncbi.nlm.nih.gov'},
77 pmid_query => {default => '/entrez/query.fcgi?cmd=Text&db=PubMed&dopt=XML&uid='},
78 search_query => {default => '/htbin-post/Entrez/query?db=m&form=4&dispmax=100&html=no&dopt=u&term='},
79 ua_agent => {default => "DA Reference::Retreive::PubMed/$REVISION"},
80 email => {default => "don+referenceretrieve$REVISION\@donarmstrong.com"},
84 my $ua = new LWP::UserAgent(agent => $options{ua_agent});
87 sub get_reference_by_pmid($;@){
88 my %options = validate_with(params => \@_,
89 spec => {pmid => {type => SCALAR|ARRAYREF,
92 pubmed_site => {default => 'http://www.ncbi.nlm.nih.gov'},
93 pmid_query => {default => '/entrez/eutils/efetch.fcgi?db=pubmed&retmode=xml&rettype=full&id='},
94 search_query => {default => '/htbin-post/Entrez/query?db=m&form=4&dispmax=100&html=no&dopt=u&term='},
95 ua_agent => {default => "DA Reference::Retreive::PubMed/$REVISION"},
96 email => {default => "don+referenceretrieve$REVISION\@donarmstrong.com"},
97 useragent => {optional => 1},
101 my $pmid = $options{pmid};
104 if ($options{useragent}) {
105 $ua = $options{useragent};
108 $ua = new LWP::UserAgent(agent=>$options{ua_agent});
110 my $url = "$options{pubmed_site}$options{pmid_query}" . (ref($pmid) ? (join('&id=',@{$pmid})) : $pmid);
111 print STDERR "url: $url" if $DEBUG;
112 my $request = HTTP::Request->new('GET', $url);
113 my $response = $ua->request($request);
114 $response = $response->content;
115 print STDERR "response: $response" if $DEBUG;
117 # For some dumb reason, they send us xml with html
118 # entities. Ditch them.
119 #$response = decode_entities($response);
120 # It's even more freaking broken; they don't double encode them.
121 #$response =~ s/\>(\s|$)/>$1/gso;
122 #$response =~ s/(?:(\s)\<|<(\/))/$1<$2/gso;
123 $response =~ s/"/"/gso;
126 $response =~ s/^\s*<\?xml[^>]+>\s*//gso;
127 $response =~ s/^\s*<\!DOCTYPE[^>]+>\s*//gso;
128 # There is also a Pubmedarticleset
129 $response =~ s/^\s*<PubmedArticleSet>\s*//gso;
130 $response =~ s#</PubmedArticleSet>\s*$##gso;
132 # Add the opt so we get an array of PubMedArticle
133 $response = "<opt>$response</opt>";
135 print STDERR $response if $DEBUG;
137 # Figure out if there was an error in the search.
139 # Response should be in XML. Parse it.
140 my $xa = new XML::Simple;
142 my $ref_struct = $xa->XMLin($response, ForceArray => [ 'PubmedArticle' ],KeyAttr=>[]);
145 print STDERR Dumper($ref_struct) if $DEBUG;
146 # Handle the XML structure
148 foreach my $ref (@{$ref_struct->{PubmedArticle}}) {
149 my $reference = _create_reference_from_xml($ref,$ua);
150 if (not defined $reference) {
151 warn "Unable to create reference for $ref->{MedlineCitation}->{PMID}\n";
153 push @references, $reference;
158 return $references[0];
161 sub _create_reference_from_xml($$){
164 # Figure out what type of reference this is. We only support
165 # Journal Articles right now.
166 my $types = {'journal article'=>'article',
167 'letter' =>'article',
169 my $ref_type = undef;
170 my $reference = undef;
171 foreach my $type (keys %{$types}) {
172 if (ref($ref->{MedlineCitation}->{Article}->{PublicationTypeList}->{PublicationType}) eq 'ARRAY'){
174 @{$pubtypes}{map {lc} @{$ref->{MedlineCitation}->{Article}->{PublicationTypeList}->{PublicationType}}} =
175 (1) x @{$ref->{MedlineCitation}->{Article}->{PublicationTypeList}->{PublicationType}};
176 if ($pubtypes->{$type}) {
177 $ref_type = $types->{$type};
184 elsif (lc($ref->{MedlineCitation}->{Article}->{PublicationTypeList}->{PublicationType}) eq lc($type)) {
185 $ref_type = $types->{$type};
189 if (not defined $ref_type) {
190 warn "Unsupported PublicationType: ".(ref($ref->{MedlineCitation}->{Article}->{PublicationTypeList}->{PublicationType})?
191 join(',',@{$ref->{MedlineCitation}->{Article}->{PublicationTypeList}->{PublicationType}}):
192 $ref->{MedlineCitation}->{Article}->{PublicationTypeList}->{PublicationType});
193 print STDERR Dumper($ref);
194 $ref_type = 'article';
196 local $_ = $ref_type;
198 use Reference::Type::Article;
199 $reference = new Reference::Type::Article;
200 my $xml_mapping = {author => [ _fix_medline_authors($ref->{MedlineCitation}->{Article}->{AuthorList}) ],
201 title => [_fix_medline_title($ref->{MedlineCitation}->{Article}->{ArticleTitle})],
202 abstract => $ref->{MedlineCitation}->{Article}->{Abstract}->{AbstractText},
203 journal => [_fix_medline_journal($ref->{MedlineCitation}->{Article}->{Journal},
204 $ref->{MedlineCitation}->{Article}->{MedlineJournalInfo},
209 # pmid => $ref->{MedlineCitation}->{PMID},
210 # medline_id => $ref->{MedlineCitation}->{MedlineID},
211 volume => [_fix_medline_ditch_empty($ref->{MedlineCitation}->{Article}->{Journal}->{JournalIssue}->{Volume})],
212 date => [_fix_medline_pubdate($ref->{MedlineCitation}->{Article}->{Journal}->{JournalIssue}->{PubDate})],
213 number => [_fix_medline_ditch_empty($ref->{MedlineCitation}->{Article}->{Journal}->{JournalIssue}->{Issue})],
214 pages => [_fix_medline_pages($ref->{MedlineCitation}->{Article}->{Pagination}->{MedlinePgn})],
215 # keywords => [_fix_medline_keywords($ref->{MedlineCitation}->{MeshHeadingList},
216 # $ref->{MedlineCitation}->{ChemicalList},
218 # &_find_pubmed_links($ref->{MedlineCitation}->{PMID},$ua),
222 foreach my $reference_key (keys %{$xml_mapping}) {
223 my $method = $reference->can($reference_key);
224 die "Reference::Type::Article was unable to handle $reference_key" if not $method;
225 if (defined $xml_mapping->{$reference_key} and $method) {
226 if (ref($xml_mapping->{$reference_key})) {
227 &{$method}($reference,@{$xml_mapping->{$reference_key}});
230 &{$method}($reference,$xml_mapping->{$reference_key});
234 warn "Reference_key $reference_key was not defined or unable to handle type of key."
235 if not defined $xml_mapping->{$reference_key} and $DEBUG;
242 sub _fix_medline_title($){
250 sub _fix_medline_authors($){
251 my $author_list = shift;
252 $author_list = $author_list->{Author};
254 $author_list = [$author_list] if ref($author_list) ne 'ARRAY';
255 foreach my $author (@{$author_list}) {
257 $au{first} = $author->{ForeName} if exists $author->{ForeName};
258 $au{last} = $author->{LastName} if exists $author->{LastName};
259 $au{initials} = $author->{Initials} if exists $author->{Initials};
263 return (author=>\@authors);
266 =head2 _fix_medline_journal
270 $reference->journal(_fix_medline_journal($ref->{MedlineCitation}->{Article}->{Journal},
271 $ref->{MedlineCitation}->{Article}->{MedlineJournalInfo},
276 From the medline citation informatino returns a properly formatted
277 list of information for the journal reference listing.
281 Journal information hashref
283 medline journal information hashref
289 sub _fix_medline_journal($$$;){
290 my ($journal,$medline_journal,$ua) = @_;
291 # journal takes fullname, issn, medlineabbr, pmid, and nlmuid
292 # Try to supply as much as possible.
293 # Use esearch to get pmjournalid
294 # http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=journals&term=0021-9258
295 # use esummary to retreive the journalid
296 # <?xml version="1.0"?>
297 # <!DOCTYPE eSearchResult PUBLIC "-//NLM//DTD eSearchResult, 11 May 2002//EN" "http://www.ncbi.nlm.nih.gov/entrez/query/DTD/eSearch_020511.dtd">
301 # <RetStart>0</RetStart>
310 # <Term>0021-9258[All Fields]</Term>
311 # <Field>All Fields</Field>
314 # <Explode>Y</Explode>
316 # </TranslationStack>
319 my $ISSN = $journal->{ISSN};
321 $ISSN = $ISSN->{content};
323 my $url = qq(http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=journals&term=$ISSN);
324 print STDERR "url: $url" if $DEBUG;
325 my $request = HTTP::Request->new('GET', $url);
326 my $response = $ua->request($request);
327 $response = $response->content;
328 print STDERR "response: $response" if $DEBUG;
330 my ($journal_id) = $response =~ m#<Id>\s*(\d+)\s*</Id>#i;
332 # http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=journals&id=4559
333 # <?xml version="1.0"?>
334 # <!DOCTYPE eSummaryResult PUBLIC "-//NLM//DTD eSummaryResult, 11 May 2002//EN" "http://www.ncbi.nlm.nih.gov/entrez/query/DTD/eSummary_020511.dtd">
338 # <Item Name="Title" Type="String">The Journal of biological chemistry.</Item>
339 # <Item Name="MedAbbr" Type="String">J Biol Chem</Item>
340 # <Item Name="IsoAbbr" Type="String">J. Biol. Chem.</Item>
341 # <Item Name="NlmId" Type="String">2985121R</Item>
343 # <Item Name="pISSN" Type="String">0021-9258</Item>
344 # <Item Name="eISSN" Type="String">1083-351X</Item>
345 # <Item Name="PublicationStartYear" Type="String">1905</Item>
346 # <Item Name="PublicationEndYear" Type="String"></Item>
347 # <Item Name="Publisher" Type="String">American Society for Biochemistry and Molecular Biology</Item>
348 # <Item Name="Language" Type="String">eng</Item>
350 # <Item Name="Country" Type="String">United States</Item>
354 $url = qq(http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=journals&id=$journal_id);
355 print STDERR "url: $url" if $DEBUG;
356 $request = HTTP::Request->new('GET', $url);
357 $response = $ua->request($request);
358 $response = $response->content;
359 print STDERR "response: $response" if $DEBUG;
362 while ($response =~ m{^\s*(?:(?:<id>\s*(\d+)</id>)| # Match ids
363 (?:<item\s+name=\"([^\"]+)\"\s+Type=\"String\">\s*([^<]+?)\.?</item>))\s* # Match item Name clauses
365 if (not defined $2) {
369 $journal{lc($2)} = $3;
372 my %journal_mapping = (title => q(title),
373 medlineabbr => q(medabbr),
374 isoabbr => q(isoabbr),
378 publisher => q(publisher),
382 foreach my $key (keys %journal_mapping) {
383 push @journal_entry,($key=>$journal{$journal_mapping{$key}});
385 return @journal_entry;
392 $reference->date(_fix_medline_pubdate($ref->{MedlineCitation}->{Article}->{Journal}->{JournalIssue}->{PubDate}));
402 sub _fix_medline_pubdate($){
404 return (year=>$date->{Year},month=>$date->{Month},day=>$date->{Day}) if exists $date->{Year};
406 if (exists $date->{MedlineDate}) {
407 my ($year,$month,$day) = split /\s+/,$date->{MedlineDate};
408 return (year=>$year,month=>$month,day=>$day)
412 =head2 _fix_medline_pages
416 pages => [_fix_medline_pages($ref->{MedlineCitation}->{Article}->{Pagination}->{MedlinePgn})],
420 Returns output with a list of pages appropriate for an Article type of
425 sub _fix_medline_pages($){
426 my ($pagination) = @_;
427 my ($start,$stop) = $pagination =~ /(\d*)\s*\-\s*(\d*)/;
429 push @return, (start=>$start) if defined $start and $start ne '';
430 push @return, (stop=>$stop) if defined $stop and $stop ne '';
434 sub _find_pubmed_links($$){
437 #http://eutils.ncbi.nlm.nih.gov/entrez/query/static/elink_help.html
438 my $url = "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/elink.fcgi?dbfrom=pubmed&cmd=llinks&id=" . (ref($pmid) ? (join('&id=',@{$pmid})) : $pmid);
439 print STDERR "url: $url" if $DEBUG;
440 my $request = HTTP::Request->new('GET', $url);
441 my $response = $ua->request($request);
442 $response = $response->content;
443 print STDERR "response: $response" if $DEBUG;
445 # Response should be in XML. Parse it.
446 my $xa = new XML::Simple;
448 my $ref_struct = $xa->XMLin($response, ForceArray => ['IdUrlSet'], KeyAttr=>[]);
451 print STDERR Dumper($ref_struct);# if $DEBUG;
452 # Rearange data around Id.
454 map {$links->{$_->{Id}}=$_->{ObjUrl}} @{$ref_struct->{LinkSet}->{IdUrlList}->{IdUrlSet}};
455 foreach my $obj_url (@{$links->{$pmid}->{ObjUrl}}) {
456 next unless $obj_url->{SubjectType} = 'publishers/providers';
457 #@links = _find_links_from_url($obj_url->{Url},$ua);
459 # Find publisher link
460 # If no publisher link, use the first aggregator link.
474 my %ids_known = (medline => 'medline_id',
479 if (exists $ref->{PubmedData}{ArticleIdList}{ArticleId}) {
480 for my $art_id (ref($ref->{PubmedData}{ArticleIdList}{ArticleId}) eq 'ARRAY' ?
481 @{$ref->{PubmedData}{ArticleIdList}{ArticleId}}:
482 ($ref->{PubmedData}{ArticleIdList}{ArticleId})) {
483 if (exists $ids_known{$art_id->{IdType}}) {
484 $ids{$ids_known{$art_id->{IdType}}} = $art_id->{content};
488 if (not exists $ids{pmid}) {
489 $ids{pmid} = $ref->{MedlineCitation}->{PMID} if defined $ref->{MedlineCitation}->{PMID};
491 if (not exists $ids{medline_id}) {
492 $ids{medline_id} = $ref->{MedlineCitation}->{MedlineID} if defined $ref->{MedlineCitation}->{MedlineID};
498 =head2 _find_links_from_url
510 sub _find_links_from_url($$){
517 sub _fix_medline_ditch_empty($){
521 if (ref($value) eq 'HASH') {
522 if (scalar keys %{$value} > 0) {
529 elsif (ref($value) eq 'ARRAY') {
530 if (scalar @{$value} > 0) {
542 return $value if defined $value;