]> git.donarmstrong.com Git - bin.git/blob - pubmed_search
output no results when there are no results
[bin.git] / pubmed_search
1 #! /usr/bin/perl
2 # , and is released
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 $
7
8
9 use warnings;
10 use strict;
11
12 use Getopt::Long;
13 use Pod::Usage;
14
15 use Bio::DB::SoapEUtilities;
16
17 use Encode qw(encode_utf8);
18 use Term::ANSIColor qw(:constants);
19 use Text::Wrap;
20
21
22 =head1 NAME
23
24 pubmed_search - Search for articles on pubmed
25
26 =head1 SYNOPSIS
27
28  pubmed_search [options] [searchterms]
29
30  Options:
31   --debug, -d debugging level (Default 0)
32   --help, -h display this help
33   --man, -m display manual
34
35 =head1 OPTIONS
36
37 =over
38
39 =item B<--debug, -d>
40
41 Debug verbosity. (Default 0)
42
43 =item B<--help, -h>
44
45 Display brief usage information.
46
47 =item B<--man, -m>
48
49 Display this manual.
50
51 =back
52
53 =head1 EXAMPLES
54
55
56 =cut
57
58
59 use vars qw($DEBUG);
60
61 my %options = (debug           => 0,
62                help            => 0,
63                man             => 0,
64                color           => 1,
65                org_mode        => 0,
66                abstract        => 1,
67               );
68
69 GetOptions(\%options,
70            'color|c!',
71            'org_mode|org-mode',
72            'pmid_only|pmid-only',
73            'abstract|a!',
74            'debug|d+','help|h|?','man|m');
75
76 pod2usage() if $options{help};
77 pod2usage({verbose=>2}) if $options{man};
78
79 $DEBUG = $options{debug};
80
81 my @USAGE_ERRORS;
82 if (not @ARGV) {
83     push @USAGE_ERRORS,"You must pass something";
84 }
85
86 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
87
88
89 my $fac = Bio::DB::SoapEUtilities->new();
90
91 my $result = $fac->esearch(-email => 'don@donarmstrong.com',
92                            -db    => 'pubmed',
93                            -term => join(' ',@ARGV),
94                            -retmax => 1000,
95                           )->run();
96 if ($result eq '0') {
97     print "No results\n";
98     exit;
99 }
100 my @ids = $result->ids;
101 if (@ids > 0 and ref($ids[0])) {
102     @ids = @{$ids[0]};
103 }
104 if ($options{org_mode}) {
105     print "* Pubmed search results for ".join(' ',@ARGV)." (".scalar(@ids).")\n";
106     print "  + ";
107 } elsif ($options{pmid_only}) {
108     print map{qq($_\n)} @ids;
109     exit 0;
110 }
111 print scalar(@ids)." results:\n";
112 exit 0 unless @ids;
113 my $raw_xml = $fac->efetch(-email => 'don@donarmstrong.com',
114                            -db    => 'pubmed',
115                            -id  => \@ids
116                           )->run(-raw_xml => 1);
117 use XML::LibXML;
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}) {
128         print "** PMID: ";
129     }
130     print BOLD GREEN if $options{color};
131     print $pmid->textContent();
132     print ": ";
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";
141         }
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);
147         print "\n\n";
148         print RESET if $options{color};
149     }
150 }
151
152 __END__