小编MKR*_*MKR的帖子

rowwise() 与 dplyr 中的列名向量求和

我再次对如何实现这一目标感到困惑:

鉴于此数据框:

df <- tibble(
  foo = c(1,0,1),
  bar = c(1,1,1),
  foobar = c(0,1,1)
)
Run Code Online (Sandbox Code Playgroud)

这个向量:

to_sum <- c("foo", "bar")
Run Code Online (Sandbox Code Playgroud)

我想获得列中值的行式总和to_sum

期望的输出:

# A tibble: 3 x 4
# Rowwise: 
    foo   bar foobar   sum
  <dbl> <dbl>  <dbl> <dbl>
1     1     1      0     2
2     0     1      1     1
3     1     1      1     2
Run Code Online (Sandbox Code Playgroud)

输入它是有效的(显然)。

df %>% rowwise() %>% 
  mutate(
    sum = sum(foo, bar)
  )
Run Code Online (Sandbox Code Playgroud)

这不会:

df %>% rowwise() %>% 
  mutate(
    sum = sum(to_sum)
  )
Run Code Online (Sandbox Code Playgroud)

我理解,因为如果我要尝试:

df %>% rowwise() …
Run Code Online (Sandbox Code Playgroud)

r dplyr rlang

7
推荐指数
2
解决办法
78
查看次数

Snakemake:如何对新创建的文件使用 glob_wildcards ?

问题:

我有一个很大的工作流程,它在某个时刻创建任意数量的文件{sample},命名为例如test1.txttest2.txt等。

然后我需要使用这些文件进行进一步处理。下一条规则的输入文件是{sample}/test1.txt{sample}/test2.txt等。因此test1test2等成为通配符。

数据结构为:

---data
 ---sample1
   ---test1.txt
   ---test2.txt
   ---test3.txt
 ---sample2
   ---test1.txt
   ---test2.txt
Snakefile
Run Code Online (Sandbox Code Playgroud)

我正在努力如何使用snakemake来解决此类问题。我研究过该函数glob_wildcards,但不知道如何使用它。

直觉上,我会做这样的事情:

samples = ['sample1', 'sample2']

rule append_hello:
  input:
    glob_wildcards('data/{sample}/{id}.txt')
  output:
    'data/{sample}/{id}_2.txt'
  shell:
    " echo {input} 'hello' >> {output} "
Run Code Online (Sandbox Code Playgroud)

我有两个问题:

  1. Snakemkae 中如何处理这个问题?
  2. 您将如何构造 arule all来运行它。

任何进一步阅读的意见或提示将不胜感激。

编辑

我认为这与通配符限制有关。当我跑步时:

assemblies = []
for filename in glob_wildcards(os.path.join("data/{sample}", "{i}.txt")):
    assemblies.append(filename)
print(assemblies)
Run Code Online (Sandbox Code Playgroud)

我得到两个相应索引匹配的列表:

[['sample1', 'sample1', 'sample1', 'sample2', 'sample2'], ['test1', 'test2', 'test3', 'test5', …
Run Code Online (Sandbox Code Playgroud)

python snakemake

2
推荐指数
1
解决办法
5839
查看次数

标签 统计

dplyr ×1

python ×1

r ×1

rlang ×1

snakemake ×1