Skip to content

json

ensembl.io.genomio.schemas.json

Handling and verification of JSON schemas module.

schema_factory(manifest_dir, metadata_types, output_dir)

Generates one JSON file per metadata type inside manifest, including "manifest.json" itself.

Each JSON file will have the file name of the metadata type, e.g. "seq_region.json".

Parameters:

Name Type Description Default
manifest_dir PathLike

Path to the folder with the manifest JSON file to check.

required
metadata_types List[str]

Metadata types to extract from manifest as JSON files.

required
output_dir PathLike

Path to the folder where to generate the JSON files.

required
Source code in src/python/ensembl/io/genomio/schemas/json/factory.py
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
def schema_factory(manifest_dir: PathLike, metadata_types: List[str], output_dir: PathLike) -> None:
    """Generates one JSON file per metadata type inside `manifest`, including "manifest.json" itself.

    Each JSON file will have the file name of the metadata type, e.g. "seq_region.json".

    Args:
        manifest_dir: Path to the folder with the manifest JSON file to check.
        metadata_types: Metadata types to extract from `manifest` as JSON files.
        output_dir: Path to the folder where to generate the JSON files.

    """
    manifest_path = Path(manifest_dir, "manifest.json")
    with manifest_path.open() as manifest_file:
        content = json.load(manifest_file)
        shutil.copyfile(manifest_path, Path(output_dir, "manifest.json"))
        json_files = {}
        # Use dir name from the manifest
        for name in content:
            if "file" in content[name]:
                file_name = content[name]["file"]
                json_files[name] = manifest_path.parent / file_name
            else:
                for key in content[name]:
                    if "file" in content[name][key]:
                        file_name = content[name][key]["file"]
                        json_files[name] = {key: manifest_path.parent / file_name}
        # Check the other JSON schemas
        for metadata_key in metadata_types:
            if metadata_key in json_files:
                if isinstance(json_files[metadata_key], dict):
                    for key, filepath in json_files[metadata_key].items():
                        shutil.copyfile(filepath, Path(output_dir, f"{metadata_key}_{key}.json"))
                else:
                    shutil.copyfile(json_files[metadata_key], Path(output_dir, f"{metadata_key}.json"))

schema_validator(json_file, json_schema)

Validates a JSON file with the provided JSON schema.

Parameters:

Name Type Description Default
json_file PathLike

Path to the JSON file to check.

required
json_schema Union[str, PathLike]

JSON schema to validate json_file against, either a string matching a existing schema (in data/schemas) or a JSON schema file.

required
Source code in src/python/ensembl/io/genomio/schemas/json/validate.py
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
def schema_validator(json_file: PathLike, json_schema: Union[str, PathLike]) -> None:
    """Validates a JSON file with the provided JSON schema.

    Args:
        json_file: Path to the JSON file to check.
        json_schema: JSON schema to validate `json_file` against, either a string matching a existing
            schema (in data/schemas) or a JSON schema file.

    """
    # Open IO for JSON files and validate it
    with Path(json_file).open("r") as fh:
        content = json.load(fh)
    # Find the json_schema file if a known identifier is provided (if not, treat it as a file path)
    if isinstance(json_schema, str) and (json_schema in _JSON_SCHEMAS):
        json_schema = _JSON_SCHEMAS[json_schema]
    with Path(json_schema).open("r") as fh:
        schema = json.load(fh)
    jsonschema.validate(instance=content, schema=schema)