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