【问题标题】:Snakemake "Function did not return str or list of str."Snakemake \"函数没有返回 str 或 str 列表。\"
【发布时间】:2022-08-18 15:33:29
【问题描述】:

我正在尝试运行 RNAseq 蛇形管道。我卡在我的输入功能上。

import pandas as pd
import os
import fnmatch
import re
 
# --- Importing Configuration Files --- #
configfile: \"/DATA/config/config.yaml\"
 
table_cols = [\'dataset\',\'sample\',\'species\',\'frr\',\'gtf_version\',\'fa_version\']
table_samples = pd.read_table(\'/DATA/config/samples.tsv\', header=0, sep=\'\\t\', names=table_cols)
DATASET = table_samples.dataset.values.tolist()
SAMPLE = table_samples[\'sample\'].values.tolist()
SPECIES = table_samples.species.values.tolist()
FRR = table_samples.frr.values.tolist()
GTF_VERSION = table_samples.gtf_version.values.tolist()
FA_VERSION = table_samples.fa_version.values.tolist()
 
print(DATASET,SAMPLE,SPECIES,FRR,GTF_VERSION,FA_VERSION)
 
 
rule all:
        input:
                directory(expand(config[\"project_path\"]+\"resources/starIndex_{species}_{fa_version}_{gtf_version}\",zip, species=SPECIES, fa_version=FA_VERSION, gtf_version=GTF_VERSION)),
                expand(config[\"project_path\"]+\"results/{dataset}/star_aligned_1pass/{sample}_{species}_Aligned.sortedByCoord.out.bam\", zip, dataset=DATASET, sample=SAMPLE, species=SPECIES)
                
wildcard_constraints:
        dataset=\"|\".join([re.escape(x) for x in DATASET]),
        sample=\"|\".join([re.escape(x) for x in SAMPLE]),
        species=\"|\".join([re.escape(x) for x in SPECIES]),
        gtf_version=\"|\".join([re.escape(x) for x in GTF_VERSION]),
        fa_version=\"|\".join([re.escape(x) for x in FA_VERSION])
 
 
## rule starIndex ##  Create star index if it does not exist yet
rule starIndex:
        priority: 1
        input:
                fasta=expand(config[\"project_path\"]+\"resources/{species}.{fa_version}.dna.primary_assembly.fa\",zip, species=SPECIES, fa_version=FA_VERSION),
                gtf=expand(config[\"project_path\"]+\"resources/{species}.{gtf_version}.gtf\",zip, species=SPECIES, gtf_version=GTF_VERSION)
        output:
                directory(config[\"project_path\"]+\"resources/starIndex_{species}_{fa_version}_{gtf_version}\")
        threads:
                20
        params:
                directory(config[\"project_path\"]+\"resources/starIndex_{species}_{fa_version}_{gtf_version}\")
        conda:
                \"envs/DTPedia_bulkRNAseq.yaml\"
        shell:
                \"\"\"
                STAR --runThreadN {threads} --runMode genomeGenerate --genomeDir {output} --genomeFastaFiles {input.fasta} --sjdbGTFfile {input.gtf}
                \"\"\"
 
rule star_1pass_alignment:
        priority: 4
        input:
                read1=config[\"project_path\"]+\"resources/raw_datasets/{dataset}/{sample}_{species}_RNA-Seq_1.fastq.gz\",
                read2=config[\"project_path\"]+\"resources/raw_datasets/{dataset}/{sample}_{species}_RNA-Seq_2.fastq.gz\",
                index=determine_species,
                prefix=config[\"project_path\"]+\"results/{dataset}/star_aligned_1pass/{sample}_{species}_\"
        output:
                bam=config[\"project_path\"]+\"results/{dataset}/star_aligned_1pass/{sample}_{species}_Aligned.sortedByCoord.out.bam\",
                log=config[\"project_path\"]+\"results/{dataset}/star_aligned_1pass/{sample}_{species}_Log.final.out\",
                sj_1pass=config[\"project_path\"]+\"results/{dataset}/star_aligned_1pass/{sample}_{species}_SJ.out.tab\"
        threads:
                12
        conda:
                \"envs/DTPedia_bulkRNAseq.yaml\"
        shell:
                \"\"\"
                STAR --runMode alignReads --genomeDir {input.index} --genomeLoad LoadAndKeep --outSAMtype BAM SortedByCoordinate --limitBAMsortRAM 10000000000 --limitGenomeGenerateRAM 20000000000 --readFilesIn {input.read1} {input.read2} --runThreadN {threads} --readFilesCommand gunzip -c --outFileNamePrefix {input.prefix}
                \"\"\"

这是错误:

[\'PRJNA493818_GSE120639_SRP162872\', \'PRJNA493818_GSE120639_SRP162872\', \'PRJNA362883_GSE93946_SRP097621\', \'PRJNA362883_GSE93946_SRP097621\'] [\'SRR7942395_GSM3406786_sAML_Control_1\', \'SRR7942395_GSM3406786_sAML_Control_1\', \'SRR5195524_GSM2465521_KrasT_45649_NoDox\', \'SRR5195524_GSM2465521_KrasT_45649_NoDox\'] [\'Homo_sapiens\', \'Homo_sapiens\', \'Mus_musculus\', \'Mus_musculus\'] [1, 2, 1, 2] [\'GRCh38.106\', \'GRCh38.106\', \'GRCm39.107\', \'GRCm39.107\'] [\'GRCh38\', \'GRCh38\', \'GRCm39\', \'GRCm39\']

Building DAG of jobs...
WorkflowError in line 113 of /DATA/workflow/snakefileV21:
Function did not return str or list of str.

例如,我尝试在返回后修改行但没有成功并且输出相同的错误:

# function determine_species_fasta # function for determining fasta input of correct species to rule starIndex
def determine_species(wildcards):
        read1 = config[\"project_path\"]+\"resources/raw_datasets/{wildcards.dataset}/{wildcards.sample}_{wildcards.species}_RNA-Seq_1.fastq.gz\"
        if fnmatch.fnmatch(read1, \'*Homo_sapiens*\'):
                return \"/DATA/resources/starIndex_Homo_sapiens_GRCh38_GRCh38.106\"
        elif fnmatch.fnmatch(read1, \'*Mus_musculus*\'):
                return \"/DATA/resources/starIndex_Mus_musculus_GRCm39_GRCm39.107\"  

也许read1 = config[\"project_path\"]+\"resources/raw_datasets/{wildcards.dataset}/{wildcards.sample}_{wildcards.species}_RNA-Seq_1.fastq.gz\" 中的通配符没有正确填写?我也尝试了unpack(),但没有成功https://snakemake.readthedocs.io/en/v6.0.0/snakefiles/rules.html#input-functions-and-unpack

我希望你能帮助(:

编辑 1

根据@SultanOrazbayev 的建议,我已将代码更改为此。这条蛇形管道分析来自小鼠和人类的 RNAseq 数据。这个 python 输入函数确定哪个物种\'starIndex使用和粘贴相关的目录(不是文件)rule starIndex. 中输出:

# function determine_species_fasta # function for determining fasta input of correct species to rule starIndex
def determine_species(wildcards):
        read1 = config[\"project_path\"]+\"resources/raw_datasets/{wildcards.dataset}/{wildcards.sample}_{wildcards.species}_RNA-Seq_1.fastq.gz\"
        if fnmatch.fnmatch(read1, \'*Homo_sapiens*\'):
                return \"/DATA/resources/starIndex_Homo_sapiens_GRCh38_GRCh38.106\"
        elif fnmatch.fnmatch(read1, \'*Mus_musculus*\'):
                return \"/DATA/resources/starIndex_Mus_musculus_GRCm39_GRCm39.107\"
        else:
                raise ValueError(f\"Wildcards do not match the desired pattern: {wildcards}\")

现在给出这个错误:

(base) @darwin:/DATA/workflow$ snakemake -s snakefileV21 --use-conda 
[\'PRJNA493818_GSE120639_SRP162872\', \'PRJNA493818_GSE120639_SRP162872\', \'PRJNA362883_GSE93946_SRP097621\', \'PRJNA362883_GSE93946_SRP097621\'] [\'SRR7942395_GSM3406786_sAML_Control_1\', \'SRR7942395_GSM3406786_sAML_Control_1\', \'SRR5195524_GSM2465521_KrasT_45649_NoDox\', \'SRR5195524_GSM2465521_KrasT_45649_NoDox\'] [\'Homo_sapiens\', \'Homo_sapiens\', \'Mus_musculus\', \'Mus_musculus\'] [1, 2, 1, 2] [\'GRCh38.106\', \'GRCh38.106\', \'GRCm39.107\', \'GRCm39.107\'] [\'GRCh38\', \'GRCh38\', \'GRCm39\', \'GRCm39\']
The flag \'directory\' used in rule all is only valid for outputs, not inputs.
The flag \'directory\' used in rule all is only valid for outputs, not inputs.
The flag \'directory\' used in rule all is only valid for outputs, not inputs.
The flag \'directory\' used in rule all is only valid for outputs, not inputs.
Building DAG of jobs...
InputFunctionException in line 115 of /DATA/workflow/snakefileV21:
ValueError: Wildcards do not match the desired pattern: PRJNA493818_GSE120639_SRP162872 SRR7942395_GSM3406786_sAML_Control_1 Homo_sapiens
Wildcards:
dataset=PRJNA493818_GSE120639_SRP162872
sample=SRR7942395_GSM3406786_sAML_Control_1
species=Homo_sapiens

    标签: python function input wildcard snakemake


    【解决方案1】:

    当前函数的一个机械问题是条件不能捕获所有情况:

    def test(x):
       if x>10:
          ...
       elif x>5:
          ...
       # will return None for x<=5
    

    我不知道在您的情况下determine_species 应该返回什么函数,但也许这会起作用:

    def determine_species(wildcards):
       read1 = ...
       if fnmatch.fnmatch(read1, '*Homo_sapiens*'):
          return ...
        elif fnmatch.fnmatch(read1, '*Mus_musculus*'):
          return ...
        else:
          raise ValueError(f"Wildcards do not match the desired pattern: {wildcards}")
    

    这将在不符合您的 fnmatch 条件的情况下出错。

    【讨论】:

      猜你喜欢
      • 2022-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-08
      • 1970-01-01
      • 2019-08-30
      • 2017-12-13
      相关资源
      最近更新 更多