Answers.Body: <p>A nice solution comes from a colleague of mine, as seen in <a href="https://github.com/AlexanderLabWHOI/tara-euk-metaG/blob/ef0f140b7d79540da22da48d4b8e939b95e21f0b/Snakefile#L150-L172" rel="nofollow noreferrer">this Snakemake workflow</a>. The trick is to access the wildcards programmatically using an anonymous (lambda) function. In my example above, it would be implemented as follows.</p>
  <pre class="lang-py prettyprint-override"><code>from glob import glob
  
  rule preprocess:
      input: lambda wildcards: glob('sample-{samp}/*.fastq'.format(samp=wildcards.samp))
      output: '{samp}-clean.fastq'
      shell: 'mypreproccmd {input} &gt; {output}'
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-02-10T13:13:58.233Z
Answers.Text: Note that your `Snakefile` needs a `from glob import glob` for this to work
Answers.UserDisplayName: Michael Schubert
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-02-10T13:33:44.453Z
Answers.Text: And it's a bit more succinct using fstrings: `glob(f'sample-{wildcards.samp}/*.fastq')`
Answers.UserDisplayName: Michael Schubert
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-03-05T20:38:54.180Z
Answers.LastActivityDate: 2023-12-07T20:21:35.277Z
Answers.LastEditDate: 2023-12-07T20:21:35.277Z
Answers.UserDisplayName: Daniel Standage
Body: <p>Consider the following scenario.</p>
  
  <pre><code>├── sample-alice
  │   ├── sequence_1.fastq
  │   ├── sequence_2.fastq
  │   ├── ...
  │   └── sequence_n.fastq
  └── sample-bob
      ├── sequence_1.fastq
      ├── sequence_2.fastq
      ├── ...
      └── sequence_m.fastq
  </code></pre>
  
  <p>I'm trying to write a single rule in my Snakefile that will preprocess each sample generically. The rule will execute a single command that takes all of Fastq files associated with the sample simultaneously. The samples do not have the same number of Fastq files.</p>
  
  <p>If it was a single sample, I could do something like this.</p>
  
  <pre class="lang-py prettyprint-override"><code>rule preprocess:
      input: glob('sample-alice/*.fastq')
      output: 'alice-clean.fastq'
      shell: 'mypreproccmd {input} &gt; {output}'
  </code></pre>
  
  <p>On the other hand, if there was a single Fastq file for each sample, I could use a wildcard to write a generic rule like this.</p>
  
  <pre class="lang-py prettyprint-override"><code>rule preprocess:
      input: 'sample-{samp}/sequence_1.fastq'
      output: '{samp}-clean.fastq'
      shell: 'mypreproccmd {input} &gt; {output}'
  </code></pre>
  
  <p>Is it possible to combine these two approaches to write a rule for handling multi-file samples generically?</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-03-05T20:29:58.743Z
Comments.Text: I'm not sure if this exactly solves your question, but in one of my workflows I use the directory as input (as specified in the config.yaml). The shell command then uses {input}/*.fastq.gz See some examples here: https://github.com/wdecoster/nano-snakemake/blob/master/rules/align.smk
Comments.UserDisplayName: Wouter De Coster
Tags: <snakemake>
Title: Mix globbing and wildcards when specifying rule input
ThreadText: # Mix globbing and wildcards when specifying rule input
  
  (Asked by: username_0 on 2019-03-05)
  
  Consider the following scenario.
  
  ```
  ├── sample-alice
  │   ├── sequence_1.fastq
  │   ├── sequence_2.fastq
  │   ├── ...
  │   └── sequence_n.fastq
  └── sample-bob
      ├── sequence_1.fastq
      ├── sequence_2.fastq
      ├── ...
      └── sequence_m.fastq
  
  ```
  
  I'm trying to write a single rule in my Snakefile that will preprocess each sample generically. The rule will execute a single command that takes all of Fastq files associated with the sample simultaneously. The samples do not have the same number of Fastq files.
  
  If it was a single sample, I could do something like this.
  
  ```py
  rule preprocess:
      input: glob('sample-alice/*.fastq')
      output: 'alice-clean.fastq'
      shell: 'mypreproccmd {input} > {output}'
  
  ```
  
  On the other hand, if there was a single Fastq file for each sample, I could use a wildcard to write a generic rule like this.
  
  ```py
  rule preprocess:
      input: 'sample-{samp}/sequence_1.fastq'
      output: '{samp}-clean.fastq'
      shell: 'mypreproccmd {input} > {output}'
  
  ```
  
  Is it possible to combine these two approaches to write a rule for handling multi-file samples generically?
  
  **Tags:** snakemake
  
  > **Comments:**
  > **username_1**: I'm not sure if this exactly solves your question, but in one of my workflows I use the directory as input (as specified in the config.yaml). The shell command then uses {input}/*.fastq.gz See some examples here: https://github.com/wdecoster/nano-snakemake/blob/master/rules/align.smk  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2019-03-05. Score: 5 | Accepted answer
  
  A nice solution comes from a colleague of mine, as seen in [this Snakemake workflow](https://github.com/AlexanderLabWHOI/tara-euk-metaG/blob/ef0f140b7d79540da22da48d4b8e939b95e21f0b/Snakefile#L150-L172). The trick is to access the wildcards programmatically using an anonymous (lambda) function. In my example above, it would be implemented as follows.
  
  ```py
  from glob import glob
  
  rule preprocess:
      input: lambda wildcards: glob('sample-{samp}/*.fastq'.format(samp=wildcards.samp))
      output: '{samp}-clean.fastq'
      shell: 'mypreproccmd {input} > {output}'
  
  ```
  
  > **Comments:**
  > **username_2**: Note that your `Snakefile` needs a `from glob import glob` for this to work  
  > **username_2**: And it's a bit more succinct using fstrings: `glob(f'sample-{wildcards.samp}/*.fastq')`  
  
  ---
  
  