5

I'm using a python-dsl called snakemake that looks like this:

from bx.intervals.cluster import ClusterTree

from epipp.config import system_prefix, include_prefix, config, expression_matrix
config["name"] = "correlate_chip_regions_and_rna_seq"

bin_sizes = {"H3K4me3": 1000, "PolII": 200, "H3K27me3": 200}

rule all:
    input:
        expand("data/{bin_size}_{modification}.bed", zip,
               bin_size=bin_sizes.values(), modification=bin_sizes.keys())

rule get_gene_expression:
    input:
        expression_matrix
    output:
        "data/expression/series.csv"
    run:
        expression_matrix = pd.read_table(input[0])
        expression_series = expression_matrix.sum(1).sort_values(ascending=False)
        expression_series.to_csv(output[0], sep=" ")

I'd like to run yapf on the stuff within run: blocks.

Is it possible to get yapf to ignore the stuff that does not exist in python, like the rule keywords and so on and only use it on specific portions of the file?

Johannes Köster
  • 1,809
  • 6
  • 8
The Unfun Cat
  • 29,987
  • 31
  • 114
  • 156
  • 1
    I see that yapf has a `--lines` option. Maybe what you want to do could be achieved with the help of a first processing of your snakefile to determine which lines are to be skipped and which are to be processed? – bli Oct 26 '16 at 15:14
  • No, still got a syntax error for the snakemake specific code even if I told it only to format lines that were valid Python :) – The Unfun Cat Oct 27 '16 at 06:59
  • Maybe it has to do with indenting. The python parts within rules are more indented than they would be if they were parts of a normal python script. – bli Oct 27 '16 at 12:05
  • Nah, it finds the syntax error in the initial parsing step :) – The Unfun Cat Oct 27 '16 at 12:09
  • Then another option might be to somehow extract the valid python blocks, reformat them separately, then reassemble them. A rather "unfun" task... – bli Oct 27 '16 at 13:03
  • 1
    That was my original thought. Exchange `rule bla:` with `for bla in bla`, run yapf, then switch back. Rather hackish though. Perhaps I should ask the yapf developers for pointers. – The Unfun Cat Oct 27 '16 at 13:12

1 Answers1

6

Yes its possible, by using the # yapf: disable and # yapf: enable comment directives.

Example from the readme:

# yapf: disable
FOO = {
    # ... some very large, complex data literal.
}

BAR = [
    # ... another large data literal.
]
# yapf: enable

You can also disable formatting for a single literal like this:

BAZ = {
    (1, 2, 3, 4),
    (5, 6, 7, 8),
    (9, 10, 11, 12),
}  # yapf: disable
Matt Smith
  • 174
  • 2
  • 14