1

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.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
furryfoo
  • 11
  • 2
  • The error message is because an OS-level restriction limits how much data you can pass on the command line. There really is no way to work around this, short of running on a system where the `ARG_MAX` kernel constant is large enough to accommodate all the data you want to pass in. But really, using a file for this amount of data is absolutely a better idea. – tripleee Sep 03 '20 at 09:20

1 Answers1

-1

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.

hmatt1
  • 4,939
  • 3
  • 30
  • 51
  • thanks. This is definitely a working method for solving my problem. However, I'm afraid I forgot to add (and have now amended the question) whether this can be done purely with variables without writing to files? If this is technically not possible then your answer is the solution. – furryfoo Sep 27 '14 at 21:30
  • @furryfoo is appears `merge` only accepts a file as input. I think you'd have to write code to do what merge does yourself to do it without writing a file. – hmatt1 Sep 27 '14 at 23:59