]> git.donarmstrong.com Git - bin.git/blob - pubmed_search
add pubmed search utility
[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::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 - 
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                );
65
66 GetOptions(\%options,
67            'debug|d+','help|h|?','man|m');
68
69 pod2usage() if $options{help};
70 pod2usage({verbose=>2}) if $options{man};
71
72 $DEBUG = $options{debug};
73
74 my @USAGE_ERRORS;
75 if (not @ARGV) {
76     push @USAGE_ERRORS,"You must pass something";
77 }
78
79 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
80
81
82 my $search = Bio::DB::EUtilities->new(-eutil => 'esearch',
83                                        -email => 'don@donarmstrong.com',
84                                        -db    => 'pubmed',
85                                        -term => join(' ',@ARGV),
86                                        -retmax => 1000,
87                                       );
88 my @ids = $search->get_ids();
89 print scalar(@ids)." results:\n";
90 my $esummary = Bio::DB::EUtilities->new(-eutil => 'efetch',
91                                         -email => 'don@donarmstrong.com',
92                                         -db    => 'pubmed',
93                                         -id  => \@ids
94                                        );
95 use XML::LibXML;
96 my $xml = XML::LibXML->load_xml(string => $esummary->get_Response()->content());
97 for my $article ($xml->findnodes('PubmedArticleSet/PubmedArticle/MedlineCitation')) {
98     # print $article->toString;
99     my ($pmid) = $article->findnodes('./PMID');
100     my ($title) = $article->findnodes('./Article/ArticleTitle');
101     my ($abstract) = $article->findnodes('./Article/Abstract');
102     print BOLD GREEN;
103     print $pmid->textContent();
104     print ": ";
105     print RESET;
106     print BOLD CYAN;
107     print $title->textContent()."\n";
108     print RESET;
109     print BOLD MAGENTA;
110     $abstract = $abstract->textContent();
111     $abstract =~ s/^\s*//mg;
112     $abstract =~ s/(.{,80})\s/$1\n/g;
113     $abstract = encode_utf8($abstract);
114     print wrap('','',$abstract);
115     print "\n\n";
116     print RESET;
117 }
118
119 __END__