0

I have a file 'filenames.txt' with in it:

a.txt
b.txt
c.txt
d.txt

I want to use these lines as input in snakemake. Does anyone know how I could do this?

  • 3
    Does this answer your question? [Snakemake - How do I use each line of a file as an input?](https://stackoverflow.com/a/57597910/11613622) (refer second part of the answer) – brc-dd Jul 05 '22 at 14:13
  • I do not know anything about snakemake so cannot really answer your question. But have you tried googling something like `dynamic import` or `dynamic file names` or similar? A quick search yields several results for me including: https://stackoverflow.com/questions/57327210/snakemake-dynamically-derive-the-targets-from-input-files (this is a slightly different approach that will read all .txt files in a directory, but something to think about). With your approach I assume you can just read original filenames.txt, then loop through each line, and inside the loop read each file. – kojow7 Jul 05 '22 at 14:18

1 Answers1

2

You can use python inside a snakefile. Here is how you could obtain a list of file names:

with open('filenames.txt') as fh:
    filenames = [line.strip() for line in fh]

Then, I'm not sure how you intend to use those file names. If you want your workflow to create those files, it should be made the input section of the top all rule:

rule all:
    input: filenames

And you should of course have a rule that can create such a file:

rule create_file:
    output: "{letter}.txt"
    shell:
        """
        touch {output[0]}
        """
bli
  • 7,549
  • 7
  • 48
  • 94