]> git.donarmstrong.com Git - bin.git/blob - pubmed_search
add no-abstract option
[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 my @ids = $result->ids;
97 if (@ids > 0 and ref($ids[0])) {
98     @ids = @{$ids[0]};
99 }
100 if ($options{org_mode}) {
101     print "* Pubmed search results for ".join(' ',@ARGV)." (".scalar(@ids).")\n";
102     print "  + ";
103 } elsif ($options{pmid_only}) {
104     print map{qq($_\n)} @ids;
105     exit 0;
106 }
107 print scalar(@ids)." results:\n";
108 exit 0 unless @ids;
109 my $raw_xml = $fac->efetch(-email => 'don@donarmstrong.com',
110                            -db    => 'pubmed',
111                            -id  => \@ids
112                           )->run(-raw_xml => 1);
113 use XML::LibXML;
114 my $xml = XML::LibXML->load_xml(string => $raw_xml);
115 print STDERR $xml->toString if $DEBUG;
116 for my $article ($xml->findnodes(q{//*[local-name()='MedlineCitation']})) {
117     print STDERR $article->toString if $DEBUG;
118     my ($pmid) = $article->findnodes(q{./*[local-name()='PMID']});
119     my ($title) = $article->findnodes(q{./*[local-name()='Article']}.
120                                       q{/*[local-name()='ArticleTitle']});
121     my ($abstract) = $article->findnodes(q{./*[local-name()='Article']}.
122                                          q{/*[local-name()='Abstract']});
123     if ($options{org_mode}) {
124         print "** PMID: ";
125     }
126     print BOLD GREEN if $options{color};
127     print $pmid->textContent();
128     print ": ";
129     print RESET if $options{color};
130     print BOLD CYAN if $options{color};
131     print encode_utf8($title->textContent())."\n";
132     print RESET if $options{color};
133     if (defined $abstract and $options{abstract}) {
134         print BOLD MAGENTA if $options{color};
135         if ($options{org_mode}) {
136             print "*** Abstract\n";
137         }
138         $abstract = $abstract->textContent();
139         $abstract =~ s/^\s*//mg;
140         $abstract =~ s/(.{,80})\s/$1\n/g;
141         $abstract = encode_utf8($abstract);
142         print wrap('','',$abstract);
143         print "\n\n";
144         print RESET if $options{color};
145     }
146 }
147
148 __END__