This file is indexed.

/usr/share/perl5/Bio/Tools/Run/BEDTools.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
#
# BioPerl module for Bio::Tools::Run::BEDTools
#
# Please direct questions and support issues to <bioperl-l@bioperl.org>
#
# Cared for by Dan Kortschak <dan.kortschak@adelaide.edu.au>
#
# Copyright Dan Kortschak
#
# You may distribute this module under the same terms as perl itself

# POD documentation - main docs before the code

=head1 NAME

Bio::Tools::Run::BEDTools - Run wrapper for the BEDTools suite of programs *BETA*

=head1 SYNOPSIS

 # use a BEDTools program
 $bedtools_fac = Bio::Tools::Run::BEDTools->new( -command => 'subtract' );
 $result_file = $bedtools_fac->run( -bed1 => 'genes.bed', -bed2 => 'mask.bed' );
 
 # if IO::Uncompress::Gunzip is available...
 $result_file = $bedtools_fac->run( -bed1 => 'genes.bed.gz', -bed2 => 'mask.bed.gz' );
 
 # be more strict
 $bedtools_fac->set_parameters( -strandedness => 1 );
 
 # and even more...
 $bedtools_fac->set_parameters( -minimum_overlap => 1e-6 );
 
 # create a Bio::SeqFeature::Collection object
 $features = $bedtools_fac->result( -want => 'Bio::SeqFeature::Collection' );
 
=head1 DEPRECATION WARNING

Most executables from BEDTools v>=2.10.1 can read GFF and VCF formats
in addition to BED format. This requires the use of a new input file param,
shown in the following documentation, '-bgv', in place of '-bed' for the
executables that can do this.

This behaviour breaks existing scripts.

=head1 DESCRIPTION

This module provides a wrapper interface for Aaron R. Quinlan and Ira M. Hall's
utilities C<BEDTools> that allow for (among other things):

=over

=item * Intersecting two BED files in search of overlapping features.

=item * Merging overlapping features.

=item * Screening for paired-end (PE) overlaps between PE sequences and existing genomic features.

=item * Calculating the depth and breadth of sequence coverage across defined "windows" in a genome. 

=back

(see L<http://code.google.com/p/bedtools/> for manuals and downloads).


=head1 OPTIONS

C<BEDTools> is a suite of 17 commandline executable. This module attempts to 
provide and options comprehensively. You can browse the choices like so:

 $bedtools_fac = Bio::Tools::Run::BEDTools->new;

 # all bowtie commands
 @all_commands = $bedtools_fac->available_parameters('commands');
 @all_commands = $bedtools_fac->available_commands; # alias

 # just for default command ('bam_to_bed')
 @btb_params = $bedtools_fac->available_parameters('params');
 @btb_switches = $bedtools_fac->available_parameters('switches');
 @btb_all_options = $bedtools_fac->available_parameters();

Reasonably mnemonic names have been assigned to the single-letter
command line options. These are the names returned by
C<available_parameters>, and can be used in the factory constructor
like typical BioPerl named parameters.

As a number of options are mutually exclusive, and the interpretation of
intent is based on last-pass option reaching bowtie with potentially unpredicted
results. This module will prevent inconsistent switches and parameters
from being passed.

See L<http://code.google.com/p/bedtools/> for details of BEDTools options.

=head1 FILES

When a command requires filenames, these are provided to the C<run> method, not
the constructor (C<new()>). To see the set of files required by a command, use
C<available_parameters('filespec')> or the alias C<filespec()>:

  $bedtools_fac = Bio::Tools::Run::BEDTools->new( -command => 'pair_to_bed' );
  @filespec = $bedtools_fac->filespec;

This example returns the following array:

 #bedpe
 #bam
 bed
 #out

This indicates that the bed (C<BEDTools> BED format) file MUST be
specified, and that the out, bedpe (C<BEDTools> BEDPE format) and bam 
(C<SAM> binary format) file MAY be specified (Note that in this case you
MUST provide ONE of bedpe OR bam, the module at this stage does not allow
this information to be queried). Use these in the C<run> call like so:

 $bedtools_fac->run( -bedpe => 'paired.bedpe',
                     -bgv => 'genes.bed',
                     -out => 'overlap' );

The object will store the programs STDERR output for you in the C<stderr()> 
attribute:

 handle_bed_warning($bedtools_fac) if ($bedtools_fac->stderr =~ /Usage:/);

For the commands 'fasta_from_bed' and 'mask_fasta_from_bed' STDOUT will also
be captured in the C<stdout()> attribute by default and all other commands
can be forced to capture program output in STDOUT by setting the -out
filespec parameter to '-'.

=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 - Dan Kortschak

 Email dan.kortschak adelaide.edu.au

=head1 CONTRIBUTORS

Additional contributors names and emails here

=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::BEDTools;
use strict;
our $HAVE_IO_UNCOMPRESS;

BEGIN {
    eval 'require IO::Uncompress::Gunzip; $HAVE_IO_UNCOMPRESS = 1';
}

use IPC::Run;

# Object preamble - inherits from Bio::Root::Root

use lib '../../..';
use Bio::Tools::Run::BEDTools::Config;
use Bio::Tools::Run::WrapperBase;
use Bio::Tools::Run::WrapperBase::CommandExts;
use Bio::Tools::GuessSeqFormat;
use Bio::SeqFeature::Generic;
use Bio::SeqFeature::Collection;
use Bio::SeqIO;
use File::Sort qw( sort_file );

use base qw( Bio::Tools::Run::WrapperBase );

## BEDTools
our $program_name = '*bedtools';
our $default_cmd = 'bam_to_bed';

# Note: Other globals imported from Bio::Tools::Run::BEDTools::Config
our $qual_param = undef;
our $use_dash = 'single';
our $join = ' ';

our %strand_translate = (
	'+' => 1,
	'-' => -1,
	'.' => 0
	);

=head2 new()

 Title   : new
 Usage   : my $obj = new Bio::Tools::Run::BEDTools();
 Function: Builds a new Bio::Tools::Run::BEDTools object
 Returns : an instance of Bio::Tools::Run::BEDTools
 Args    :

=cut

sub new {
	my ($class,@args) = @_;
	unless (grep /command/, @args) {
		push @args, '-command', $default_cmd;
	}
	my $self = $class->SUPER::new(@args);
	foreach (keys %command_executables) {
		$self->executables($_, $self->_find_executable($command_executables{$_}));
	}
	$self->want($self->_rearrange([qw(WANT)],@args));
	$self->parameters_changed(1); # set on instantiation, per Bio::ParameterBaseI
	return $self;
}

=head2 run()

 Title   : run
 Usage   : $result = $bedtools_fac->run(%params);
 Function: Run a BEDTools command.
 Returns : Command results (file, IO object or Bio object)
 Args    : Dependent on filespec for command. 
           See $bedtools_fac->filespec and BEDTools Manual.
           Also accepts -want => '(raw|format|<object_class>)' - see want().
 Note    : gzipped inputs are allowed if IO::Uncompress::Gunzip
           is available

=cut

sub run {
	my $self = shift;

	my ($ann, $bed, $bg, $bgv, $bgv1, $bgv2, $bam, $bedpe, $bedpe1, $bedpe2, $seq, $genome, $out);
	
	if (!(@_ % 2)) {
		my %args = @_;
		if ((grep /^-\w+/, keys %args) == keys %args) {
			($ann, $bed, $bg, $bgv, $bgv1, $bgv2, $bam, $bedpe, $bedpe1, $bedpe2, $seq, $genome, $out) =
				$self->_rearrange([qw( ANN BED BG BGV BGV1 BGV2 BAM 
				                       BEDPE BEDPE1 BEDPE2
				                       SEQ GENOME OUT )], @_);
		} else {
			$self->throw("Badly formed named args: ".join(' ',@_));
		}
	} else {
		if (grep /^-\w+/, @_) {
			$self->throw("Badly formed named args: ".join(' ',@_));
		} else {
			$self->throw("Require named args.");
		}
	}

	# Sanity checks
	$self->executable || $self->throw("No executable!");
	my $cmd = $self->command if $self->can('command');

	for ($cmd) {

=pod

           Command              	<in>			<out>

           annotate             bgv		ann(s)		#out

=cut
		m/^annotate$/ && do {
			$bgv = $self->_uncompress($bgv);
			$self->_validate_file_input(-bgv => $bgv) || $self->throw("File '$bgv' not BED/GFF/VCF format.");
			@$ann = map {
				my $a = $_;
	            $a = $self->_uncompress($a);
				$self->_validate_file_input(-ann => $a) || $self->throw("File '$a' not BED/GFF/VCF format.");
				$a;
			} @$ann;
			last;
		};
		
=pod

           graph_union	        bg_files			#out

=cut
		m/^graph_union$/ && do {
			@$bg = map {
				my $g = $_;
	            $g = $self->_uncompress($g);
				$self->_validate_file_input(-bg => $g) || $self->throw("File '$a' not BedGraph format.");
				$g;
			} @$bg;
			last;
		};
		
=pod

           fasta_from_bed       seq		bgv		#out

           mask_fasta_from_bed  seq		bgv		#out

=cut
		m/fasta_from_bed$/ && do {
			($out // 0) eq '-' &&
				$self->throw("Cannot capture results in STDOUT with sequence commands.");
			$seq = $self->_uncompress($seq);
			$self->_validate_file_input(-seq => $seq) || $self->throw("File '$seq' not fasta format.");
			$bgv = $self->_uncompress($bgv);
			$self->_validate_file_input(-bgv => $bgv) || $self->throw("File '$bgv' not BED/GFF/VCF format.");
			last;
		};
		
=pod

           bam_to_bed           bam 				#out

=cut

		m/^bam_to_bed$/ && do {
			$bam = $self->_uncompress($bam);
			$self->_validate_file_input(-bam => $bam) || $self->throw("File '$bam' not BAM format.");
			last;
		};


=pod

           bed_to_IGV           bgv 				#out

           merge                bgv 				#out

           sort                 bgv 				#out

           links                bgv 				#out

=cut

		m/^(?:bed_to_IGV|merge|sort|links)$/ && do {
			$bgv = $self->_uncompress($bgv);
			$self->_validate_file_input(-bgv => $bgv) || $self->throw("File '$bgv' not BED/GFF/VCF format.");
		};

=pod

           b12_to_b6            bed 				#out

           overlap              bed 				#out

           group_by             bed 				#out

=cut

		m/^(?:b12_to_b6|overlap|group_by)$/ && do {
			$bed = $self->_uncompress($bed);
			$self->_validate_file_input(-bed => $bed) || $self->throw("File '$bgv' not BED format.");
			if ($cmd eq 'group_by') {
				my $c =(my @c)= split(",",$self->columns);
				my $o =(my @o)= split(",",$self->operations);
				unless ($c > 0 && $o == $c) {
					$self->throw("The command 'group_by' requires "."paired "x($o == $c)."'-columns' and '-operations' parameters");
				}
			}
			last;
		};

=pod

           bed_to_bam           bgv 				#out

           shuffle              bgv 		genome		#out

           slop                 bgv 		genome		#out

           complement           bgv 		genome		#out

=cut

		m/^(?:bed_to_bam|shuffle|slop|complement)$/ && do {
			$bgv = $self->_uncompress($bgv);
			$self->_validate_file_input(-bgv => $bgv) || $self->throw("File '$bgv' not BED/GFF/VCF format.");
			$genome = $self->_uncompress($genome);
			$self->_validate_file_input(-genome => $genome) || $self->throw("File '$genome' not genome format.");
			if ($cmd eq 'slop') {
				my $l = defined $self->add_to_left;
				my $r = defined $self->add_to_right;
				my $b = defined $self->add_bidirectional;
				# I think I have a lisp
				unless (($l && $r) || ($b xor ($l || $r))) {
					$self->throw("The command 'slop' requires an unambiguous description of the slop you want");
				}
			}
			last;
		};

=pod

           genome_coverage      bed 		genome		#out

=cut

		m/^genome_coverage$/ && do {
			$bed = $self->_uncompress($bed);
			$self->_validate_file_input(-bed => $bed) || $self->throw("File '$bed' not BED format.");
			$genome = $self->_uncompress($genome);
			$self->_validate_file_input(-genome => $genome) || $self->throw("File '$genome' not genome format.");
			my ($th, $tf) = $self->io->tempfile( -dir => $self->tempdir(), -suffix => '.bed' );
			$th->close;
			sort_file({k => 1, I => $bed, o => $tf});
			$bed = $tf;
			last;
		};

=pod

           window               bgv1		bgv2		#out

           closest              bgv1		bgv2		#out

           coverage             bgv1		bgv2		#out

           subtract             bgv1		bgv2		#out

=cut

		m/^(?:window|closest|coverage|subtract)$/ && do {
			$bgv1 = $self->_uncompress($bgv1);
			$self->_validate_file_input(-bgv1 => $bgv1) || $self->throw("File '$bgv1' not BED/GFF/VCF format.");
			$bgv2 = $self->_uncompress($bgv2);
			$self->_validate_file_input(-bgv2 => $bgv2) || $self->throw("File '$bgv2' not BED/GFF/VCF format.");
		};

=pod

           pair_to_pair         bedpe1		bedpe2		#out

=cut
		m/^pair_to_pair$/ && do {
			$bedpe1 = $self->_uncompress($bedpe1);
			$self->_validate_file_input(-bedpe1 => $bedpe1) || $self->throw("File '$bedpe1' not BEDPE format.");
			$bedpe2 = $self->_uncompress($bedpe2);
			$self->_validate_file_input(-bedpe2 => $bedpe2) || $self->throw("File '$bedpe2' not BEDPE format.");
			last;
		};

=pod

           intersect            bgv1|bam	bgv2		#out

=cut
		m/^intersect$/ && do {
			$bgv1 = $self->_uncompress($bgv1);
			$bam = $self->_uncompress($bam);
			($bam && $self->_validate_file_input(-bam => $bam)) || ($bgv1 && $self->_validate_file_input(-bgv1 => $bgv1)) ||
				$self->throw("File in position 1. not correct format.");
			$bgv2 = $self->_uncompress($bgv2);
			$self->_validate_file_input(-bgv2 => $bgv2) || $self->throw("File '$bgv2' not BED/GFF/VCF format.");			
			last;
		};

=pod

           pair_to_bed          bedpe|bam	bgv		#out

           bgv* signifies any of BED, GFF or VCF. ann is a bgv.
           
           NOTE: Replace 'bgv' with 'bed' unless $use_bgv is set.


=cut

		m/^pair_to_bed$/ && do {
			$bedpe = $self->_uncompress($bedpe);
			$bam = $self->_uncompress($bam);
			($bam && $self->_validate_file_input(-bam => $bam)) || ($bedpe && $self->_validate_file_input(-bedpe => $bedpe)) ||
				$self->throw("File in position 1. not correct format.");
			$bgv = $self->_uncompress($bgv);
			$self->_validate_file_input(-bgv => $bgv) || $self->throw("File '$bed' not BED/GFF/VCF format.");
			last;
		}
	}
	
	my %params = (    
		'-ann'        => $ann,
		'-bam'        => $bam,
		'-bed'        => $bed,
		'-bgv'        => $bgv,
		'-bg'         => $bg,
		'-bgv1'       => $bgv1,
		'-bgv2'       => $bgv2,
		'-bedpe'      => $bedpe,
		'-bedpe1'     => $bedpe1,
		'-bedpe2'     => $bedpe2,
		'-seq'        => $seq,
		'-genome'     => $genome
	);
	map {
		delete $params{$_} unless defined $params{$_}
	} keys %params;

	my $format = $self->_determine_format(\%params);
	my $suffix = '.'.$format;
	
	if (!defined $out) {
		my ($outh, $outf) = $self->io->tempfile( -dir => $self->tempdir(), -suffix => $suffix );
		$outh->close;
		$out = $outf;
	} elsif ($out ne '-') {
		$out .= $suffix;
	} else {
		undef $out;
	}
	$params{'-out'} = $out if defined $out;

	$self->_run(%params);

	$self->{'_result'}->{'file_name'} = $out // '-';
	$self->{'_result'}->{'format'} = $format;
	$self->{'_result'}->{'file'} = defined $out ? Bio::Root::IO->new( -file => $out ) : undef;
	
	return $self->result;
}

sub _uncompress {
        my ($self, $file) = @_;

	return if !defined $file;

        return $file unless ($file =~ m/\.gz[^.]*$/);

	return $file unless (-e $file && -r _); # other people will deal with this

        unless ($HAVE_IO_UNCOMPRESS) {
                croak( "IO::Uncompress::Gunzip not available, can't expand '$file'" );
        }
        my ($tfh, $tf) = $self->io->tempfile( -dir => $self->tempdir() );
        my $z = IO::Uncompress::Gunzip->new($file);
        while (my $block = $z->getline) { print $tfh $block }
        close $tfh;

        return $tf
}

=head2 want()

 Title   : want
 Usage   : $bowtiefac->want( $class )
 Function: make factory return $class, or 'raw' results in file
           or 'format' for result format
           All commands can return Bio::Root::IO
           commands returning:       can return object:
           - BED or BEDPE            - Bio::SeqFeature::Collection
           - sequence                - Bio::SeqIO
 Returns : return wanted type
 Args    : [optional] string indicating class or raw of wanted result

=cut

sub want {
	my $self = shift;
	return $self->{'_want'} = shift if @_;
	return $self->{'_want'};
}

=head2 result()

 Title   : result
 Usage   : $bedtoolsfac->result( [-want => $type|$format] )
 Function: return result in wanted format
 Returns : results
 Args    : [optional] hashref of wanted type
 Note    : -want arg does not persist between result() call when
           specified in result(), for persistence, use want()

=cut

sub result {
	my ($self, @args) = @_;
	
	my $want = $self->_rearrange([qw(WANT)],@args);
	$want ||= $self->want;
	my $cmd = $self->command if $self->can('command');
	my $format = $self->{'_result'}->{'format'};
	my $file_name = $self->{'_result'}->{'file_name'};

	return $self->{'_result'}->{'format'} if (defined $want && $want eq 'format');
	return $self->{'_result'}->{'file_name'} if (!$want || $want eq 'raw');
	return $self->{'_result'}->{'file'} if ($want =~ m/^Bio::Root::IO/); # this will be undef if -out eq '-'
	
	for ($format) { # these are dissected more finely than seems resonable to allow easy extension 
		m/bed/ && do {
			for ($want) {
				m/Bio::SeqFeature::Collection/ && do {
					unless (defined $self->{'_result'}->{'object'} &&
						ref($self->{'_result'}->{'object'}) =~ m/^Bio::SeqFeature::Collection/) {
							$self->{'_result'}->{'object'} = $self->_read_bed;
					}
					return $self->{'_result'}->{'object'};
				};
				$self->warn("Cannot make '$_' for $format.");
				return;
			}
			last;
		};
		m/bedpe/ && do {
			for ($want) {
				m/Bio::SeqFeature::Collection/ && do {
					unless (defined $self->{'_result'}->{'object'} &&
						ref($self->{'_result'}->{'object'}) =~ m/^Bio::SeqFeature::Collection/) {
							$self->{'_result'}->{'object'} = $self->_read_bedpe;
					}
					return $self->{'_result'}->{'object'};
				};
				$self->warn("Cannot make '$_' for $format.");
				return;
			}
			last;
		};
		m/bam/ && do {
			$self->warn("Cannot make '$_' for $format.");
			return;
		};
		m/^(?:fasta|raw)$/ && do {
			for ($want) {
				m/Bio::SeqIO/ && do {
					$file_name eq '-' && $self->throw("Cannot make a SeqIO object from STDOUT.");
					unless (defined $self->{'_result'}->{'object'} &&
						ref($self->{'_result'}->{'object'}) =~ m/^Bio::SeqIO/) {
							$self->{'_result'}->{'object'} =
								Bio::SeqIO->new(-file => $file_name,
								                -format => $format);
					}
					return $self->{'_result'}->{'object'};
				};
				$self->warn("Cannot make '$_' for $format.");
				return;
			}
			last;
		};
		m/tab/ && do {
			$self->warn("Cannot make '$_' for $format.");
			return;
		};
		m/igv/ && do {
			$self->warn("Cannot make '$_' for $format.");
			return;
		};
		m/html/ && do {
			$self->warn("Cannot make '$_' for $format.");
			return;
		};
		do {
			$self->warn("Result format '$_' not recognised - have you called run() yet?");
		}
	}
}

=head2 _determine_format()

 Title   : _determine_format( $has_run )
 Usage   : $bedtools-fac->_determine_format
 Function: determine the format of output for current options
 Returns : format of bowtie output
 Args    : [optional] boolean to indicate result exists

=cut

sub _determine_format {
        my ($self, $params) = @_;

	my $cmd = $self->command if $self->can('command');
	my $format = $format_lookup{$cmd};
	
	#special cases - dependent on switches and files
	for ($cmd) {
		m/^intersect$/ && do {
			return 'bed' if !defined $$params{'-bam'} || $self->write_bed;
			return 'bam';
		};
		m/^pair_to_bed$/ && do {
			return 'bedpe' if !defined $$params{'-bam'} || $self->write_bedpe;
			return 'bam';
		};
		m/^fasta_from_bed$/ && do {
			return $self->output_tab_format ? 'tab' : 'fasta';
		}
	}

	return $format;
}

=head2 _read_bed()

 Title   : _read_bed()
 Usage   : $bedtools_fac->_read_bed
 Function: return a Bio::SeqFeature::Collection object from a BED file 
 Returns : Bio::SeqFeature::Collection
 Args    : 

=cut

sub _read_bed {
	my ($self) = shift;
	
	my @features;
	
	if ($self->{'_result'}->{'file_name'} ne '-') {
		my $in = $self->{'_result'}->{'file'};
		while (my $feature = $in->_readline) {
			chomp $feature;
			push @features, _read_bed_line($feature);
		}
	} else {
		for my $feature (split("\cJ", $self->stdout)) {
			push @features, _read_bed_line($feature);
		}
	}
	
	my $collection = Bio::SeqFeature::Collection->new;
	$collection->add_features(\@features);
	
	return $collection;
}

sub _read_bed_line {
	my $feature = shift;

	my ($chr, $start, $end, $name, $score, $strand,
	    $thick_start, $thick_end, $item_RGB, $block_count, $block_size, $block_start) =
		split("\cI",$feature);
	$strand ||= '.'; # BED3 doesn't have strand data - for 'merge' and 'complement'
	
	return Bio::SeqFeature::Generic->new( -seq_id  => $chr,
	                                      -primary => $name,
	                                      -start   => $start,
	                                      -end     => $end,
	                                      -strand  => $strand_translate{$strand},
	                                      -score   => $score,
	                                      -tag     => { thick_start => $thick_start,
	                                                    thick_end   => $thick_end,
	                                                    item_RGB    => $item_RGB,
	                                                    block_count => $block_count,
	                                                    block_size  => $block_size,
	                                                    block_start => $block_size }
	                                    );
}

=head2 _read_bedpe()

 Title   : _read_bedpe()
 Usage   : $bedtools_fac->_read_bedpe
 Function: return a Bio::SeqFeature::Collection object from a BEDPE file 
 Returns : Bio::SeqFeature::Collection
 Args    : 

=cut

sub _read_bedpe {
	my ($self) = shift;
	
	my @features;
	
	if ($self->{'_result'}->{'file_name'} ne '-') {
		my $in = $self->{'_result'}->{'file'};
		while (my $feature = $in->_readline) {
			chomp $feature;
			push @features, _read_bedpe_line($feature);
		}
	} else {
		for my $feature (split("\cJ", $self->stdout)) {
			push @features, _read_bedpe_line($feature);
		}
	}
	
	my $collection = Bio::SeqFeature::Collection->new;
	$collection->add_features(\@features);
	
	return $collection;
}

sub _read_bedpe_line {
	my $feature = shift;
	
	my ($chr1, $start1, $end1, $chr2, $start2, $end2, $name, $score, $strand1, $strand2, @add) =
		split("\cI",$feature);
	$strand1 ||= '.';
	$strand2 ||= '.';
	
	return Bio::SeqFeature::FeaturePair->new( -primary       => $name,
	                                          -seq_id        => $chr1,
	                                          -start         => $start1,
	                                          -end           => $end1,
	                                          -strand        => $strand_translate{$strand1},

	                                          -hprimary_tag  => $name,
	                                          -hseqname      => $chr2,
	                                          -hstart        => $start2,
	                                          -hend          => $end2,
	                                          -hstrand       => $strand_translate{$strand2},
	
	                                          -score         => $score
	                                        );
}

=head2 _validate_file_input()

 Title   : _validate_file_input
 Usage   : $bedtools_fac->_validate_file_input( -type => $file )
 Function: validate file type for file spec
 Returns : file type if valid type for file spec
 Args    : hash of filespec => file_name

=cut

sub _validate_file_input {
	my ($self, @args) = @_;
	my (%args);
	if (grep (/^-/, @args) && (@args > 1)) { # named parms
		$self->throw("Wrong number of args - requires one named arg") if (@args > 2);
		s/^-// for @args;
		%args = @args;
	} else {
		$self->throw("Must provide named filespec");
	}
	
	for (keys %args) {
		m/bam/ && do {
			return 'bam';
		};
		do {
			return unless ( -e $args{$_} && -r _ );
			my $guesser = Bio::Tools::GuessSeqFormat->new(-file=>$args{$_});
			return $guesser->guess if grep {$guesser->guess =~ m/$_/} @{$accepted_types{$_}};
		}
	}
	return;
}

=head2 version()

 Title   : version
 Usage   : $version = $bedtools_fac->version()
 Function: Returns the program version (if available)
 Returns : string representing location and version of the program

=cut

sub version{
	my ($self) = @_;

	my $cmd = $self->command if $self->can('command');

	defined $cmd or $self->throw("No command defined - cannot determine program executable");

	# new bahaviour for some BEDTools executables - breaks previous approach to getting version
	# $dummy can be any non-recognised parameter - '-version' works for most
	my $dummy = '-version';
	$dummy = '-examples' if $cmd =~ /graph_union/;

	my ($in, $out, $err);
	my $dum;
	$in = \$dum;
	$out = \$self->{'stdout'};
	$err = \$self->{'stderr'};

	# Get program executable
	my $exe = $self->executable;

	my @ipc_args = ( $exe, $dummy );
	
	eval {
		IPC::Run::run(\@ipc_args, $in, $out, $err) or
			die ("There was a problem running $exe : $!");
	};
	# We don't bother trying to catch this: version is returned as an illegal file seek

	my @details = split("\n",$self->stderr);
	(my $version) = grep /^Program: .*$/, @details;
	$version =~ s/^Program: //;

	return $version;
}

sub available_commands { shift->available_parameters('commands') };

sub filespec { shift->available_parameters('filespec') };

1;