0

如何在我的配置文件中使用相对路径,以便用户无需更改USER输出目录的路径?

我有这个:

配置.yml

proj_name: H1N1_rhesus
contact:
  email: user.edu
  person: user
01-preprocess: /home/user/2022-h1n1/01-preprocess/
02-salmon: /home/user/2022-h1n1/02-salmon/
raw-data: /tmp/H1N1_rhesus/
reference: /tmp/

蛇文件

#----SET VARIABLES----#
PROJ = config["proj_name"]
INPUTDIR = config["raw-data"]
PREPROCESS = config["01-preprocess"]
SALMON = config["02-salmon"]
REFERENCE = config["reference"

但想做这样的事情:

proj_name: H1N1_rhesus
contact:
  email: user.edu
  person: user
01-preprocess: /home/$(USER)/2022-h1n1/01-preprocess/
02-salmon: /home/$(USER)/2022-h1n1/02-salmon/
raw-data: /tmp/H1N1_rhesus/
reference: /tmp/

或这个:

proj_name: H1N1_rhesus
contact:
  email: user.edu
  person: user
01-preprocess: /home/$(PWD)/01-preprocess/
02-salmon: /home/$(PWD)/02-salmon/
raw-data: /tmp/H1N1_rhesus/
reference: /tmp/

但是我尝试过的方法都没有奏效。

4

2 回答 2

1

一种选择是使用 f 字符串格式(在 Snakefile 中)。所以.yaml可能包含:

proj_name: H1N1_rhesus
paths:
   01-preprocess: /home/{user}/2022-h1n1/01-preprocess/
   02-salmon: /home/{user}/2022-h1n1/02-salmon/
   raw-data: /tmp/H1N1_rhesus/
   reference: /tmp/

在里面Snakefile你会有:

config: 'config.yaml'

# to identify the user, see comments: https://stackoverflow.com/a/842096/10693596
import getpass

paths = {k: v.format(user=getpass.getuser()) for k,v in config['paths'].items()}

paths对象是具有格式化路径的字典。

于 2022-01-19T23:33:12.273 回答
1

另一种选择是intake用于定义数据目录。这允许引用环境变量,例如:

sources:
  01-preprocess:
    args:
      url: "/home/{{env(USER)}}/2022-h1n1/01-preprocess/"

在里面Snakefile,你会有:

import intake
cat = intake.open_catalog('config.yml')
data = cat['01-preprocess'].urlpath
于 2022-01-19T23:50:57.697 回答