从的R脚本运行bash脚本 [英] Run a bash script from an R script
问题描述
所以,我有我想从CMD行中使用,一个文件转换为另一种这个程序samtools。它的工作原理是这样的:
的bash-4.2 $ samtools查看filename.bam | AWK'{OFS =\\ t的;打印>$ 1\\ n$ 10}' - > filename.fasta
因为我想自动执行此,我想通过使用的R脚本来自动完成它。我知道你可以使用system()来运行操作系统命令,但我不能让它通过努力工作
系统(samtools查看filename.bam | awk的'{OFS =\\ t的;打印>$ 1\\ n$ 10}' - > filename.fasta)
这是否只是一种使用正则表达式来摆脱空间和东西,所以逗号次论证系统(命令)是可读的事?我该怎么做呢?
编辑:
系统(samtools查看filename.bam | awk的'{OFS =\\ t的;打印>$ 1\\ n$ 10}' - > first_batch_1.fasta)
错误:意外输入系统(samtools查看filename.bam | awk的'{OFS =\\
块引用>EDIT2:
系统(samtools查看filename.bam | awk的'{OFS = \\\\ t \\打印\\> \\$ 1 \\\\ n \\$ 10}' - > filename.fasta)
块引用>AWK:CMD。行:1:{OFS =;打印>$ 1
AWK:CMD。行:1:^未终止的字符串
AWK:CMD。行:1:{OFS =;打印>$ 1
AWK:CMD。行:1:^语法错误
>EDIT3:
而获奖者是:系统(samtools查看filename.bam | awk的'{OFS = \\\\\\牛逼\\打印\\> \\$ 1 \\\\\\ñ\\$ 10个}' - > filename.fasta)
解决方案要调试这个问题的方法是使用
猫
来测试是否您的字符串已经正确转义。所以:
- 创建一个对象
X
与字符串- 仔细逃避所有的特殊字符,在这种情况下引号和反斜杠
- 使用
猫(X)
检查结果字符串。例如:
X< - 'samtools查看filename.bam | awk的\\'{OFS =\\\\ T;打印>$ 1\\\\ N$ 10} \\' - > filename.fasta猫(X)
samtools查看filename.bam | AWK'{OFS =\\ t的;打印>$ 1\\ n$ 10}' - > filename.fasta如果这给正确的字符串,那么你应该能够使用
系(x)
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")
解决方案The way to debug this is to use
cat
to test whether your character string has been escaped correctly. So:
- Create an object
x
with your string- Carefully escape all the special characters, in this case quotes and backslashes
- 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)
这篇关于从的R脚本运行bash脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!