Skip to content
Snippets Groups Projects
Commit 00cc796e authored by Yb Tweezer's avatar Yb Tweezer
Browse files

redo SuperSequence.save as files in directory

parent ea3daab8
No related branches found
No related tags found
No related merge requests found
......@@ -5,6 +5,8 @@ from .plotdefs import Plotter
import copy
import pathlib
import toml
import shutil
import os
"""
Builds on abstractions defined in lib.structures.
......@@ -15,13 +17,18 @@ def _titlecase(s: str) -> str:
for _s in s.replace(" ", "_").split("_"))
def _fresh_filename(path: pathlib.Path, overwrite: bool=False) -> pathlib.Path:
if overwrite:
return path
_path = path
while _path.is_file():
print(f"WARNING: found existing file {_path}")
_path = _path.parent.joinpath(_path.stem + "_" + _path.suffix)
print(f"Write instead to {_path}")
while _path.exists():
print(f"WARNING: {_path} exists in filesystem")
if overwrite:
print(f":: rm -rf {_path}")
if _path.is_dir():
shutil.rmtree(str(_path))
else:
os.remove(str(_path))
else:
_path = _path.parent.joinpath(_path.stem + "_" + _path.suffix)
print(f"Write instead to {_path}")
return _path
def min_n(generator, default=None):
......@@ -55,7 +62,7 @@ class SuperSequence:
"""
def __init__(self, outdir: pathlib.Path, name: str,
sequences: dict[str, Sequence]=None,
sequences: dict[str, Sequence]=None, params: dict[str, ...]=None,
defaults: ConnectionLayout=None):
"""
Constructor.
......@@ -70,6 +77,7 @@ class SuperSequence:
self.outdir = outdir
self.name = name
self.sequences = dict() if sequences is None else sequences
self.params = dict() if params is None else params
self.defaults = defaults
def __getitem__(self, key: str) -> Sequence:
......@@ -154,21 +162,18 @@ class SuperSequence:
def save(self, target: pathlib.Path=None, overwrite: bool=False,
printflag: bool=True):
T = self.outdir.joinpath(self.name) if target is None else target
T = T.parent.joinpath(T.name + ".toml") if T.suffix != ".toml" else T
if printflag: print(f"[sequencing] Saving sequence data to '{T}':")
sequence_file = _fresh_filename(T, overwrite)
if printflag: print(f"[sequencing] save sequence '{sequence_file.stem}'")
print(sequence_file.parent.is_dir())
print(sequence_file.exists())
if not sequence_file.parent.is_dir():
print(f":: mkdir -p '{sequence_file.parent}'")
sequence_file.parent.mkdir(parents=True, exist_ok=True)
print(sequence_file.parent.is_dir())
sequence_file = sequence_file.parent.joinpath(sequence_file.name)
print(sequence_file)
print(sequence_file.parent.is_dir())
with sequence_file.open('w') as outfile:
T = _fresh_filename(T, overwrite)
T_seq = T.joinpath("sequence.toml")
T_par = T.joinpath("parameters.toml")
if not T.is_dir():
print(f":: mkdir -p '{T}'")
T.mkdir(parents=True, exist_ok=True)
with T_seq.open('w') as outfile:
toml.dump(self.to_primitives(), outfile)
with T_par.open('w') as outfile:
toml.dump(self.params, outfile)
return self
@staticmethod
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment