Skip to content

Commit a58d1c7

Browse files
author
Nalini Ganapati
committed
Add field class/type/length to info output for list-fields
1 parent 069c1b7 commit a58d1c7

1 file changed

Lines changed: 60 additions & 5 deletions

File tree

examples/genomicsdb_query

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,66 @@ def parse_callset_json_for_split_row_ranges(callset_file, chunk_size):
116116
return split_row_ranges
117117

118118

119+
def print_fields(key, val):
120+
if "vcf_field_class" not in val:
121+
val["vcf_field_class"] = ["FILTER"]
122+
if "length" not in val:
123+
val["length"] = "1"
124+
for idx in range(len(val["vcf_field_class"])):
125+
field_val = val["vcf_field_class"][idx]
126+
if isinstance(val["type"], list):
127+
if idx < len(val["type"]):
128+
field_type = val["type"][idx]
129+
else:
130+
field_type = val["type"][0]
131+
else:
132+
field_type = val["type"]
133+
if isinstance(val["length"], list):
134+
if idx < len(val["length"]):
135+
field_length = val["length"][idx]
136+
else:
137+
field_length = val["length"][0]
138+
if "variable_length_descriptor" in field_length:
139+
field_length = field_length["variable_length_descriptor"]
140+
else:
141+
field_length = val["length"]
142+
if field_type == "int":
143+
field_type = "Integer"
144+
elif field_type == "float":
145+
field_type = "Float"
146+
elif field_type == "char":
147+
if field_length.lower() == "var":
148+
field_type = "String"
149+
else:
150+
field_type = "Char"
151+
print(f"{key:<20} {field_val:10} {field_type:10} {field_length}")
152+
153+
154+
def parse_vidmap_json_and_print_fields(vidmap_file):
155+
vidmap = json.loads(genomicsdb.read_entire_file(vidmap_file))
156+
fields = vidmap["fields"]
157+
print(f"{'Field':20} {'Class':10} {'Type':10} {'Length'}")
158+
print(f"{'-----':20} {'-----':10} {'----':10} {'------'}")
159+
if isinstance(fields, list):
160+
{print_fields(field["name"], field) for field in fields}
161+
else: # Old style vidmap json
162+
for key, val in fields.items():
163+
# breakpoint()
164+
print_fields(key, val)
165+
abbreviations = {
166+
"A": "Number of alternate alleles",
167+
"R": "Number of alleles (including reference allele)",
168+
"G": "Number of possible genotypes",
169+
"PP or P": "Ploidy",
170+
"VAR or var": "variable length",
171+
}
172+
print("--")
173+
print("Abbreviations : ")
174+
{print(f" {k}: {v}") for k, v in abbreviations.items()}
175+
176+
119177
def parse_vidmap_json_for_attributes(vidmap_file, attributes=None):
120-
if attributes is None:
178+
if attributes is None or len(attributes) == 0:
121179
return ["GT"]
122180
else:
123181
vidmap = json.loads(genomicsdb.read_entire_file(vidmap_file))
@@ -126,8 +184,6 @@ def parse_vidmap_json_for_attributes(vidmap_file, attributes=None):
126184
fields = [field["name"] for field in fields]
127185
else: # Old style vidmap json
128186
fields = fields.keys()
129-
if len(attributes) == 0:
130-
return fields
131187
attributes = attributes.replace(" ", "").split(",")
132188
not_found = [attribute for attribute in attributes if attribute not in fields]
133189
if len(not_found) > 0:
@@ -322,8 +378,7 @@ def setup():
322378

323379
# List fields
324380
if args.list_fields:
325-
fields = parse_vidmap_json_for_attributes(vidmap_file, attributes="")
326-
print(*fields, sep="\n")
381+
parse_vidmap_json_and_print_fields(vidmap_file)
327382
sys.exit(0)
328383

329384
intervals = args.interval

0 commit comments

Comments
 (0)