pori-python 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. pori_python/__init__.py +1 -0
  2. pori_python/graphkb/__init__.py +2 -0
  3. pori_python/graphkb/constants.py +220 -0
  4. pori_python/graphkb/genes.py +540 -0
  5. pori_python/graphkb/match.py +710 -0
  6. pori_python/graphkb/py.typed +0 -0
  7. pori_python/graphkb/statement.py +48 -0
  8. pori_python/graphkb/util.py +523 -0
  9. pori_python/graphkb/vocab.py +198 -0
  10. pori_python/ipr/__init__.py +0 -0
  11. pori_python/ipr/annotate.py +373 -0
  12. pori_python/ipr/connection.py +214 -0
  13. pori_python/ipr/constants.py +9 -0
  14. pori_python/ipr/inputs.py +593 -0
  15. pori_python/ipr/ipr.py +390 -0
  16. pori_python/ipr/main.py +512 -0
  17. pori_python/ipr/py.typed +0 -0
  18. pori_python/ipr/summary.py +412 -0
  19. pori_python/ipr/therapeutic_options.py +83 -0
  20. pori_python/ipr/util.py +173 -0
  21. pori_python/types.py +177 -0
  22. pori_python-0.1.0.dist-info/LICENSE +674 -0
  23. pori_python-0.1.0.dist-info/METADATA +151 -0
  24. pori_python-0.1.0.dist-info/RECORD +49 -0
  25. pori_python-0.1.0.dist-info/WHEEL +5 -0
  26. pori_python-0.1.0.dist-info/dependency_links.txt +1 -0
  27. pori_python-0.1.0.dist-info/entry_points.txt +2 -0
  28. pori_python-0.1.0.dist-info/top_level.txt +2 -0
  29. tests/__init__.py +0 -0
  30. tests/test_graphkb/__init__.py +0 -0
  31. tests/test_graphkb/data.py +78 -0
  32. tests/test_graphkb/test_genes.py +306 -0
  33. tests/test_graphkb/test_graphkb.py +32 -0
  34. tests/test_graphkb/test_match.py +662 -0
  35. tests/test_graphkb/test_statement.py +100 -0
  36. tests/test_graphkb/test_util.py +151 -0
  37. tests/test_graphkb/test_vocab.py +85 -0
  38. tests/test_ipr/__init__.py +0 -0
  39. tests/test_ipr/constants.py +3 -0
  40. tests/test_ipr/test_annotate.py +149 -0
  41. tests/test_ipr/test_connection.py +97 -0
  42. tests/test_ipr/test_inputs.py +295 -0
  43. tests/test_ipr/test_ipr.py +338 -0
  44. tests/test_ipr/test_main.py +154 -0
  45. tests/test_ipr/test_probe.py +72 -0
  46. tests/test_ipr/test_summary.py +157 -0
  47. tests/test_ipr/test_upload.py +274 -0
  48. tests/test_ipr/test_util.py +29 -0
  49. tests/test_ipr/util.py +9 -0
@@ -0,0 +1,593 @@
1
+ """
2
+ Read/Validate the variant input files
3
+ """
4
+
5
+ from __future__ import annotations
6
+
7
+ import json
8
+ import jsonschema
9
+ import os
10
+ import pandas as pd
11
+ from Bio.Data.IUPACData import protein_letters_3to1
12
+ from typing import Callable, Dict, Iterable, List, Set, Tuple, cast
13
+
14
+ from pori_python.graphkb.match import INPUT_COPY_CATEGORIES, INPUT_EXPRESSION_CATEGORIES
15
+ from pori_python.types import (
16
+ IprCopyVariant,
17
+ IprExprVariant,
18
+ IprFusionVariant,
19
+ IprSmallMutationVariant,
20
+ IprVariant,
21
+ )
22
+
23
+ from .constants import DEFAULT_URL
24
+ from .util import hash_key, logger, pandas_falsy
25
+
26
+ protein_letters_3to1.setdefault("Ter", "*")
27
+
28
+ SPECIFICATION = os.path.join(os.path.dirname(__file__), "content.spec.json")
29
+
30
+ # content in the local specification should match the values in IPR_API_SPEC_JSON_URL
31
+ IPR_API_SPEC_JSON_URL = f'{os.environ.get("IPR_URL", DEFAULT_URL)}/spec.json'
32
+
33
+ # TODO: GERO-307 - use SPECIFICATION json to derive the variant required and optional details defined below
34
+
35
+ # 'cnvState' is for display
36
+ COPY_REQ = ["gene", "kbCategory"]
37
+ COPY_KEY = ["gene"]
38
+ COPY_OPTIONAL = [
39
+ "cnvState",
40
+ "copyChange",
41
+ "lohState", # Loss of Heterzygosity state - informative detail to analyst
42
+ "chromosomeBand",
43
+ "start",
44
+ "end",
45
+ "size",
46
+ "log2Cna",
47
+ "cna",
48
+ "comments",
49
+ "library",
50
+ "germline",
51
+ ]
52
+
53
+ SMALL_MUT_REQ = ["gene", "proteinChange"]
54
+ # alternate details in the key, can distinguish / subtype events.
55
+ SMALL_MUT_KEY = SMALL_MUT_REQ + [
56
+ "altSeq",
57
+ "chromosome",
58
+ "endPosition",
59
+ "refSeq",
60
+ "startPosition",
61
+ "transcript",
62
+ ]
63
+ SMALL_MUT_OPTIONAL = [
64
+ "altSeq",
65
+ "comments",
66
+ "chromosome",
67
+ "endPosition",
68
+ "germline",
69
+ "hgvsCds",
70
+ "hgvsGenomic",
71
+ "hgvsProtein",
72
+ "library",
73
+ "ncbiBuild",
74
+ "normalAltCount",
75
+ "normalDepth",
76
+ "normalRefCount",
77
+ "refSeq",
78
+ "rnaAltCount",
79
+ "rnaDepth",
80
+ "rnaRefCount",
81
+ "startPosition",
82
+ "transcript",
83
+ "tumourAltCount",
84
+ "tumourAltCopies",
85
+ "tumourDepth",
86
+ "tumourRefCount",
87
+ "tumourRefCopies",
88
+ "zygosity",
89
+ ]
90
+
91
+ EXP_REQ = ["gene", "kbCategory"]
92
+ EXP_KEY = ["gene"]
93
+ EXP_OPTIONAL = [
94
+ "biopsySiteFoldChange",
95
+ "biopsySitePercentile",
96
+ "biopsySiteQC",
97
+ "biopsySiteZScore",
98
+ "biopsySitekIQR",
99
+ "comments",
100
+ "diseaseFoldChange",
101
+ "diseasekIQR",
102
+ "diseasePercentile",
103
+ "diseaseQC",
104
+ "diseaseZScore",
105
+ "expressionState",
106
+ "histogramImage",
107
+ "library",
108
+ "primarySiteFoldChange",
109
+ "primarySitekIQR",
110
+ "primarySitePercentile",
111
+ "primarySiteQC",
112
+ "primarySiteZScore",
113
+ "internalPancancerFoldChange",
114
+ "internalPancancerkIQR",
115
+ "internalPancancerPercentile",
116
+ "internalPancancerQC",
117
+ "internalPancancerZScore",
118
+ "rnaReads",
119
+ "rpkm",
120
+ "tpm",
121
+ ]
122
+
123
+ SV_REQ = [
124
+ "eventType",
125
+ "breakpoint",
126
+ "gene1", # prev: nterm_hugo
127
+ "gene2", # prev: cterm_hugo
128
+ "exon1", # n-terminal
129
+ "exon2", # c-terminal
130
+ ]
131
+ SV_KEY = SV_REQ[:]
132
+ SV_OPTIONAL = [
133
+ "ctermTranscript",
134
+ "ntermTranscript",
135
+ "ctermGene", # combined hugo ensembl form
136
+ "ntermGene", # combined hugo ensembl form
137
+ "detectedIn",
138
+ "conventionalName",
139
+ "svg",
140
+ "svgTitle",
141
+ "name",
142
+ "frame",
143
+ "omicSupport",
144
+ "highQuality",
145
+ "comments",
146
+ "library",
147
+ "rnaAltCount",
148
+ "rnaDepth",
149
+ "tumourAltCount",
150
+ "tumourDepth",
151
+ "germline",
152
+ "mavis_product_id",
153
+ ]
154
+
155
+
156
+ def validate_variant_rows(
157
+ rows: Iterable[Dict], required: List[str], optional: List[str], row_to_key: Callable
158
+ ) -> List[IprVariant]:
159
+ """
160
+ - check that the required columns are present
161
+ - check that a unique key can be formed for each row
162
+ - drop any non-defined columns
163
+
164
+ Args:
165
+ rows: the input files rows
166
+ required list of required column names
167
+ optional: list of optional column names
168
+ row_to_key: function to generate a key for a given row
169
+
170
+ Raises:
171
+ ValueError: row keys are not unique
172
+ ValueError: A required column is missing
173
+
174
+ Returns:
175
+ the rows from the tab file as dictionaries
176
+ """
177
+ header = required + optional + ["key"]
178
+
179
+ result = []
180
+ keys = set()
181
+
182
+ header_validated = False
183
+ for row in rows:
184
+ if not header_validated:
185
+ for req_col in required:
186
+ if req_col not in row:
187
+ raise ValueError(f"header missing required column ({req_col})")
188
+ header_validated = True
189
+ row_key = hash_key(row_to_key(row))
190
+ if row_key in keys:
191
+ raise ValueError(f"duplicate row key ({row_key}) from ({row_to_key(row)})")
192
+ row["key"] = row_key
193
+ keys.add(row_key)
194
+ for k, v in row.items():
195
+ if v is pd.NA:
196
+ row[k] = ""
197
+
198
+ result.append(cast(IprVariant, {col: row.get(col, "") for col in header}))
199
+
200
+ return result
201
+
202
+
203
+ def preprocess_copy_variants(rows: Iterable[Dict]) -> List[IprCopyVariant]:
204
+ """
205
+ Validate the input rows contain the minimum required fields and
206
+ generate any default values where possible
207
+ """
208
+ # default map for display - concise names
209
+ display_name_mapping = {
210
+ INPUT_COPY_CATEGORIES.DEEP: "deep deletion",
211
+ INPUT_COPY_CATEGORIES.AMP: "amplification",
212
+ INPUT_COPY_CATEGORIES.GAIN: "copy gain",
213
+ INPUT_COPY_CATEGORIES.LOSS: "copy loss",
214
+ }
215
+ display_name_mapping.update(dict([(v, v) for v in display_name_mapping.values()]))
216
+
217
+ def row_key(row: Dict) -> Tuple[str, ...]:
218
+ return tuple(["cnv"] + [row[key] for key in COPY_KEY])
219
+
220
+ result = validate_variant_rows(rows, COPY_REQ, COPY_OPTIONAL, row_key)
221
+ ret_list = [cast(IprCopyVariant, var) for var in result]
222
+ for row in ret_list:
223
+ kb_cat = row.get("kbCategory")
224
+ kb_cat = "" if pd.isnull(kb_cat) else str(kb_cat)
225
+ if kb_cat:
226
+ if kb_cat not in INPUT_COPY_CATEGORIES.values():
227
+ raise ValueError(f"invalid copy variant kbCategory value ({kb_cat})")
228
+ if not row.get("cnvState"): # apply default short display name
229
+ row["cnvState"] = display_name_mapping[kb_cat]
230
+ row["variant"] = kb_cat
231
+ row["variantType"] = "cnv"
232
+
233
+ return ret_list
234
+
235
+
236
+ def preprocess_small_mutations(rows: Iterable[Dict]) -> List[IprSmallMutationVariant]:
237
+ """
238
+ Validate the input rows contain the minimum required fields and
239
+ generate any default values where possible
240
+ """
241
+
242
+ def row_key(row: IprSmallMutationVariant) -> Tuple[str, ...]:
243
+ key_vals = []
244
+ for kval in [row.get(key, "") for key in SMALL_MUT_KEY]:
245
+ key_vals.append(str(kval) if pd.notnull(kval) else "")
246
+ return tuple(["small mutation"] + key_vals)
247
+
248
+ result = validate_variant_rows(rows, SMALL_MUT_REQ, SMALL_MUT_OPTIONAL, row_key)
249
+ if not result:
250
+ return []
251
+
252
+ def pick_variant(row: IprSmallMutationVariant) -> str:
253
+ protein_change = row.get("proteinChange")
254
+ if not pandas_falsy(protein_change):
255
+ for longAA, shortAA in protein_letters_3to1.items():
256
+ protein_change = str(protein_change).replace(longAA, shortAA)
257
+ hgvsp = "{}:{}".format(row["gene"], protein_change)
258
+ return hgvsp
259
+
260
+ for field in ["hgvsProtein", "hgvsCds", "hgvsGenomic"]:
261
+ if not pandas_falsy(row.get(field)):
262
+ return str(row.get(field))
263
+
264
+ raise ValueError(
265
+ "Variant field cannot be empty. Must include proteinChange or one of the hgvs fields (hgvsProtein, hgvsCds, hgvsGenomic) to build the variant string"
266
+ )
267
+
268
+ # 'location' and 'refAlt' are not currently used for matching; still optional and allowed blank
269
+
270
+ # change 3 letter AA to 1 letter AA notation
271
+ # for row in result:
272
+ def convert_sm(row: IprVariant) -> IprSmallMutationVariant:
273
+ ret = cast(IprSmallMutationVariant, row)
274
+ ret["variant"] = pick_variant(ret)
275
+ ret["variantType"] = "mut"
276
+
277
+ if ret.get("startPosition") and not ret.get("endPosition"):
278
+ ret["endPosition"] = ret["startPosition"]
279
+
280
+ # default depth to alt + ref if not given
281
+ for sample_type in ("normal", "rna", "tumour"):
282
+ if (
283
+ ret.get(f"{sample_type}RefCount")
284
+ and ret.get(f"{sample_type}AltCount")
285
+ and not ret.get(f"{sample_type}Depth")
286
+ ):
287
+ ret[f"{sample_type}Depth"] = ( # type: ignore
288
+ ret[f"{sample_type}RefCount"] + ret[f"{sample_type}AltCount"] # type: ignore
289
+ )
290
+ return ret
291
+
292
+ res_list = [convert_sm(var) for var in result]
293
+
294
+ return res_list
295
+
296
+
297
+ def preprocess_expression_variants(rows: Iterable[Dict]) -> List[IprExprVariant]:
298
+ """
299
+ Validate the input rows contain the minimum required fields and
300
+ generate any default values where possible
301
+ """
302
+
303
+ def row_key(row: Dict) -> Tuple[str, ...]:
304
+ return tuple(["expression"] + [row[key] for key in EXP_KEY])
305
+
306
+ variants = validate_variant_rows(rows, EXP_REQ, EXP_OPTIONAL, row_key)
307
+ result = [cast(IprExprVariant, var) for var in variants]
308
+ float_columns = [
309
+ col
310
+ for col in EXP_REQ + EXP_OPTIONAL
311
+ if col.endswith("kIQR")
312
+ or col.endswith("Percentile")
313
+ or col.endswith("FoldChange")
314
+ or col.endswith("QC")
315
+ or col.endswith("ZScore")
316
+ or col in ["tpm", "rpkm"]
317
+ ]
318
+
319
+ errors = []
320
+ for row in result:
321
+ row["variant"] = row["kbCategory"]
322
+ if not row["expressionState"] and row["kbCategory"]:
323
+ row["expressionState"] = row["kbCategory"]
324
+
325
+ if row["variant"] and not pd.isnull(row["variant"]):
326
+ if row["variant"] not in INPUT_EXPRESSION_CATEGORIES.values():
327
+ err_msg = f"{row['gene']} variant '{row['variant']}' not in {INPUT_EXPRESSION_CATEGORIES.values()}"
328
+ errors.append(err_msg)
329
+ logger.error(err_msg)
330
+ row["variantType"] = "exp"
331
+
332
+ for col in float_columns:
333
+ if row.get(col) in ["inf", "+inf", "-inf"]:
334
+ row[col] = row[col].replace("inf", "Infinity") # type: ignore
335
+
336
+ # check images exist
337
+ if row["histogramImage"] and not os.path.exists(row["histogramImage"]):
338
+ raise FileNotFoundError(f'missing image ({row["histogramImage"]})')
339
+
340
+ if errors:
341
+ raise ValueError(f"{len(errors)} Invalid expression variants in file")
342
+
343
+ return result
344
+
345
+
346
+ def create_graphkb_sv_notation(row: IprFusionVariant) -> str:
347
+ """Generate GKB/IPR fusion style notation from a structural variant."""
348
+ gene1 = row["gene1"] or "?"
349
+ gene2 = row["gene2"] or "?"
350
+ exon1 = str(row["exon1"]) if row["exon1"] else "?"
351
+ exon2 = str(row["exon2"]) if row["exon2"] else "?"
352
+ if not row["gene1"]:
353
+ gene1, gene2 = gene2, gene1
354
+ exon1, exon2 = exon2, exon1
355
+ if gene1 == "?":
356
+ raise ValueError(
357
+ f'both genes cannot be blank for a structural variant {row["key"]}. At least 1 gene must be entered'
358
+ )
359
+ # force exons to integer repr string
360
+ exon1 = exon1[:-2] if exon1.endswith(".0") else exon1
361
+ exon2 = exon2[:-2] if exon2.endswith(".0") else exon2
362
+ return f"({gene1},{gene2}):fusion(e.{exon1},e.{exon2})"
363
+
364
+
365
+ def preprocess_structural_variants(rows: Iterable[Dict]) -> List[IprFusionVariant]:
366
+ """
367
+ Validate the input rows contain the minimum required fields and
368
+ generate any default values where possible
369
+ """
370
+
371
+ def row_key(row: Dict) -> Tuple[str, ...]:
372
+ return tuple(["sv"] + [row[key] for key in SV_KEY])
373
+
374
+ variants = validate_variant_rows(rows, SV_REQ, SV_OPTIONAL, row_key)
375
+ result = [cast(IprFusionVariant, var) for var in variants]
376
+ # genes are optional for structural variants
377
+ for row in result:
378
+ row["variant"] = create_graphkb_sv_notation(row)
379
+ row["variantType"] = "sv"
380
+
381
+ # check and load the svg file where applicable
382
+ if row["svg"] and not pd.isnull(row["svg"]):
383
+ if not os.path.exists(row["svg"]):
384
+ raise FileNotFoundError(row["svg"])
385
+ with open(row["svg"], "r") as fh:
386
+ row["svg"] = fh.read()
387
+
388
+ return result
389
+
390
+
391
+ def check_variant_links(
392
+ small_mutations: List[IprSmallMutationVariant],
393
+ expression_variants: List[IprExprVariant],
394
+ copy_variants: List[IprCopyVariant],
395
+ structural_variants: List[IprFusionVariant],
396
+ ) -> Set[str]:
397
+ """
398
+ Check matching information for any genes with variants.
399
+ Warn about genes with only one experimental measure.
400
+
401
+ Args:
402
+ small_mutations: list of small mutations
403
+ expression_variants: list of expression variants
404
+ copy_variants: list of copy variants
405
+ structural_variants: list of structural variants
406
+
407
+ Returns:
408
+ set of gene names with variants (used for filtering before upload to IPR)
409
+ """
410
+ # filter excess variants not required for extra gene information
411
+ missing_information_genes = set()
412
+ missing_information_errors = set()
413
+
414
+ copy_variant_genes = {variant["gene"] for variant in copy_variants}
415
+ expression_variant_genes = {variant["gene"] for variant in expression_variants}
416
+ genes_with_variants = set() # filter excess copy variants
417
+ variant: IprCopyVariant | IprExprVariant | IprFusionVariant | IprSmallMutationVariant
418
+
419
+ for variant in copy_variants:
420
+ gene = variant["gene"]
421
+ if not gene:
422
+ logger.error("copy_variant data cannot be applied to an empty genename")
423
+ elif variant["variant"]:
424
+ genes_with_variants.add(gene)
425
+
426
+ if expression_variant_genes and gene not in expression_variant_genes:
427
+ missing_information_genes.add(gene)
428
+ missing_information_errors.add(
429
+ f"gene ({gene}) has a copy variant but is missing expression information"
430
+ )
431
+
432
+ for variant in expression_variants:
433
+ gene = variant["gene"]
434
+ if not gene:
435
+ logger.error("expression_variant data cannot be applied to an empty genename")
436
+ elif variant["variant"]:
437
+ genes_with_variants.add(gene)
438
+
439
+ if copy_variant_genes and gene not in copy_variant_genes:
440
+ missing_information_genes.add(gene)
441
+ missing_information_errors.add(
442
+ f"gene ({gene}) has an expression variant but is missing copy number information"
443
+ )
444
+
445
+ for variant in small_mutations:
446
+ gene = variant["gene"]
447
+ if not gene:
448
+ logger.error("small_mutation data cannot be applied to an empty genename")
449
+ continue
450
+
451
+ if copy_variant_genes and gene not in copy_variant_genes:
452
+ missing_information_genes.add(gene)
453
+ missing_information_errors.add(
454
+ f"gene ({gene}) has a small mutation but is missing copy number information"
455
+ )
456
+ if expression_variant_genes and gene not in expression_variant_genes:
457
+ missing_information_genes.add(gene)
458
+ missing_information_errors.add(
459
+ f"gene ({gene}) has a small mutation but is missing expression information"
460
+ )
461
+ genes_with_variants.add(gene)
462
+
463
+ for variant in structural_variants:
464
+ for gene in [variant["gene1"], variant["gene2"]]:
465
+ if gene: # genes are optional for structural variants
466
+ if gene not in copy_variant_genes:
467
+ missing_information_genes.add(gene)
468
+ missing_information_errors.add(
469
+ f"gene ({gene}) has a structural variant but is missing copy number information"
470
+ )
471
+ if gene not in expression_variant_genes:
472
+ missing_information_genes.add(gene)
473
+ missing_information_errors.add(
474
+ f"gene ({gene}) has a structural variant but is missing expression information"
475
+ )
476
+ genes_with_variants.add(gene)
477
+
478
+ if missing_information_genes:
479
+ for err_msg in sorted(missing_information_errors):
480
+ logger.debug(err_msg)
481
+ link_err_msg = (
482
+ f"Missing information variant links on {len(missing_information_genes)} genes"
483
+ )
484
+ logger.warning(link_err_msg)
485
+ return genes_with_variants
486
+
487
+
488
+ def check_comparators(content: Dict, expresssionVariants: List[IprExprVariant] = []) -> None:
489
+ """
490
+ Given the optional content dictionary, check that based on the analyses present the
491
+ correct/sufficient comparators have also been specified
492
+ """
493
+ mutation_burden = "mutationBurden"
494
+ comparator_roles = {c["analysisRole"] for c in content.get("comparators", [])}
495
+
496
+ for image in content.get("images", []):
497
+ key = image["key"]
498
+ if key.startswith(mutation_burden):
499
+ comp_type = key.split(".")[-1]
500
+ role = f"mutation burden ({comp_type})"
501
+ if role in comparator_roles:
502
+ continue
503
+ if "_sv." in key:
504
+ sv_role = f"mutation burden SV ({comp_type})"
505
+ if sv_role in comparator_roles:
506
+ continue
507
+ raise ValueError(f"missing required comparator definition ({role})")
508
+
509
+ if expresssionVariants:
510
+ required_comparators = {"expression (disease)"}
511
+
512
+ def all_none(row: IprExprVariant, columns: List[str]) -> bool:
513
+ return all([row.get(col) is None or row.get(col) == "" for col in columns])
514
+
515
+ for exp in expresssionVariants:
516
+ if not all_none(
517
+ exp,
518
+ [
519
+ "primarySitekIQR",
520
+ "primarySitePercentile",
521
+ "primarySiteZScore",
522
+ "primarySiteFoldChange",
523
+ ],
524
+ ):
525
+ required_comparators.add("expression (primary site)")
526
+
527
+ if not all_none(
528
+ exp,
529
+ [
530
+ "biopsySitekIQR",
531
+ "biopsySitePercentile",
532
+ "biopsySiteZScore",
533
+ "biopsySiteFoldChange",
534
+ ],
535
+ ):
536
+ required_comparators.add("expression (biopsy site)")
537
+
538
+ if not all_none(
539
+ exp,
540
+ [
541
+ "internalPancancerkIQR",
542
+ "internalPancancerPercentile",
543
+ "internalPancancerZScore",
544
+ "internalPancancerFoldChange",
545
+ ],
546
+ ):
547
+ required_comparators.add("expression (internal pancancer cohort)")
548
+
549
+ if required_comparators - comparator_roles:
550
+ missing = "; ".join(sorted(list(required_comparators - comparator_roles)))
551
+ raise ValueError(f"missing required comparator definitions ({missing})")
552
+
553
+
554
+ def extend_with_default(validator_class):
555
+ # https://python-jsonschema.readthedocs.io/en/latest/faq/#why-doesn-t-my-schema-s-default-property-set-the-default-on-my-instance
556
+ validate_properties = validator_class.VALIDATORS["properties"]
557
+
558
+ def set_defaults(validator, properties, instance, schema):
559
+ for property, subschema in properties.items():
560
+ if "default" in subschema:
561
+ instance.setdefault(property, subschema["default"])
562
+
563
+ for error in validate_properties(validator, properties, instance, schema):
564
+ yield error
565
+
566
+ def check_null(checker, instance):
567
+ return (
568
+ validator_class.TYPE_CHECKER.is_type(instance, "null")
569
+ or pd.isnull(instance)
570
+ or instance == ""
571
+ )
572
+
573
+ type_checker = validator_class.TYPE_CHECKER.redefine("null", check_null)
574
+
575
+ return jsonschema.validators.extend(
576
+ validator_class, validators={"properties": set_defaults}, type_checker=type_checker
577
+ )
578
+
579
+
580
+ # Customize the default jsonschema behaviour to add default values and treat np.nan as null
581
+ DefaultValidatingDraft7Validator = extend_with_default(jsonschema.Draft7Validator)
582
+
583
+
584
+ def validate_report_content(content: Dict, schema_file: str = SPECIFICATION) -> None:
585
+ """
586
+ Validate a report content input JSON object against the schema specification
587
+
588
+ Adds defaults as reccommended by: https://python-jsonschema.readthedocs.io/en/latest/faq/#why-doesn-t-my-schema-s-default-property-set-the-default-on-my-instance
589
+ """
590
+ with open(schema_file, "r") as fh:
591
+ schema = json.load(fh)
592
+
593
+ return DefaultValidatingDraft7Validator(schema).validate(content)