]> git.donarmstrong.com Git - bin.git/blob - gene_search
add gene search
[bin.git] / gene_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                color           => 1,
65                org_mode        => 0,
66                );
67
68 GetOptions(\%options,
69            'color|c!',
70            'org_mode|org-mode',
71            'debug|d+','help|h|?','man|m');
72
73 pod2usage() if $options{help};
74 pod2usage({verbose=>2}) if $options{man};
75
76 $DEBUG = $options{debug};
77
78 my @USAGE_ERRORS;
79 if (not @ARGV) {
80     push @USAGE_ERRORS,"You must pass something";
81 }
82
83 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
84
85 my $search = Bio::DB::EUtilities->new(-eutil => 'esearch',
86                                        -email => 'don@donarmstrong.com',
87                                        -db    => 'gene',
88                                        -term => join(' ',@ARGV),
89                                        -retmax => 1000,
90                                       );
91 my @ids = $search->get_ids();
92 # print scalar(@ids)." results:\n";
93 exit 0 unless @ids;
94 my $esummary = Bio::DB::EUtilities->new(-eutil => 'efetch',
95                                         -email => 'don@donarmstrong.com',
96                                         -db    => 'gene',
97                                         -id  => \@ids,
98                                         -retmode => 'xml',
99                                        );
100 #print $esummary->get_Response()->content();
101 use XML::LibXML;
102 my $xml = XML::LibXML->load_xml(string => $esummary->get_Response()->content());
103 for my $gene ($xml->findnodes('Entrezgene-Set/Entrezgene')) {
104     # print $article->toString;
105     my ($locus) = $gene->findnodes('.//Gene-ref_locus');
106     my ($desc) = $gene->findnodes('.//Gene-ref_desc');
107     my ($summary) = $gene->findnodes('.//Entrezgene_summary');
108     if ($options{org_mode}) {
109         print "* ";
110     }
111     print BOLD GREEN if $options{color};
112     print $locus->textContent();
113     print ": ";
114     print RESET if $options{color};
115     print BOLD CYAN if $options{color};
116     print encode_utf8($desc->textContent())."\n";
117     print RESET if $options{color};
118     print BOLD MAGENTA if $options{color};
119     if (defined $summary) {
120         if ($options{org_mode}) {
121             print "** Summary\n";
122         }
123         $summary = $summary->textContent();
124         $summary =~ s/^\s*//mg;
125         $summary = encode_utf8($summary);
126         print wrap('','',$summary);
127         print "\n\n";
128         print RESET if $options{color};
129     }
130 }
131
132 __END__