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")
RStudio supports multiple terminal sessions. To start another terminal session, use the New Terminal command on the Terminal dropdown menu, or Alt+Shift+R.
In RStudio, commands can be executed from shell scripts by pressing Ctrl + Enter .
To run the entire document press the Ctrl+Shift+Enter key (or use the Source toolbar button).
The way to debug this is to use cat
to test whether your character string has been escaped correctly. So:
x
with your stringcat(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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With