This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Snakemake Module Error: string indices must be integers

Hey, everyone! I'm having struggles importing a module in Snakemake. When I run Snakemake -np in the directory outside of the following snakefile:

import subprocess
from pathlib import Path
from snakemake.utils import min_version
min_version("6.0")

config_path = "workflow/config/config.yaml"
configfile: config_path

main_dir = "results"
reference_fasta = Path(config['ref_fna']).name
ref_base = Path(reference_fasta).with_suffix('')

wildcard_constraints:
    SRR = r"[R-S0-9]{10}"

module haplo_call:
    snakefile: github("alemanac/GATK_haplotype_module", path="workflow/snakefile", tag="main")
    config: config_path

use rule all from haplo_call as haplo_call with:
    input:
        expand("{main_dir}/{SRR}/lifted_over_and_combined_vcfs/variants.final.vcf", main_dir=main_dir, SRR=config['test_sample'])

I get the following error:

TypeError in file https://raw.githubusercontent.com/alemanac/GATK_haplotype_module/main/workflow/snakefile, line 7:
string indices must be integers
  File "/home/ac.aleman/Bacterial_GATK_SNPs_aleman/workflow/snakefile", line 36, in <module>
  File "https://raw.githubusercontent.com/alemanac/GATK_haplotype_module/main/workflow/snakefile", line 7, in <module>

I'm unsure how to resolve this. Here's the snakefile of the imported module:

import subprocess
from pathlib import Path

config_path = "workflow/config/config.yaml"
configfile: config_path

print(config)
reference_fasta = Path(config['ref_fna']).name
ref_base = Path(reference_fasta).with_suffix('')
main_dir = f"results/{config['run_name']}"

wildcard_constraints:
    SRR = r"[R-S0-9]{10}"

include: "rules/alignment.smk"
include: "rules/alignment_shifted.smk"
include: "rules/ref_processing.smk"
include: "rules/haplotype_caller.smk"

rule all:
    input:
        expand("{main_dir}/{SRR}/lifted_over_and_combined_vcfs/variants.final.vcf", main_dir=main_dir, SRR=config['test_sample']),
        expand("{main_dir}/copied_config.yaml", main_dir=main_dir)

rule copy_config:
    output:
        copied_config = "{main_dir}/copied_config.yaml"
    params:
        config_path = config_path
    shell:
        """
        cp {params.config_path} {output.copied_config}
        """

The module runs fine on its own. Unsure what to do. I would appreciate any help! TY!

gatk snakemake

My guess is that the config file of the parent snakefile is passed as a string to the module rather than a dictionary as usual. How could I resolve this while ensuring the module works on its own?

1 answer

I've resolved this by passing the config file via

module haplo_call:
snakefile: "/home/ac.aleman/git/GATK_haplotype_module/workflow/snakefile"
config: yaml.safe_load(open(config["other_workflow"]))

I feel like this isn't intended, though? Is there a better way of going about this?

But wait, is the idea with modules that you supply a preloaded config for that module, or that you supply a path to a separate config, like you're doing? I'm not familiar with the modules feature, but it seems to me that the documentation is ambiguous. I would read the wording ("It is possible to overwrite the global config dictionary for the module") to imply that it assumes your whole config is stored in one place, with a nested structure for the module. In that case config: config["other-workflow"] should be a dictionary of config information and not a path, which seems to match what you ran into.

So if I'm reading the docs right, then the approach of having the per-module info in a separate config file isn't supported by the config directive, and it'd be simplest to just have it all in the same YAML file as the main config under a dictionary with the key other-workflow.

Log in to answer this question.