-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnextclade-tree-to-text.py
73 lines (59 loc) · 2.36 KB
/
nextclade-tree-to-text.py
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
from collections import defaultdict
from dataclasses import replace
import datetime
import json
import pandas
import collections.abc
def flatten(dictionary, parent_key=False, separator='.', log=False):
"""
Turn a nested dictionary into a flattened dictionary
:param dictionary: The dictionary to flatten
:param parent_key: The string to prepend to dictionary's keys
:param separator: The string used to separate flattened keys
:param log : Bool used to control logging to the terminal
:return: A flattened dictionary
"""
items = []
for key, value in dictionary.items():
if log: print('checking:',key)
new_key = str(parent_key) + separator + key if parent_key else key
if isinstance(value, collections.abc.MutableMapping):
if log: print(new_key,': dict found')
if not value.items():
if log: print('Adding key-value pair:',new_key,None)
items.append((new_key,None))
else:
items.extend(flatten(value, new_key, separator).items())
elif isinstance(value, list):
if log: print(new_key,': list found')
if len(value):
for k, v in enumerate(value):
items.extend(flatten({str(k): v}, new_key).items())
else:
if log: print('Adding key-value pair:',new_key,None)
items.append((new_key,None))
else:
if log: print('Adding key-value pair:',new_key,value)
items.append((new_key, value))
return dict(items)
def main():
"""
Main - program execute
"""
print (str(datetime.datetime.now()) + ' Starting ...')
filename = 'tree.json'
datadir = 'C:/Dev/nextclade/data/sars-cov-2/'
with open(datadir + filename, 'r') as fh:
tree_dict = json.load(fh)
# Flatten json to dict
a = flatten(tree_dict["tree"])
# Load to dataframe
df = pandas.DataFrame.from_dict(a, orient='index', dtype=str)
# Filter accordingly
df = df[df.index.str.contains("node_attrs.Nextclade_pango|node_attrs.partiallyAliased")]
print(df)
df.to_csv(datadir + str.replace(filename,".json",".tsv"), sep='\t')
print (str(datetime.datetime.now()) + ' Finished!')
exit()
if __name__ == '__main__':
main()