user2183097 user2183097 - 4 months ago 11
Bash Question

Brackets in system() command in R

I have been trying all day to find a way to run this line (which works in bash) in R, and I keep getting errors about the round brackets... I understand that the paste command gets confused when dealing with brackets, but I have tried escaping the brackets, putting them in double quotes like this "')'" but nothing works so I am out of resources. Does anybody have any idea how this could work in R?

system(paste("sortBed -i <(awk -v a=1 -v b=2 -v c=3 -v d=4 '{OFS=FS=\"\t\"} {if ($d < 0.5) print \"value\"$a, $b-\"'$d'\", $c+\"'$d'\"}' file.in > file.out", sep=""))

sh: -c: line 0: syntax error near unexpected token `('

Answer

I get the same error as you when running the line from R. As far as I can see there's missing a final parenthesis for the output process substitution in the bash script but adding that doesn't prevent the error. Also the tabulator should be double-escaped to make sure the backslash is passed onto the awk script.

One solution that we found out works in this case is to pipe the output from awk directly into sortBed.

system(paste("awk -v a=1 -v b=2 -v c=3 -v d=4 '{OFS=FS=\"\\t\"} {if ($d < 0.5) print \"value\"$a, $b-\"'$d'\", $c+\"'$d'\"}' file.in | sortBed -i", sep=""))

We didn't really get the output process substitution to work, so if anyone has any suggestions for that it would be nice to hear.