| 1 | #!/usr/bin/env perl | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | |||
| 6 | use Data::Dumper; | ||
| 7 | use Carp; | ||
| 8 | |||
| 9 | use 5.010; | ||
| 10 | |||
| 11 | use Getopt::Long::Descriptive; | ||
| 12 | use Bio::Gonzales::Search::IO::BLAST qw/makeblastdb/; | ||
| 13 | use File::Temp qw/tempdir tempfile/; | ||
| 14 | |||
| 15 | use constant { | ||
| 16 | BLAST_CMD => 0, | ||
| 17 | ALPHABET => 1 | ||
| 18 | }; | ||
| 19 | |||
| 20 | our %blast_type = ( | ||
| 21 | n => [ 'blastn', 'n' ], | ||
| 22 | p => [ 'blastp', 'p' ], | ||
| 23 | tn => [ 'tblastn', 'n' ], | ||
| 24 | tx => [ 'tblastx', 'n' ], | ||
| 25 | x => [ 'blastx', 'p' ] | ||
| 26 | ); | ||
| 27 | |||
| 28 | my ( $opt, $usage ) = describe_options( | ||
| 29 | "%c %o <query> <subject>\n" . "%c %o --db_only <subject>\n", | ||
| 30 | [], | ||
| 31 | [ 'verbose|v', "print extra stuff" ], | ||
| 32 | [ 'db_only' => 'create only the database, do not run any blast cmd' ], | ||
| 33 | [ 'db_prefix=s' => 'set this as db_prefix for the blast db name' ], | ||
| 34 | [ 'db_hashidx' => 'pass hash_index cmd to makeblastdb' ], | ||
| 35 | [ 'db_parseids' => 'pass parse_seqids to makeblastdb' ], | ||
| 36 | [ | ||
| 37 | "type" => hidden => { | ||
| 38 | one_of => [ | ||
| 39 | [ 'n', 'use blastn' ], | ||
| 40 | [ 'p', 'use blastp' ], | ||
| 41 | [ 'tn', 'use tblastn' ], | ||
| 42 | [ 'tx', 'use tblastx' ], | ||
| 43 | [ 'x', 'use blastx' ], | ||
| 44 | ], | ||
| 45 | required => 1, | ||
| 46 | |||
| 47 | } | ||
| 48 | ], | ||
| 49 | [ 'use_db=s' => 'use preexisting database' ], | ||
| 50 | [ 'opt|o=s@', "add these options to the blast search, e.g. --opt '-evalue 10e-5' or -o '-outfmt 7'" ], | ||
| 51 | [ 'help', "print usage message and exit" ], | ||
| 52 | [ 'wd=s', "set working dir, use temp dir as default" ], | ||
| 53 | ); | ||
| 54 | |||
| 55 | print( $usage->text ), exit if $opt->help; | ||
| 56 | |||
| 57 | my $dir = $opt->wd // tempdir( CLEANUP => 1 ); | ||
| 58 | |||
| 59 | my ( $qfile, $sfile ) = @ARGV; | ||
| 60 | |||
| 61 | if ( $opt->use_db ) { | ||
| 62 | say STDERR "Using existing db " . $opt->use_db; | ||
| 63 | |||
| 64 | } elsif ( $opt->db_only && $qfile && -f $qfile ) { | ||
| 65 | $sfile = $qfile; | ||
| 66 | } else { | ||
| 67 | map { confess $_ ? "$_ does not exist" : "no files given" unless ( $_ && -f $_ ); } ( $qfile, $sfile ); | ||
| 68 | } | ||
| 69 | |||
| 70 | my $db_location; | ||
| 71 | if ( $opt->use_db ) { | ||
| 72 | $db_location = $opt->use_db; | ||
| 73 | } else { | ||
| 74 | my $opts = { | ||
| 75 | seq_file => $sfile, | ||
| 76 | alphabet => $blast_type{ $opt->type }[ALPHABET], | ||
| 77 | wd => $dir, | ||
| 78 | hash_index => $opt->db_hashidx, | ||
| 79 | parse_seqids => $opt->db_parseids, | ||
| 80 | }; | ||
| 81 | $opts->{db_prefix} = $opt->db_prefix if ( $opt->db_prefix ); | ||
| 82 | $db_location = makeblastdb($opts); | ||
| 83 | } | ||
| 84 | |||
| 85 | exit if ( $opt->db_only ); | ||
| 86 | |||
| 87 | my @cmd; | ||
| 88 | push @cmd, $blast_type{ $opt->type }[BLAST_CMD], '-db', $db_location, '-query', $qfile; | ||
| 89 | |||
| 90 | push @cmd, '-verbose' if ( $opt->verbose ); | ||
| 91 | push @cmd, map { split /\s+/, $_, 2 } @{ $opt->opt } if ( $opt->opt ); | ||
| 92 | say STDERR join( " ", @cmd ); | ||
| 93 | system(@cmd); |