Search code examples
pythonpython-3.xsnakemake

Prevent scientific writing when creating a file through Snakemake


Not sure if it's a Python issue or a Snakemake issue, but I have this target:

SCORES = expand(expand(RESULTS_DIR + "/{{sp}}_{{st}}/{{seq}}/scores/{tf}_{th}.scores.tab", zip, tf=TF_NAME, th=TF_THR), sp=SPECIES, st=STRAINS, seq=SEQ)

where TF_THR is a list of floats. In some cases, the output file is written using scientific notation, which I wanna prevent from happening. I tried this:

th='{:.6f}'.format(TF_THR)

and this:

th=format(TF_THR, '0.6f'))

but in both cases I get the following error:

TypeError in line 44 of myworkflow.py:
unsupported format string passed to Series.__format__
  File "myworkflow.py", line 44, in <module>

Thank you for your help!


Solution

  • Expand, just like all strings in Python, makes use of the 'format minilang'. I'm not sure how well this is documented for Snakemake.

    To solve your issue we can simply do something like this:

    TF_THR = [0.12345, 0.6789]
    print(expand('{th:0.3f}', th=TF_THR))
    # ['0.123', '0.679']