title: Snakemake: expanding only a subset of wildcards
body: <p>I can't find the solution for this probably easy problem:</p>
  
  <p>I have this snakefile, which first produces the following files:</p>
  
  <ul>
  <li><p>data/sample1_P1.txt</p></li>
  <li><p>data/sample1_P2.txt</p></li>
  <li><p>data/sample2_P1.txt</p></li>
  <li><p>data/sample2_P2.txt</p></li>
  </ul>
  
  <p>In the next step, it just concatenates the files to one file <code>concatenated/concatenated.txt</code>.</p>
  
  <p>This is the minimal, reproducible example:</p>
  
  <pre><code>pairs = {"P1" : "P1", "P2" : "P2"}
  
  samples = {
      "sample1": "sample1",
      "sample2": "sample2"
  }
  
  rule all:
      input: "concatenated/concatenated.txt"
  
  rule get_txt_files:
      output:
          "data/{sample}_{pair}.txt"
      shell:
          """
          echo 1 &gt; {output}
          """
  
  rule concatenate:
    input:
      expand("data/{sample}_{pair}.txt", sample=samples, \
          pair=pairs)
    output:
      "concatenated/concatenated.txt"
    shell:
      "cat {input} &gt; {output};"
  
  </code></pre>
  
  <p>My question is simple: How can I modify the rule <code>concatenate</code>, so that it concatenates the files with the same sample name?</p>
  
  <p>Desired output would be:</p>
  
  <ul>
  <li><p>concatenated/sample1.txt</p></li>
  <li><p>concatenated/sample2.txt</p></li>
  </ul>
  
  <p>Any help would be appreciated.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I have a very similar follow-up question, so I don't think it's necessary to open a new question again:</p>
  
  <p>What if my expected output would be as follows:</p>
  
  <ul>
  <li><p>data/sample1/sample1_P1</p></li>
  <li><p>data/sample1/sample1_P2</p></li>
  <li><p>data/sample2/sample2_P1</p></li>
  <li><p>data/sample2/sample2_P2</p></li>
  </ul>
  
  <p>To be clear: I only want to create a new direcotry and move the files into that bespoke direcoty.</p>
  
  <p>It seemed intuitive to do it like this:</p>
  
  <pre><code>pairs = {"P1" : "P1", "P2" : "P2"}
  
  samples = {
      "sample1": "sample1",
      "sample2": "sample2"
  }
  
  rule all:
      input: expand("data/{sample}/{sample}_{pair}.txt", sample=samples, pair = pairs)
  
  rule get_txt_files:
      output:
          "data/{sample}_{pair}.txt"
      shell:
          """
          echo 1 &gt; {output}
          """
  
  rule reorganise:
    input:
      expand("data/{{sample}}_{pair}.txt", \
          pair=pairs)
    output:
      "data/{sample}/{sample}_{pair}.txt"
    shell:
      "mv {input} data/{wildcards.sample}/.;"
  
  </code></pre>
  
  <p>Can you spot the problem?</p>
  
  <p>Thanks a lot in advance</p>