| 1 | # $Id$ | ||
| 2 | package Bio::Tools::Run::BWA; | ||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | # | ||
| 5 | # BioPerl module for Bio::Tools::Run::BWA | ||
| 6 | # | ||
| 7 | # Please direct questions and support issues to <bioperl-l@bioperl.org> | ||
| 8 | # | ||
| 9 | # Cared for by Mark A. Jensen <maj@fortinbras.us> | ||
| 10 | # | ||
| 11 | # Copyright Mark A. Jensen | ||
| 12 | # | ||
| 13 | # You may distribute this module under the same terms as perl itself | ||
| 14 | |||
| 15 | # POD documentation - main docs before the code | ||
| 16 | |||
| 17 | =head1 NAME | ||
| 18 | |||
| 19 | Bio::Tools::Run::BWA - Run wrapper for the BWA short-read assembler *BETA* | ||
| 20 | |||
| 21 | =head1 SYNOPSIS | ||
| 22 | |||
| 23 | # create an assembly | ||
| 24 | |||
| 25 | # run BWA commands separately | ||
| 26 | |||
| 27 | =head1 DESCRIPTION | ||
| 28 | |||
| 29 | This module provides a wrapper interface for Heng Li's | ||
| 30 | reference-directed short read assembly suite C<bwa> (see | ||
| 31 | L<http://bio-bwa.sourceforge.net/bwa.shtml> for manuals and | ||
| 32 | downloads). | ||
| 33 | |||
| 34 | Manipulating the alignments requires C<samtools> | ||
| 35 | (L<http://sourceforge.net/projects/samtools/>) and Lincoln Stein's package | ||
| 36 | C<Bio-SamTools> (L<http://search.cpan.org/perldoc?Bio::DB::Sam>). | ||
| 37 | |||
| 38 | There are two modes of action. | ||
| 39 | |||
| 40 | =over | ||
| 41 | |||
| 42 | =item * Easy assembly | ||
| 43 | |||
| 44 | The first is a simple pipeline through the C<maq> commands, taking | ||
| 45 | your read data in and squirting out an assembly object of type | ||
| 46 | L<Bio::Assembly::IO::maq>. The pipeline is based on the one performed | ||
| 47 | by C<maq.pl easyrun>: | ||
| 48 | |||
| 49 | Action maq commands | ||
| 50 | ------ ------------ | ||
| 51 | data conversion to fasta2bfa, fastq2bfq | ||
| 52 | maq binary formats | ||
| 53 | |||
| 54 | map sequence reads map | ||
| 55 | to reference seq | ||
| 56 | |||
| 57 | assemble, creating assemble | ||
| 58 | consensus | ||
| 59 | |||
| 60 | convert map & cns mapview, cns2fq | ||
| 61 | files to plaintext | ||
| 62 | (for B:A:IO:maq) | ||
| 63 | |||
| 64 | Command-line options can be directed to the C<map>, C<assemble>, and | ||
| 65 | C<cns2fq> steps. See L</OPTIONS> below. | ||
| 66 | |||
| 67 | =item * BWA command mode | ||
| 68 | |||
| 69 | The second mode is direct access to C<bwa> commands. To run a C<bwa> | ||
| 70 | command, construct a run factory, specifying the desired command using | ||
| 71 | the C<-command> argument in the factory constructor, along with | ||
| 72 | options specific to that command (see L</OPTIONS>): | ||
| 73 | |||
| 74 | $bwafac = Bio::Tools::Run::BWA->new( -command => 'fasta2bfa' ); | ||
| 75 | |||
| 76 | To execute, use the C<run_bwa> methods. Input and output files are | ||
| 77 | specified in the arguments of C<run_bwa> (see L</FILES>): | ||
| 78 | |||
| 79 | $bwafac->run_bwa( -fas => "myref.fas", -bfa => "myref.bfa" ); | ||
| 80 | |||
| 81 | =back | ||
| 82 | |||
| 83 | =head1 OPTIONS | ||
| 84 | |||
| 85 | C<bwa> is complex, with many subprograms (commands) and command-line | ||
| 86 | options and file specs for each. This module attempts to provide | ||
| 87 | commands and options comprehensively. You can browse the choices like so: | ||
| 88 | |||
| 89 | $bwafac = Bio::Tools::Run::BWA->new( -command => 'aln' ); | ||
| 90 | # all maq commands | ||
| 91 | @all_commands = $bwafac->available_parameters('commands'); | ||
| 92 | @all_commands = $bwafac->available_commands; # alias | ||
| 93 | # just for aln | ||
| 94 | @aln_params = $bwafac->available_parameters('params'); | ||
| 95 | @aln_switches = $bwafac->available_parameters('switches'); | ||
| 96 | @aln_all_options = $bwafac->available_parameters(); | ||
| 97 | |||
| 98 | Reasonably mnemonic names have been assigned to the single-letter | ||
| 99 | command line options. These are the names returned by | ||
| 100 | C<available_parameters>, and can be used in the factory constructor | ||
| 101 | like typical BioPerl named parameters. | ||
| 102 | |||
| 103 | See L<http://bio-bwa.sourceforge.net/bwa.shtml> for the gory details. | ||
| 104 | |||
| 105 | =head1 FILES | ||
| 106 | |||
| 107 | When a command requires filenames, these are provided to the C<run_bwa> method, not | ||
| 108 | the constructor (C<new()>). To see the set of files required by a command, use | ||
| 109 | C<available_parameters('filespec')> or the alias C<filespec()>: | ||
| 110 | |||
| 111 | $bwafac = Bio::Tools::Run::BWA->new( -command => 'aln' ); | ||
| 112 | @filespec = $bwafac->filespec; | ||
| 113 | |||
| 114 | This example returns the following array: | ||
| 115 | |||
| 116 | fas | ||
| 117 | faq | ||
| 118 | >sai | ||
| 119 | |||
| 120 | This indicates that the FASTA database (faq) and the FASTQ reads (faq) | ||
| 121 | MUST be specified, and the STDOUT of this program (SA coordinates) MAY be | ||
| 122 | slurped into a file specified in the C<run_bwa> argument list: | ||
| 123 | |||
| 124 | $bwafac->run_bwa( -fas => 'my.db.fas', -faq => 'reads.faq', | ||
| 125 | -sai => 'out.sai' ); | ||
| 126 | |||
| 127 | If files are not specified per the filespec, text sent to STDOUT and | ||
| 128 | STDERR is saved and is accessible with C<$bwafac->stdout()> and | ||
| 129 | C<$bwafac->stderr()>. | ||
| 130 | |||
| 131 | =head1 FEEDBACK | ||
| 132 | |||
| 133 | =head2 Mailing Lists | ||
| 134 | |||
| 135 | User feedback is an integral part of the evolution of this and other | ||
| 136 | Bioperl modules. Send your comments and suggestions preferably to | ||
| 137 | the Bioperl mailing list. Your participation is much appreciated. | ||
| 138 | |||
| 139 | bioperl-l@bioperl.org - General discussion | ||
| 140 | http://bioperl.org/wiki/Mailing_lists - About the mailing lists | ||
| 141 | |||
| 142 | =head2 Support | ||
| 143 | |||
| 144 | Please direct usage questions or support issues to the mailing list: | ||
| 145 | |||
| 146 | L<bioperl-l@bioperl.org> | ||
| 147 | |||
| 148 | rather than to the module maintainer directly. Many experienced and | ||
| 149 | reponsive experts will be able look at the problem and quickly | ||
| 150 | address it. Please include a thorough description of the problem | ||
| 151 | with code and data examples if at all possible. | ||
| 152 | |||
| 153 | =head2 Reporting Bugs | ||
| 154 | |||
| 155 | Report bugs to the Bioperl bug tracking system to help us keep track | ||
| 156 | of the bugs and their resolution. Bug reports can be submitted via | ||
| 157 | the web: | ||
| 158 | |||
| 159 | http://redmine.open-bio.org/projects/bioperl/ | ||
| 160 | |||
| 161 | =head1 AUTHOR - Mark A. Jensen | ||
| 162 | |||
| 163 | Email maj -at- fortinbras -dot- us | ||
| 164 | |||
| 165 | =head1 APPENDIX | ||
| 166 | |||
| 167 | The rest of the documentation details each of the object methods. | ||
| 168 | Internal methods are usually preceded with a _ | ||
| 169 | |||
| 170 | =cut | ||
| 171 | |||
| 172 | # Let the code begin... | ||
| 173 | |||
| 174 | |||
| 175 | 1 | ▶ | use IPC::Run; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 176 | our $HAVE_IO_UNCOMPRESS; | ||
| 177 | our $HAVE_SAMTOOLS; | ||
| 178 | BEGIN { | ||
| 179 | 1 | ▶ | eval "require IO::Uncompress::Gunzip; \$HAVE_IO_UNCOMPRESS = 1"; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 180 | 1 | eval "require Bio::Tools::Run::Samtools; \$HAVE_SAMTOOLS = 1"; | |
| 181 | } | ||
| 182 | |||
| 183 | # Object preamble - inherits from Bio::Root::Root | ||
| 184 | |||
| 185 | 1 | ▶ | use Bio::Root::Root; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 186 | 1 | ▶ | use Bio::Tools::Run::BWA::Config; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 187 | 1 | ▶ | use Bio::Tools::GuessSeqFormat; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 188 | 1 | ▶ | use File::Basename qw(fileparse); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 189 | 1 | ▶ | use File::Copy; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 190 | 1 | ▶ | use Cwd; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 191 | |||
| 192 | 1 | ▶ | use base qw(Bio::Root::Root Bio::Tools::Run::AssemblerBase ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 193 | |||
| 194 | our $program_name = 'bwa'; # name of the executable | ||
| 195 | |||
| 196 | # Note: | ||
| 197 | # other globals required by Bio::Tools::Run::AssemblerBase are | ||
| 198 | # imported from Bio::Tools::Run::BWA::Config | ||
| 199 | |||
| 200 | our $qual_param = 'quality_file'; | ||
| 201 | our $use_dash = 1; | ||
| 202 | our $join = ' '; | ||
| 203 | |||
| 204 | # Bio::Assembly::IO::sam now workable! | ||
| 205 | our $asm_format = 'sam'; | ||
| 206 | |||
| 207 | =head2 new() | ||
| 208 | |||
| 209 | Title : new | ||
| 210 | Usage : my $obj = new Bio::Tools::Run::BWA(); | ||
| 211 | Function: Builds a new Bio::Tools::Run::BWA object | ||
| 212 | Returns : an instance of Bio::Tools::Run::BWA | ||
| 213 | Args : | ||
| 214 | |||
| 215 | =cut | ||
| 216 | |||
| 217 | sub new { | ||
| 218 | my ($class,@args) = @_; | ||
| 219 | my $self = $class->SUPER::new(@args); | ||
| 220 | $self->parameters_changed(1); | ||
| 221 | $self->_register_program_commands( \@program_commands, \%command_prefixes ); | ||
| 222 | unless (grep /command/, @args) { | ||
| 223 | push @args, '-command', 'run'; | ||
| 224 | } | ||
| 225 | $self->_set_program_options(\@args, \@program_params, \@program_switches, | ||
| 226 | \%param_translation, $qual_param, $use_dash, $join); | ||
| 227 | $self->program_name($program_name) if not defined $self->program_name(); | ||
| 228 | $self->parameters_changed(1); # set on instantiation, per Bio::ParameterBaseI | ||
| 229 | $self->_assembly_format($asm_format); | ||
| 230 | return $self; | ||
| 231 | } | ||
| 232 | |||
| 233 | =head2 run | ||
| 234 | |||
| 235 | Title : run | ||
| 236 | Usage : $assembly = $bwafac->run( @args ); | ||
| 237 | Function: Run the bwa assembly pipeline. | ||
| 238 | Returns : Assembly results (file, IO object or Assembly object) | ||
| 239 | Args : - fastq file containing single-end reads | ||
| 240 | - fasta file containing the reference sequence | ||
| 241 | - [optional] fastq file containing paired-end reads | ||
| 242 | |||
| 243 | =cut | ||
| 244 | |||
| 245 | sub run { | ||
| 246 | my ($self, $rd1_file, $ref_file, $rd2_file) = @_; | ||
| 247 | |||
| 248 | # Sanity checks | ||
| 249 | $self->_check_executable(); | ||
| 250 | unless ($HAVE_SAMTOOLS) { | ||
| 251 | cluck( "Bio::Tools::Run::Samtools is not available. A .sam output alignment will be created, but must be converted to binary SAM (.bam) before it can be passed to Bio::Assembly::IO, as follows: \n\t\$ samtools view -Sb out.sam > out.bam" ); | ||
| 252 | } | ||
| 253 | $rd1_file or $self->throw("Fastq reads file required at arg 1"); | ||
| 254 | $ref_file or $self->throw("Fasta refseq file required at arg 2"); | ||
| 255 | for ($rd1_file, $ref_file, $rd2_file) { | ||
| 256 | next unless $_; | ||
| 257 | if (/\.gz[^.]*$/) { | ||
| 258 | unless ($HAVE_IO_UNCOMPRESS) { | ||
| 259 | croak( "IO::Uncompress::Gunzip not available, can't expand '$_'" ); | ||
| 260 | } | ||
| 261 | my ($tfh, $tf) = $self->io->tempfile; | ||
| 262 | my $z = IO::Uncompress::Gunzip->new($_); | ||
| 263 | while (<$z>) { print $tfh $_ } | ||
| 264 | close $tfh; | ||
| 265 | $_ = $tf; | ||
| 266 | } | ||
| 267 | } | ||
| 268 | |||
| 269 | my $guesser = Bio::Tools::GuessSeqFormat->new(-file=>$rd1_file); | ||
| 270 | |||
| 271 | $guesser->guess eq 'fastq' or $self->throw("Reads file doesn't look like fastq at arg 1"); | ||
| 272 | $guesser = Bio::Tools::GuessSeqFormat->new(-file=>$ref_file); | ||
| 273 | $guesser->guess eq 'fasta' or $self->throw("Refseq file doesn't look like fasta at arg 2"); | ||
| 274 | if ($rd2_file) { | ||
| 275 | $guesser = Bio::Tools::GuessSeqFormat->new(-file=>$rd2_file); | ||
| 276 | $guesser->guess eq 'fastq' or $self->throw("Reads file doesn't look like fastq at arg 3"); | ||
| 277 | } | ||
| 278 | |||
| 279 | #Assemble | ||
| 280 | my ($sam_file) = $self->_run($rd1_file, $ref_file, $rd2_file); | ||
| 281 | |||
| 282 | if ($HAVE_SAMTOOLS) { | ||
| 283 | my ($nm,$dr,$suf) = fileparse($sam_file, ".sam"); | ||
| 284 | # goofy kludge for samtools... | ||
| 285 | my $pwd = getcwd; | ||
| 286 | chdir($dr); | ||
| 287 | my $samt = Bio::Tools::Run::Samtools->new( -command => 'view', | ||
| 288 | -sam_input => 1, | ||
| 289 | -bam_output => 1, | ||
| 290 | -refseq => $ref_file); | ||
| 291 | my $bam_file = $nm.'.bam'; | ||
| 292 | $samt->run( -bam => $nm.$suf, -out => $bam_file ) or croak( "Problem converting .sam file"); | ||
| 293 | $samt = Bio::Tools::Run::Samtools->new( -command => 'sort' ); | ||
| 294 | $samt->run( -bam => $bam_file, -pfx => $nm.'.srt' ) or croak( "Problem sorting .bam file"); | ||
| 295 | move( $nm.'.srt.bam', $bam_file ); | ||
| 296 | $samt = Bio::Tools::Run::Samtools->new( -command => 'index' ); | ||
| 297 | $samt->run( -bam => $bam_file ); | ||
| 298 | $bam_file = File::Spec->catfile($dr, $bam_file); | ||
| 299 | $sam_file = $bam_file; | ||
| 300 | chdir($pwd); | ||
| 301 | } | ||
| 302 | |||
| 303 | # Export results in desired object type | ||
| 304 | my $asm = $self->_export_results($sam_file, -refdb => $ref_file, -keep_asm => 1); | ||
| 305 | return $asm; | ||
| 306 | |||
| 307 | } | ||
| 308 | |||
| 309 | =head2 run_bwa() | ||
| 310 | |||
| 311 | Title : run_bwa | ||
| 312 | Usage : $obj->run_bwa( @file_args ) | ||
| 313 | Function: Run a bwa command as specified during object contruction | ||
| 314 | Returns : | ||
| 315 | Args : a specification of the files to operate on: | ||
| 316 | |||
| 317 | =cut | ||
| 318 | |||
| 319 | sub run_bwa { | ||
| 320 | my ($self, @args) = @_; | ||
| 321 | # _translate_params will provide an array of command/parameters/switches | ||
| 322 | # -- these are set at object construction | ||
| 323 | # to set up the run, need to add the files to the call | ||
| 324 | # -- provide these as arguments to this function | ||
| 325 | |||
| 326 | my $cmd = $self->command if $self->can('command'); | ||
| 327 | $self->throw("No maq command specified for the object") unless $cmd; | ||
| 328 | # setup files necessary for this command | ||
| 329 | my $filespec = $command_files{$cmd}; | ||
| 330 | $self->throw("No command-line file specification is defined for command '$cmd'; check Bio::Tools::Run::Maq::Config") unless $filespec; | ||
| 331 | |||
| 332 | # parse args based on filespec | ||
| 333 | # require named args | ||
| 334 | $self->throw("Named args are required") unless !(@args % 2); | ||
| 335 | s/^-// for @args; | ||
| 336 | my %args = @args; | ||
| 337 | # validate | ||
| 338 | my @req = map { | ||
| 339 | my $s = $_; | ||
| 340 | $s =~ s/^[012]?[<>]//; | ||
| 341 | $s =~ s/[^a-zA-Z0-9_]//g; | ||
| 342 | $s | ||
| 343 | } grep !/[#]/, @$filespec; | ||
| 344 | !defined($args{$_}) && $self->throw("Required filearg '$_' not specified") for @req; | ||
| 345 | # set up redirects | ||
| 346 | my ($in, $out, $err); | ||
| 347 | for (@$filespec) { | ||
| 348 | m/^1?>(.*)/ && do { | ||
| 349 | defined($args{$1}) && ( open($out,">", $args{$1}) or $self->throw("Open for write error : $!")); | ||
| 350 | next; | ||
| 351 | }; | ||
| 352 | m/^2>#?(.*)/ && do { | ||
| 353 | defined($args{$1}) && (open($err, ">", $args{$1}) or $self->throw("Open for write error : $!")); | ||
| 354 | next; | ||
| 355 | }; | ||
| 356 | m/^<#?(.*)/ && do { | ||
| 357 | defined($args{$1}) && (open($in, "<", $args{$1}) or $self->throw("Open for read error : $!")); | ||
| 358 | next; | ||
| 359 | } | ||
| 360 | } | ||
| 361 | my $dum; | ||
| 362 | $in || ($in = \$dum); | ||
| 363 | $out || ($out = \$self->{'stdout'}); | ||
| 364 | $err || ($err = \$self->{'stderr'}); | ||
| 365 | |||
| 366 | # Get program executable | ||
| 367 | my $exe = $self->executable; | ||
| 368 | # Get command-line options | ||
| 369 | my $options = $self->_translate_params(); | ||
| 370 | # Get file specs sans redirects in correct order | ||
| 371 | my @specs = map { | ||
| 372 | my $s = $_; | ||
| 373 | $s =~ s/[^a-zA-Z0-9_]//g; | ||
| 374 | $s | ||
| 375 | } grep !/[<>]/, @$filespec; | ||
| 376 | my @files = @args{@specs}; | ||
| 377 | # expand arrayrefs | ||
| 378 | my $l = $#files; | ||
| 379 | for (0..$l) { | ||
| 380 | splice(@files, $_, 1, @{$files[$_]}) if (ref($files[$_]) eq 'ARRAY'); | ||
| 381 | } | ||
| 382 | @files = map { defined $_ ? $_ : () } @files; # squish undefs | ||
| 383 | my @ipc_args = ( $exe, @$options, @files ); | ||
| 384 | |||
| 385 | eval { | ||
| 386 | IPC::Run::run(\@ipc_args, $in, $out, $err) or | ||
| 387 | die ("There was a problem running $exe : $!"); | ||
| 388 | }; | ||
| 389 | if ($@) { | ||
| 390 | $self->throw("$exe call crashed: $@"); | ||
| 391 | } | ||
| 392 | |||
| 393 | # return arguments as specified on call | ||
| 394 | return @args; | ||
| 395 | } | ||
| 396 | |||
| 397 | =head2 stdout() | ||
| 398 | |||
| 399 | Title : stdout | ||
| 400 | Usage : $fac->stdout() | ||
| 401 | Function: store the output from STDOUT for the run, | ||
| 402 | if no file specified in run_maq() | ||
| 403 | Example : | ||
| 404 | Returns : scalar string | ||
| 405 | Args : on set, new value (a scalar or undef, optional) | ||
| 406 | |||
| 407 | =cut | ||
| 408 | |||
| 409 | sub stdout { | ||
| 410 | my $self = shift; | ||
| 411 | |||
| 412 | return $self->{'stdout'} = shift if @_; | ||
| 413 | return $self->{'stdout'}; | ||
| 414 | } | ||
| 415 | |||
| 416 | =head2 stderr() | ||
| 417 | |||
| 418 | Title : stderr | ||
| 419 | Usage : $fac->stderr() | ||
| 420 | Function: store the output from STDERR for the run, | ||
| 421 | if no file is specified in run_maq() | ||
| 422 | Example : | ||
| 423 | Returns : scalar string | ||
| 424 | Args : on set, new value (a scalar or undef, optional) | ||
| 425 | |||
| 426 | =cut | ||
| 427 | |||
| 428 | sub stderr { | ||
| 429 | my $self = shift; | ||
| 430 | |||
| 431 | return $self->{'stderr'} = shift if @_; | ||
| 432 | return $self->{'stderr'}; | ||
| 433 | } | ||
| 434 | |||
| 435 | =head1 Bio::Tools::Run::AssemblerBase overrides | ||
| 436 | |||
| 437 | =head2 _check_sequence_input() | ||
| 438 | |||
| 439 | No-op. | ||
| 440 | |||
| 441 | =cut | ||
| 442 | |||
| 443 | sub _check_sequence_input { | ||
| 444 | return 1; | ||
| 445 | } | ||
| 446 | |||
| 447 | =head2 _check_optional_quality_input() | ||
| 448 | |||
| 449 | No-op. | ||
| 450 | |||
| 451 | =cut | ||
| 452 | |||
| 453 | sub _check_optional_quality_input { | ||
| 454 | return 1; | ||
| 455 | } | ||
| 456 | |||
| 457 | =head2 _prepare_input_sequences | ||
| 458 | |||
| 459 | Convert input fastq and fasta to maq format. | ||
| 460 | |||
| 461 | =cut | ||
| 462 | |||
| 463 | sub _prepare_input_sequences { | ||
| 464 | shift->throw_not_implemented; | ||
| 465 | } | ||
| 466 | |||
| 467 | =head2 _collate_subcmd_args() | ||
| 468 | |||
| 469 | Title : _collate_subcmd_args | ||
| 470 | Usage : $args_hash = $self->_collate_subcmd_args | ||
| 471 | Function: collate parameters and switches into command-specific | ||
| 472 | arg lists for passing to new() | ||
| 473 | Returns : hash of named argument lists | ||
| 474 | Args : [optional] composite cmd prefix (scalar string) | ||
| 475 | [default is 'run'] | ||
| 476 | |||
| 477 | =cut | ||
| 478 | |||
| 479 | sub _collate_subcmd_args { | ||
| 480 | my $self = shift; | ||
| 481 | my $cmd = shift; | ||
| 482 | my %ret; | ||
| 483 | # default command is 'run' | ||
| 484 | $cmd ||= 'run'; | ||
| 485 | my @subcmds = @{$composite_commands{$cmd}}; | ||
| 486 | my %subcmds; | ||
| 487 | my $cur_options = $self->{'_options'}; | ||
| 488 | |||
| 489 | # collate | ||
| 490 | foreach my $subcmd (@subcmds) { | ||
| 491 | # find the composite cmd form of the argument in | ||
| 492 | # the current params and switches | ||
| 493 | # e.g., map_max_mismatches | ||
| 494 | my @params = grep /^${subcmd}_/, @{$$cur_options{'_params'}}; | ||
| 495 | my @switches = grep /^${subcmd}_/, @{$$cur_options{'_switches'}}; | ||
| 496 | $ret{$subcmd} = []; | ||
| 497 | # create an argument list suitable for passing to new() of | ||
| 498 | # the subcommand factory... | ||
| 499 | foreach my $opt (@params, @switches) { | ||
| 500 | my $subopt = $opt; | ||
| 501 | $subopt =~ s/^${subcmd}_//; | ||
| 502 | push(@{$ret{$subcmd}}, '-'.$subopt => $self->$opt) if defined $self->$opt; | ||
| 503 | } | ||
| 504 | } | ||
| 505 | return \%ret; | ||
| 506 | } | ||
| 507 | |||
| 508 | =head2 _run() | ||
| 509 | |||
| 510 | Title : _run | ||
| 511 | Usage : $factory->_run() | ||
| 512 | Function: Run a bwa assembly pipeline | ||
| 513 | Returns : a text-formatted sam alignment | ||
| 514 | Args : - single end read file in maq bfq format | ||
| 515 | - reference seq file in maq bfa format | ||
| 516 | - [optional] paired end read file in maq bfq format | ||
| 517 | |||
| 518 | =cut | ||
| 519 | |||
| 520 | sub _run { | ||
| 521 | my ($self, $rd1_file, $ref_file, $rd2_file) = @_; | ||
| 522 | my ($cmd, $filespec, @ipc_args); | ||
| 523 | # Get program executable | ||
| 524 | my $exe = $self->executable; | ||
| 525 | my $paired = $rd1_file && $rd2_file; | ||
| 526 | |||
| 527 | my $tdir = $self->tempdir(); | ||
| 528 | my ($saih, $saif) = $self->io->tempfile(-template => 'saiXXXX', -dir=>$tdir); | ||
| 529 | my ($sai2h, $sai2f) = $self->io->tempfile(-template => 'saiXXXX', -dir=>$tdir) | ||
| 530 | if $paired; | ||
| 531 | my ($samh, $samf) = $self->io->tempfile(-template => 'saiXXXX', -dir=>$tdir); | ||
| 532 | $_->close for ($saih, $samh); | ||
| 533 | $sai2h->close if $paired; | ||
| 534 | |||
| 535 | my $subcmd_args = $self->_collate_subcmd_args(); | ||
| 536 | # index the fasta file (bwa's, not samtools', index...) | ||
| 537 | my $bwa = Bio::Tools::Run::BWA->new( | ||
| 538 | -command => 'index', | ||
| 539 | @{$subcmd_args->{idx}} | ||
| 540 | ); | ||
| 541 | $bwa->run_bwa( -fas => $ref_file ); | ||
| 542 | # map reads to reference seqs | ||
| 543 | $bwa = Bio::Tools::Run::BWA->new( | ||
| 544 | -command => 'aln', | ||
| 545 | @{$subcmd_args->{aln}} | ||
| 546 | ); | ||
| 547 | |||
| 548 | $bwa->run_bwa( -fas => $ref_file, -faq => $rd1_file, -sai => $saif ); | ||
| 549 | # do paired run if nec | ||
| 550 | $bwa->run_bwa( -fas => $ref_file, -faq => $rd2_file, -sai => $sai2f ) | ||
| 551 | if $paired; | ||
| 552 | # assemble reads | ||
| 553 | $bwa = Bio::Tools::Run::BWA->new( | ||
| 554 | -command => ($paired ? 'sampe' : 'samse'), | ||
| 555 | @{$subcmd_args->{ ($paired ? 'smp' : 'sms' ) }} | ||
| 556 | ); | ||
| 557 | if ($paired) { | ||
| 558 | $bwa->run_bwa( -fas => $ref_file, -sai1 => $saif, -faq1 => $rd1_file, | ||
| 559 | -sai2 => $sai2f, -faq2 => $rd2_file, -sam => $samf ); | ||
| 560 | } | ||
| 561 | else { | ||
| 562 | $bwa->run_bwa( -fas => $ref_file, -sai => $saif, -faq => $rd1_file, | ||
| 563 | -sam => $samf ); | ||
| 564 | } | ||
| 565 | # note this returns a text-sam file-- needs conversion for B:A:IO::sam... | ||
| 566 | # conversion done in run(), if Bio::Tools::Run::Samtools available. | ||
| 567 | return $samf; | ||
| 568 | |||
| 569 | } | ||
| 570 | |||
| 571 | =head2 available_parameters() | ||
| 572 | |||
| 573 | Title : available_parameters | ||
| 574 | Usage : @cmds = $fac->available_commands('commands'); | ||
| 575 | Function: Use to browse available commands, params, or switches | ||
| 576 | Returns : array of scalar strings | ||
| 577 | Args : 'commands' : all bwa commands | ||
| 578 | 'params' : parameters for this object's command | ||
| 579 | 'switches' : boolean switches for this object's command | ||
| 580 | 'filespec' : the filename spec for this object's command | ||
| 581 | 4Geeks : Overrides Bio::ParameterBaseI via | ||
| 582 | Bio::Tools::Run::AssemblerBase | ||
| 583 | |||
| 584 | =cut | ||
| 585 | |||
| 586 | sub available_parameters { | ||
| 587 | my $self = shift; | ||
| 588 | my $subset = shift; | ||
| 589 | for ($subset) { # get commands | ||
| 590 | !defined && do { # delegate | ||
| 591 | return $self->SUPER::available_parameters($subset); | ||
| 592 | }; | ||
| 593 | m/^c/i && do { | ||
| 594 | return grep !/^run$/, @program_commands; | ||
| 595 | }; | ||
| 596 | m/^f/i && do { # get file spec | ||
| 597 | return @{$command_files{$self->command}}; | ||
| 598 | }; | ||
| 599 | do { #else delegate... | ||
| 600 | return $self->SUPER::available_parameters($subset); | ||
| 601 | }; | ||
| 602 | } | ||
| 603 | } | ||
| 604 | |||
| 605 | sub available_commands { shift->available_parameters('commands') }; | ||
| 606 | |||
| 607 | sub filespec { shift->available_parameters('filespec') }; | ||
| 608 | |||
| 609 | 1; | ||