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::EUtilities;
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;
90 Bio::DB::EUtilities->new(-email => 'don@donarmstrong.com',
92 -term => join(' ',@ARGV),
98 # this warns for everything, so hide the warning.
99 local $SIG{__WARN__} = sub {};
100 @ids = $result->get_ids('pubmed')
103 print "No results\n";
106 if (@ids > 0 and ref($ids[0])) {
109 if ($options{org_mode}) {
110 print "* Pubmed search results for ".join(' ',@ARGV)." (".scalar(@ids).")\n";
112 } elsif ($options{pmid_only}) {
113 print map{qq($_\n)} @ids;
116 print scalar(@ids)." results:\n";
118 my $raw_xml = Bio::DB::EUtilities->new(-email => 'don@donarmstrong.com',
124 my $xml = XML::LibXML->load_xml(string => $raw_xml->get_Response->content);
125 print STDERR $xml->toString if $DEBUG;
126 for my $article ($xml->findnodes(q{//*[local-name()='MedlineCitation']})) {
127 print STDERR $article->toString if $DEBUG;
128 my ($pmid) = $article->findnodes(q{./*[local-name()='PMID']});
129 my ($title) = $article->findnodes(q{./*[local-name()='Article']}.
130 q{/*[local-name()='ArticleTitle']});
131 my ($abstract) = $article->findnodes(q{./*[local-name()='Article']}.
132 q{/*[local-name()='Abstract']});
133 if ($options{org_mode}) {
136 print BOLD GREEN if $options{color};
137 print $pmid->textContent();
139 print RESET if $options{color};
140 print BOLD CYAN if $options{color};
141 print encode_utf8($title->textContent())."\n";
142 print RESET if $options{color};
143 if (defined $abstract and $options{abstract}) {
144 print BOLD MAGENTA if $options{color};
145 if ($options{org_mode}) {
146 print "*** Abstract\n";
148 $abstract = $abstract->textContent();
149 $abstract =~ s/^\s*//mg;
150 $abstract =~ s/(.{,80})\s/$1\n/g;
151 $abstract = encode_utf8($abstract);
152 print wrap('','',$abstract);
154 print RESET if $options{color};