python - Snakemake:是否可以使用目录作为通配符?

标签 python bioinformatics pipeline snakemake

您好,我是 Snakemake 新手,有一个问题。我想对多个数据集运行一个工具。一个数据集代表一个组织,每个组织都存在 fastq 文件,这些文件存储在各自的组织目录中。该工具的粗略命令是:

  python TEcount.py -rosette rosettefile -TE te_references -count result/tissue/output.csv -RNA <LIST OF FASTQ FILE FOR THE RESPECTIVE SAMPLE>          

组织应为通配符。我怎样才能做到这一点?下面是我的第一次尝试,但没有成功。

import os                                                                        

#collect data sets                                                               
SAMPLES=os.listdir("data/rnaseq/")                                               


rule all:                                                                        
    input:                                                                       
        expand("results/{sample}/TEtools.{sample}.output.csv", sample=SAMPLES)                   

rule run_TEtools:                                                                
    input:                                                                       
        TEcount='scripts/TEtools/TEcount.py',                                    
        rosette='data/prepared_data/rosette/rosette',                            
        te_references='data/prepared_data/references/all_TE_instances.fa'        
    params:
        #collect the fastq files in the tissue directory                                                              
        fastq_files = os.listdir("data/rnaseq/{sample}")                         
    output:                                                                      
        'results/{sample}/TEtools.{sample}.output.csv'                           
    shell:                                                                       
        'python {input.TEcount} -rosette {input.rosette} -TE                     
{input.te_references} -count {output} -RNA {params.fastq_files}'

在规则 run_TEtools 中,它不知道 {sample} 是什么。

最佳答案

snakemake 通配符可以是任何东西。它基本上只是一个字符串。
您实现目标的方式存在一些问题。

好的,这就是我的做法。解释如下:

import os                                                                        

#collect data sets
# Beware no other directories or files (than those containing fastqs) should be in that folder                                                        
SAMPLES=os.listdir("data/rnaseq/")                                               

def getFastqFilesForTissu(wildcards):
    fastqs = list()
    # Beware no other files than fastqs should be there
    for s in os.listdir("data/rnaseq/"+wildcards.sample):
        fastqs.append(os.path.join("data/rnaseq",wildcards.sample,s))
    return fastqs

rule all:                                                                        
    input:                                                                       
        expand("results/{sample}/TEtools.{sample}.output.csv", sample=SAMPLES)                   

rule run_TEtools:                                                                
    input:                                                                       
        TEcount='scripts/TEtools/TEcount.py',                                    
        rosette='data/prepared_data/rosette/rosette',                            
        te_references='data/prepared_data/references/all_TE_instances.fa',
        fastq_files = getFastqFilesForTissu        
    output:                                                                      
        'results/{sample}/TEtools.{sample}.output.csv'                           
    shell:                                                                       
        'python {input.TEcount} -rosette {input.rosette} -TE {input.te_references} -count {output} -RNA {input.fastq_files}'

首先,您的 fastq 文件应定义为输入,以便 Snakemake 知道它们是文件,并且如果它们发生更改,则必须重新运行规则。将输入文件定义为 params 是非常糟糕的做法。 params 是为参数创建的,通常不是为文件创建的。
其次,您的脚本文件被定义为输入。您必须注意,每次修改它时,规则都会重新运行。也许这就是你想要的。

我将使用定义的函数来获取每个目录中的 fastq 文件。如果您想使用函数(例如os.listdir()),则不能直接使用通配符。您必须将其作为 python 对象注入(inject)到函数中。您可以定义一个采用一个参数的函数、一个包含所有通配符的通配符对象,或者使用 lambda 关键字(例如:input = lamdba wildcards: myFuntion(wildcards.sample))。
您遇到的另一个问题是 os.listdir() 返回没有相对路径的文件列表。另请注意,os.listdir() 返回 fastq 文件的顺序未知。也许这对你的命令来说并不重要。

关于python - Snakemake:是否可以使用目录作为通配符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56932937/

相关文章:

python - 不能再在 Windows 上运行 python

python - 为什么验证形式不起作用?

java - 在 java 中使用 python M2Crypto AES 密码

Perl 模块无法识别

python - 在 Python 中使用 OpenCV 消除切向透视失真

python计算序列列表中子字符串的存在和不存在的数量

将 SNP 映射到 CDS 的 Python 脚本

arrays - 为什么 PowerShell 会自动展平数组?

powershell - 为什么 $_ 在 .ps1 和 .psm1 文件中的行为不同?

java - 使用 ExecutorService 对管道模式中的一个阶段进行多线程处理