| 1 | package Bio::Gonzales::Search::IO::BLAST; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | use Carp; | ||
| 6 | |||
| 7 | use 5.010; | ||
| 8 | |||
| 9 | use base 'Exporter'; | ||
| 10 | use Bio::Gonzales::Util::File qw/basename regex_glob is_archive/; | ||
| 11 | use List::Util qw/min max/; | ||
| 12 | use File::Temp qw/tempdir tempfile/; | ||
| 13 | use Bio::Gonzales::Seq::IO qw(faiterate faspew); | ||
| 14 | use Capture::Tiny qw/capture_merged/; | ||
| 15 | use Path::Tiny; | ||
| 16 | |||
| 17 | use Params::Validate qw/validate/; | ||
| 18 | our ( @EXPORT, @EXPORT_OK, %EXPORT_TAGS ); | ||
| 19 | our $VERSION = '0.058'; # VERSION | ||
| 20 | |||
| 21 | @EXPORT = qw(); | ||
| 22 | %EXPORT_TAGS = (); | ||
| 23 | @EXPORT_OK = qw(makeblastdb); | ||
| 24 | |||
| 25 | sub makeblastdb { | ||
| 26 | my %c = validate( | ||
| 27 | @_, | ||
| 28 | { | ||
| 29 | seq_file => 1, | ||
| 30 | title => 0, | ||
| 31 | parse_seqids => { default => 0 }, | ||
| 32 | hash_index => { default => 1 }, | ||
| 33 | alphabet => 1, | ||
| 34 | db_prefix => 0, | ||
| 35 | wd => 0, | ||
| 36 | } | ||
| 37 | ); | ||
| 38 | |||
| 39 | $c{wd} //= './'; | ||
| 40 | my $unlink; | ||
| 41 | my $seqf = $c{seq_file}; | ||
| 42 | my $basename = basename( $c{seq_file} ); | ||
| 43 | if ( is_archive($seqf) ) { | ||
| 44 | say STDERR "$seqf is an archive, extracting first ..."; | ||
| 45 | my $fait = faiterate($seqf); | ||
| 46 | my ( $fh, $fn ) = tempfile('tempXXXXX', DIR => $c{wd} ); | ||
| 47 | while ( my $s = $fait->() ) { | ||
| 48 | faspew( $fh, $s ); | ||
| 49 | } | ||
| 50 | $fh->close; | ||
| 51 | $unlink = 1; | ||
| 52 | $seqf = $fn; | ||
| 53 | say STDERR "extraction finished. making blast DB"; | ||
| 54 | $basename = basename($basename); # remove 2nd extension, e.g. a.fa.gz -> a.fa -> a | ||
| 55 | } | ||
| 56 | |||
| 57 | my @cmd = 'makeblastdb'; | ||
| 58 | push @cmd, '-in', $seqf; | ||
| 59 | push @cmd, '-title', $basename; | ||
| 60 | push @cmd, '-parse_seqids' if ( $c{parse_seqids} ); | ||
| 61 | push @cmd, '-hash_index' if ( $c{hash_index} ); | ||
| 62 | |||
| 63 | if ( $c{alphabet} =~ /^(?:a|p)/ ) { push @cmd, '-dbtype', 'prot' } | ||
| 64 | elsif ( $c{alphabet} =~ /^(?:n|d|r)/ ) { push @cmd, '-dbtype', 'nucl' } | ||
| 65 | |||
| 66 | $c{db_prefix} //= $basename; | ||
| 67 | $c{db_prefix} .= '.bdb'; | ||
| 68 | my $db_name = File::Spec->catfile( $c{wd}, $c{db_prefix} ); | ||
| 69 | push @cmd, '-out', $db_name; | ||
| 70 | |||
| 71 | my @existing_db_files = regex_glob( $c{wd}, qr/^\Q$c{db_prefix}.\En\w\w$/ ); | ||
| 72 | if ( @existing_db_files > 0 ) { | ||
| 73 | my $oldest_db_file_age = min( map { ( stat $_ )[9] } @existing_db_files ); | ||
| 74 | my $seq_file_age = ( stat $c{seq_file} )[9]; | ||
| 75 | |||
| 76 | unless ( $seq_file_age > $oldest_db_file_age ) { | ||
| 77 | #sequence file is older than db, so do noting | ||
| 78 | say STDERR "sequence file $c{seq_file} is older than $db_name"; | ||
| 79 | say STDERR "Skipping blast db creation"; | ||
| 80 | return $db_name; | ||
| 81 | } | ||
| 82 | } | ||
| 83 | |||
| 84 | say STDERR "Creating blast db:"; | ||
| 85 | say STDERR join " ", @cmd; | ||
| 86 | |||
| 87 | my $merged = capture_merged { system @cmd }; | ||
| 88 | |||
| 89 | say STDERR $merged; | ||
| 90 | |||
| 91 | |||
| 92 | unlink $seqf if ($unlink); | ||
| 93 | return $db_name; | ||
| 94 | } | ||
| 95 | |||
| 96 | 1; | ||
| 97 | __END__ | ||
| 98 | |||
| 99 | =head1 NAME | ||
| 100 | |||
| 101 | Bio::Gonzales::Search::IO::BLAST | ||
| 102 | |||
| 103 | =head1 SYNOPSIS | ||
| 104 | |||
| 105 | Bio::Gonzales::Search::IO::BLAST qw(makeblastdb instantblast) | ||
| 106 | |||
| 107 | =head1 DESCRIPTION | ||
| 108 | |||
| 109 | =head1 OPTIONS | ||
| 110 | |||
| 111 | =head1 SUBROUTINES | ||
| 112 | |||
| 113 | =over 4 | ||
| 114 | |||
| 115 | =item B<< $db_location = makeblastdb(\%config) >> | ||
| 116 | |||
| 117 | Creates a blast database with the config options supplied. Config options are: | ||
| 118 | |||
| 119 | %config = ( | ||
| 120 | 'seq_file!' => undef, | ||
| 121 | title => 'basename of seq_file', | ||
| 122 | parse_seqids => 1, | ||
| 123 | hash_index => 1, | ||
| 124 | 'alphabet!' => 'n(ucleotide)? || p(rotein)? || d(na)? || a(a)?', | ||
| 125 | db_prefix => 'basename of seq_file.bdb', | ||
| 126 | wd => './', | ||
| 127 | ); | ||
| 128 | |||
| 129 | Options with C<!> are required. | ||
| 130 | |||
| 131 | =back | ||
| 132 | |||
| 133 | =head1 SEE ALSO | ||
| 134 | |||
| 135 | =head1 AUTHOR | ||
| 136 | |||
| 137 | jw bargsten, C<< <joachim.bargsten at wur.nl> >> | ||
| 138 | |||
| 139 | =cut |