Skip to content

Commit b832a7b

Browse files
authored
Merge pull request #396 from lldelisle/mkdir
Mkdir
2 parents 8cd392c + d3af8bf commit b832a7b

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

docs/content/releases/3.7.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ Minor enhancements:
4141

4242
- the help message for BED has been improved to highlight the fact that the extension needs to be part of the output.
4343

44+
- if the directory of the output file does not exists, it is created.
45+
4446
Drop support for python 3.6
4547
^^^^^^^^^^^^^^^^^^^^^^^^^^^
4648

pygenometracks/plotTracks.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@
142142
"""
143143

144144
import sys
145+
import os
145146
import argparse
146147
import warnings
147148

@@ -275,6 +276,10 @@ def main(args=None):
275276
track_label_width=args.trackLabelFraction,
276277
plot_regions=regions, plot_width=args.plotWidth)
277278

279+
# Create dir if dir does not exists:
280+
# Modified from https://stackoverflow.com/questions/12517451/automatically-creating-directories-with-file-output
281+
os.makedirs(os.path.dirname(os.path.abspath(args.outFileName)), exist_ok=True)
282+
278283
# Plot them
279284
if args.BED:
280285
name = args.outFileName.split(".")

pygenometracks/tests/test_tracks_label.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# -*- coding: utf-8 -*-
22
import matplotlib as mpl
33
from matplotlib.testing.compare import compare_images
4-
from tempfile import NamedTemporaryFile
4+
from tempfile import NamedTemporaryFile, TemporaryDirectory
55
import os.path
66
import pygenometracks.plotTracks
77
mpl.use('agg')
@@ -148,3 +148,21 @@ def test_fixed_height():
148148
assert res is None, res
149149

150150
os.remove(outfile.name)
151+
152+
153+
def test_non_existing_dir():
154+
155+
outdir = TemporaryDirectory()
156+
output_file = os.path.join(outdir.name, "pGT_test", "test.png")
157+
ini_file = os.path.join(ROOT, "title.ini")
158+
region = "X:3000000-3500000"
159+
expected_file = os.path.join(ROOT, 'master_title_0.2.png')
160+
args = f"--tracks {ini_file} --region {region} "\
161+
"--trackLabelFraction 0.2 --width 38 --dpi 130 "\
162+
f"--outFileName {output_file}".split()
163+
pygenometracks.plotTracks.main(args)
164+
res = compare_images(expected_file,
165+
output_file, tolerance)
166+
assert res is None, res
167+
168+
outdir.cleanup()

0 commit comments

Comments
 (0)