3

I'm running associations for a list of genes and markers. I have a list of genes genes = ['gene1', 'gene2', ...] and a dictionary where the keys are gene names and the values are lists of markers that I want to associate with that gene, i.e. markers = {'gene1': ['marker1.1', 'marker1.2', ...], 'gene2': ['marker2.1', 'marker2.2', ...], ...}. I have a rule that outputs a file gene/assoc/marker for a given gene and a marker.

Is it possible expand on the genes list and the markers dictionary simultaneously, such that the gene that is being expanded on works as a key into the dict? Something akin to the following:

markers = {
    'gene1': ['marker1.1', 'marker1.2', ...],
    'gene2': ['marker2.1', 'marker2.2', ...],
    ...
}
genes = markers.keys()

rule all:
    input:
        expand('{gene}/assoc/{marker}', gene=genes, marker=markers[current_gene])
kreld
  • 732
  • 1
  • 5
  • 16

1 Answers1

2

You can use expand in advance:

gimme_files = []
for marker in markers:
    _gimme_per_marker = expand('{gene}/assoc/{marker}', gene=marker, marker=markers[marker])
    gimme_files.extend(_gimme_per_marker)

rule all:
    input:
        gimme_files
The Unfun Cat
  • 29,987
  • 31
  • 114
  • 156
  • 1
    But I guess the answer to your question is no, not in any native way :) – The Unfun Cat Dec 16 '19 at 12:08
  • Thanks! I assume the `gimme_files.expand()` call should be `gimme_files.extend()`? I'm wondering though whether that's even less readable than just doing it in a list comprehension, i.e. `files = [f'{gene}/assoc/{marker}' for gene in genes for marker in markers[gene]]` ? – kreld Dec 16 '19 at 13:07
  • 1
    Yeah, I prefer your last solution :) – The Unfun Cat Dec 16 '19 at 13:38