Search code examples
snakemake

snakemake: how to sort glob_wildcards()


How can I get files in alphabetical order using glob_wildcards()?

Suppose I have sample1.txt, sample2.txt, sample3.txt, and sample4.txt in the same directory. When I run this code:

file_pattern = 'dir_test/{sample}.txt'
FILES = glob_wildcards(file_pattern)
SAMPLES = FILES.sample


rule all:
    input:
        expand(file_pattern, sample=SAMPLES),
        "concat.txt"

rule concat:
    input:
        expand("dir_test/{sample}.txt", sample=SAMPLES),
    output:
        "concat.txt"
    shell:
        """
        cat {input} > {output}
        """

I got the results of glob_wildcards() come out in an arbitrary order as follows:

$ cat concat.txt

This is Sample4
This is Sample1
This is Sample2
This is Sample3

Solution

  • Have you tried something like:

    SAMPLES = sorted(list(FILES.sample))
    

    (not sure that list() is even necessary).