#! /usr/bin/perl # pubmed_search searches for articles on pubmed, and is released # under the terms of the GPL version 2, or any later version, at your # option. See the file README and COPYING for more information. # Copyright 2011,2017 by Don Armstrong . use warnings; use strict; use Getopt::Long; use Pod::Usage; use Bio::DB::EUtilities; use Encode qw(encode_utf8); use Term::ANSIColor qw(:constants); use Text::Wrap; =head1 NAME pubmed_search - Search for articles on pubmed =head1 SYNOPSIS pubmed_search [options] [searchterms] Options: --debug, -d debugging level (Default 0) --help, -h display this help --man, -m display manual =head1 OPTIONS =over =item B<--debug, -d> Debug verbosity. (Default 0) =item B<--help, -h> Display brief usage information. =item B<--man, -m> Display this manual. =back =head1 EXAMPLES =cut use vars qw($DEBUG); my %options = (debug => 0, help => 0, man => 0, color => 1, org_mode => 0, abstract => 1, ); GetOptions(\%options, 'color|c!', 'org_mode|org-mode', 'pmid_only|pmid-only', 'abstract|abstracts|a!', 'debug|d+','help|h|?','man|m'); pod2usage() if $options{help}; pod2usage({verbose=>2}) if $options{man}; $DEBUG = $options{debug}; my @USAGE_ERRORS; if (not @ARGV) { push @USAGE_ERRORS,"You must pass something"; } pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS; my $result = Bio::DB::EUtilities->new(-email => 'don@donarmstrong.com', -db => 'pubmed', -term => join(' ',@ARGV), -retmax => 1000, -eutil => 'esearch', ); my @ids; eval { # this warns for everything, so hide the warning. local $SIG{__WARN__} = sub {}; @ids = $result->get_ids('pubmed') }; if (not @ids) { print "No results\n"; exit; } if (@ids > 0 and ref($ids[0])) { @ids = @{$ids[0]}; } if ($options{org_mode}) { print "* Pubmed search results for ".join(' ',@ARGV)." (".scalar(@ids).")\n"; print " + "; } elsif ($options{pmid_only}) { print map{qq($_\n)} @ids; exit 0; } print scalar(@ids)." results:\n"; exit 0 unless @ids; my $raw_xml = Bio::DB::EUtilities->new(-email => 'don@donarmstrong.com', -db => 'pubmed', -id => \@ids, -eutil => 'efetch', ); use XML::LibXML; my $xml = XML::LibXML->load_xml(string => $raw_xml->get_Response->content); print STDERR $xml->toString if $DEBUG; for my $article ($xml->findnodes(q{//*[local-name()='MedlineCitation']})) { print STDERR $article->toString if $DEBUG; my ($pmid) = $article->findnodes(q{./*[local-name()='PMID']}); my ($title) = $article->findnodes(q{./*[local-name()='Article']}. q{/*[local-name()='ArticleTitle']}); my ($abstract) = $article->findnodes(q{./*[local-name()='Article']}. q{/*[local-name()='Abstract']}); if ($options{org_mode}) { print "** PMID: "; } print BOLD GREEN if $options{color}; print $pmid->textContent(); print ": "; print RESET if $options{color}; print BOLD CYAN if $options{color}; print encode_utf8($title->textContent())."\n"; print RESET if $options{color}; if (defined $abstract and $options{abstract}) { print BOLD MAGENTA if $options{color}; if ($options{org_mode}) { print "*** Abstract\n"; } $abstract = $abstract->textContent(); $abstract =~ s/^\s*//mg; $abstract =~ s/(.{,80})\s/$1\n/g; $abstract = encode_utf8($abstract); print wrap('','',$abstract); print "\n\n"; print RESET if $options{color}; } } __END__