| 1 | # $Id: BEDTools.pm kortsch $ | ||
| 2 | # | ||
| 3 | # BioPerl module for Bio::Tools::Run::BEDTools | ||
| 4 | # | ||
| 5 | # Please direct questions and support issues to <bioperl-l@bioperl.org> | ||
| 6 | # | ||
| 7 | # Cared for by Dan Kortschak <dan.kortschak@adelaide.edu.au> | ||
| 8 | # | ||
| 9 | # Copyright Dan Kortschak | ||
| 10 | # | ||
| 11 | # You may distribute this module under the same terms as perl itself | ||
| 12 | |||
| 13 | # POD documentation - main docs before the code | ||
| 14 | |||
| 15 | =head1 NAME | ||
| 16 | |||
| 17 | Bio::Tools::Run::BEDTools - Run wrapper for the BEDTools suite of programs *BETA* | ||
| 18 | |||
| 19 | =head1 SYNOPSIS | ||
| 20 | |||
| 21 | # use a BEDTools program | ||
| 22 | $bedtools_fac = Bio::Tools::Run::BEDTools->new( -command => 'subtract' ); | ||
| 23 | $result_file = $bedtools_fac->run( -bed1 => 'genes.bed', -bed2 => 'mask.bed' ); | ||
| 24 | |||
| 25 | # if IO::Uncompress::Gunzip is available... | ||
| 26 | $result_file = $bedtools_fac->run( -bed1 => 'genes.bed.gz', -bed2 => 'mask.bed.gz' ); | ||
| 27 | |||
| 28 | # be more strict | ||
| 29 | $bedtools_fac->set_parameters( -strandedness => 1 ); | ||
| 30 | |||
| 31 | # and even more... | ||
| 32 | $bedtools_fac->set_parameters( -minimum_overlap => 1e-6 ); | ||
| 33 | |||
| 34 | # create a Bio::SeqFeature::Collection object | ||
| 35 | $features = $bedtools_fac->result( -want => 'Bio::SeqFeature::Collection' ); | ||
| 36 | |||
| 37 | =head1 DEPRECATION WARNING | ||
| 38 | |||
| 39 | Most executables from BEDTools v>=2.10.1 can read GFF and VCF formats | ||
| 40 | in addition to BED format. This requires the use of a new input file param, | ||
| 41 | shown in the following documentation, '-bgv', in place of '-bed' for the | ||
| 42 | executables that can do this. | ||
| 43 | |||
| 44 | This behaviour breaks existing scripts. | ||
| 45 | |||
| 46 | =head1 DESCRIPTION | ||
| 47 | |||
| 48 | This module provides a wrapper interface for Aaron R. Quinlan and Ira M. Hall's | ||
| 49 | utilities C<BEDTools> that allow for (among other things): | ||
| 50 | |||
| 51 | =over | ||
| 52 | |||
| 53 | =item * Intersecting two BED files in search of overlapping features. | ||
| 54 | |||
| 55 | =item * Merging overlapping features. | ||
| 56 | |||
| 57 | =item * Screening for paired-end (PE) overlaps between PE sequences and existing genomic features. | ||
| 58 | |||
| 59 | =item * Calculating the depth and breadth of sequence coverage across defined "windows" in a genome. | ||
| 60 | |||
| 61 | =back | ||
| 62 | |||
| 63 | (see L<http://code.google.com/p/bedtools/> for manuals and downloads). | ||
| 64 | |||
| 65 | |||
| 66 | =head1 OPTIONS | ||
| 67 | |||
| 68 | C<BEDTools> is a suite of 17 commandline executable. This module attempts to | ||
| 69 | provide and options comprehensively. You can browse the choices like so: | ||
| 70 | |||
| 71 | $bedtools_fac = Bio::Tools::Run::BEDTools->new; | ||
| 72 | |||
| 73 | # all bowtie commands | ||
| 74 | @all_commands = $bedtools_fac->available_parameters('commands'); | ||
| 75 | @all_commands = $bedtools_fac->available_commands; # alias | ||
| 76 | |||
| 77 | # just for default command ('bam_to_bed') | ||
| 78 | @btb_params = $bedtools_fac->available_parameters('params'); | ||
| 79 | @btb_switches = $bedtools_fac->available_parameters('switches'); | ||
| 80 | @btb_all_options = $bedtools_fac->available_parameters(); | ||
| 81 | |||
| 82 | Reasonably mnemonic names have been assigned to the single-letter | ||
| 83 | command line options. These are the names returned by | ||
| 84 | C<available_parameters>, and can be used in the factory constructor | ||
| 85 | like typical BioPerl named parameters. | ||
| 86 | |||
| 87 | As a number of options are mutually exclusive, and the interpretation of | ||
| 88 | intent is based on last-pass option reaching bowtie with potentially unpredicted | ||
| 89 | results. This module will prevent inconsistent switches and parameters | ||
| 90 | from being passed. | ||
| 91 | |||
| 92 | See L<http://code.google.com/p/bedtools/> for details of BEDTools options. | ||
| 93 | |||
| 94 | =head1 FILES | ||
| 95 | |||
| 96 | When a command requires filenames, these are provided to the C<run> method, not | ||
| 97 | the constructor (C<new()>). To see the set of files required by a command, use | ||
| 98 | C<available_parameters('filespec')> or the alias C<filespec()>: | ||
| 99 | |||
| 100 | $bedtools_fac = Bio::Tools::Run::BEDTools->new( -command => 'pair_to_bed' ); | ||
| 101 | @filespec = $bedtools_fac->filespec; | ||
| 102 | |||
| 103 | This example returns the following array: | ||
| 104 | |||
| 105 | #bedpe | ||
| 106 | #bam | ||
| 107 | bed | ||
| 108 | #out | ||
| 109 | |||
| 110 | This indicates that the bed (C<BEDTools> BED format) file MUST be | ||
| 111 | specified, and that the out, bedpe (C<BEDTools> BEDPE format) and bam | ||
| 112 | (C<SAM> binary format) file MAY be specified (Note that in this case you | ||
| 113 | MUST provide ONE of bedpe OR bam, the module at this stage does not allow | ||
| 114 | this information to be queried). Use these in the C<run> call like so: | ||
| 115 | |||
| 116 | $bedtools_fac->run( -bedpe => 'paired.bedpe', | ||
| 117 | -bgv => 'genes.bed', | ||
| 118 | -out => 'overlap' ); | ||
| 119 | |||
| 120 | The object will store the programs STDERR output for you in the C<stderr()> | ||
| 121 | attribute: | ||
| 122 | |||
| 123 | handle_bed_warning($bedtools_fac) if ($bedtools_fac->stderr =~ /Usage:/); | ||
| 124 | |||
| 125 | For the commands 'fasta_from_bed' and 'mask_fasta_from_bed' STDOUT will also | ||
| 126 | be captured in the C<stdout()> attribute by default and all other commands | ||
| 127 | can be forced to capture program output in STDOUT by setting the -out | ||
| 128 | filespec parameter to '-'. | ||
| 129 | |||
| 130 | =head1 FEEDBACK | ||
| 131 | |||
| 132 | =head2 Mailing Lists | ||
| 133 | |||
| 134 | User feedback is an integral part of the evolution of this and other | ||
| 135 | Bioperl modules. Send your comments and suggestions preferably to | ||
| 136 | the Bioperl mailing list. Your participation is much appreciated. | ||
| 137 | |||
| 138 | bioperl-l@bioperl.org - General discussion | ||
| 139 | http://bioperl.org/wiki/Mailing_lists - About the mailing lists | ||
| 140 | |||
| 141 | =head2 Support | ||
| 142 | |||
| 143 | Please direct usage questions or support issues to the mailing list: | ||
| 144 | |||
| 145 | L<bioperl-l@bioperl.org> | ||
| 146 | |||
| 147 | Rather than to the module maintainer directly. Many experienced and | ||
| 148 | reponsive experts will be able look at the problem and quickly | ||
| 149 | address it. Please include a thorough description of the problem | ||
| 150 | with code and data examples if at all possible. | ||
| 151 | |||
| 152 | =head2 Reporting Bugs | ||
| 153 | |||
| 154 | Report bugs to the Bioperl bug tracking system to help us keep track | ||
| 155 | of the bugs and their resolution. Bug reports can be submitted via | ||
| 156 | the web: | ||
| 157 | |||
| 158 | http://redmine.open-bio.org/projects/bioperl/ | ||
| 159 | |||
| 160 | =head1 AUTHOR - Dan Kortschak | ||
| 161 | |||
| 162 | Email dan.kortschak adelaide.edu.au | ||
| 163 | |||
| 164 | =head1 CONTRIBUTORS | ||
| 165 | |||
| 166 | Additional contributors names and emails here | ||
| 167 | |||
| 168 | =head1 APPENDIX | ||
| 169 | |||
| 170 | The rest of the documentation details each of the object methods. | ||
| 171 | Internal methods are usually preceded with a _ | ||
| 172 | |||
| 173 | =cut | ||
| 174 | |||
| 175 | # Let the code begin... | ||
| 176 | |||
| 177 | |||
| 178 | package Bio::Tools::Run::BEDTools; | ||
| 179 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 180 | our $HAVE_IO_UNCOMPRESS; | ||
| 181 | |||
| 182 | BEGIN { | ||
| 183 | 1 | ▶ | eval 'require IO::Uncompress::Gunzip; $HAVE_IO_UNCOMPRESS = 1'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 184 | } | ||
| 185 | |||
| 186 | 1 | ▶ | use IPC::Run; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 187 | |||
| 188 | # Object preamble - inherits from Bio::Root::Root | ||
| 189 | |||
| 190 | 1 | ▶ | use lib '../../..'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 191 | 1 | ▶ | use Bio::Tools::Run::BEDTools::Config; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 192 | 1 | ▶ | use Bio::Tools::Run::WrapperBase; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 193 | 1 | ▶ | use Bio::Tools::Run::WrapperBase::CommandExts; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 194 | 1 | ▶ | use Bio::Tools::GuessSeqFormat; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 195 | 1 | ▶ | use Bio::SeqFeature::Generic; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 196 | 1 | ▶ | use Bio::SeqFeature::Collection; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 197 | use Bio::SeqIO; | ||
| 198 | use File::Sort qw( sort_file ); | ||
| 199 | |||
| 200 | use base qw( Bio::Tools::Run::WrapperBase ); | ||
| 201 | |||
| 202 | ## BEDTools | ||
| 203 | our $program_name = '*bedtools'; | ||
| 204 | our $default_cmd = 'bam_to_bed'; | ||
| 205 | |||
| 206 | # Note: Other globals imported from Bio::Tools::Run::BEDTools::Config | ||
| 207 | our $qual_param = undef; | ||
| 208 | our $use_dash = 'single'; | ||
| 209 | our $join = ' '; | ||
| 210 | |||
| 211 | our %strand_translate = ( | ||
| 212 | '+' => 1, | ||
| 213 | '-' => -1, | ||
| 214 | '.' => 0 | ||
| 215 | ); | ||
| 216 | |||
| 217 | =head2 new() | ||
| 218 | |||
| 219 | Title : new | ||
| 220 | Usage : my $obj = new Bio::Tools::Run::BEDTools(); | ||
| 221 | Function: Builds a new Bio::Tools::Run::BEDTools object | ||
| 222 | Returns : an instance of Bio::Tools::Run::BEDTools | ||
| 223 | Args : | ||
| 224 | |||
| 225 | =cut | ||
| 226 | |||
| 227 | sub new { | ||
| 228 | my ($class,@args) = @_; | ||
| 229 | unless (grep /command/, @args) { | ||
| 230 | push @args, '-command', $default_cmd; | ||
| 231 | } | ||
| 232 | my $self = $class->SUPER::new(@args); | ||
| 233 | foreach (keys %command_executables) { | ||
| 234 | $self->executables($_, $self->_find_executable($command_executables{$_})); | ||
| 235 | } | ||
| 236 | $self->want($self->_rearrange([qw(WANT)],@args)); | ||
| 237 | $self->parameters_changed(1); # set on instantiation, per Bio::ParameterBaseI | ||
| 238 | return $self; | ||
| 239 | } | ||
| 240 | |||
| 241 | =head2 run() | ||
| 242 | |||
| 243 | Title : run | ||
| 244 | Usage : $result = $bedtools_fac->run(%params); | ||
| 245 | Function: Run a BEDTools command. | ||
| 246 | Returns : Command results (file, IO object or Bio object) | ||
| 247 | Args : Dependent on filespec for command. | ||
| 248 | See $bedtools_fac->filespec and BEDTools Manual. | ||
| 249 | Also accepts -want => '(raw|format|<object_class>)' - see want(). | ||
| 250 | Note : gzipped inputs are allowed if IO::Uncompress::Gunzip | ||
| 251 | is available | ||
| 252 | |||
| 253 | =cut | ||
| 254 | |||
| 255 | sub run { | ||
| 256 | my $self = shift; | ||
| 257 | |||
| 258 | my ($ann, $bed, $bg, $bgv, $bgv1, $bgv2, $bam, $bedpe, $bedpe1, $bedpe2, $seq, $genome, $out); | ||
| 259 | |||
| 260 | if (!(@_ % 2)) { | ||
| 261 | my %args = @_; | ||
| 262 | if ((grep /^-\w+/, keys %args) == keys %args) { | ||
| 263 | ($ann, $bed, $bg, $bgv, $bgv1, $bgv2, $bam, $bedpe, $bedpe1, $bedpe2, $seq, $genome, $out) = | ||
| 264 | $self->_rearrange([qw( ANN BED BG BGV BGV1 BGV2 BAM | ||
| 265 | BEDPE BEDPE1 BEDPE2 | ||
| 266 | SEQ GENOME OUT )], @_); | ||
| 267 | } else { | ||
| 268 | $self->throw("Badly formed named args: ".join(' ',@_)); | ||
| 269 | } | ||
| 270 | } else { | ||
| 271 | if (grep /^-\w+/, @_) { | ||
| 272 | $self->throw("Badly formed named args: ".join(' ',@_)); | ||
| 273 | } else { | ||
| 274 | $self->throw("Require named args."); | ||
| 275 | } | ||
| 276 | } | ||
| 277 | |||
| 278 | # Sanity checks | ||
| 279 | $self->executable || $self->throw("No executable!"); | ||
| 280 | my $cmd = $self->command if $self->can('command'); | ||
| 281 | |||
| 282 | for ($cmd) { | ||
| 283 | |||
| 284 | =pod | ||
| 285 | |||
| 286 | Command <in> <out> | ||
| 287 | |||
| 288 | annotate bgv ann(s) #out | ||
| 289 | |||
| 290 | =cut | ||
| 291 | m/^annotate$/ && do { | ||
| 292 | $bgv = $self->_uncompress($bgv); | ||
| 293 | $self->_validate_file_input(-bgv => $bgv) || $self->throw("File '$bgv' not BED/GFF/VCF format."); | ||
| 294 | @$ann = map { | ||
| 295 | my $a = $_; | ||
| 296 | $a = $self->_uncompress($a); | ||
| 297 | $self->_validate_file_input(-ann => $a) || $self->throw("File '$a' not BED/GFF/VCF format."); | ||
| 298 | $a; | ||
| 299 | } @$ann; | ||
| 300 | last; | ||
| 301 | }; | ||
| 302 | |||
| 303 | =pod | ||
| 304 | |||
| 305 | graph_union bg_files #out | ||
| 306 | |||
| 307 | =cut | ||
| 308 | m/^graph_union$/ && do { | ||
| 309 | @$bg = map { | ||
| 310 | my $g = $_; | ||
| 311 | $g = $self->_uncompress($g); | ||
| 312 | $self->_validate_file_input(-bg => $g) || $self->throw("File '$a' not BedGraph format."); | ||
| 313 | $g; | ||
| 314 | } @$bg; | ||
| 315 | last; | ||
| 316 | }; | ||
| 317 | |||
| 318 | =pod | ||
| 319 | |||
| 320 | fasta_from_bed seq bgv #out | ||
| 321 | |||
| 322 | mask_fasta_from_bed seq bgv #out | ||
| 323 | |||
| 324 | =cut | ||
| 325 | m/fasta_from_bed$/ && do { | ||
| 326 | ($out // 0) eq '-' && | ||
| 327 | $self->throw("Cannot capture results in STDOUT with sequence commands."); | ||
| 328 | $seq = $self->_uncompress($seq); | ||
| 329 | $self->_validate_file_input(-seq => $seq) || $self->throw("File '$seq' not fasta format."); | ||
| 330 | $bgv = $self->_uncompress($bgv); | ||
| 331 | $self->_validate_file_input(-bgv => $bgv) || $self->throw("File '$bgv' not BED/GFF/VCF format."); | ||
| 332 | last; | ||
| 333 | }; | ||
| 334 | |||
| 335 | =pod | ||
| 336 | |||
| 337 | bam_to_bed bam #out | ||
| 338 | |||
| 339 | =cut | ||
| 340 | |||
| 341 | m/^bam_to_bed$/ && do { | ||
| 342 | $bam = $self->_uncompress($bam); | ||
| 343 | $self->_validate_file_input(-bam => $bam) || $self->throw("File '$bam' not BAM format."); | ||
| 344 | last; | ||
| 345 | }; | ||
| 346 | |||
| 347 | |||
| 348 | =pod | ||
| 349 | |||
| 350 | bed_to_IGV bgv #out | ||
| 351 | |||
| 352 | merge bgv #out | ||
| 353 | |||
| 354 | sort bgv #out | ||
| 355 | |||
| 356 | links bgv #out | ||
| 357 | |||
| 358 | =cut | ||
| 359 | |||
| 360 | m/^(?:bed_to_IGV|merge|sort|links)$/ && do { | ||
| 361 | $bgv = $self->_uncompress($bgv); | ||
| 362 | $self->_validate_file_input(-bgv => $bgv) || $self->throw("File '$bgv' not BED/GFF/VCF format."); | ||
| 363 | }; | ||
| 364 | |||
| 365 | =pod | ||
| 366 | |||
| 367 | b12_to_b6 bed #out | ||
| 368 | |||
| 369 | overlap bed #out | ||
| 370 | |||
| 371 | group_by bed #out | ||
| 372 | |||
| 373 | =cut | ||
| 374 | |||
| 375 | m/^(?:b12_to_b6|overlap|group_by)$/ && do { | ||
| 376 | $bed = $self->_uncompress($bed); | ||
| 377 | $self->_validate_file_input(-bed => $bed) || $self->throw("File '$bgv' not BED format."); | ||
| 378 | if ($cmd eq 'group_by') { | ||
| 379 | my $c =(my @c)= split(",",$self->columns); | ||
| 380 | my $o =(my @o)= split(",",$self->operations); | ||
| 381 | unless ($c > 0 && $o == $c) { | ||
| 382 | $self->throw("The command 'group_by' requires "."paired "x($o == $c)."'-columns' and '-operations' parameters"); | ||
| 383 | } | ||
| 384 | } | ||
| 385 | last; | ||
| 386 | }; | ||
| 387 | |||
| 388 | =pod | ||
| 389 | |||
| 390 | bed_to_bam bgv #out | ||
| 391 | |||
| 392 | shuffle bgv genome #out | ||
| 393 | |||
| 394 | slop bgv genome #out | ||
| 395 | |||
| 396 | complement bgv genome #out | ||
| 397 | |||
| 398 | =cut | ||
| 399 | |||
| 400 | m/^(?:bed_to_bam|shuffle|slop|complement)$/ && do { | ||
| 401 | $bgv = $self->_uncompress($bgv); | ||
| 402 | $self->_validate_file_input(-bgv => $bgv) || $self->throw("File '$bgv' not BED/GFF/VCF format."); | ||
| 403 | $genome = $self->_uncompress($genome); | ||
| 404 | $self->_validate_file_input(-genome => $genome) || $self->throw("File '$genome' not genome format."); | ||
| 405 | if ($cmd eq 'slop') { | ||
| 406 | my $l = defined $self->add_to_left; | ||
| 407 | my $r = defined $self->add_to_right; | ||
| 408 | my $b = defined $self->add_bidirectional; | ||
| 409 | # I think I have a lisp | ||
| 410 | unless (($l && $r) || ($b xor ($l || $r))) { | ||
| 411 | $self->throw("The command 'slop' requires an unambiguous description of the slop you want"); | ||
| 412 | } | ||
| 413 | } | ||
| 414 | last; | ||
| 415 | }; | ||
| 416 | |||
| 417 | =pod | ||
| 418 | |||
| 419 | genome_coverage bed genome #out | ||
| 420 | |||
| 421 | =cut | ||
| 422 | |||
| 423 | m/^genome_coverage$/ && do { | ||
| 424 | $bed = $self->_uncompress($bed); | ||
| 425 | $self->_validate_file_input(-bed => $bed) || $self->throw("File '$bed' not BED format."); | ||
| 426 | $genome = $self->_uncompress($genome); | ||
| 427 | $self->_validate_file_input(-genome => $genome) || $self->throw("File '$genome' not genome format."); | ||
| 428 | my ($th, $tf) = $self->io->tempfile( -dir => $self->tempdir(), -suffix => '.bed' ); | ||
| 429 | $th->close; | ||
| 430 | sort_file({k => 1, I => $bed, o => $tf}); | ||
| 431 | $bed = $tf; | ||
| 432 | last; | ||
| 433 | }; | ||
| 434 | |||
| 435 | =pod | ||
| 436 | |||
| 437 | window bgv1 bgv2 #out | ||
| 438 | |||
| 439 | closest bgv1 bgv2 #out | ||
| 440 | |||
| 441 | coverage bgv1 bgv2 #out | ||
| 442 | |||
| 443 | subtract bgv1 bgv2 #out | ||
| 444 | |||
| 445 | =cut | ||
| 446 | |||
| 447 | m/^(?:window|closest|coverage|subtract)$/ && do { | ||
| 448 | $bgv1 = $self->_uncompress($bgv1); | ||
| 449 | $self->_validate_file_input(-bgv1 => $bgv1) || $self->throw("File '$bgv1' not BED/GFF/VCF format."); | ||
| 450 | $bgv2 = $self->_uncompress($bgv2); | ||
| 451 | $self->_validate_file_input(-bgv2 => $bgv2) || $self->throw("File '$bgv2' not BED/GFF/VCF format."); | ||
| 452 | }; | ||
| 453 | |||
| 454 | =pod | ||
| 455 | |||
| 456 | pair_to_pair bedpe1 bedpe2 #out | ||
| 457 | |||
| 458 | =cut | ||
| 459 | m/^pair_to_pair$/ && do { | ||
| 460 | $bedpe1 = $self->_uncompress($bedpe1); | ||
| 461 | $self->_validate_file_input(-bedpe1 => $bedpe1) || $self->throw("File '$bedpe1' not BEDPE format."); | ||
| 462 | $bedpe2 = $self->_uncompress($bedpe2); | ||
| 463 | $self->_validate_file_input(-bedpe2 => $bedpe2) || $self->throw("File '$bedpe2' not BEDPE format."); | ||
| 464 | last; | ||
| 465 | }; | ||
| 466 | |||
| 467 | =pod | ||
| 468 | |||
| 469 | intersect bgv1|bam bgv2 #out | ||
| 470 | |||
| 471 | =cut | ||
| 472 | m/^intersect$/ && do { | ||
| 473 | $bgv1 = $self->_uncompress($bgv1); | ||
| 474 | $bam = $self->_uncompress($bam); | ||
| 475 | ($bam && $self->_validate_file_input(-bam => $bam)) || ($bgv1 && $self->_validate_file_input(-bgv1 => $bgv1)) || | ||
| 476 | $self->throw("File in position 1. not correct format."); | ||
| 477 | $bgv2 = $self->_uncompress($bgv2); | ||
| 478 | $self->_validate_file_input(-bgv2 => $bgv2) || $self->throw("File '$bgv2' not BED/GFF/VCF format."); | ||
| 479 | last; | ||
| 480 | }; | ||
| 481 | |||
| 482 | =pod | ||
| 483 | |||
| 484 | pair_to_bed bedpe|bam bgv #out | ||
| 485 | |||
| 486 | bgv* signifies any of BED, GFF or VCF. ann is a bgv. | ||
| 487 | |||
| 488 | NOTE: Replace 'bgv' with 'bed' unless $use_bgv is set. | ||
| 489 | |||
| 490 | |||
| 491 | =cut | ||
| 492 | |||
| 493 | m/^pair_to_bed$/ && do { | ||
| 494 | $bedpe = $self->_uncompress($bedpe); | ||
| 495 | $bam = $self->_uncompress($bam); | ||
| 496 | ($bam && $self->_validate_file_input(-bam => $bam)) || ($bedpe && $self->_validate_file_input(-bedpe => $bedpe)) || | ||
| 497 | $self->throw("File in position 1. not correct format."); | ||
| 498 | $bgv = $self->_uncompress($bgv); | ||
| 499 | $self->_validate_file_input(-bgv => $bgv) || $self->throw("File '$bed' not BED/GFF/VCF format."); | ||
| 500 | last; | ||
| 501 | } | ||
| 502 | } | ||
| 503 | |||
| 504 | my %params = ( | ||
| 505 | '-ann' => $ann, | ||
| 506 | '-bam' => $bam, | ||
| 507 | '-bed' => $bed, | ||
| 508 | '-bgv' => $bgv, | ||
| 509 | '-bg' => $bg, | ||
| 510 | '-bgv1' => $bgv1, | ||
| 511 | '-bgv2' => $bgv2, | ||
| 512 | '-bedpe' => $bedpe, | ||
| 513 | '-bedpe1' => $bedpe1, | ||
| 514 | '-bedpe2' => $bedpe2, | ||
| 515 | '-seq' => $seq, | ||
| 516 | '-genome' => $genome | ||
| 517 | ); | ||
| 518 | map { | ||
| 519 | delete $params{$_} unless defined $params{$_} | ||
| 520 | } keys %params; | ||
| 521 | |||
| 522 | my $format = $self->_determine_format(\%params); | ||
| 523 | my $suffix = '.'.$format; | ||
| 524 | |||
| 525 | if (!defined $out) { | ||
| 526 | my ($outh, $outf) = $self->io->tempfile( -dir => $self->tempdir(), -suffix => $suffix ); | ||
| 527 | $outh->close; | ||
| 528 | $out = $outf; | ||
| 529 | } elsif ($out ne '-') { | ||
| 530 | $out .= $suffix; | ||
| 531 | } else { | ||
| 532 | undef $out; | ||
| 533 | } | ||
| 534 | $params{'-out'} = $out if defined $out; | ||
| 535 | |||
| 536 | $self->_run(%params); | ||
| 537 | |||
| 538 | $self->{'_result'}->{'file_name'} = $out // '-'; | ||
| 539 | $self->{'_result'}->{'format'} = $format; | ||
| 540 | $self->{'_result'}->{'file'} = defined $out ? Bio::Root::IO->new( -file => $out ) : undef; | ||
| 541 | |||
| 542 | return $self->result; | ||
| 543 | } | ||
| 544 | |||
| 545 | sub _uncompress { | ||
| 546 | my ($self, $file) = @_; | ||
| 547 | |||
| 548 | return if !defined $file; | ||
| 549 | |||
| 550 | return $file unless ($file =~ m/\.gz[^.]*$/); | ||
| 551 | |||
| 552 | return $file unless (-e $file && -r _); # other people will deal with this | ||
| 553 | |||
| 554 | unless ($HAVE_IO_UNCOMPRESS) { | ||
| 555 | croak( "IO::Uncompress::Gunzip not available, can't expand '$file'" ); | ||
| 556 | } | ||
| 557 | my ($tfh, $tf) = $self->io->tempfile( -dir => $self->tempdir() ); | ||
| 558 | my $z = IO::Uncompress::Gunzip->new($file); | ||
| 559 | while (my $block = $z->getline) { print $tfh $block } | ||
| 560 | close $tfh; | ||
| 561 | |||
| 562 | return $tf | ||
| 563 | } | ||
| 564 | |||
| 565 | =head2 want() | ||
| 566 | |||
| 567 | Title : want | ||
| 568 | Usage : $bowtiefac->want( $class ) | ||
| 569 | Function: make factory return $class, or 'raw' results in file | ||
| 570 | or 'format' for result format | ||
| 571 | All commands can return Bio::Root::IO | ||
| 572 | commands returning: can return object: | ||
| 573 | - BED or BEDPE - Bio::SeqFeature::Collection | ||
| 574 | - sequence - Bio::SeqIO | ||
| 575 | Returns : return wanted type | ||
| 576 | Args : [optional] string indicating class or raw of wanted result | ||
| 577 | |||
| 578 | =cut | ||
| 579 | |||
| 580 | sub want { | ||
| 581 | my $self = shift; | ||
| 582 | return $self->{'_want'} = shift if @_; | ||
| 583 | return $self->{'_want'}; | ||
| 584 | } | ||
| 585 | |||
| 586 | =head2 result() | ||
| 587 | |||
| 588 | Title : result | ||
| 589 | Usage : $bedtoolsfac->result( [-want => $type|$format] ) | ||
| 590 | Function: return result in wanted format | ||
| 591 | Returns : results | ||
| 592 | Args : [optional] hashref of wanted type | ||
| 593 | Note : -want arg does not persist between result() call when | ||
| 594 | specified in result(), for persistence, use want() | ||
| 595 | |||
| 596 | =cut | ||
| 597 | |||
| 598 | sub result { | ||
| 599 | my ($self, @args) = @_; | ||
| 600 | |||
| 601 | my $want = $self->_rearrange([qw(WANT)],@args); | ||
| 602 | $want ||= $self->want; | ||
| 603 | my $cmd = $self->command if $self->can('command'); | ||
| 604 | my $format = $self->{'_result'}->{'format'}; | ||
| 605 | my $file_name = $self->{'_result'}->{'file_name'}; | ||
| 606 | |||
| 607 | return $self->{'_result'}->{'format'} if (defined $want && $want eq 'format'); | ||
| 608 | return $self->{'_result'}->{'file_name'} if (!$want || $want eq 'raw'); | ||
| 609 | return $self->{'_result'}->{'file'} if ($want =~ m/^Bio::Root::IO/); # this will be undef if -out eq '-' | ||
| 610 | |||
| 611 | for ($format) { # these are dissected more finely than seems resonable to allow easy extension | ||
| 612 | m/bed/ && do { | ||
| 613 | for ($want) { | ||
| 614 | m/Bio::SeqFeature::Collection/ && do { | ||
| 615 | unless (defined $self->{'_result'}->{'object'} && | ||
| 616 | ref($self->{'_result'}->{'object'}) =~ m/^Bio::SeqFeature::Collection/) { | ||
| 617 | $self->{'_result'}->{'object'} = $self->_read_bed; | ||
| 618 | } | ||
| 619 | return $self->{'_result'}->{'object'}; | ||
| 620 | }; | ||
| 621 | $self->warn("Cannot make '$_' for $format."); | ||
| 622 | return; | ||
| 623 | } | ||
| 624 | last; | ||
| 625 | }; | ||
| 626 | m/bedpe/ && do { | ||
| 627 | for ($want) { | ||
| 628 | m/Bio::SeqFeature::Collection/ && do { | ||
| 629 | unless (defined $self->{'_result'}->{'object'} && | ||
| 630 | ref($self->{'_result'}->{'object'}) =~ m/^Bio::SeqFeature::Collection/) { | ||
| 631 | $self->{'_result'}->{'object'} = $self->_read_bedpe; | ||
| 632 | } | ||
| 633 | return $self->{'_result'}->{'object'}; | ||
| 634 | }; | ||
| 635 | $self->warn("Cannot make '$_' for $format."); | ||
| 636 | return; | ||
| 637 | } | ||
| 638 | last; | ||
| 639 | }; | ||
| 640 | m/bam/ && do { | ||
| 641 | $self->warn("Cannot make '$_' for $format."); | ||
| 642 | return; | ||
| 643 | }; | ||
| 644 | m/^(?:fasta|raw)$/ && do { | ||
| 645 | for ($want) { | ||
| 646 | m/Bio::SeqIO/ && do { | ||
| 647 | $file_name eq '-' && $self->throw("Cannot make a SeqIO object from STDOUT."); | ||
| 648 | unless (defined $self->{'_result'}->{'object'} && | ||
| 649 | ref($self->{'_result'}->{'object'}) =~ m/^Bio::SeqIO/) { | ||
| 650 | $self->{'_result'}->{'object'} = | ||
| 651 | Bio::SeqIO->new(-file => $file_name, | ||
| 652 | -format => $format); | ||
| 653 | } | ||
| 654 | return $self->{'_result'}->{'object'}; | ||
| 655 | }; | ||
| 656 | $self->warn("Cannot make '$_' for $format."); | ||
| 657 | return; | ||
| 658 | } | ||
| 659 | last; | ||
| 660 | }; | ||
| 661 | m/tab/ && do { | ||
| 662 | $self->warn("Cannot make '$_' for $format."); | ||
| 663 | return; | ||
| 664 | }; | ||
| 665 | m/igv/ && do { | ||
| 666 | $self->warn("Cannot make '$_' for $format."); | ||
| 667 | return; | ||
| 668 | }; | ||
| 669 | m/html/ && do { | ||
| 670 | $self->warn("Cannot make '$_' for $format."); | ||
| 671 | return; | ||
| 672 | }; | ||
| 673 | do { | ||
| 674 | $self->warn("Result format '$_' not recognised - have you called run() yet?"); | ||
| 675 | } | ||
| 676 | } | ||
| 677 | } | ||
| 678 | |||
| 679 | =head2 _determine_format() | ||
| 680 | |||
| 681 | Title : _determine_format( $has_run ) | ||
| 682 | Usage : $bedtools-fac->_determine_format | ||
| 683 | Function: determine the format of output for current options | ||
| 684 | Returns : format of bowtie output | ||
| 685 | Args : [optional] boolean to indicate result exists | ||
| 686 | |||
| 687 | =cut | ||
| 688 | |||
| 689 | sub _determine_format { | ||
| 690 | my ($self, $params) = @_; | ||
| 691 | |||
| 692 | my $cmd = $self->command if $self->can('command'); | ||
| 693 | my $format = $format_lookup{$cmd}; | ||
| 694 | |||
| 695 | #special cases - dependent on switches and files | ||
| 696 | for ($cmd) { | ||
| 697 | m/^intersect$/ && do { | ||
| 698 | return 'bed' if !defined $$params{'-bam'} || $self->write_bed; | ||
| 699 | return 'bam'; | ||
| 700 | }; | ||
| 701 | m/^pair_to_bed$/ && do { | ||
| 702 | return 'bedpe' if !defined $$params{'-bam'} || $self->write_bedpe; | ||
| 703 | return 'bam'; | ||
| 704 | }; | ||
| 705 | m/^fasta_from_bed$/ && do { | ||
| 706 | return $self->output_tab_format ? 'tab' : 'fasta'; | ||
| 707 | } | ||
| 708 | } | ||
| 709 | |||
| 710 | return $format; | ||
| 711 | } | ||
| 712 | |||
| 713 | =head2 _read_bed() | ||
| 714 | |||
| 715 | Title : _read_bed() | ||
| 716 | Usage : $bedtools_fac->_read_bed | ||
| 717 | Function: return a Bio::SeqFeature::Collection object from a BED file | ||
| 718 | Returns : Bio::SeqFeature::Collection | ||
| 719 | Args : | ||
| 720 | |||
| 721 | =cut | ||
| 722 | |||
| 723 | sub _read_bed { | ||
| 724 | my ($self) = shift; | ||
| 725 | |||
| 726 | my @features; | ||
| 727 | |||
| 728 | if ($self->{'_result'}->{'file_name'} ne '-') { | ||
| 729 | my $in = $self->{'_result'}->{'file'}; | ||
| 730 | while (my $feature = $in->_readline) { | ||
| 731 | chomp $feature; | ||
| 732 | push @features, _read_bed_line($feature); | ||
| 733 | } | ||
| 734 | } else { | ||
| 735 | for my $feature (split("\cJ", $self->stdout)) { | ||
| 736 | push @features, _read_bed_line($feature); | ||
| 737 | } | ||
| 738 | } | ||
| 739 | |||
| 740 | my $collection = Bio::SeqFeature::Collection->new; | ||
| 741 | $collection->add_features(\@features); | ||
| 742 | |||
| 743 | return $collection; | ||
| 744 | } | ||
| 745 | |||
| 746 | sub _read_bed_line { | ||
| 747 | my $feature = shift; | ||
| 748 | |||
| 749 | my ($chr, $start, $end, $name, $score, $strand, | ||
| 750 | $thick_start, $thick_end, $item_RGB, $block_count, $block_size, $block_start) = | ||
| 751 | split("\cI",$feature); | ||
| 752 | $strand ||= '.'; # BED3 doesn't have strand data - for 'merge' and 'complement' | ||
| 753 | |||
| 754 | return Bio::SeqFeature::Generic->new( -seq_id => $chr, | ||
| 755 | -primary => $name, | ||
| 756 | -start => $start, | ||
| 757 | -end => $end, | ||
| 758 | -strand => $strand_translate{$strand}, | ||
| 759 | -score => $score, | ||
| 760 | -tag => { thick_start => $thick_start, | ||
| 761 | thick_end => $thick_end, | ||
| 762 | item_RGB => $item_RGB, | ||
| 763 | block_count => $block_count, | ||
| 764 | block_size => $block_size, | ||
| 765 | block_start => $block_size } | ||
| 766 | ); | ||
| 767 | } | ||
| 768 | |||
| 769 | =head2 _read_bedpe() | ||
| 770 | |||
| 771 | Title : _read_bedpe() | ||
| 772 | Usage : $bedtools_fac->_read_bedpe | ||
| 773 | Function: return a Bio::SeqFeature::Collection object from a BEDPE file | ||
| 774 | Returns : Bio::SeqFeature::Collection | ||
| 775 | Args : | ||
| 776 | |||
| 777 | =cut | ||
| 778 | |||
| 779 | sub _read_bedpe { | ||
| 780 | my ($self) = shift; | ||
| 781 | |||
| 782 | my @features; | ||
| 783 | |||
| 784 | if ($self->{'_result'}->{'file_name'} ne '-') { | ||
| 785 | my $in = $self->{'_result'}->{'file'}; | ||
| 786 | while (my $feature = $in->_readline) { | ||
| 787 | chomp $feature; | ||
| 788 | push @features, _read_bedpe_line($feature); | ||
| 789 | } | ||
| 790 | } else { | ||
| 791 | for my $feature (split("\cJ", $self->stdout)) { | ||
| 792 | push @features, _read_bedpe_line($feature); | ||
| 793 | } | ||
| 794 | } | ||
| 795 | |||
| 796 | my $collection = Bio::SeqFeature::Collection->new; | ||
| 797 | $collection->add_features(\@features); | ||
| 798 | |||
| 799 | return $collection; | ||
| 800 | } | ||
| 801 | |||
| 802 | sub _read_bedpe_line { | ||
| 803 | my $feature = shift; | ||
| 804 | |||
| 805 | my ($chr1, $start1, $end1, $chr2, $start2, $end2, $name, $score, $strand1, $strand2, @add) = | ||
| 806 | split("\cI",$feature); | ||
| 807 | $strand1 ||= '.'; | ||
| 808 | $strand2 ||= '.'; | ||
| 809 | |||
| 810 | return Bio::SeqFeature::FeaturePair->new( -primary => $name, | ||
| 811 | -seq_id => $chr1, | ||
| 812 | -start => $start1, | ||
| 813 | -end => $end1, | ||
| 814 | -strand => $strand_translate{$strand1}, | ||
| 815 | |||
| 816 | -hprimary_tag => $name, | ||
| 817 | -hseqname => $chr2, | ||
| 818 | -hstart => $start2, | ||
| 819 | -hend => $end2, | ||
| 820 | -hstrand => $strand_translate{$strand2}, | ||
| 821 | |||
| 822 | -score => $score | ||
| 823 | ); | ||
| 824 | } | ||
| 825 | |||
| 826 | =head2 _validate_file_input() | ||
| 827 | |||
| 828 | Title : _validate_file_input | ||
| 829 | Usage : $bedtools_fac->_validate_file_input( -type => $file ) | ||
| 830 | Function: validate file type for file spec | ||
| 831 | Returns : file type if valid type for file spec | ||
| 832 | Args : hash of filespec => file_name | ||
| 833 | |||
| 834 | =cut | ||
| 835 | |||
| 836 | sub _validate_file_input { | ||
| 837 | my ($self, @args) = @_; | ||
| 838 | my (%args); | ||
| 839 | if (grep (/^-/, @args) && (@args > 1)) { # named parms | ||
| 840 | $self->throw("Wrong number of args - requires one named arg") if (@args > 2); | ||
| 841 | s/^-// for @args; | ||
| 842 | %args = @args; | ||
| 843 | } else { | ||
| 844 | $self->throw("Must provide named filespec"); | ||
| 845 | } | ||
| 846 | |||
| 847 | for (keys %args) { | ||
| 848 | m/bam/ && do { | ||
| 849 | return 'bam'; | ||
| 850 | }; | ||
| 851 | do { | ||
| 852 | return unless ( -e $args{$_} && -r _ ); | ||
| 853 | my $guesser = Bio::Tools::GuessSeqFormat->new(-file=>$args{$_}); | ||
| 854 | return $guesser->guess if grep {$guesser->guess =~ m/$_/} @{$accepted_types{$_}}; | ||
| 855 | } | ||
| 856 | } | ||
| 857 | return; | ||
| 858 | } | ||
| 859 | |||
| 860 | =head2 version() | ||
| 861 | |||
| 862 | Title : version | ||
| 863 | Usage : $version = $bedtools_fac->version() | ||
| 864 | Function: Returns the program version (if available) | ||
| 865 | Returns : string representing location and version of the program | ||
| 866 | |||
| 867 | =cut | ||
| 868 | |||
| 869 | sub version{ | ||
| 870 | my ($self) = @_; | ||
| 871 | |||
| 872 | my $cmd = $self->command if $self->can('command'); | ||
| 873 | |||
| 874 | defined $cmd or $self->throw("No command defined - cannot determine program executable"); | ||
| 875 | |||
| 876 | # new bahaviour for some BEDTools executables - breaks previous approach to getting version | ||
| 877 | # $dummy can be any non-recognised parameter - '-version' works for most | ||
| 878 | my $dummy = '-version'; | ||
| 879 | $dummy = '-examples' if $cmd =~ /graph_union/; | ||
| 880 | |||
| 881 | my ($in, $out, $err); | ||
| 882 | my $dum; | ||
| 883 | $in = \$dum; | ||
| 884 | $out = \$self->{'stdout'}; | ||
| 885 | $err = \$self->{'stderr'}; | ||
| 886 | |||
| 887 | # Get program executable | ||
| 888 | my $exe = $self->executable; | ||
| 889 | |||
| 890 | my @ipc_args = ( $exe, $dummy ); | ||
| 891 | |||
| 892 | eval { | ||
| 893 | IPC::Run::run(\@ipc_args, $in, $out, $err) or | ||
| 894 | die ("There was a problem running $exe : $!"); | ||
| 895 | }; | ||
| 896 | # We don't bother trying to catch this: version is returned as an illegal file seek | ||
| 897 | |||
| 898 | my @details = split("\n",$self->stderr); | ||
| 899 | (my $version) = grep /^Program: .*$/, @details; | ||
| 900 | $version =~ s/^Program: //; | ||
| 901 | |||
| 902 | return $version; | ||
| 903 | } | ||
| 904 | |||
| 905 | sub available_commands { shift->available_parameters('commands') }; | ||
| 906 | |||
| 907 | sub filespec { shift->available_parameters('filespec') }; | ||
| 908 | |||
| 909 | 1; | ||