-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
134 lines (119 loc) · 3.58 KB
/
utils.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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# -*- coding: utf-8 -*-
"""
These functions is designed for crystal structure formation and
the 3D molecular viewer application
"""
import json
import plotly.graph_objects as go
import dash_html_components as html
import dash_bio as bio
def load_json(file_path):
"""
Loading the structure json file
Parameters
----------
file_path: str
Returns
-------
"""
with open(file_path, 'r', encoding='utf-8') as json_file:
mol_data, style_data = json.load(json_file)
return mol_data, style_data
def th4_plot(df, x_axis_column_name, y_axis_column_name, colour_column_value):
"""
Plotly Figure object for th4 data
Parameters
----------
df: DataFrame
Data table
x_axis_column_name: str
y_axis_column_name: str
colour_column_value: str
Returns
-------
Figure
Plotly figure object
"""
fig = go.Figure()
fig.add_trace(go.Scatter(
# X and Y coordinates from data table
x=df[x_axis_column_name],
y=df[y_axis_column_name],
text=df.index,
mode='markers',
# Set the format of scatter
marker=dict(
symbol='circle',
opacity=0.7,
line=dict(color='rgb(40, 40, 40)', width=0.2),
size=8,
# Colour bar
color=df[colour_column_value],
colorscale='RdBu',
colorbar=dict(
thicknessmode='pixels',
thickness=20,
title=dict(text=colour_column_value, side='right')
),
reversescale=True,
showscale=True
)
))
# Set the format of axes
axis_template = dict(linecolor='#444', tickcolor='#444',
ticks='outside', showline=True, zeroline=False,
gridcolor='lightgray')
fig.update_layout(
xaxis=dict(axis_template, **dict(title=x_axis_column_name)),
yaxis=dict(axis_template, **dict(title=y_axis_column_name)),
clickmode='event+select',
hovermode='closest',
plot_bgcolor='white'
)
return fig
def structure_viewer(df, interactive_data):
"""
The molecular 3D viewer
Parameters
----------
df: Dataframe
Data table
interactive_data: dict
Plotly callback information
Returns
-------
list
A list of viewer object
"""
def single_3d_viewer(json_file, structure_index):
mol_data, style_data = load_json(json_file)
mol_3d = html.Div(
id='viewer',
children=[
html.P('Structure ID: {}'.format(structure_index)),
bio.Molecule3dViewer(
id='mol-3d-viewer',
selectionType='atom',
styles=style_data,
modelData=mol_data
)
]
)
return mol_3d
mol_div = []
# Loading multiple 3D viewer
try:
for i in range(len(interactive_data['points'])):
# Find index from plotly callback information
index = int(interactive_data['points'][i]['pointIndex'])
origin_idx = index
# Get structure name
structure_name = int(df.iloc[index].Name)
# Path of parsed structure file
json_path = './data/th4/{}.json'.format(structure_name)
mol_div.append(single_3d_viewer(json_path, origin_idx))
# Default structure
except TypeError:
json_path = './data/th4/100020031487063.json'
mol_div.append(single_3d_viewer(json_path, 'TH4 global minimum'))
return mol_div