3 # under the terms of the GPL version 2, or any later version, at your
4 # option. See the file README and COPYING for more information.
5 # Copyright 2011 by Don Armstrong <don@donarmstrong.com>.
6 # $Id: perl_script 1825 2011-01-02 01:53:43Z don $
15 use Bio::DB::SoapEUtilities;
17 use Encode qw(encode_utf8);
18 use Term::ANSIColor qw(:constants);
24 pubmed_search - Search for articles on pubmed
28 pubmed_search [options] [searchterms]
31 --debug, -d debugging level (Default 0)
32 --help, -h display this help
33 --man, -m display manual
41 Debug verbosity. (Default 0)
45 Display brief usage information.
61 my %options = (debug => 0,
71 'pmid_only|pmid-only',
72 'debug|d+','help|h|?','man|m');
74 pod2usage() if $options{help};
75 pod2usage({verbose=>2}) if $options{man};
77 $DEBUG = $options{debug};
81 push @USAGE_ERRORS,"You must pass something";
84 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
87 my $fac = Bio::DB::SoapEUtilities->new();
89 my $result = $fac->esearch(-email => 'don@donarmstrong.com',
91 -term => join(' ',@ARGV),
94 my @ids = $result->ids;
95 if (@ids > 0 and ref($ids[0])) {
98 if ($options{org_mode}) {
99 print "* Pubmed search results for ".join(' ',@ARGV)." (".scalar(@ids).")\n";
101 } elsif ($options{pmid_only}) {
102 print map{qq($_\n)} @ids;
105 print scalar(@ids)." results:\n";
107 my $raw_xml = $fac->efetch(-email => 'don@donarmstrong.com',
110 )->run(-raw_xml => 1);
112 my $xml = XML::LibXML->load_xml(string => $raw_xml);
113 print STDERR $xml->toString if $DEBUG;
114 for my $article ($xml->findnodes(q{//*[local-name()='MedlineCitation']})) {
115 print STDERR $article->toString if $DEBUG;
116 my ($pmid) = $article->findnodes(q{./*[local-name()='PMID']});
117 my ($title) = $article->findnodes(q{./*[local-name()='Article']}.
118 q{/*[local-name()='ArticleTitle']});
119 my ($abstract) = $article->findnodes(q{./*[local-name()='Article']}.
120 q{/*[local-name()='Abstract']});
121 if ($options{org_mode}) {
124 print BOLD GREEN if $options{color};
125 print $pmid->textContent();
127 print RESET if $options{color};
128 print BOLD CYAN if $options{color};
129 print encode_utf8($title->textContent())."\n";
130 print RESET if $options{color};
131 print BOLD MAGENTA if $options{color};
132 if (defined $abstract) {
133 if ($options{org_mode}) {
134 print "*** Abstract\n";
136 $abstract = $abstract->textContent();
137 $abstract =~ s/^\s*//mg;
138 $abstract =~ s/(.{,80})\s/$1\n/g;
139 $abstract = encode_utf8($abstract);
140 print wrap('','',$abstract);
142 print RESET if $options{color};