]> git.donarmstrong.com Git - bin.git/blob - pubmed_search
Merge branch 'master' of git+ssh://git.donarmstrong.com/srv/git/bin
[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                );
67
68 GetOptions(\%options,
69            'color|c!',
70            'org_mode|org-mode',
71            'pmid_only|pmid-only',
72            'debug|d+','help|h|?','man|m');
73
74 pod2usage() if $options{help};
75 pod2usage({verbose=>2}) if $options{man};
76
77 $DEBUG = $options{debug};
78
79 my @USAGE_ERRORS;
80 if (not @ARGV) {
81     push @USAGE_ERRORS,"You must pass something";
82 }
83
84 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
85
86
87 my $fac = Bio::DB::SoapEUtilities->new();
88
89 my $result = $fac->esearch(-email => 'don@donarmstrong.com',
90                            -db    => 'pubmed',
91                            -term => join(' ',@ARGV),
92                            -retmax => 1000,
93                           )->run();
94 my @ids = $result->ids;
95 if (@ids > 0 and ref($ids[0])) {
96     @ids = @{$ids[0]};
97 }
98 if ($options{org_mode}) {
99     print "* Pubmed search results for ".join(' ',@ARGV)." (".scalar(@ids).")\n";
100     print "  + ";
101 } elsif ($options{pmid_only}) {
102     print map{qq($_\n)} @ids;
103     exit 0;
104 }
105 print scalar(@ids)." results:\n";
106 exit 0 unless @ids;
107 my $raw_xml = $fac->efetch(-email => 'don@donarmstrong.com',
108                            -db    => 'pubmed',
109                            -id  => \@ids
110                           )->run(-raw_xml => 1);
111 use XML::LibXML;
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}) {
122         print "** PMID: ";
123     }
124     print BOLD GREEN if $options{color};
125     print $pmid->textContent();
126     print ": ";
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";
135         }
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);
141         print "\n\n";
142         print RESET if $options{color};
143     }
144 }
145
146 __END__