using perl array as input to bash bedtools command

357 Views Asked by At

I'm wondering if it is possible to use a perl array as the input to a program called bedtools ( http://bedtools.readthedocs.org/en/latest/ )

The array is itself generated by bedtools via the backticks method in perl. When I try to use the perl array in another bedtools bash command it complains that the argument list is too long because it seems to treat each word or number in the array as a separate argument.

Example code:

my @constit_super  = `bedtools intersect -wa -a $enhancers -b $super_enhancer`;

that works fine and can be viewed by:

print @constit_super 

which looks like this onscreen:

chr10   73629894    73634938
chr10   73636240    73639574
chr10   73639726    73657218

but then if I try to use this array in bedtools again e.g.

my $bedtools = `bedtools merge -i @constit_super`;

then i get this error message:

Can't exec "/bin/sh": Argument list too long

Is there anyway to use this perl array in bedtools?

many thanks

27/9/14 thanks for the info on doing it via a file. however, sorry to be a pain I would really like to do this without writing a file if possible.

1

There are 1 best solutions below

2
On

I haven't tested this but I think it would work.

bedtools is expecting one argument with the -i flag, the name of a .bed file. This was in the docs. You need to write your array to a file and then input it into the bedtools merge command.

open(my $fh, '>', "input.bed") or die $!;
print $fh join("", @constit_super);
close $fh;

Then you can sort it with this command from the docs:

`sort -k1,1 -k2,2n input.bed > input.sorted.bed`;

Finally, you can run your merge command.

my $bedtools = `bedtools merge -i input.sorted.bed`;

Hopefully this sets you on the right track.