23

So I have this programme samtools that I want to use from cmd line, converting one file to another. It works like this:

bash-4.2$ samtools view filename.bam | awk '{OFS="\t"; print ">"$1"\n"$10}' - > filename.fasta

As I want to automate this, I would like to automate it by using an R script. I know you can use system() to run an OS command, but I cannot get it to work by trying

system(samtools view filename.bam | awk '{OFS="\t"; print ">"$1"\n"$10}' - > filename.fasta)

Is it just a matter of using regexes to get rid of spaces and stuff so the comma nd argument system(command) is readable? How do I do this?

EDIT:

system("samtools view filename.bam | awk '{OFS="\t"; print ">"$1"\n"$10}' - > first_batch_1.fasta") Error: unexpected input in "system("samtools view filename.bam | awk '{OFS="\"

EDIT2:

system("samtools view filename.bam | awk '{OFS=\"\t\"; print \">\"$1\"\n\"$10}' - > filename.fasta")

awk: cmd. line:1: {OFS="    "; print ">"$1"
awk: cmd. line:1:                         ^ unterminated string
awk: cmd. line:1: {OFS="    "; print ">"$1"
awk: cmd. line:1:                         ^ syntax error
> 

EDIT3: And the winner is:

system("samtools view filename.bam | awk '{OFS=\"\\t\"; print \">\"$1\"\\n\"$10}' -> filename.fasta")
6
  • 1
    system() takes a character string as argument, so you need to wrap your argument in quotes. Commented Jul 9, 2012 at 12:43
  • I had tried that already. Shown as edit above. Commented Jul 9, 2012 at 12:47
  • 1
    try system("samtools view filename.bam | awk '{OFS=\"\t\"; print \">\"$1\"\n\"$10}' - > filename.fasta") You need to escape the double quotation marks, in order for R to read the entire command, instead of only the command part between the first two double quotation marks Commented Jul 9, 2012 at 12:50
  • EDIT2: shows that your suggestion doesn't work. Escaping all the quotes within the awk regex makes it not work properly. Commented Jul 9, 2012 at 12:54
  • 1
    Souldn't you also escape the backslashes? system("samtools view filename.bam | awk '{OFS=\"\\t\"; print \">\"$1\"\\n\"$10}' -> filename.fasta") Commented Jul 9, 2012 at 12:57

1 Answer 1

32

The way to debug this is to use cat to test whether your character string has been escaped correctly. So:

  1. Create an object x with your string
  2. Carefully escape all the special characters, in this case quotes and backslashes
  3. Use cat(x) to inspect the resulting string.

For example:

x <- 'samtools view filename.bam | awk \'{OFS="\\t"; print ">"$1"\\n"$10}\' - > filename.fasta'

cat(x)
samtools view filename.bam | awk '{OFS="\t"; print ">"$1"\n"$10}' - > filename.fasta

If this gives the correct string, then you should be able to use

system(x)
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, I need to learn how to debug this myself. Thanks Andrie!
@Andrie i have a bash script safe in"c:/test" folder with nametest.sh . I want to call this bash script in r code. I try thissystem command but getting an error dear will you help me?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.