diff --git a/petab/v2/petab1to2.py b/petab/v2/petab1to2.py index f2965497..081c96c8 100644 --- a/petab/v2/petab1to2.py +++ b/petab/v2/petab1to2.py @@ -3,6 +3,8 @@ from itertools import chain from pathlib import Path +from pandas.io.common import get_handle, is_url + import petab.C from petab.yaml import get_path_prefix @@ -56,14 +58,14 @@ def petab1to2(yaml_config: Path | str, output_dir: Path | str = None): # Write new YAML file output_dir = Path(output_dir) output_dir.mkdir(parents=True, exist_ok=True) - output_file = output_dir / Path(yaml_file).name - write_yaml(new_yaml_config, output_file) + new_yaml_file = output_dir / Path(yaml_file).name + write_yaml(new_yaml_config, new_yaml_file) # Update tables # condition tables, observable tables, SBML files, parameter table: # no changes - just copy file = yaml_config[petab.C.PARAMETER_FILE] - shutil.copy(get_src_path(file), get_dest_path(file)) + _copy_file(get_src_path(file), get_dest_path(file)) for problem_config in yaml_config[petab.C.PROBLEMS]: for file in chain( @@ -71,11 +73,12 @@ def petab1to2(yaml_config: Path | str, output_dir: Path | str = None): problem_config[petab.C.OBSERVABLE_FILES], problem_config[petab.C.SBML_FILES], ): - shutil.copy(get_src_path(file), get_dest_path(file)) + _copy_file(get_src_path(file), get_dest_path(file)) # TODO: Measurements: preequilibration to experiments/timecourses - # TODO: ... - ... + + # validate updated Problem + # TODO validate_problem(new_yaml_file) def _update_yaml(yaml_config: dict) -> dict: @@ -83,7 +86,7 @@ def _update_yaml(yaml_config: dict) -> dict: yaml_config = yaml_config.copy() # Update format_version - yaml_config["format_version"] = 2 + yaml_config["format_version"] = "2.0.0" # Add extensions yaml_config["extensions"] = [] @@ -91,3 +94,17 @@ def _update_yaml(yaml_config: dict) -> dict: # TODO models needs ID and format return yaml_config + + +def _copy_file(src: Path | str, dest: Path | str): + """Copy file.""" + src = str(src) + dest = str(dest) + + if is_url(src): + with get_handle(src, mode="r") as src_handle: + with open(dest, "w") as dest_handle: + dest_handle.write(src_handle.handle.read()) + return + + shutil.copy(str(src), str(dest))