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,
72 'pmid_only|pmid-only',
74 'debug|d+','help|h|?','man|m');
76 pod2usage() if $options{help};
77 pod2usage({verbose=>2}) if $options{man};
79 $DEBUG = $options{debug};
83 push @USAGE_ERRORS,"You must pass something";
86 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
89 my $fac = Bio::DB::SoapEUtilities->new();
91 my $result = $fac->esearch(-email => 'don@donarmstrong.com',
93 -term => join(' ',@ARGV),
100 my @ids = $result->ids;
101 if (@ids > 0 and ref($ids[0])) {
104 if ($options{org_mode}) {
105 print "* Pubmed search results for ".join(' ',@ARGV)." (".scalar(@ids).")\n";
107 } elsif ($options{pmid_only}) {
108 print map{qq($_\n)} @ids;
111 print scalar(@ids)." results:\n";
113 my $raw_xml = $fac->efetch(-email => 'don@donarmstrong.com',
116 )->run(-raw_xml => 1);
118 my $xml = XML::LibXML->load_xml(string => $raw_xml);
119 print STDERR $xml->toString if $DEBUG;
120 for my $article ($xml->findnodes(q{//*[local-name()='MedlineCitation']})) {
121 print STDERR $article->toString if $DEBUG;
122 my ($pmid) = $article->findnodes(q{./*[local-name()='PMID']});
123 my ($title) = $article->findnodes(q{./*[local-name()='Article']}.
124 q{/*[local-name()='ArticleTitle']});
125 my ($abstract) = $article->findnodes(q{./*[local-name()='Article']}.
126 q{/*[local-name()='Abstract']});
127 if ($options{org_mode}) {
130 print BOLD GREEN if $options{color};
131 print $pmid->textContent();
133 print RESET if $options{color};
134 print BOLD CYAN if $options{color};
135 print encode_utf8($title->textContent())."\n";
136 print RESET if $options{color};
137 if (defined $abstract and $options{abstract}) {
138 print BOLD MAGENTA if $options{color};
139 if ($options{org_mode}) {
140 print "*** Abstract\n";
142 $abstract = $abstract->textContent();
143 $abstract =~ s/^\s*//mg;
144 $abstract =~ s/(.{,80})\s/$1\n/g;
145 $abstract = encode_utf8($abstract);
146 print wrap('','',$abstract);
148 print RESET if $options{color};