从的R脚本运行bash脚本 [英] Run a bash script from an R script

查看:474
本文介绍了从的R脚本运行bash脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有我想从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)


解决方案

要调试这个问题的方法是使用来测试是否您的字符串已经正确转义。所以:


  1. 创建一个对象 X 与字符串

  2. 仔细逃避所有的特殊字符,在这种情况下引号和反斜杠

  3. 使用猫(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:

  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)

这篇关于从的R脚本运行bash脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆