use Getopt::Long;
use Pod::Usage;
-use Bio::DB::EUtilities;
+use Bio::DB::SoapEUtilities;
use Encode qw(encode_utf8);
use Term::ANSIColor qw(:constants);
=head1 NAME
-pubmed_search -
+pubmed_search - Search for articles on pubmed
=head1 SYNOPSIS
my %options = (debug => 0,
help => 0,
man => 0,
+ color => 1,
+ org_mode => 0,
);
GetOptions(\%options,
- 'debug|d+','help|h|?','man|m');
+ 'color|c!',
+ 'org_mode|org-mode',
+ 'pmid_only|pmid-only',
+ 'debug|d+','help|h|?','man|m');
pod2usage() if $options{help};
pod2usage({verbose=>2}) if $options{man};
pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
-my $search = Bio::DB::EUtilities->new(-eutil => 'esearch',
- -email => 'don@donarmstrong.com',
- -db => 'pubmed',
- -term => join(' ',@ARGV),
- -retmax => 1000,
- );
-my @ids = $search->get_ids();
+my $fac = Bio::DB::SoapEUtilities->new();
+
+my $result = $fac->esearch(-email => 'don@donarmstrong.com',
+ -db => 'pubmed',
+ -term => join(' ',@ARGV),
+ -retmax => 1000,
+ )->run();
+my @ids = $result->ids;
+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";
-my $esummary = Bio::DB::EUtilities->new(-eutil => 'efetch',
- -email => 'don@donarmstrong.com',
- -db => 'pubmed',
- -id => \@ids
- );
+exit 0 unless @ids;
+my $raw_xml = $fac->efetch(-email => 'don@donarmstrong.com',
+ -db => 'pubmed',
+ -id => \@ids
+ )->run(-raw_xml => 1);
use XML::LibXML;
-my $xml = XML::LibXML->load_xml(string => $esummary->get_Response()->content());
-for my $article ($xml->findnodes('PubmedArticleSet/PubmedArticle/MedlineCitation')) {
- # print $article->toString;
- my ($pmid) = $article->findnodes('./PMID');
- my ($title) = $article->findnodes('./Article/ArticleTitle');
- my ($abstract) = $article->findnodes('./Article/Abstract');
- print BOLD GREEN;
+my $xml = XML::LibXML->load_xml(string => $raw_xml);
+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;
- print BOLD CYAN;
- print $title->textContent()."\n";
- print RESET;
- print BOLD MAGENTA;
- $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;
+ print RESET if $options{color};
+ print BOLD CYAN if $options{color};
+ print encode_utf8($title->textContent())."\n";
+ print RESET if $options{color};
+ print BOLD MAGENTA if $options{color};
+ if (defined $abstract) {
+ 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__