如何使用Snakemake中的expand函数对列表进行排列或组合

2024-05-23 16:42:32 发布

您现在位置:Python中文网/ 问答频道 /正文

可能是蛇形游戏中一个非常基本的问题,但我目前还没有找到答案。假设我有一份样品清单

SAMPLES = ["A", "B", "C"]

典型的expand命令如下所示:

expand("{sample}.txt", sample=SAMPLES)

但我想得到相同样本列表的组合(甚至排列)

做:

expand("{sample}-{sample}.txt", sample=SAMPLES)

我会给你

 A-A.txt, A-B.txt, A-C.txt, B-A.txt, B-B.txt, B-C.txt, C-A.txt, C-B.txt, C-C.txt

相反,我想要:

A-B.txt, A-C.txt, B-C.txt

expand function的蛇形文件中,他们说:

默认情况下,expand使用python itertools函数product,该函数生成所提供的通配符值的所有组合。但是,通过插入第二个位置参数,它可以被任何组合函数替换,例如zip

但是,我不能用itertools.combinations函数替换product,因为从source code of expand可以看出,您不能将r(输出元组的长度)参数指定给expand。做

import itertools
expand("{sample}-{sample}.txt", itertools.combinations, sample=SAMPLES)

返回一个错误'list' object cannot be interpreted as an integer。但是它与itertools.product一起工作

我想我可以在调用expand之前使用itertools.combinations创建两个规则之外的列表,但我希望从Snakemake社区获得一种优雅的方法

谢谢


Tags: sample函数答案txt游戏列表参数样品
1条回答
网友
1楼 · 发布于 2024-05-23 16:42:32

也许可以在expand内完成,但我认为如果没有它,生成感兴趣的列表会更容易。例如:

SAMPLES = ["A", "B", "C"]

combs = []
for x in itertools.combinations(SAMPLES, 2):
    combs.append('%s-%s.txt' %(x[0], x[1]))

print(combs)                                                                                                                                                                                                                                                                      
['A-B.txt', 'A-C.txt', 'B-C.txt']

现在使用combs无论您在哪里使用expand(...)。假设^ {< CD1>}只是一个返回列表的便利函数,但不必使用它

相关问题 更多 >