]> git.donarmstrong.com Git - bin.git/blob - pubmed_search
add mutt alias which executes neomutt if that exists
[bin.git] / pubmed_search
1 #! /usr/bin/perl
2 # pubmed_search searches for articles on pubmed, 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,2017 by Don Armstrong <don@donarmstrong.com>.
6
7
8
9 use warnings;
10 use strict;
11
12 use Getopt::Long;
13 use Pod::Usage;
14
15 use Bio::DB::EUtilities;
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|abstracts|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 $result =
90     Bio::DB::EUtilities->new(-email => 'don@donarmstrong.com',
91                              -db    => 'pubmed',
92                              -term => join(' ',@ARGV),
93                              -retmax => 1000,
94                              -eutil  => 'esearch',
95                             );
96 my @ids;
97 eval {
98     # this warns for everything, so hide the warning.
99     local $SIG{__WARN__} = sub {};
100     @ids = $result->get_ids('pubmed')
101 };
102 if (not @ids) {
103     print "No results\n";
104     exit;
105 }
106 if (@ids > 0 and ref($ids[0])) {
107     @ids = @{$ids[0]};
108 }
109 if ($options{org_mode}) {
110     print "* Pubmed search results for ".join(' ',@ARGV)." (".scalar(@ids).")\n";
111     print "  + ";
112 } elsif ($options{pmid_only}) {
113     print map{qq($_\n)} @ids;
114     exit 0;
115 }
116 print scalar(@ids)." results:\n";
117 exit 0 unless @ids;
118 my $raw_xml = Bio::DB::EUtilities->new(-email => 'don@donarmstrong.com',
119                                        -db    => 'pubmed',
120                                        -id  => \@ids,
121                                        -eutil => 'efetch',
122                                       );
123 use XML::LibXML;
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}) {
134         print "** PMID: ";
135     }
136     print BOLD GREEN if $options{color};
137     print $pmid->textContent();
138     print ": ";
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";
147         }
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);
153         print "\n\n";
154         print RESET if $options{color};
155     }
156 }
157
158 __END__