Database.pm untested

filter: Statementstmt 0.0% 0 / 295 Branchbran 0.0% 0 / 92 Conditioncond 0.0% 0 / 75 MC/DCmcdc 0.0% 0 / 37 Subroutinesub 0.0% 0 / 38 Podpod 0.0% 0 / 20 totaltotal 0.0% 0 / 557 CC 47 SCAR 38.5
CC
47
Cov
0%
format_from_file
182.0
check_format_permissions
72.0
files_are_complete
56.0
CRAP 47.2
1 package Bio::BLAST::Database;
2 BEGIN {
3 $Bio::BLAST::Database::AUTHORITY = 'cpan:RBUELS';
4 }
5 BEGIN {
6 $Bio::BLAST::Database::VERSION = '0.4';
7 }
8 # ABSTRACT: work with formatted BLAST databases
9  
10 use strict;
11 use warnings;
12  
13 use POSIX;
14  
15 use IO::Pipe;
16 use IPC::Cmd qw/ can_run /;
17  
18 use Carp;
19 use Memoize;
20  
21 use File::Basename;
22 use File::Copy;
23 use File::Path;
24 use File::Slurp qw/slurp/;
25 use File::Spec::Functions qw/ splitdir catdir devnull /;
26  
27 use IPC::System::Simple 'systemx';
28  
29 use List::Util qw/ min max /;
30 use List::MoreUtils qw/ all any /;
31  
32 use Bio::BLAST::Database::Seq;
33  
34 use namespace::clean;
35  
36  
37 use base qw/ Class::Accessor::Fast /;
38  
39  
40  
41 sub new { croak "use open(), not new()" }
42  
43 sub open {
44 my $class = shift;
45 #validate the args
46 @_ % 2 and croak 'invalid args to open()';
47 my %args = @_;
48 my %valid_keys = map {$_ => 1} qw( full_file_basename type write create_dirs );
49 $valid_keys{$_} or croak "invalid param '$_' passed to open()" for keys %args;
50  
51 my $self = $class->SUPER::new(\%args);
52  
53 $self->full_file_basename or croak 'must provide a full_file_basename';
54  
55 unless( $self->type ) {
56 $self->type( $self->_guess_type )
57 or croak 'type not provided, and could not guess it';
58 }
59  
60 if( $self->write ) {
61 $self->create_dirs || -d dirname( $self->full_file_basename )
62 or croak 'either directory must exist, or create_dirs must be set true';
63  
64 my $perm_error = $self->check_format_permissions;
65 croak $perm_error if $perm_error;
66 }
67  
68 # set some of our attrs from the existing files
69 $self->_read_fastacmd_info;
70  
71 if( $self->write ) {
72 return $self;
73 } else {
74 # open succeeds if all the files are there
75 return $self if $self->files_are_complete;
76  
77 #carp "cannot open for reading, not a complete set of files:\n",
78 # map " - $_\n", $self->list_files;
79 return;
80 }
81 }
82  
83  
84 __PACKAGE__->mk_accessors('full_file_basename');
85  
86  
87 __PACKAGE__->mk_accessors('create_dirs');
88  
89  
90 __PACKAGE__->mk_accessors('write');
91  
92  
93 __PACKAGE__->mk_accessors('title');
94  
95  
96 sub indexed_seqs { #< indexed_seqs is read-only externally
97 my ($self,@args) = @_;
98 croak 'indexed_seqs() is read-only' if @args;
99 shift->_indexed_seqs;
100 }
101 __PACKAGE__->mk_accessors('_indexed_seqs');
102  
103  
104 sub type {
105 my $self = shift;
106  
107 if( @_ ) {
108 my $type = shift;
109 !defined $type || $type eq 'nucleotide' || $type eq 'protein'
110 or croak "invalid type '$type'";
111 $self->{type} = $type;
112 }
113  
114 return $self->{type};
115 }
116  
117  
118  
119 sub to_fasta {
120 my ($self) = @_;
121  
122 $self->_check_external_tools;
123  
124 my $pipe = IO::Pipe->new;
125 if(my $pid = fork) {
126 $pipe->reader;
127 return $pipe;
128 } elsif(defined $pid) {
129 $pipe->writer;
130  
131 #figure out the right -D flag to use, depending on fastacmd version
132 my $d = do {
133 my ($version) = `fastacmd --help` =~ /\s+fastacmd\s+([\d\.]+)/;
134 if( _ver_cmp($version,'2.2.14') >= 0) {
135 '1'
136 }
137 else {
138 'T'
139 }
140 };
141  
142 my $cmd = "fastacmd -d ".$self->full_file_basename." -D $d |";
143 CORE::open my $fh, "$cmd"
144 or die "Could not run $cmd: $!\n";
145 while (<$fh>) {
146 if(/^>/) {
147 #remove renamed idents for genbank-accessioned databases
148 s/^>gnl\|\S+\s+/>/;
149 #remove renamed idents for local-accessioned databases
150 s/^>lcl\|/>/;
151 }
152 print $pipe $_;
153 }
154 close $pipe;
155 POSIX::_exit(0);
156 } else {
157 die "could not fork: $!";
158 }
159 }
160 sub _ver_cmp { #compares two version numbers like '2.2.10' and '2.2.14'
161 my ($v1,$v2) = @_;
162 my @v1 = split /\./,$v1;
163 my @v2 = split /\./,$v2;
164 for(my $i=0;$i<@v2||$i<@v1;$i++) {
165 my $m1 = $v1[$i] || 0;
166 my $m2 = $v2[$i] || 0;
167 my $cmp = $m1 <=> $m2;
168 return $cmp if $cmp;
169 }
170 return 0;
171 }
172  
173 memoize('_check_external_tools');
174 sub _check_external_tools {
175  
176 my @missing;
177 for my $tool ( qw/ formatdb fastacmd / ) {
178 unless( can_run( $tool ) ) {
179 push @missing, "External tool `$tool` not found in path. Please install it.\n";
180 }
181 }
182  
183 croak @missing, "Please install missing tools before using ".__PACKAGE__.".\n"
184 if @missing;
185  
186 return;
187 }
188  
189  
190 sub format_from_file {
191 my ($self,%args) = @_;
192  
193 #validate arg keys
194 my %valid_keys = map {$_ => 1} qw/seqfile title indexed_seqs/;
195 $valid_keys{$_} or croak "invalid arg '$_'" foreach keys %args;
196  
197 my $seqfile = $args{seqfile}
198 or croak 'must provide seqfile';
199 my $title = $args{title};
200  
201 $self->_check_external_tools;
202  
203 #check whether the file looks like it's a fasta file
204 CORE::open my $seqfh, '<', $seqfile
205 or croak "Could not open '$seqfile' for reading: $!";
206 while(<$seqfh>) {
207 next unless /\S/; #go to first non-whitespace line
208 croak "$seqfile does not seem to be a valid FASTA file (got line: $_)"
209 unless /^\s*>\s*\S+/;
210 last;
211 }
212 close $seqfh;
213  
214 unless( $self->write ) {
215 if( my @files = $self->list_files ) {
216 croak "cannot format from file, files are in the way:\n",map " - $_\n",$self->list_files;
217 }
218 }
219  
220 #now run formatdb, formatting into files with a -blast-db-new
221 #appended to the filebase, so the old databases are still available
222 #while the format is running
223 my $ffbn = $self->full_file_basename;
224 my $new_ffbn = "$ffbn-blast-db-new";
225 my (undef,$ffbn_subdir,undef) = fileparse($ffbn);
226 #make sure the destination directories exist. Create them if not.
227 -d $ffbn_subdir or $self->create_dirs && mkpath([$ffbn_subdir])
228 or croak "Could not make path '$ffbn_subdir': $!\n";
229 unless( -d $ffbn_subdir ) {
230 croak $self->create_dirs ? "Could not create dir '$ffbn_subdir'"
231 : "Directory '$ffbn_subdir' does not exist, and create_dirs not set\n";
232 }
233 -w $ffbn_subdir or croak "Directory '$ffbn_subdir' is not writable\n";
234  
235 systemx( 'formatdb',
236 -i => $seqfile,
237 -n => $new_ffbn,
238 ($title ? (-t => $title) : ()),
239 -l => devnull(),
240 -o => $args{indexed_seqs} ? 'T' : 'F',
241 -p => $self->type eq 'protein' ? 'T' : 'F',
242 );
243  
244 #now if it made an alias file, fix it up to remove the -blast-db-new
245 #and the absolute paths, so that when we move it into place, it works
246 if( my $aliasfile = do {
247 my %exts = ( protein => '.pal', nucleotide => '.nal');
248 my $n = $new_ffbn.$exts{$self->type};
249 (-f $n) ? $n : undef;
250 }
251 ) {
252 my $aliases = slurp($aliasfile);
253 $aliases =~ s/-blast-db-new//g; #remove the new extension
254 $aliases =~ s/$ffbn_subdir\/*//g; #remove absolute paths
255 CORE::open my $a_fh, '>', $aliasfile or confess "Could not open $aliasfile for writing";
256 print $a_fh $aliases;
257 #closing not necessary for indirect filehandles in lexical variables
258 }
259  
260 #list of files we will be replacing
261 my @oldfiles = _list_files($ffbn,$self->type);
262  
263 #move the newly formatted files (almost) seamlessly into place
264 foreach my $newfile ( sort (_list_files($new_ffbn,$self->type)) ) {
265 my $dest = $newfile;
266 $dest =~ s/-blast-db-new\./\./;
267  
268 #move it into the right place
269 move( $newfile => $dest );
270  
271 #remove this file from the old files array if it's there,
272 #since it has just been overwritten
273 @oldfiles = grep { $_ ne $dest } @oldfiles;
274 }
275  
276 #delete any old files that were not overwritten
277 if(@oldfiles) {
278 unlink @oldfiles;
279 carp "WARNING: these files for database ".$self->full_file_basename." are no longer used and have been removed:\n",map {"-$_\n"} @oldfiles;
280 }
281  
282  
283 #and now reread our data from the new database
284 $self->_read_fastacmd_info;
285 }
286  
287  
288 sub file_modtime {
289 my $this = shift;
290 my ($basename,$ext) = $this->full_file_basename;
291 my $db_mtime = min( map { (stat($_))[9] } $this->list_files );
292 return $db_mtime;
293 }
294  
295  
296  
297  
298 __PACKAGE__->mk_accessors('format_time');
299  
300  
301  
302 sub check_format_permissions {
303 my ($self) = @_;
304 my $ffbn = $self->full_file_basename;
305  
306 my $err_str = '';
307  
308 #check the dir
309 my $dir = dirname($ffbn);
310 unless( $self->create_dirs ) {
311 unless( -d $dir ) {
312 $err_str .= "Directory '$dir' does not exist\n";
313 }
314 elsif( ! -w $dir ) {
315 $err_str .= "Directory $dir exists, but is not writable\n";
316 }
317 } else {
318 my @dirs = splitdir($dir);
319 #use Data::Dumper;
320 #die Dumper \@dirs;
321 pop @dirs while @dirs && ! -d catdir(@dirs);
322 my $d = catdir(@dirs);
323 if( ! @dirs ) {
324 $err_str .= "Entire directory tree for '$dir' does not exist!\n";
325 }
326 elsif(! -w $d ) {
327 $err_str .= "Directory $d is not writable, cannot make dirs\n";
328 }
329 }
330  
331  
332 #check writability of any files that are already there
333 my @files = $self->list_files();
334 foreach (@files) {
335 if( -f && !-w ) {
336 $err_str .= "Blast DB component file $_ exists, but is not overwritable\n";
337 }
338 }
339 return $err_str if $err_str;
340 return;
341 }
342  
343  
344 sub is_split {
345 my ($self) = @_;
346 my $ffbn = $self->full_file_basename;
347 return 1 if grep /^$ffbn\.\d{2,3}\.[np]\w\w$/,$self->list_files;
348 return 0;
349 }
350  
351  
352 sub files_are_complete {
353 my ($self) = @_;
354  
355 #list of files belonging to this db
356 my @files = $self->list_files;
357  
358 #certainly not complete if fewer than 3 files
359 return 0 unless @files >= 3;
360  
361 #assemble list of necessary extensions
362 my @necessary_extensions = (qw/sq hr in/, #base database files
363 #add seqid indexes if called for
364 $self->indexed_seqs ? qw/sd si/ : (),
365 );
366  
367 #add protein/nucleotide prefix to extensions
368 my $norp = $self->type eq 'protein' ? '.p' : '.n';
369 $_ = $norp.$_ foreach @necessary_extensions;
370  
371 #deal with large, split databases
372 if( $self->is_split ) {
373 #if the database is split, add all of the fragment numbers to
374 #the extensions we have to have
375  
376 #maximum index number of all fragments present
377 my $max_frag_num = 0 + max( map { /\.(\d{2,3})\.[np]\w\w$/ ? $1 : 0 } @files);
378  
379 #make extensions with all of the necessary fragment numbers
380 @necessary_extensions = map { my $ext = $_;
381 map {sprintf(".%02d$ext",$_)} (0..$max_frag_num)
382 } @necessary_extensions;
383  
384 #also remember that we have to have an alias file for split dbs
385 push @necessary_extensions, $norp.'al';
386 }
387  
388 #now that we have our list of all the file extensions we need to have,
389 #check if they are actually there
390 my $ffbn = $self->full_file_basename;
391 return all {
392 my $ext = $_;
393 (grep {$_ eq "$ffbn$ext"} @files) ? 1 : 0
394 } @necessary_extensions;
395 }
396  
397  
398 sub list_files {
399 my $self = shift;
400 croak "cannot list files without knowing the database type" unless $self->type;
401 _list_files($self->full_file_basename, $self->type);
402 }
403 #our internal version of this function just takes a full file basename,
404 #and a db type, and returns all the files that go with that database
405 sub _list_files {
406 my ($ffbn,$type) = @_;
407  
408 #file extensions for each type of blast database
409 my %valid_extensions = ( protein => [qw/.psq .phr .pin .psd .psi .pal .pnd .pni/],
410 nucleotide => [qw/.nsq .nhr .nin .nsd .nsi .nal .nnd .nni/],
411 );
412  
413 #file extensions for _this_ database
414 $valid_extensions{$type} or confess 'invalid type '.$type;
415 my @search_extensions = @{$valid_extensions{$type}};
416  
417 #this gives us all files which have our basename, and one of the right search extensions
418 my @myfiles =
419 grep {
420 my $file = $_;
421 grep {$file =~ /^$ffbn(\.\d{2})?$_$/} @search_extensions
422 } glob("$ffbn*");
423  
424 for (@myfiles) { -f or confess 'sanity check failed' };
425  
426 return @myfiles;
427 }
428  
429  
430 __PACKAGE__->mk_accessors('sequences_count');
431  
432  
433  
434 sub get_sequence {
435 my ($self, $seqname) = @_;
436  
437 croak "cannot call get_sequence on an incomplete database!"
438 unless $self->files_are_complete;
439  
440 croak "cannot call get_sequence on a database that has not been indexed for retrieval!"
441 unless $self->indexed_seqs;
442  
443 return Bio::BLAST::Database::Seq->new(
444 -bdb => $self,
445 -id => $seqname,
446 );
447 }
448  
449 # internal function to set the title, sequence count, type,
450 # format_time, and indexed_seqs from the set of files on disk and from
451 # the output of fastacmd
452 sub _read_fastacmd_info {
453 my ($self) = @_;
454  
455 my @files = $self->list_files
456 or return;
457  
458 $self->_check_external_tools;
459  
460 my $ffbn = $self->full_file_basename;
461 my $cmd = "fastacmd -d $ffbn -I";
462 my $fastacmd = `$cmd 2>&1`;
463 #warn "$fastacmd";
464  
465 my ($title) = $fastacmd =~ /Database:\s*([\s\S]+)sequences/
466 or die "could not parse output of fastacmd (0):\n$fastacmd";
467 $title =~ s/\s*[\d,]+\s*$//;
468  
469 my ($seq_cnt) = $fastacmd =~ /([\d,]+)\s*sequences/
470 or die "could not parse output of fastacmd (1):\n$fastacmd";
471 $seq_cnt =~ s/,//g;
472  
473 my ($datestr) =
474 $fastacmd =~ m(
475 Date: \s* ( \w [\S\ ]+ \w )
476 \s+
477 Version:
478 )x
479 or die "could not parse output of fastacmd (2):\n$fastacmd";
480  
481  
482 my $indexed = (any {/sd$/} @files) && (any {/si$/} @files);
483  
484 ### set our data
485 $self->type( $self->_guess_type )
486 or confess 'could not determine db type';
487  
488 ### type: $self->type
489  
490 $self->format_time( _parse_datestr($datestr) ); #< will die on failure
491 $title =~ s/\s+$//;
492 $self->title( $title );
493 $self->_indexed_seqs( $indexed );
494 $self->sequences_count( $seq_cnt );
495 }
496 sub _guess_type {
497 my ($self) = @_;
498 my $saved_type = $self->type;
499  
500 foreach my $guess (qw( protein nucleotide )) {
501 $self->type( $guess );
502 if( $self->files_are_complete ) {
503 $self->type( $saved_type );
504 return $guess;
505 }
506 }
507  
508 $self->type( $saved_type );
509 return;
510 }
511 sub _parse_datestr {
512 my ($datestr) = @_;
513 my @split = split /\W+/,$datestr;
514 my ($mon,$d,$y,$h,$min,$ampm) = @split
515 or die "could not parse data string '$datestr'";
516  
517 # warn "got $mon,$d,$y,$h,$min,$ampm\n";
518 my %months = do{ my $n = 0; map { $_ => $n++ } qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/};
519 exists $months{$mon} or return;
520 $mon = $months{$mon};
521 $h = 0 if $h == 12;
522 $h += 12 if lc($ampm) eq 'pm';
523 #warn "mktime $min,$h,$d,$mon,".($y-1900)."\n";
524 my $time = mktime(0,$min,$h,$d,$mon,$y-1900,0,0,-1);
525 # warn "$datestr => ".ctime($time)."\n";
526 return $time;
527 }
528  
529 ###
530 1;#do not remove
531 ###
532  
533 __END__