1+ """
2+ Reload folding data and GUI
3+ Run local_cluster.py in anothor thread
4+
5+ """
6+
7+ import sys
8+ from pathlib import Path
9+
10+ import pandas as pd
11+ import panel as pn
12+ import yaml
13+
14+ from pyhdx .batch_processing import yaml_to_hdxm
15+ from pyhdx .fileIO import csv_to_dataframe , load_fitresult
16+ from pyhdx .fileIO import csv_to_protein
17+ from pyhdx .web .apps import rfu_app
18+ from pyhdx .web .base import STATIC_DIR
19+ from pyhdx .web .utils import load_state , fix_multiindex_dtypes
20+
21+ sys ._excepthook = sys .excepthook
22+
23+ import traceback as tb
24+ def my_exception_hook (exctype , value , traceback ):
25+ # Print the error and traceback
26+ # https://stackoverflow.com/questions/43039048/pyqt5-fails-with-cryptic-message/43039363#43039363
27+ tb .print_tb (traceback , file = sys .stdout )
28+ print (exctype , value , traceback )
29+
30+ tb .print_stack ()
31+ print (traceback .format_exc ())
32+ # or
33+ print (sys .exc_info ()[2 ])
34+ # Call the normal Exception hook after
35+ sys ._excepthook (exctype , value , traceback )
36+ sys .exit (1 )
37+
38+
39+
40+ # Set the exception hook to our wrapping function
41+ sys .excepthook = my_exception_hook
42+
43+
44+ ctrl , tmpl = rfu_app ()
45+
46+
47+ cwd = Path (__file__ ).parent .resolve ()
48+ # root_dir = cwd.parent.parent
49+ # test_dir = cwd / 'test_data'
50+ #
51+ # fpath_1 = root_dir / 'tests' / 'test_data' / 'ecSecB_apo.csv'
52+ # fpath_2 = root_dir / 'tests' / 'test_data' / 'ecSecB_dimer.csv'
53+ # fitresult_dir = root_dir / 'tests' / 'test_data' / 'output' / 'ecsecb_tetramer_dimer'
54+ #
55+ #
56+ # data_dir = root_dir / 'tests' / 'test_data' / 'input'
57+ # #data_dir = cwd / 'rinky_data'
58+ #
59+ # yaml_dict = yaml.safe_load(Path(data_dir / 'data_states.yaml').read_text())
60+ # pdb_string = (test_dir / '1qyn.pdb').read_text()
61+
62+
63+
64+ def reload_tables ():
65+ test_dir = cwd / 'test_data'
66+ src = ctrl .sources ['main' ]
67+
68+ df = csv_to_dataframe (test_dir / 'peptides.csv' )
69+ # names = df.columns.names
70+ # df = df.convert_dtypes()
71+ # df.columns.names = names
72+ table_names = [
73+ 'rfu_residues.csv' ,
74+ 'rates.csv' ,
75+ 'peptides.csv' ,
76+ 'dG_fits.csv' ,
77+ 'ddG_comparison.csv' ,
78+ 'd_calc.csv' ,
79+ 'loss.csv' ,
80+ 'peptide_mse.csv'
81+ ]
82+ for name in table_names :
83+ try :
84+ df = csv_to_dataframe (test_dir / name )
85+ df .columns = fix_multiindex_dtypes (df .columns )
86+ src .tables [name .split ('.' )[0 ]] = df
87+ except Exception as e :
88+ print (e )
89+ print ('not loaded:' , name )
90+
91+ src .param .trigger ('updated' )
92+
93+
94+ #ctrl.views['protein'].object = pdb_string
95+
96+ #ctrl.views['protein'].object = pdb_string
97+
98+
99+ def reload_dashboard ():
100+ data_objs = {k : yaml_to_hdxm (v , data_dir = data_dir ) for k , v in yaml_dict .items ()}
101+ for k , v in data_objs .items ():
102+ v .metadata ['name' ] = k
103+
104+ source = ctrl .sources ['main' ]
105+ for ds in ['peptides' , 'peptides_mse' , 'd_calc' , 'rfu' , 'rates' , 'global_fit' , 'losses' ]:
106+ df = csv_to_protein (test_dir / f'{ ds } .csv' )
107+ source .add_df (df , ds )
108+
109+ #Temporary workaround for comment characters in csv files
110+ ds = 'colors'
111+ df = pd .read_csv (test_dir / f'{ ds } .csv' , header = [0 , 1 , 2 ], index_col = 0 ,
112+ skiprows = 3 )
113+ source .add_df (df , ds )
114+
115+
116+ def init_dashboard ():
117+ for k , v in yaml_dict .items ():
118+ load_state (ctrl , v , data_dir = data_dir , name = k )
119+
120+ # k = next(iter(yaml_dict.keys()))
121+ # load_state(ctrl, yaml_dict[k], data_dir=data_dir, name=k)
122+
123+ src = ctrl .sources ['main' ]
124+ fit_control = ctrl .control_panels ['FitControl' ]
125+
126+ fit_control .r1 = 0.05
127+ fit_control .r2 = 0.1
128+ fit_control .epochs = 200
129+ fit_control .patience = 100
130+
131+
132+ # ngl = ctrl.views['protein']
133+ # ngl._ngl.pdb_string = Path(test_dir / '1qyn.pdb').read_text()
134+ fit_result = load_fitresult (fitresult_dir )
135+
136+ src .add (fit_result , 'gibbs_fit_1' )
137+
138+ pdb_src = ctrl .sources ['pdb' ]
139+ pdb_src .add_from_string (pdb_string , '1qyn' )
140+
141+ diff = ctrl .control_panels ['DifferentialControl' ]
142+ #diff._action_add_comparison()
143+
144+ # ctrl.views['protein'].object = pdb_string
145+ #
146+ # fit_result = load_fitresult(fitresult_dir)
147+ # src.add(fit_result, 'fit_1')
148+
149+
150+ #if __name__ == '__main__':
151+ #pn.state.onload(reload_dashboard)
152+ #pn.state.onload(reload_tables)
153+ #pn.state.onload(init_dashboard)
154+
155+ if __name__ == '__main__' :
156+
157+ #init_dashboard()
158+
159+ pn .serve (tmpl , show = True , static_dirs = {'pyhdx' : STATIC_DIR })
160+
161+ elif __name__ .startswith ('bokeh_app' ):
162+ tmpl .servable ()
163+
164+ #ctrl.template.servable()
165+
166+
167+ # panel serve --show --autoreload --static-dirs pyhdx=C:\Users\jhsmi\pp\PyHDX\pyhdx\web\static
0 commit comments