Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StreamlitApp #167

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
323 changes: 323 additions & 0 deletions Scripts/BuildDatabank/databankLibrary.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

import pandas as pd

import streamlit as st

__pdoc__ = {}

logger = logging.getLogger("__name__")
Expand Down Expand Up @@ -2443,3 +2445,324 @@ def getHydrationLevel(system):
Nlipid += np.sum(system['COMPOSITION'][molecule]['COUNT'])
Nwater = system['COMPOSITION']['SOL']['COUNT']
return Nwater/Nlipid



######################## StreamlitApp ###########################


def ShowTableGui(SortedQualities, quality):
"""
Shows a table of simulation qualities against experimental data in a Streamlit app.

:param SortedQualities: list of dictionaries to be shown, available in folder ``Data/Ranking/``
:param quality: should be either ``TotalQuality`` or universal lipid name. First one shows the system total quality. Latter shows the individual lipid quality.
"""
rounding = ['headgroup', 'sn-1', 'sn-2', 'total', 'tails', 'FFQuality']
QualityTable = []

for i in SortedQualities:
StoredToTable = {}

for k, v in i[quality].items():
if k in rounding:
if v and v != float("inf") and not math.isnan(v):
StoredToTable[k] = round(float(v), 2)

StoredToTable['Forcefield'] = i['system']['FF']
molecules = ''
MolNumbers = ''
for lipid in i['system']['COMPOSITION']:
molecules += lipid + ':'
MolNumbers += str(np.sum(i['system']['COMPOSITION'][lipid]['COUNT'])) + ':'
StoredToTable['Molecules'] = molecules[:-1]
StoredToTable['Number of molecules'] = ' (' + MolNumbers[:-1] + ')'
StoredToTable['Temperature'] = i['system']['TEMPERATURE']
StoredToTable['ID'] = i['system']['ID']

QualityTable.append(StoredToTable)

df = pd.json_normalize(QualityTable)
st.table(df)



def plotFormFactorGui(expFormFactor, k, legend, PlotColor):
plt.figure() # Create a new figure for Streamlit
xValues, yValues = [], []
for i in expFormFactor:
xValues.append(i[0])
yValues.append(k*i[1])
plt.plot(xValues, yValues, label=legend, color=PlotColor, linewidth=4.0)
plt.xlabel(r'$q_{z} [Å^{-1}]$', size=20)
plt.ylabel(r'$|F(q_{z})|$', size=20)
plt.xticks(size=20)
plt.yticks(size=20)
plt.xlim([0, 0.69])
plt.ylim([-10, 250])
plt.legend(loc="upper right")
# Use Streamlit's method to display the plot
st.pyplot(plt)

def plotOrderParametersGui(OPsim, OPexp):
""":meta private:"""
xValuesHG = []
xValuesSN1 = []
xValuesSN2 = []

yValuesHGsim = []
yValuesSN1sim = []
yValuesSN2sim = []
yValuesHGsimERR = []
yValuesSN1simERR = []
yValuesSN2simERR = []
yValuesHGexp = []
yValuesSN1exp = []
yValuesSN2exp = []
xValuesHGexp = []
xValuesSN1exp = []
xValuesSN2exp = []

sn1carbons = {'M_G1C3_M M_G1C3H1_M' :2,
'M_G1C3_M M_G1C3H2_M' :2,
'M_G1C4_M M_G1C4H1_M' :3,
'M_G1C4_M M_G1C4H2_M' :3,
'M_G1C5_M M_G1C5H1_M' :4,
'M_G1C5_M M_G1C5H2_M' :4,
'M_G1C6_M M_G1C6H1_M' :5,
'M_G1C6_M M_G1C6H2_M' :5,
'M_G1C7_M M_G1C7H1_M' :6,
'M_G1C7_M M_G1C7H2_M' :6,
'M_G1C8_M M_G1C8H1_M' :7,
'M_G1C8_M M_G1C8H2_M' :7,
'M_G1C9_M M_G1C9H1_M' :8,
'M_G1C9_M M_G1C9H2_M' :8,
'M_G1C10_M M_G1C10H1_M' :9,
'M_G1C10_M M_G1C10H2_M' :9,
'M_G1C11_M M_G1C11H1_M' :10,
'M_G1C11_M M_G1C11H2_M' :10,
'M_G1C12_M M_G1C12H1_M' :11,
'M_G1C12_M M_G1C12H2_M' :11,
'M_G1C13_M M_G1C13H1_M' :12,
'M_G1C13_M M_G1C13H2_M' :12,
'M_G1C14_M M_G1C14H1_M' :13,
'M_G1C14_M M_G1C14H2_M' :13,
'M_G1C15_M M_G1C15H1_M' :14,
'M_G1C15_M M_G1C15H2_M' :14,
'M_G1C16_M M_G1C16H1_M' :15,
'M_G1C16_M M_G1C16H2_M' :15,
'M_G1C17_M M_G1C17H1_M' :16,
'M_G1C17_M M_G1C17H2_M' :16,
'M_G1C17_M M_G1C17H3_M' :16,
}

sn2carbons = {'M_G2C3_M M_G2C3H1_M' :2,
'M_G2C3_M M_G2C3H2_M' :2,
'M_G2C4_M M_G2C4H1_M' :3,
'M_G2C4_M M_G2C4H2_M' :3,
'M_G2C5_M M_G2C5H1_M' :4,
'M_G2C5_M M_G2C5H2_M' :4,
'M_G2C6_M M_G2C6H1_M' :5,
'M_G2C6_M M_G2C6H2_M' :5,
'M_G2C7_M M_G2C7H1_M' :6,
'M_G2C7_M M_G2C7H2_M' :6,
'M_G2C8_M M_G2C8H1_M' :7,
'M_G2C8_M M_G2C8H2_M' :7,
'M_G2C9_M M_G2C9H1_M' :8,
'M_G2C9_M M_G2C9H2_M' :8,
'M_G2C10_M M_G2C10H1_M' :9,
'M_G2C10_M M_G2C10H2_M' :9,
'M_G2C11_M M_G2C11H1_M' :10,
'M_G2C11_M M_G2C11H2_M' :10,
'M_G2C12_M M_G2C12H1_M' :11,
'M_G2C12_M M_G2C12H2_M' :11,
'M_G2C13_M M_G2C13H1_M' :12,
'M_G2C13_M M_G2C13H2_M' :12,
'M_G2C14_M M_G2C14H1_M' :13,
'M_G2C14_M M_G2C14H2_M' :13,
'M_G2C15_M M_G2C15H1_M' :14,
'M_G2C15_M M_G2C15H2_M' :14,
'M_G2C16_M M_G2C16H1_M' :15,
'M_G2C16_M M_G2C16H2_M' :15,
'M_G2C17_M M_G2C17H1_M' :16,
'M_G2C17_M M_G2C17H2_M' :16,
'M_G2C17_M M_G2C17H3_M' :16,
'M_G2C18_M M_G2C18H1_M' :17,
'M_G2C18_M M_G2C18H2_M' :17,
'M_G2C18_M M_G2C18H3_M' :17,
'M_G2C19_M M_G2C19H1_M' :18,
'M_G2C19_M M_G2C19H2_M' :18,
'M_G2C19_M M_G2C19H3_M' :18,
}

HGcarbons = {'M_G3N6C1_M M_G3N6C1H1_M' : 1,
'M_G3N6C1_M M_G3N6C1H2_M' : 1,
'M_G3N6C1_M M_G3N6C1H3_M' : 1,
'M_G3N6C2_M M_G3N6C2H1_M' : 1,
'M_G3N6C2_M M_G3N6C2H2_M' : 1,
'M_G3N6C2_M M_G3N6C2H3_M' : 1,
'M_G3N6C3_M M_G3N6C3H1_M' : 1,
'M_G3N6C3_M M_G3N6C3H2_M' : 1,
'M_G3N6C3_M M_G3N6C3H3_M' : 1,
'M_G3C5_M M_G3C5H1_M' : 2,
'M_G3C5_M M_G3C5H2_M' : 2,
'M_G3C4_M M_G3C4H1_M' : 3,
'M_G3C4_M M_G3C4H2_M' : 3,
'M_G3_M M_G3H1_M' : 4,
'M_G3_M M_G3H2_M' : 4,
'M_G2_M M_G2H1_M' : 5,
'M_G1_M M_G1H1_M' : 6,
'M_G1_M M_G1H2_M' : 6,
}


for key in OPsim:
if 'M_G1C' in key:
try:
xValuesSN1.append(sn1carbons[key])
yValuesSN1sim.append(float(OPsim[key][0][0]))
yValuesSN1simERR.append(float(OPsim[key][0][2]))
yValuesSN1exp.append(OPexp[key][0][0])
xValuesSN1exp.append(sn1carbons[key])
except:
pass
elif 'M_G2C' in key:
try:
xValuesSN2.append(sn2carbons[key])
yValuesSN2sim.append(float(OPsim[key][0][0]))
yValuesSN2simERR.append(float(OPsim[key][0][2]))
yValuesSN2exp.append(OPexp[key][0][0])
xValuesSN2exp.append(sn2carbons[key])
except:
pass
elif 'M_G3' in key or 'M_G2_M' in key or 'M_G1_M' in key:
try:
xValuesHG.append(HGcarbons[key])
yValuesHGsim.append(float(OPsim[key][0][0]))
yValuesHGsimERR.append(float(OPsim[key][0][2]))
yValuesHGexp.append(OPexp[key][0][0])
xValuesHGexp.append(HGcarbons[key])
except:
pass
#print(xValues,yValues)
# Plotting headgroup order parameters
plt.figure()
plt.errorbar(xValuesHGexp, yValuesHGexp, yerr=0.02, fmt='.', color='black', markersize=25)
plt.errorbar(xValuesHG, yValuesHGsim, yerr=yValuesHGsimERR, fmt='.', color='red', markersize=20)
plt.xticks([1, 2, 3, 4, 5, 6], ['\u03B3', '\u03B2', '\u03B1', '$g_{1}$', '$g_{2}$', '$g_{3}$'], size=20)
plt.ylabel(r'$S_{CH}$', size=25)
plt.yticks(size=20)
st.pyplot(plt)

# Plotting sn-1 order parameters
plt.figure()
plt.errorbar(xValuesSN1exp, yValuesSN1exp, yerr=0.02, fmt='.', color='black', markersize=20)
plt.errorbar(xValuesSN1, yValuesSN1sim, yerr=yValuesSN1simERR, fmt='.', color='red', markersize=25)
plt.xlabel('Carbon', size=25)
plt.ylabel(r'$S_{CH}$', size=25)
plt.xticks(size=20)
plt.yticks(size=20)
st.pyplot(plt)

# Plotting sn-2 order parameters
plt.figure()
plt.errorbar(xValuesSN2exp, yValuesSN2exp, yerr=0.02, fmt='.', color='black', markersize=20)
plt.errorbar(xValuesSN2, yValuesSN2sim, yerr=yValuesSN2simERR, fmt='.', color='red', markersize=25)
plt.xlabel('Carbon', size=25)
plt.ylabel(r'$S_{CH}$', size=25)
plt.xticks(size=20)
plt.yticks(size=20)
st.pyplot(plt)



def plotSimulationGui(ID, lipid):
"""
Creates plots of form factor and C-H bond order parameters for the selected ``lipid`` from a simulation with the given ``ID`` number.

:param ID: NMRlipids databank ID number of the simulation
:param lipid: universal molecul name of the lipid

"""
DataBankPath = os.path.dirname(os.path.realpath(__file__)) + '/../../'
systems = initialize_databank(DataBankPath)
#print(systems)
for system in systems:
#print(system)
if system['ID'] == ID:
path = DataBankPath + 'Data/Simulations/' + system['path']
#lipid = 'POPC'
FFpathSIM = path + 'FormFactor.json'
OPpathSIM = path + lipid + 'OrderParameters.json'
READMEfilepath = path + '/README.yaml'
FFQualityFilePath = path + '/FormFactorQuality.json'


with open(READMEfilepath) as yaml_file:
readme = yaml.load(yaml_file, Loader=yaml.FullLoader)

# Create a clickable DOI link
doi_link = f"[{readme['DOI']}](https://doi.org/{readme['DOI']})"
st.markdown(f'DOI: {doi_link}', unsafe_allow_html=True)

try:
with open(FFQualityFilePath) as json_file:
FFq = json.load(json_file)
st.write('Form factor quality: ', FFq[0])
for subdir, dirs, files in os.walk(DataBankPath + 'Data/experiments/FormFactors/' + readme['EXPERIMENT']['FORMFACTOR'] + '/'):
for filename in files:
#filepath = '../../Data/experiments/FormFactors/' + expFFpath + '/' + filename
if filename.endswith('_FormFactor.json'):
FFpathEXP = subdir + filename
#FFpathEXP = DataBankPath + 'experiments/FormFactors/' + readme['EXPERIMENT']['FORMFACTOR'] + '/POPS_ULV_25Cin0D_SHE_FormFactor.json'
with open(FFpathEXP) as json_file:
FFexp = json.load(json_file)

except:
print('Force field quality not found')


with open(OPpathSIM) as json_file:
OPsim = json.load(json_file)

OPexp = {}
for expOPfolder in list(readme['EXPERIMENT']['ORDERPARAMETER'][lipid].values()):
#expOPfolder = list(readme['EXPERIMENT']['ORDERPARAMETER'][lipid].values())[0]
OPpathEXP = DataBankPath + 'Data/experiments/OrderParameters/' + expOPfolder + '/' + lipid + '_Order_Parameters.json'
#print(OPpathEXP)
with open(OPpathEXP) as json_file:
OPexp.update(json.load(json_file))
#print(OPexp)

try:
with open(FFpathSIM) as json_file:
FFsim = json.load(json_file)
plotFormFactorGui(FFsim, 1, 'Simulation', 'red')
plotFormFactorGui(FFexp, FFq[1], 'Experiment', 'black')
except:
st.write('Form factor plotting failed')

plotOrderParametersGui(OPsim, OPexp)



def ShowEquilibrationTimesGui(system):
"""
Prints relative equilibration time for each lipid within a simulation defined by ``system``.
Relative equilibration times are calculated with ``NMRPCA_timerelax.py`` and stored in ``eq_times.json`` files.

:param system: NMRlipids databank dictionary defining a simulation.
"""

EqTimesPath = os.path.dirname(os.path.realpath(__file__)) + '/../../Data/Simulations/' + system['path'] + 'eq_times.json'

try:
#if (os.path.isfile(EqTimesPath)):
with open(EqTimesPath) as f:
EqTimeDict = json.load(f)
except:
print('eq_times.json not found')
exit()

for i in EqTimeDict:
st.write(i+':', EqTimeDict[i])
Loading