File Coverage

blib/lib/App/PDBRun.pm
Criterion Covered Total %
statement 68 84 80.9
branch 9 28 32.1
condition 7 19 36.8
subroutine 17 18 94.4
pod 4 4 100.0
total 105 153 68.6


line stmt bran cond sub pod time code
1             package App::PDBRun;
2              
3 1     1   23091 use 5.006;
  1         4  
  1         32  
4 1     1   4 use strict;
  1         2  
  1         27  
5 1     1   4 use warnings;
  1         5  
  1         78  
6              
7             =head1 NAME
8              
9             App::PDBRun - Run commands with PDB IDs as arguments
10              
11             =head1 VERSION
12              
13             This document describes "App::PDBRun" version 0.00_01 (August 28, 2009).
14              
15             The latest version is hosted on Google Code as part of
16             L.
17              
18             =cut
19              
20             our $VERSION = '0.00_01';
21             $VERSION = eval $VERSION;
22              
23             =head1 SYNOPSIS
24              
25             use App::PDBRun;
26            
27             # something interesting happens
28              
29             =head1 DESCRIPTION
30              
31             Some module is a wonderful piece of software.
32              
33             =cut
34              
35 1     1   4 use Carp;
  1         2  
  1         92  
36 1     1   4 use Exporter;
  1         2  
  1         25  
37 1     1   4 use Fcntl;
  1         1  
  1         253  
38 1     1   134176 use File::HomeDir;
  1         9840  
  1         78  
39 1     1   10 use File::Path;
  1         2  
  1         70  
40 1     1   6 use File::Spec;
  1         2  
  1         29  
41 1     1   1703 use File::Temp qw(tempdir);
  1         26921  
  1         55  
42 1     1   724 use IO::File;
  1         922  
  1         279  
43 1     1   244555 use IO::Uncompress::Gunzip;
  1         545233  
  1         49  
44 1     1   1242 use Net::FTP;
  1         40991  
  1         843  
45              
46             our @ISA = ();
47             our @EXPORT_OK = ();
48              
49             my $tmpdir = tempdir(CLEANUP => 1);
50              
51             =head1 FUNCTIONS
52              
53             Generic text about all the nifty things this module can do.
54              
55             =over 4
56              
57             =item App::PDBRun->config( [ $FILE ] )
58              
59             Configuration.
60              
61             =cut
62              
63             sub config {
64 0     0 1 0 my($pkg, $file) = @_;
65 0 0       0 unless($file) {
66 0         0 my $dir = File::Spec->catfile(File::HomeDir->my_home, '.pdb');
67 0         0 File::Path::mkpath($dir);
68 0         0 $file = File::Spec->catfile($dir, 'config.pl');
69             }
70 0 0       0 unless(-e $file) {
71 0 0       0 open my $fh, ">$file"
72             or croak "$file doesn't exist and couldn't be opened for writing";
73 0         0 printf($fh <<'EOF', map {"\Q$_\E"} ($pkg, $pkg->VERSION));
  0         0  
74             ### BEGIN MAGIC ################################################################
75             # This code does some initialization work. It should be safe to ignore.
76              
77             use warnings;
78             use strict;
79              
80             use File::Spec;
81              
82             my $pkg = "%s";
83             my $observed = $pkg->VERSION;
84             my $expected = "%s";
85             my $file = (caller(0))[6];
86             my $dir = join('', (File::Spec->splitpath($file))[0..1]);
87             unless($observed eq $expected) {
88             printf(STDERR "File %%s configured %%s %%s, but current version is %%s\n",
89             $file, $pkg, $expected, $observed);
90             exit(1);
91             }
92             ### END MAGIC ##################################################################
93              
94              
95             ### BEGIN CONFIG ###############################################################
96             # Do your configuration here!
97              
98             # By default files are downloaded on each run, but if you can afford the space,
99             # you might save some time with a local cache:
100             #$pkg->cache('/some/directory/to/use/as/cache');
101              
102             # The following line would store downloaded files in a directory called
103             # "cache" in the same part of the filesystem as this configuration file
104             # (probably the directory ".pdb" within your user's home directory):
105             #$pkg->cache(File::Spec->catfile($dir, 'cache'));
106              
107             ### END CONFIG #################################################################
108              
109             1;
110             EOF
111 0         0 close $fh;
112             }
113 0         0 require $file;
114             }
115              
116             =item App::PDBRun->ftp( [ $FTP ] )
117              
118             Returns the host name for the PDB FTP archive, first setting it to $FTP if
119             it's specified. Default value is F.
120              
121             =cut
122              
123             my $ftp;
124             sub ftp {
125 1     1 1 3 my $pkg = shift;
126 1 50 50     16 return $ftp = @_ ? shift : $ftp || 'ftp.wwpdb.org';
127             }
128              
129             =item App::PDBRun->cache( [ $CACHE ] )
130              
131             Returns the path used as cache by this package, first setting it to $CACHE, if
132             it's specified. Default value is C.
133              
134             =cut
135              
136             my $cache;
137             sub cache {
138 3     3 1 7 my $pkg = shift;
139 3 50 50     389 return $cache = @_ ? shift : $cache || undef;
140             }
141              
142             =item App::PDBRun->run( $CMD, @ARGS )
143              
144             Runs $CMD (by calling C) with the specified @ARGS.
145              
146             =cut
147              
148             sub run {
149 1     1 1 15 my($pkg, $cmd, @args) = @_;
150 1         3 for(@args) {
151 1 50       9 if(/\A([a-z0-9]{4})\.pdb\z/i) {
152 1         4 my $pdbid = lc($1);
153 1         5 $pdbid =~ /.(..)./;
154 1         8 $_ = $pkg->_get_file(
155             "${pdbid}.pdb",
156             qw(pub pdb data structures divided pdb),
157             $1, "pdb${pdbid}.ent.gz"
158             );
159             }
160             }
161 1         7939 return system($cmd, @args);
162             }
163              
164             =back
165              
166             =cut
167              
168             ################################################################################
169              
170             sub _get_file {
171 1     1   5 my($pkg, $filename, @dir) = @_;
172 1         1 my($dir, $local_path, $store);
173 1         3 my $file = pop @dir;
174 1 50       6 if($pkg->cache) {
175 0         0 $dir = File::Spec->catfile($pkg->cache, @dir);
176 0         0 $local_path = File::Spec->catfile($dir, $file);
177             }
178 1 50 33     4 unless($pkg->cache && ($store = new IO::File($local_path))) {
179 1         2 my $ftp;
180 1 50 33     5 if( ($ftp = new Net::FTP($pkg->ftp, Debug => 0))
      33        
181             && $ftp->login(qw(anonymous -anonymous@))
182             && $ftp->cwd(join('', map("/$_", @dir)))
183             ) {
184 1 0 33     459450 $store = IO::File->new_tmpfile unless $pkg->cache
      33        
185             && File::Path::mkpath($dir)
186             && ($store = new IO::File($local_path, '+>'));
187 1 50       13 if($ftp->get($file => $store)) {
188 1         507056 seek($store, 0, SEEK_SET);
189             }
190             else {
191 0         0 undef $store;
192 0 0       0 $pkg->cache and unlink $local_path;
193             }
194 1         9 $ftp->quit;
195             }
196             }
197 1 50       82807 if($store) {
198 1         30 $filename = File::Spec->catfile($tmpdir, $filename);
199 1 50       186 if(open my $fh, ">$filename") {
200 1         8 IO::Uncompress::Gunzip::gunzip($store => $fh);
201 1         33443 close $store;
202 1         24 close $fh;
203 1         20 return $filename;
204             }
205             }
206 0           return undef;
207             }
208              
209             1;
210              
211             __END__