-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract_key_value.py
More file actions
executable file
·32 lines (25 loc) · 1.03 KB
/
extract_key_value.py
File metadata and controls
executable file
·32 lines (25 loc) · 1.03 KB
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
#!/usr/bin/env python3
import os
import json
import sys
path_to_tracking = sys.argv[1]
def extract_key_value(input_file):
key_value_pairs = {}
with open(input_file, 'r') as file:
for line in file:
parts = line.strip().split()
if len(parts) >= 4:
# Extract all parts between the second and the penultimate fields
relevant_part = ' '.join(parts[2:-2])
# Split by the first '|' symbol
split_parts = relevant_part.split('|', 1)
if len(split_parts) == 2:
value = split_parts[0].strip()
key = split_parts[1].strip()
key_value_pairs[key] = value
return key_value_pairs
# Generate output JSON file name based on input file
output_json_name = os.path.basename(path_to_tracking).replace('.tracking', '_keyvalue.json')
extracted_data = extract_key_value(path_to_tracking)
with open("key_value_genes.json", 'w') as json_file:
json.dump(extracted_data, json_file, indent=4)