-
Notifications
You must be signed in to change notification settings - Fork 4
/
validator
executable file
·119 lines (114 loc) · 3.83 KB
/
validator
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright © 2023 Ye Chang yech1990@gmail.com
# Distributed under terms of the GNU license.
#
# Created: 2023-01-01 22:48
"""
validate input YAML file before starting the pipeline
"""
import sys
import yaml
from jsonschema import validate
filename = sys.argv[1]
schema = {
"type": "object",
"properties": {
"reference": {
"type": "object",
"properties": {
"contamination": {
"type": "object",
"properties": {
"fa": {"type": "string"},
"bt2": {"type": "string"},
},
"required": ["fa"],
},
"genes": {
"type": "object",
"properties": {
"fa": {"type": "string"},
"bt2": {"type": "string"},
},
"required": ["fa"],
},
"genome": {
"type": "object",
"properties": {
"fa": {"type": "string"},
"star": {"type": "string"},
},
"required": ["fa", "star"],
},
},
"required": ["genes", "genome"],
},
"samples": {
"type": "object",
"patternProperties": {
"^.*$": {
"type": "object",
"properties": {
"data": {
"type": "array",
"items": {
"type": "object",
"patternProperties": {
"R1": {"type": "string"},
"R2": {"type": "string"},
},
"required": ["R1"],
},
},
"bam": {
"type": "object",
"properties": {
"genes": {"type": "string"},
"genome": {"type": "string"},
},
"required": ["genes", "genome"],
},
"group": {
"oneOf": [
{"type": "string"},
{"type": "array", "items": {"type": "string"}},
]
},
"treated": {"type": "boolean"},
"barcode": {"type": "string"},
"forward_stranded": {"type": "boolean"},
},
"if": {"not": {"required": ["bam"]}},
"then": {"required": ["data"]},
}
},
},
},
}
try:
yml = yaml.load(open(filename, "r").read(), yaml.SafeLoader)
try:
validate(instance=yml, schema=schema)
except Exception as e:
print(f"Error validating YAML file when {e.path}\n\n {e.message}")
sys.exit(1)
except yaml.YAMLError as exc:
print(f"Error while parsing {filename} file:")
if hasattr(exc, "problem_mark"):
if exc.context != None:
print(".............")
print(
str(exc.problem_mark)
+ "\n "
+ str(exc.problem)
+ " "
+ str(exc.context)
)
else:
print(str(exc.problem_mark) + "\n " + str(exc.problem))
print("..............\nPlease correct data and retry.")
else:
print("Something went wrong while parsing yaml file")
sys.exit(1)