/usr/share/perl5/Bio/Tools/Run/StandAloneBlastPlus.pm is in libbio-perl-run-perl 1.7.1-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 | #
# BioPerl module for Bio::Tools::Run::StandAloneBlastPlus
#
# Please direct questions and support issues to <bioperl-l@bioperl.org>
#
# Cared for by Mark A. Jensen <maj -at- fortinbras -dot- us>
#
# Copyright Mark A. Jensen
#
# You may distribute this module under the same terms as perl itself
# POD documentation - main docs before the code
=head1 NAME
Bio::Tools::Run::StandAloneBlastPlus - Compute with NCBI's blast+ suite *ALPHA*
=head1 SYNOPSIS
B<NOTE>: This module is related to the
L<Bio::Tools::Run::StandAloneBlast> system in name (and inspiration)
only. You must use this module directly.
# existing blastdb:
$fac = Bio::Tools::Run::StandAloneBlastPlus->new(
-db_name => 'mydb'
);
# create blastdb from fasta file and attach
$fac = Bio::Tools::Run::StandAloneBlastPlus->new(
-db_name => 'mydb',
-db_data => 'myseqs.fas',
-create => 1
);
# create blastdb from BioPerl sequence collection objects
$alnio = Bio::AlignIO->new( -file => 'alignment.msf' );
$fac = Bio::Tools::Run::StandAloneBlastPlus->new(
-db_name => 'mydb',
-db_data => $alnio,
-create => 1
);
@seqs = $alnio->next_aln->each_seq;
$fac = Bio::Tools::Run::StandAloneBlastPlus->new(
-db_name => 'mydb',
-db_data => \@seqs,
-create => 1
);
# create database with masks
$fac = Bio::Tools::Run::StandAloneBlastPlus->new(
-db_name => 'my_masked_db',
-db_data => 'myseqs.fas',
-masker => 'dustmasker',
-mask_data => 'maskseqs.fas',
-create => 1
);
# create a mask datafile separately
$mask_file = $fac->make_mask(
-data => 'maskseqs.fas',
-masker => 'dustmasker'
);
# query database for metadata
$info_hash = $fac->db_info;
$num_seq = $fac->db_num_sequences;
@mask_metadata = @{ $fac->db_filter_algorithms };
# perform blast methods
$result = $fac->tblastn( -query => $seqio );
# see Bio::Tools::Run::StandAloneBlastPlus::BlastMethods
# for many more details
=head1 DESCRIPTION
B<NOTE:> This module requires BLAST+ v. 2.2.24+ and higher. Until the API
stabilizes for BLAST+, consider this module highly experimental.
This module along with
L<Bio::Tools::Run::StandAloneBlastPlus::BlastMethods> allows the user
to perform BLAST functions using the external program suite C<blast+>
(available at
L<ftp://ftp.ncbi.nlm.nih.gov/blast/executables/blast+/LATEST/>), using
BioPerl objects and L<Bio::SearchIO> facilities. This wrapper can
prepare BLAST databases as well as run BLAST searches. It can also be
used to run C<blast+> programs independently.
This module encapsulates object construction and production of
databases and masks. Blast analysis methods (C<blastp, psiblast>,
etc>) are contained in
L<Bio::Tools::Run::StandAloneBlastPlus::BlastMethods>.
=head1 USAGE
The basic mantra is to (1) create a BlastPlus factory using the
C<new()> constructor, and (2) perform BLAST analyses by calling the
desired BLAST program by name off the factory object. The blast
database itself and any masking data are attached to the factory
object (step 1). Query sequences and any parameters associated with
particular programs are provided to the blast method call (step 2),
and are run against the attached database.
=head2 Factory construction/initialization
The factory needs to be told where the blast+ programs live. The
C<BLASTPLUSDIR> environment variable will be checked for the default
executable directory. The program directory can be set for individual
factory instances with the C<PROG_DIR> parameter. All the blast+
programs must be accessible from that directory (i.e., as executable
files or symlinks).
Either the database or BLAST subject data must be specified at object
construction. Databases can be pre-existing formatted BLAST dbs, or
can be built directly from fasta sequence files or BioPerl sequence
object collections of several kinds. The key constructor parameters
are C<DB_NAME>, C<DB_DATA>, C<DB_DIR>.
To specify a pre-existing BLAST database, use C<DB_NAME> alone:
$fac = Bio::Tools::Run::StandAloneBlastPlus->new(
-DB_NAME => 'mydb'
);
The directory can be specified along with the basename, or separately
with C<DB_DIR>:
$fac = Bio::Tools::Run::StandAloneBlastPlus->new(
-DB_NAME => '~/home/blast/mydb'
);
#same as
$fac = Bio::Tools::Run::StandAloneBlastPlus->new(
-DB_NAME => 'mydb', -DB_DIR => '~/home/blast'
);
To create a BLAST database de novo, see L</Creating a BLAST database>.
If you wish to apply pre-existing mask data (i.e., the final ASN1
output from one of the blast+ masker programs), to the database before
querying, specify it with C<MASK_FILE>:
$fac = Bio::Tools::Run::StandAloneBlastPlus->new(
-DB_NAME => 'mydb', -MASK_FILE => 'mymaskdata.asn'
);
=head2 Creating a BLAST database
There are several options for creating the database de novo using
attached data, both before and after factory construction. If a
temporary database (one that can be deleted by the C<cleanup()>
method) is desired, leave out the C<-db_name> parameter. If
C<-db_name> is specified, the database will be preserved with the
basename specified.
Use C<-create => 1> to create a new database (otherwise the factory
will look for an existing database). Use C<-overwrite => 1> to create
and overwrite an existing database.
Note that the database is not created immediately on factory
construction. It will be created if necessary on the first use of a
factory BLAST method, or you can force database creation by executing
$fac->make_db();
=over
=item * Specify data during construction
With a FASTA file:
$fac = Bio::Tools::Run::StandAloneBlastPlus->new(
-db_name => 'mydb',
-db_data => 'myseqs.fas',
-create => 1
);
With another BioPerl object collection:
$alnio = Bio::AlignIO->new( -file => 'alignment.msf' );
$fac = Bio::Tools::Run::StandAloneBlastPlus->new(
-db_name => 'mydb',
-db_data => $alnio,
-create => 1
);
@seqs = $alnio->next_aln->each_seq;
$fac = Bio::Tools::Run::StandAloneBlastPlus->new(
-db_name => 'mydb',
-db_data => \@seqs,
-create => 1
);
Other collections (e.g., L<Bio::SeqIO>) are valid. If a certain type
does not work, please submit an enhancement request.
To create temporary databases, leave out the C<-db_name>, e.g.
$fac = Bio::Tools::Run::StandAloneBlastPlus->new(
-db_data => 'myseqs.fas',
-create => 1
);
To get the tempfile basename, do:
$dbname = $fac->db;
=item * Specify data post-construction
Use the explicit attribute setters:
$fac = Bio::Tools::Run::StandAloneBlastPlus->new(
-create => 1
);
$fac->set_db_data('myseqs.fas');
$fac->make_db;
=back
=head2 Creating and using mask data
The blast+ mask utilities C<windowmasker>, C<segmasker>, and
C<dustmasker> are available. Masking can be rolled into database
creation, or can be executed later. If your mask data is already
created and in ASN1 format, set the C<-mask_file> attribute on
construction (see L</Factory constuction/initialization>).
To create a mask from raw data or an existing database and apply the
mask upon database creation, construct the factory like so:
$fac = Bio::Tools::Run::StandAloneBlastPlus->new(
-db_name => 'my_masked_db',
-db_data => 'myseqs.fas',
-masker => 'dustmasker',
-mask_data => 'maskseqs.fas',
-create => 1
);
The masked database will be created during C<make_db>.
The C<-mask_data> parameter can be a FASTA filename or any BioPerl
sequence object collection. If the datatype ('nucl' or 'prot') of the
mask data is not compatible with the selected masker, an exception
will be thrown with a message to that effect.
To create a mask ASN1 file that can be used in the C<-mask_file>
parameter separately from the attached database, use the
C<make_mask()> method directly:
$mask_file = $fac->make_mask(-data => 'maskseqs.fas',
-masker => 'dustmasker');
# segmasker can use a blastdb as input
$mask_file = $fac->make_mask(-mask_db => 'mydb',
-masker => 'segmasker')
$fac = Bio::Tools::Run::StandAloneBlastPlus->new(
-db_name => 'my_masked_db',
-db_data => 'myseqs.fas',
-mask_file => $mask_file
-create => 1
);
=head2 Getting database information
To get a hash containing useful metadata on an existing database
(obtained by running C<blastdbcmd -info>, use C<db_info()>:
# get info on the attached database..
$info = $fac->db_info;
# get info on another database
$info = $fac->db_info('~/home/blastdbs/another');
To get a particular info element for the attached database, just call
the element name off the factory:
$num_seqs = $fac->db_num_sequences;
# info on all the masks applied to the db, if any:
@masking_info = @{ $fac->db_filter_algorithms };
=head2 Accessing the L<Bio::Tools::Run::BlastPlus> factory
The blast+ programs are actually executed by a
L<Bio::Tools::Run::BlastPlus> wrapper instance. This instance is
available for peeking and poking in the L<StandAloneBlastPlus>
C<factory()> attribute. For convenience, C<BlastPlus> methods can be
run from the C<StandAloneBlastPlus> object, and are delegated to the
C<factory()> attribute. For example, to get the blast+ program to be
executed, examine either
$fac->factory->command
or
$fac->command
Similarly, the current parameters for the C<BlastPlus> factory are
@parameters = $fac->get_parameters
=head2 Cleaning up temp files
Temporary analysis files produced under a single factory instances can
be unlinked by running
$fac->cleanup;
Tempfiles are generally not removed unless this method is explicitly
called. C<cleanup()> only unlinks "registered" files and
databases. All temporary files are automatically registered; in
particular, "anonymous" databases (such as
$fac->Bio::Tools::Run::StandAloneBlastPlus->new(
-db_data => 'myseqs.fas',
-create => 1
);
without a C<-db_name> specification) are registered for cleanup. Any
file or database can be registered with an internal method:
$fac->_register_temp_for_cleanup('testdb');
=head2 Other Goodies
=over
=item
You can check whether a given basename points to a properly formatted
BLAST database by doing
$is_good = $fac->check_db('putative_db');
=item
User parameters can be passed to the underlying blast+ programs (if
you know what you're doing) with C<db_make_args> and C<mask_make_args>:
$fac = Bio::Tools::Run::StandAloneBlastPlus->new(
-db_name => 'customdb',
-db_data => 'myseqs.fas',
-db_make_args => [ '-taxid_map' => 'seq_to_taxa.txt' ],
-masker => 'windowmasker',
-mask_data => 'myseqs.fas',
-mask_make_args => [ '-dust' => 'T' ],
-create => 1
);
=item
You can prevent exceptions from being thrown by failed blast+ program
executions by setting C<no_throw_on_crash>. Examine the error with
C<stderr()>:
$fac->no_throw_on_crash(1);
$fac->make_db;
if ($fac->stderr =~ /Error:/) {
#handle error
...
}
=back
=head1 SEE ALSO
L<Bio::Tools::Run::StandAloneBlastPlus::BlastMethods>,
L<Bio::Tools::Run::BlastPlus>
=head1 FEEDBACK
=head2 Mailing Lists
User feedback is an integral part of the evolution of this and other
Bioperl modules. Send your comments and suggestions preferably to
the Bioperl mailing list. Your participation is much appreciated.
bioperl-l@bioperl.org - General discussion
http://bioperl.org/wiki/Mailing_lists - About the mailing lists
=head2 Support
Please direct usage questions or support issues to the mailing list:
L<bioperl-l@bioperl.org>
rather than to the module maintainer directly. Many experienced and
reponsive experts will be able look at the problem and quickly
address it. Please include a thorough description of the problem
with code and data examples if at all possible.
=head2 Reporting Bugs
Report bugs to the Bioperl bug tracking system to help us keep track
of the bugs and their resolution. Bug reports can be submitted via
the web:
http://redmine.open-bio.org/projects/bioperl/
=head1 AUTHOR - Mark A. Jensen
Email maj -at- fortinbras -dot- us
=head1 CONTRIBUTORS
=head1 APPENDIX
The rest of the documentation details each of the object methods.
Internal methods are usually preceded with a _
=cut
# Let the code begin...
package Bio::Tools::Run::StandAloneBlastPlus;
use strict;
our $AUTOLOAD;
# Object preamble - inherits from Bio::Root::Root
use lib '../../..';
use Bio::Root::Root;
use Bio::SeqIO;
use Bio::Tools::GuessSeqFormat;
use Bio::Tools::Run::StandAloneBlastPlus::BlastMethods;
use File::Temp 0.22;
use IO::String;
use base qw(Bio::Root::Root);
unless ( eval "require Bio::Tools::Run::BlastPlus" ) {
Bio::Root::Root->throw("This module requires 'Bio::Tools::Run::BlastPlus'");
}
my %AVAILABLE_MASKERS = (
'windowmasker' => 'nucl',
'dustmasker' => 'nucl',
'segmasker' => 'prot'
);
# NOTE: After testing all possible output formats, only 'maskinfo_asn1_text'
# is currently working correctly as input for makeblastdb '-mask_data' argument,
# the others return an 'Unknown encoding for mask data' error
my %MASKER_ENCODING = (
'windowmasker' => 'maskinfo_asn1_text',
'dustmasker' => 'maskinfo_asn1_text',
'segmasker' => 'maskinfo_asn1_text'
);
my $bp_class = 'Bio::Tools::Run::BlastPlus';
# what's the desire here?
#
# * factory object (created by new())
# - points to some blast db entity, so all functions run off the
# the factory (except bl2seq?) use the associated db
#
# * create a blast formatted database:
# - specify a file, or an AlignI object
# - store for later, or store in a tempfile to throw away
# - object should store its own database pointer
# - provide masking options based on the maskers provided
#
# * perform database actions via db-oriented blast+ commands
# via the object
#
# * perform blast searches against the database
# - blastx, blastp, blastn, tblastx, tblastn
# - specify Bio::Seq objects or files as queries
# - output the results as a file or as a Bio::Search::Result::BlastResult
# * perform 'special' (i.e., ones I don't know) searches
# - psiblast, megablast, rpsblast, rpstblastn
# some of these are "tasks" under particular programs
# check out psiblast, why special (special 'iteration' handling in
# ...::BlastResult)
# check out rpsblast, megablast
#
# * perform bl2seq
# - return the alignment directly as a convenience, using Bio::Search
# functions
# lazy db formatting: makeblastdb only on first blast request...
# ParameterBaseI delegation : use AUTOLOAD
#
#
=head2 new
Title : new
Usage : my $obj = new Bio::Tools::Run::StandAloneBlastPlus();
Function: Builds a new Bio::Tools::Run::StandAloneBlastPlus object
Returns : an instance of Bio::Tools::Run::StandAloneBlastPlus
Args : named argument (key => value) pairs:
-db : blastdb name
=cut
sub new {
my ($class,@args) = @_;
my $self = $class->SUPER::new(@args);
my ($db_name, $db_data, $db_dir, $db_make_args,
$mask_file, $mask_data, $mask_make_args, $masker,
$create, $overwrite, $is_remote, $prog_dir, $program_dir)
= $self->_rearrange([qw(
DB_NAME
DB_DATA
DB_DIR
DB_MAKE_ARGS
MASK_FILE
MASK_DATA
MASK_MAKE_ARGS
MASKER
CREATE
OVERWRITE
REMOTE
PROG_DIR
PROGRAM_DIR
)], @args);
# parm taint checks
if ($db_name) {
$self->throw("DB name contains invalid characters") unless $db_name =~ m{^[a-z0-9_/:.+-]+$}i;
}
if ( $db_dir ) {
$self->throw("DB directory (DB_DIR) not found") unless (-d $db_dir);
$self->{'_db_dir'} = $db_dir;
}
else {
$self->{'_db_dir'} = '.';
}
$program_dir ||= $prog_dir; # alias
# now handle these systematically (bug #3003)
# allow db_name to include path info
# let db_dir act as root if present and db_name is a relative path
# db property contains the pathless name only
if ($db_name) {
my ($v,$d,$f) = File::Spec->splitpath($db_name);
$self->throw("No DB name at the end of path '$db_name'") unless $f;
$f =~ s/\..*$//; # tolerant of extensions, but ignore them
$self->{_db} = $f;
# now establish db_path property as the internal authority on
# db location...
if ( File::Spec->file_name_is_absolute($db_name) ) {
$self->throw("Path specified in DB name ('$d') does not exist") unless !$d || (-d $d);
$self->{_db_path} = File::Spec->catfile($d,$f);
$self->{_db_dir} = $d;
# ignore $db_dir, give heads-up
$self->warn("DB name is an absolute path; setting db_dir to '".$self->db_dir."'") if $db_dir;
}
else {
$d = File::Spec->catdir($self->db_dir, $d);
$self->throw("Path specified by DB_DIR+DB_NAME ('$d') does not exist") unless !$d || (-d $d);
$self->{_db_path} = File::Spec->catfile($d,$f);
}
}
if ($masker) {
$self->throw("Masker '$masker' not available") unless
grep /^$masker$/, keys %AVAILABLE_MASKERS;
$self->{_masker} = $masker;
}
if ($program_dir) {
$self->throw("Can't find program directory '$program_dir'") unless
-d $program_dir;
$self->{_program_dir} = $program_dir;
}
elsif ($ENV{BLASTPLUSDIR}) {
$self->{_program_dir} = $ENV{BLASTPLUSDIR};
}
$Bio::Tools::Run::BlastPlus::program_dir = $self->{_program_dir} ||
$Bio::Tools::Run::BlastPlus::program_dir;
$self->set_db_make_args( $db_make_args) if ( $db_make_args );
$self->set_mask_make_args( $mask_make_args) if ($mask_make_args);
$self->{'_create'} = $create;
$self->{'_overwrite'} = $overwrite;
$self->{'_is_remote'} = $is_remote;
$self->{'_db_data'} = $db_data;
$self->{'_mask_file'} = $mask_file;
$self->{'_mask_data'} = $mask_data;
# check db
if (defined $self->check_db and $self->check_db == 0 and !$self->is_remote) {
$self->throw("DB '".$self->db."' can't be found. To create, set -create => 1.") unless ($create || $overwrite);
}
if (!$self->db) {
# allow this to pass; catch lazily at make_db...
if (!$self->db_data) {
$self->debug('No database or db data specified. '.
'To create a new database, provide '.
'-db_data => [fasta|\@seqs|$seqio_object]')
}
# no db specified; create temp db
$self->{_create} = 1;
if ($self->db_dir) {
my $fh = File::Temp->new(TEMPLATE => 'DBXXXXX',
DIR => $self->db_dir,
UNLINK => 1);
my ($v,$d,$f) = File::Spec->splitpath($fh->filename);
$self->{_db} = $f;
$self->{_db_path} = $fh->filename;
$self->_register_temp_for_cleanup($self->db_path);
$fh->close;
}
else {
$self->{_db_dir} = File::Temp->newdir('DBDXXXXX');
$self->{_db} = 'DBTEMP';
$self->{_db_path} = File::Spec->catfile($self->db_dir,
$self->db);
}
}
return $self;
}
=head2 db()
Title : db
Usage : $obj->db($newval)
Function: contains the basename of the local blast database
Example :
Returns : value of db (a scalar string)
Args : readonly
=cut
sub db { shift->{_db} }
sub db_name { shift->{_db} }
sub set_db_name { shift->{_db} = shift }
sub db_dir { shift->{_db_dir} }
sub set_db_dir { shift->{_db_dir} = shift }
sub db_path { shift->{_db_path} }
sub db_data { shift->{_db_data} }
sub set_db_data { shift->{_db_data} = shift }
sub db_type { shift->{_db_type} }
sub masker { shift->{_masker} }
sub set_masker { shift->{_masker} = shift }
sub mask_file { shift->{_mask_file} }
sub set_mask_file { shift->{_mask_file} = shift }
sub mask_data { shift->{_mask_data} }
sub set_mask_data { shift->{_mask_data} = shift }
=head2 factory()
Title : factory
Usage : $obj->factory($newval)
Function: attribute containing the Bio::Tools::Run::BlastPlus
factory
Example :
Returns : value of factory (Bio::Tools::Run::BlastPlus object)
Args : readonly
=cut
sub factory { shift->{_factory} }
sub create { shift->{_create} }
sub overwrite { shift->{_overwrite} }
sub is_remote { shift->{_is_remote} }
=head2 program_version()
Title : program_version
Usage : $version = $bedtools_fac->program_version()
Function: Returns the program version (if available)
Returns : string representing location and version of the program
Note : this works around the WrapperBase::version() method conflicting with
the -version parameter for SABlast (good argument for not having
getter/setters for these)
=cut
=head2 package_version()
Title : package_version
Usage : $version = $bedtools_fac->package_version()
Function: Returns the BLAST+ package version (if available)
Returns : string representing BLAST+ package version (may differ from version())
=cut
sub program_version {
my $self = shift;
my $fac = $self->factory;
$fac->program_version(@_) if $fac;
}
sub package_version {
my $self = shift;
my $fac = $self->factory;
$fac->package_version(@_) if $fac;
}
=head1 DB methods
=head2 make_db()
Title : make_db
Usage :
Function: create the blast database (if necessary),
imposing masking if specified
Returns : true on success
Args :
=cut
# should also provide facility for creating subdatabases from
# existing databases (i.e., another format for $data: the name of an
# existing blastdb...)
sub make_db {
my $self = shift;
my @args = @_;
return 1 if ( $self->check_db && !$self->overwrite ); # already there or force make
$self->throw('No database or db data specified. '.
'To create a new database, provide '.
'-db_data => [fasta|\@seqs|$seqio_object]')
unless $self->db_data;
# db_data can be: fasta file, array of seqs, Bio::SeqIO object
my $data = $self->db_data;
$data = $self->_fastize($data);
my $testio = Bio::SeqIO->new(-file=>$data, -format=>'fasta');
$self->{_db_type} = ($testio->next_seq->alphabet =~ /.na/) ? 'nucl' : 'prot';
$testio->close;
my ($v,$d,$name) = File::Spec->splitpath($data);
$name =~ s/\.fas$//;
$self->{_db} ||= $name;
$self->{_db_path} = File::Spec->catfile($self->db_dir,$self->db);
# <#######[
# deal with creating masks here,
# and provide correct parameters to the
# makeblastdb ...
# accomodate $self->db_make_args here -- allow them
# to override defaults, or allow only those args
# that are not specified here?
my $usr_db_args ||= $self->db_make_args;
my %usr_args = @$usr_db_args if $usr_db_args;
my %db_args = (
-in => $data,
-dbtype => $self->db_type,
-out => $self->db_path,
-title => $self->db,
-parse_seqids => 1 # necessary for masking
);
# usr arg override
if (%usr_args) {
$db_args{$_} = $usr_args{$_} for keys %usr_args;
}
# do masking if requested
# if the (masker and mask_data) OR mask_file attributes of this
# object are set, assume that masking is desired
#
if ($self->mask_file) { # the actual masking data is provided
$db_args{'-mask_data'} = $self->mask_file;
}
elsif ($self->masker && $self->mask_data) { # build the mask
$db_args{'-mask_data'} = $self->make_mask(-data => $self->mask_data);
$self->throw("Masker error: message is '".$self->stderr."'") unless
$db_args{'-mask_data'};
$self->{_mask_data} = $db_args{'-mask_data'};
}
$self->{_factory} = $bp_class->new(
-command => 'makeblastdb',
%db_args
);
$self->factory->no_throw_on_crash($self->no_throw_on_crash);
return $self->factory->_run;
}
=head2 make_mask()
Title : make_mask
Usage :
Function: create masking data based on specified parameters
Returns : mask data filename (scalar string)
Args :
=cut
# mask program usage (based on blast+ manual)
#
# program dbtype opn
# windowmasker nucl mask overrep data, low-complexity (optional)
# dustmasker nucl mask low-complexity
# segmasker prot
sub make_mask {
my $self = shift;
my @args = @_;
my ($data, $mask_db, $make_args, $masker) = $self->_rearrange([qw(
DATA
MASK_DB
MAKE_ARGS
MASKER)], @args);
my (%mask_args,%usr_args,$db_type);
my $infmt = 'fasta';
$self->throw("make_mask requires -data argument") unless $data;
$masker ||= $self->masker;
$self->throw("no masker specified and no masker default set in object")
unless $masker;
my $usr_make_args ||= $self->mask_make_args;
%usr_args = @$usr_make_args if $usr_make_args;
unless (grep /^$masker$/, keys %AVAILABLE_MASKERS) {
$self->throw("Masker '$masker' not available");
}
if ($self->check_db($data)) {
unless ($masker eq 'segmasker') {
$self->throw("Masker '$masker' can't use a blastdb as primary input");
}
unless ($self->db_info($data)->{_db_type} eq
$AVAILABLE_MASKERS{$masker}) {
$self->throw("Masker '$masker' is incompatible with input db sequence type");
}
$infmt = 'blastdb';
}
else {
$data = $self->_fastize($data);
my $sio = Bio::SeqIO->new(-file=>$data);
my $s = $sio->next_seq;
my $type;
if ($s->alphabet =~ /.na/) {
$type = 'nucl';
}
elsif ($s->alphabet =~ /protein/) {
$type = 'prot';
}
else {
$type = 'UNK';
}
unless ($type eq $AVAILABLE_MASKERS{$masker}) {
$self->throw("Masker '$masker' is incompatible with sequence type '$type'");
}
}
# check that sequence type and masker program match:
# now, need to provide reasonable default masker arg settings,
# and override these with $usr_make_args as necessary and appropriate
my $mh = File::Temp->new(TEMPLATE=>'MSKXXXXX',
UNLINK => 0,
DIR => $self->db_dir);
my $mask_outfile = $mh->filename;
$mh->close;
$self->_register_temp_for_cleanup(File::Spec->catfile($self->db_dir,$mask_outfile));
# NOTE: '-outfmt' argument must not be included in the default args because
# it conflicts with windowmasker '-mk_counts' argument
%mask_args = (
-in => $data,
-parse_seqids => 1,
);
# usr arg override
if (%usr_args) {
$mask_args{$_} = $usr_args{$_} for keys %usr_args;
}
# masker-specific pipelines
my $status;
for ($masker) {
m/dustmasker/ && do {
$mask_args{'-out'} = $mask_outfile;
$mask_args{'-outfmt'} = $MASKER_ENCODING{$masker};
$self->{_factory} = $bp_class->new(-command => $masker,
%mask_args);
$self->factory->no_throw_on_crash($self->no_throw_on_crash);
$status = $self->factory->_run;
last;
};
m/windowmasker/ && do {
# check mask_db if present
if ($mask_db) {
unless ($self->check_db($mask_db)) {
$self->throw("Mask database '$mask_db' is not present or valid");
}
}
my $cth = File::Temp->new(TEMPLATE=>'MCTXXXXX',
DIR => $self->db_dir);
my $ct_file = $cth->filename;
$cth->close;
$mask_args{'-out'} = $ct_file;
$mask_args{'-mk_counts'} = 'true';
$self->{_factory} = $bp_class->new(-command => $masker,
%mask_args);
$self->factory->no_throw_on_crash($self->no_throw_on_crash);
$status = $self->factory->_run;
last unless $status;
delete $mask_args{'-mk_counts'};
$mask_args{'-ustat'} = $ct_file;
$mask_args{'-out'} = $mask_outfile;
$mask_args{'-outfmt'} = $MASKER_ENCODING{$masker};
if ($mask_db) {
$mask_args{'-in'} = $mask_db;
$mask_args{'-infmt'} = 'blastdb';
}
$self->factory->reset_parameters(%mask_args);
$self->factory->no_throw_on_crash($self->no_throw_on_crash);
$status = $self->factory->_run;
last;
};
m/segmasker/ && do {
$mask_args{'-infmt'} = $infmt;
$mask_args{'-out'} = $mask_outfile;
$mask_args{'-outfmt'} = $MASKER_ENCODING{$masker};
$self->{_factory} = $bp_class->new(-command => $masker,
%mask_args);
$self->factory->no_throw_on_crash($self->no_throw_on_crash);
$status = $self->factory->_run;
last;
};
do {
$self->throw("Masker program '$masker' not recognized");
};
}
return $status ? $mask_outfile : $status;
}
=head2 db_info()
Title : db_info
Usage :
Function: get info for database
(via blastdbcmd -info); add factory attributes
Returns : hash of database attributes
Args : [optional] db name (scalar string) (default: currently attached db)
=cut
sub db_info {
my $self = shift;
my $db = shift;
$db ||= $self->db_path;
unless ($db) {
$self->warn("db_info: db not specified and no db attached");
return;
}
if ($self->is_remote) {
$self->warn("db_info: sorry, can't get info for remote database (complain to NCBI)");
return;
}
if ($db eq $self->db and $self->{_db_info}) {
return $self->{_db_info}; # memoized
}
my $db_info_text;
$self->{_factory} = $bp_class->new( -command => 'blastdbcmd',
-info => 1,
-db => $db );
$self->factory->no_throw_on_crash(1);
$self->factory->_run();
$self->factory->no_throw_on_crash(0);
if ($self->factory->stderr =~ /No alias or index file found/) {
$self->warn("db_info: Couldn't find database ".$self->db."; make with make_db()");
return;
}
$db_info_text = $self->factory->stdout;
# parse info into attributes
my $infh = IO::String->new($db_info_text);
my %attr;
while (<$infh>) {
/Database: (.*)/ && do {
$attr{db_info_name} = $1;
next;
};
/([0-9,]+) sequences; ([0-9,]+) total/ && do {
$attr{db_num_sequences} = $1;
$attr{db_total_bases} = $2;
$attr{db_num_sequences} =~ s/,//g;
$attr{db_total_bases} =~ s/,//g;
next;
};
/Date: (.*?)\s+Longest sequence: ([0-9,]+)/ && do {
$attr{db_date} = $1; # convert to more usable date object
$attr{db_longest_sequence} = $2;
$attr{db_longest_sequence} =~ s/,//g;
next;
};
/Algorithm ID/ && do {
my $alg = $attr{db_filter_algorithms} = [];
while (<$infh>) {
if (/\s*([0-9]+)\s+([a-z0-9_]+)\s+(.*)/i) {
my ($alg_id, $alg_name, $alg_opts) = ($1, $2, $3);
$alg_opts =~ s/\s+$//;
push @$alg, { algorithm_id => $alg_id,
algorithm_name => $alg_name,
algorithm_opts => $alg_opts };
}
else {
last;
}
}
next;
};
}
# get db type
if ( -e $db.'.psq' ) {
$attr{_db_type} = 'prot';
}
elsif (-e $db.'.nsq') {
$attr{_db_type} = 'nucl';
}
else {
$attr{_db_type} = 'UNK'; # bork
}
if ($db eq $self->db) {
$self->{_db_type} = $attr{_db_type};
$self->{_db_info_text} = $db_info_text;
$self->{_db_info} = \%attr;
}
return \%attr;
}
=head2 set_db_make_args()
Title : set_db_make_args
Usage :
Function: set the DB make arguments attribute
with checking
Returns : true on success
Args : arrayref or hashref of named arguments
=cut
sub set_db_make_args {
my $self = shift;
my $args = shift;
$self->throw("Arrayref or hashref required at DB_MAKE_ARGS") unless
ref($args) =~ /^ARRAY|HASH$/;
if (ref($args) eq 'HASH') {
my @a = %$args;
$args = \@a;
}
$self->throw("Named args required for DB_MAKE_ARGS") unless !(@$args % 2);
$self->{'_db_make_args'} = $args;
return 1;
}
sub db_make_args { shift->{_db_make_args} }
=head2 set_mask_make_args()
Title : set_mask_make_args
Usage :
Function: set the masker make arguments attribute
with checking
Returns : true on success
Args : arrayref or hasref of named arguments
=cut
sub set_mask_make_args {
my $self = shift;
my $args = shift;
$self->throw("Arrayref or hashref required at MASK_MAKE_ARGS") unless
ref($args) =~ /^ARRAY|HASH$/;
if (ref($args) eq 'HASH') {
my @a = %$args;
$args = \@a;
}
$self->throw("Named args required at MASK_MAKE_ARGS") unless !(@$args % 2);
$self->{'_mask_make_args'} = $args;
return 1;
}
sub mask_make_args { shift->{_mask_make_args} }
=head2 check_db()
Title : check_db
Usage :
Function: determine if database with registered name and dir
exists
Returns : 1 if db present, 0 if not present, undef if name/dir not
set
Args : [optional] db name (default is 'registered' name in $self->db)
[optional] db directory (default is 'registered' dir in
$self->db_dir)
=cut
sub check_db {
my $self = shift;
my ($db) = @_;
my $db_path;
if ($db) {
my ($v,$d,$f) = File::Spec->splitpath($db);
$f =~ s/\..*$//; # ignore extensions
$db_path = File::Spec->catfile($d||'.',$f);
}
else {
$db_path = $self->db_path;
}
if ( $db_path ) {
$self->{_factory} = $bp_class->new( -command => 'blastdbcmd',
-info => 1,
-db => $db_path );
# $DB::single=1;
$self->factory->no_throw_on_crash(1);
$self->factory->_run();
$self->factory->no_throw_on_crash(0);
return 0 if ($self->factory->stderr =~ /No alias or index file found/);
return 1;
}
return;
}
=head2 no_throw_on_crash()
Title : no_throw_on_crash
Usage : $fac->no_throw_on_crash($newval)
Function: set to prevent an exeception throw on a failed
blast program execution
Example :
Returns : value of no_throw_on_crash (boolean)
Args : on set, new value (boolean)
=cut
sub no_throw_on_crash {
my $self = shift;
return $self->{'no_throw_on_crash'} = shift if @_;
return $self->{'no_throw_on_crash'};
}
=head1 Internals
=head2 _fastize()
Title : _fastize
Usage :
Function: convert a sequence collection to a temporary
fasta file (sans gaps)
Returns : fasta filename (scalar string)
Args : sequence collection
=cut
sub _fastize {
my $self = shift;
my $data = shift;
for ($data) {
!ref && do {
# suppose a fasta file name
$self->throw('Sequence file not found') unless -e $data;
my $guesser = Bio::Tools::GuessSeqFormat->new(-file => $data);
$self->throw('Sequence file not in FASTA format') unless
$guesser->guess eq 'fasta';
last;
};
(ref eq 'ARRAY') && (ref $$data[0]) &&
($$data[0]->isa('Bio::Seq') || $$data[0]->isa('Bio::PrimarySeq'))
&& do {
my $fh = File::Temp->new(TEMPLATE => 'DBDXXXXX',
UNLINK => 0,
DIR => $self->db_dir,
SUFFIX => '.fas');
my $fname = $fh->filename;
$fh->close;
$self->_register_temp_for_cleanup($fname);
my $fasio = Bio::SeqIO->new(-file=>">$fname", -format=>"fasta")
or $self->throw("Can't create temp fasta file");
for (@$data) {
my $s = $_->seq;
my $a = $_->alphabet;
$s =~ s/[$Bio::PrimarySeq::GAP_SYMBOLS]//g;
$_->seq( $s );
$_->alphabet($a);
$fasio->write_seq($_);
}
$fasio->close;
$data = $fname;
last;
};
ref && do { # some kind of object
my ($fmt) = ref($data) =~ /.*::(.*)/;
if ($fmt eq 'fasta') {
$data = $data->file; # use the fasta file directly
}
else {
# convert
my $fh = File::Temp->new(TEMPLATE => 'DBDXXXXX',
UNLINK => 0,
DIR => $self->db_dir,
SUFFIX => '.fas');
my $fname = $fh->filename;
$fh->close;
$self->_register_temp_for_cleanup($fname);
my $fasio = Bio::SeqIO->new(-file=>">$fname", -format=>"fasta")
or $self->throw("Can't create temp fasta file");
require Bio::PrimarySeq;
if ($data->isa('Bio::AlignIO')) {
my $aln = $data->next_aln;
for ($aln->each_seq) {
# must de-gap
my $s = $_->seq;
my $a = $_->alphabet;
$s =~ s/[$Bio::PrimarySeq::GAP_SYMBOLS]//g;
$_->seq( $s );
$_->alphabet($a);
$fasio->write_seq($_)
}
}
elsif ($data->isa('Bio::SeqIO')) {
while (local $_ = $data->next_seq) {
my $s = $_->seq;
my $a = $_->alphabet;
$s =~ s/[$Bio::PrimarySeq::GAP_SYMBOLS]//g;
$_->seq( $s );
$_->alphabet($a);
$fasio->write_seq($_);
}
}
elsif ($data->isa('Bio::Align::AlignI')) {
for( $data->each_seq) {
my $s = $_->seq;
my $a = $_->alphabet;
$s =~ s/[$Bio::PrimarySeq::GAP_SYMBOLS]//g;
$_->seq( $s );
$_->alphabet($a);
$fasio->write_seq($_)
}
}
elsif ($data->isa('Bio::Seq') || $data->isa('Bio::PrimarySeq')) {
my $s = $data->seq;
my $a = $data->alphabet;
$s =~ s/[$Bio::PrimarySeq::GAP_SYMBOLS]//g;
$data->seq($s);
$data->alphabet($a);
$fasio->write_seq($data);
}
else {
$self->throw("Can't handle sequence container object ".
"of type '".ref($data)."'");
}
$fasio->close;
$data = $fname;
}
last;
};
}
return $data;
}
=head2 _register_temp_for_cleanup()
Title : _register_temp_for_cleanup
Usage :
Function: register a file for cleanup with
cleanup() method
Returns : true on success
Args : a file name or a blastdb basename
(scalar string)
=cut
sub _register_temp_for_cleanup {
my $self = shift;
my @files = @_;
for (@files) {
my ($v, $d, $n) = File::Spec->splitpath($_);
$_ = File::Spec->catfile($self->db_dir, $n) unless length($d);
push @{$self->{_cleanup_list}}, File::Spec->rel2abs($_);
}
return 1;
}
=head2 cleanup()
Title : cleanup
Usage :
Function: unlink files registered for cleanup
Returns : true on success
Args :
=cut
sub cleanup {
my $self = shift;
return unless $self->{_cleanup_list};
my $self_file = '';
if (exists $self->{_results}->{_file}) {
$self_file = $self->{_results}->{_file};
}
for (@{$self->{_cleanup_list}}) {
# Close $self_file filehandle if it appears on the cleanup list,
# to avoid 'permission denied' errors when unlinking
if ($self_file ne '' and $_ =~ m/$self_file$/) {
close $self->{_results}->_fh;
}
m/(\.[a-z0-9_]+)+$/i && do {
unlink $_;
next;
};
do { # catch all index files
if ( -e $_.".psq" ) {
unlink glob($_.".p*");
unlink glob($_.".??.p*");
}
elsif ( -e $_.".nsq" ) {
unlink glob($_.".n*");
unlink glob($_.".??.n*");
}
else {
unlink $_;
}
next;
};
}
return 1;
}
=head2 AUTOLOAD
In this module, C<AUTOLOAD()> delegates L<Bio::Tools::Run::WrapperBase> and
L<Bio::Tools::Run::WrapperBase::CommandExts> methods (including those
of L<Bio::ParamterBaseI>) to the C<factory()> attribute:
$fac->stderr
gives you
$fac->factory->stderr
If $AUTOLOAD isn't pointing to a WrapperBase method, then AUTOLOAD attempts to return a C<db_info> attribute: e.g.
$fac->db_num_sequences
works by looking in the $fac->db_info() hash.
Finally, if $AUTOLOAD is pointing to a blast query method, AUTOLOAD
runs C<run> with the C<-method> parameter appropriately set.
=cut
sub AUTOLOAD {
my $self = shift;
my @args = @_;
my $method = $AUTOLOAD;
$method =~ s/.*:://;
my @ret;
if (grep /^$method$/, @Bio::Tools::Run::StandAloneBlastPlus::BlastMethods) {
push @args, ('-method_args' => ['-remote' => 1] ) if ($self->is_remote);
return $self->run( -method => $method, @args );
}
if ($self->factory and $self->factory->can($method)) { # factory method
return $self->factory->$method(@args);
}
if ($self->db_info and grep /^$method$/, keys %{$self->db_info}) {
return $self->db_info->{$method};
}
# else, fail
$self->throw("Can't locate method '$method' in class ".ref($self));
}
1;
|