Skip to content
Merged
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
16 changes: 8 additions & 8 deletions multi-ego-basic.ff/aminoacids.rtp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
CA CAH 0.000 0
CB CH2 0.000 0
CG CH2 0.000 0
CD CH2 0.000 0
CD CH2t 0.000 0
NE NE 0.000 0
CZ CZ 0.000 0
NH1 NZ 0.000 0
Expand Down Expand Up @@ -164,7 +164,7 @@
CA CAH 0.000 0
CB CH2 0.000 0
CG CS 0.000 0
OD1 O 0.000 0
OD1 OS 0.000 0
ND2 NT 0.000 0
C C 0.000 0
O O 0.000 0
Expand Down Expand Up @@ -267,7 +267,7 @@
N N 0.000 0
H H 0.000 0
CA CAH 0.000 0
CB CH2 0.000 0
CB CH2t 0.000 0
SG SH 0.000 0
C C 0.000 0
O O 0.000 0
Expand Down Expand Up @@ -310,7 +310,7 @@
N N 0.000 0
H H 0.000 0
CA CAH 0.000 0
CB CH2 0.000 0
CB CH2t 0.000 0
SG S 0.000 0
C C 0.000 0
O O 0.000 0
Expand Down Expand Up @@ -357,7 +357,7 @@
CB CH2 0.000 0
CG CH2 0.000 0
CD CS 0.000 0
OE1 O 0.000 0
OE1 OS 0.000 0
NE2 NT 0.000 0
C C 0.000 0
O O 0.000 0
Expand Down Expand Up @@ -674,7 +674,7 @@
CB CH2 0.000 0
CG CH2 0.000 0
CD CH2 0.000 0
CE CH2 0.000 0
CE CH2t 0.000 0
NZ NL 0.000 0
C C 0.000 0
O O 0.000 0
Expand Down Expand Up @@ -730,7 +730,7 @@
CB CH2 0.000 0
CG CH2 0.000 0
SD SM 0.000 0
CE CH3 0.000 0
CE CH3t 0.000 0
C C 0.000 0
O O 0.000 0
[ bonds ]
Expand Down Expand Up @@ -898,7 +898,7 @@
N N 0.000 0
H H 0.000 0
CA CAH 0.000 0
CB CH2 0.000 0
CB CH2t 0.000 0
OG OA 0.000 0
C C 0.000 0
O O 0.000 0
Expand Down
3 changes: 3 additions & 0 deletions multi-ego-basic.ff/atomtypes.atp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
O 15.99940 ; carbonyl oxygen (C=O)
OS 15.99940 ; carbonyl oxygen (C=O)
OM 15.99940 ; carboxyl oxygen (CO-)
OA 16.99940 ; hydroxyl, sugar or ester oxygen
; N 15.00670 ; peptide nitrogen (N or NH)
Expand All @@ -18,8 +19,10 @@
CH1 13.01900 ; aliphatic or sugar CH-group
CH1t 13.01900 ; aliphatic or sugar CH-group
CH2 14.02700 ; aliphatic or sugar CH2-group
CH2t 14.02700 ; aliphatic or sugar CH2-group
CAH2 14.02700 ; aliphatic or sugar CH2-group
CH3 15.03500 ; aliphatic CH3-group
CH3t 15.03500 ; aliphatic CH3-group
CH2r 14.02700 ; CH2-group in a ring
S 32.06000 ; sulfur
SM 32.06000 ; sulfur MET
Expand Down
3 changes: 3 additions & 0 deletions multi-ego-basic.ff/ffnonbonded.itp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[ atomtypes ]
;name at.num mass charge ptype c6 c12
O 8 0.000 0.000 A 0.0000000000 2.631580e-07
OS 8 0.000 0.000 A 0.0000000000 2.631580e-07
OM 8 0.000 0.000 A 0.0000000000 1.724403e-07
OA 8 0.000 0.000 A 0.0000000000 5.018430e-07
N 7 0.000 0.000 A 0.0000000000 8.752940e-07
Expand All @@ -19,8 +20,10 @@
CH1 6 0.000 0.000 A 0.0000000000 6.555574e-05
CH1t 6 0.000 0.000 A 0.0000000000 6.555574e-05
CH2 6 0.000 0.000 A 0.0000000000 1.543890e-05
CH2t 6 0.000 0.000 A 0.0000000000 1.543890e-05
CAH2 6 0.000 0.000 A 0.0000000000 1.543890e-05
CH3 6 0.000 0.000 A 0.0000000000 8.595562e-06
CH3t 6 0.000 0.000 A 0.0000000000 8.595562e-06
CH2r 6 0.000 0.000 A 0.0000000000 1.193966e-05
S 16 0.000 0.000 A 0.0000000000 2.724050e-06
SM 16 0.000 0.000 A 0.0000000000 2.724050e-06
Expand Down
85 changes: 83 additions & 2 deletions src/multiego/Minimum_CUTOFF_Manual_modif.csv

Large diffs are not rendered by default.

Binary file modified src/multiego/atdhisto.pkl
Binary file not shown.
Binary file added src/multiego/atdhisto_nosimm.pkl
Binary file not shown.
11 changes: 11 additions & 0 deletions src/multiego/fileio.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@


from . import _term
from . import type_definitions


def strip_gz_h5_suffix(filename):
Expand Down Expand Up @@ -391,6 +392,16 @@

f.write("\nContact parameters:\n")
f.write(stat_str)
if parameters.egos == "mg":
f.write("\nInternal parameters:\n")
# write contents of the internal parameters file from type_definitions.py
f.write(f" - EMAX: {type_definitions.EMAX}\n")
f.write(f" - P_TH: {type_definitions.P_TH}\n")
f.write(f" - EMAX_BKBN: {type_definitions.EMAX_BKBN}\n")
f.write(f" - P_TH_BKBN: {type_definitions.P_TH_BKBN}\n")
f.write(f" - P_TH_SC_BKBN: {type_definitions.P_TH_SC_BKBN}\n")
f.write(f" - EMAX_SC_BKBN: {type_definitions.EMAX_SC_BKBN}\n")
f.write(f" - OM_SCALEDOWN: {type_definitions.OM_SCALEDOWN}\n")

def save_matrix_png(output_path):
# import matrix from type definition and use the class function to plot it
Expand Down
153 changes: 72 additions & 81 deletions src/multiego/interaction_matrix.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from fileinput import filename

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
Expand All @@ -6,6 +8,10 @@
from matplotlib.colors import LinearSegmentedColormap, TwoSlopeNorm, Normalize
from matplotlib.cm import ScalarMappable

bkbn_attypes = ["H", "O", "N", "C", "CAH"]#, "CAH2"]
H_interactions = ["O", "H", "OA", "OM", "OS"]

noplot = ["C0", "CH3p", "CR1", "OE", "P"]

class AttrDict(dict):
def __getattr__(self, key):
Expand All @@ -21,14 +27,19 @@

class InteractionMatrix:

def __init__(self, pkl_file, emax=0.16, c12_rep_df=None, pth=None, show=False, pth_bkbn=None, emax_bkbn=None):
self.atmat = self.read_pickle_file(pkl_file)
def __init__(self, pkl_file, emax=0.16, c12_rep_df=None, pth=None, show=False, pth_bkbn=None, emax_bkbn=None, pth_sc_bkbn=None, emax_sc_bkbn=None, om_scaledown=None, pkl_file2=None, r2_corr_sb=None):
self.atmat = self.build_matrix_dataframe(pkl_file, filename2=pkl_file2, r2_corr_sb=r2_corr_sb)
self.emax = emax
self.emax_bkbn = emax_bkbn if emax_bkbn is not None else emax
self.emax_sc_bkbn = emax_sc_bkbn if emax_sc_bkbn is not None else self.emax_bkbn
self.pth = pth
self.pth_bkbn = pth_bkbn if pth_bkbn is not None else pth
self.pth_sc_bkbn = pth_sc_bkbn if pth_sc_bkbn is not None else (self.pth + self.pth_bkbn) / 2
self.om_scaledown = om_scaledown
self.r2_corr_sb = r2_corr_sb
self.c12_rep_df = c12_rep_df
self.define_p_threshold()

# self.define_p_threshold()
self.nonlocal_matrix()
if show:
self.plot_energy_matrix(show=show)
Expand All @@ -52,7 +63,7 @@
e /= np.max(e)
e = np.where(e==-np.inf, -2, e)
e = np.where(e > 0.0, 0.07 + e* (emax-0.07), e)
e = np.array([self.roundPartial(ei) for ei in e])
# e = np.array([self.roundPartial(ei) for ei in e])
return e

def plot_energy_matrix(self, out_path = None, show = False):
Expand All @@ -63,16 +74,15 @@
# TODO read them by gromos to avoid hardcoding them here
bkbnd_atoms = ["C", "O", "N", "CAH", "CAH2", "H"]
attype_ordering_charged = [ "OM", "NL", "NZ"]
attype_ordering_polar = [ "NE", "NR", "NT", "OA", "CZ", "S", "SM", "SH", "CH2r", "CH1t"]
attype_ordering_apolar = ["CH1","CR", "CH", "CH2", "CH3", "CS"]
attype_ordering_polar = [ "NE", "NR", "NT", "OA","OS", "CZ", "CS", "CH1t", "CH2r", "CH2t", "CH3t", "S", "SM", "SH"]
attype_ordering_apolar = ["CH1", "CR", "CH", "CH2", "CH3"]



list_atom1 = np.sort(np.array(self.atmat["attype1"].unique()))
list_atom2 = np.sort(np.array(self.atmat["attype2"].unique()))

# TODO read them by gromos to avoid hardcoding them here
noplot = ["C0", "CH3p", "CR1", "OE", "P"]
# remove from list_atom1 and list_atom2 the atoms in noplot
list_atom1 = [at for at in list_atom1 if at not in noplot]
list_atom2 = [at for at in list_atom2 if at not in noplot]
Expand All @@ -87,8 +97,8 @@
if at1 in noplot or at2 in noplot:
continue
energy = self.atmat.loc[((self.atmat["attype1"]==at1) & (self.atmat["attype2"]==at2)) | ((self.atmat["attype1"]==at2) & (self.atmat["attype2"]==at1)), "energy"].values
if (at1 == "H" and at2 not in ["O", "H", "OA", "OM"]) or (at2 == "H" and at1 not in ["O", "H", "OA", "OM"]):
energies.append(-2)
if (at1 == "H" and at2 not in H_interactions) or (at2 == "H" and at1 not in H_interactions):
energies.append(-np.inf) # force repulsion for H interactions with non-interaction-H atoms
continue
if len(energy) > 0:
energy = energy[0]
Expand Down Expand Up @@ -130,17 +140,7 @@
if show:
plt.show()


def read_pickle_file(self, filename):
"""
Read atdhito data from pikle file per atomtype pair and return a pandas dataframe with the following columns:
- atom_pair: atomtype pair
- probability: probability of the atomtype pair
- exp_aver: expected average distance of the atomtype pair
- attype1: first atomtype
- attype2: second atomtype
- cutoff: cutoff distance of the atomtype pair
"""
_this_dir = os.path.dirname(os.path.abspath(__file__))
pikle_path = os.path.join(_this_dir, filename)

Expand Down Expand Up @@ -180,53 +180,50 @@
atmat["sum_probs"] = sum_probs
# fill nan values in probability with 0
atmat["probability"] = atmat["probability"].fillna(0)

# # for all bkbn atom pairs set multiply the probability by a factor f_bkbn to compansate for the different statistics
# bkbn_pairs = ["O_H", "O_O", "N_N", "C_C", "CAH_CAH", "CAH2_CAH2", "O_N", "O_C", "O_CAH","O_CAH2", "N_C","N_CAH", "C_CAH", "N_CAH2", "C_CAH2", "CAH_CAH2"]
# #bkbn_pairs = ["O_H", "O_O", "N_N", "C_C", "CAH_CAH", "O_N", "O_C", "O_CAH", "N_C","N_CAH", "C_CAH" ]
# for pair in bkbn_pairs:
# atmat.loc[atmat["atom_pair"]==pair, "probability"] *= self.f_bkbn
# where O-H set distance to 0.195
# atmat.loc[atmat["atom_pair"]=="O_H", "exp_aver"] = 0.195
# atmat.loc[atmat["atom_pair"]=="O_N", "exp_aver"] = 0.29
# atmat.loc[atmat["atom_pair"]=="C_N", "exp_aver"] = 0.41

return atmat


def define_p_threshold(self):
def build_matrix_dataframe(self, filename, filename2=None, r2_corr_sb=None):
"""
Define p_threshold used to split attractive and repulsive interactions
Read atdhito data from pikle file per atomtype pair and return a pandas dataframe with the following columns:
- atom_pair: atomtype pair
- probability: probability of the atomtype pair
- exp_aver: expected average distance of the atomtype pair
- attype1: first atomtype
- attype2: second atomtype
- cutoff: cutoff distance of the atomtype pair
"""
if self.pth is None:
print("Neither atpairref nor pth provided, or both provided. Optimizing pth to best fit KL_SCALE.")
# find the minimum pth such that the following atom-pairs have energy < 0
# OM_OM, O_O, NL_NL, O_OM
pth = 0.001 # Initialize with a default value
dp = 0.001
while True:
self.atmat["energy"] = np.array(self.regall(self.atmat["probability"].to_numpy(), 1, self.emax, pth))
if (self.atmat.loc[self.atmat["atom_pair"]=="NL_NL", "energy"].values[0] < 0):
# self.atmat.loc[self.atmat["atom_pair"]=="OM_OM", "energy"].values[0] < 0 and
# self.atmat.loc[self.atmat["atom_pair"]=="O_O", "energy"].values[0] < 0 and
# self.atmat.loc[self.atmat["atom_pair"]=="OA_OA", "energy"].values[0] < 0):
break
pth += dp
# find also the maximum pth such that the following atom-pairs have energy > 0
# OM_NZ
self.pth = pth
pth_max = pth
while True:
self.atmat["energy"] = np.array(self.regall(self.atmat["probability"].to_numpy(), 1, self.emax, pth_max))
if self.atmat.loc[self.atmat["atom_pair"]=="CH_CH", "energy"].values[0] < 0:
break
pth_max += dp
pth_max -= dp
print(f"Minimum pth: {pth}, Maximum pth: {pth_max}")
self.pth_max = pth_max
else:
self.pth_max = self.pth
print(f"Using provided pth: {self.pth}")
_this_dir = os.path.dirname(os.path.abspath(__file__))

atmat = self.read_pickle_file(filename)

if filename2 != None:
atmat_sb = self.read_pickle_file(filename2)

# use exp_aver and probability from atmat_sb for atom pairs where one of the atomtypes is in bkbn_attypes and the other is not
if filename2 != None:
for index, row in atmat.iterrows():
at1 = row["attype1"]
at2 = row["attype2"]
if (at1 in bkbn_attypes and at2 not in bkbn_attypes) or (at2 in bkbn_attypes and at1 not in bkbn_attypes):
# find the corresponding row in atmat_sb
sb_row = atmat_sb.loc[(atmat_sb["attype1"]==at1) & (atmat_sb["attype2"]==at2)]
if len(sb_row) == 0:
sb_row = atmat_sb.loc[(atmat_sb["attype1"]==at2) & (atmat_sb["attype2"]==at1)]
if len(sb_row) > 0:
atmat.at[index, "exp_aver"] = sb_row["exp_aver"].values[0]
atmat.at[index, "probability"] = sb_row["probability"].values[0]

# read sc-bkbn correction:
if r2_corr_sb is not None:
corr = pd.read_csv(os.path.join(_this_dir, r2_corr_sb), sep=r"\s+")
for index, row in atmat.iterrows():
atpair = row["atom_pair"]
corr_row = corr.loc[(corr["atom_pair"]==atpair)]
if len(corr_row) == 0:
corr_row = corr.loc[(corr["atom_pair"]==atpair)]
if len(corr_row) > 0:
atmat.at[index, "probability"] = atmat.at[index, "probability"] - atmat.at[index, "probability"] *corr_row["r2"].values[0]
return atmat

def define_special_nonlocal_dict(self):
"""
Expand All @@ -237,7 +234,7 @@
for atom_pair in self.atmat["atom_pair"].unique():
atp1, atp2 = atom_pair.split("_")
# write only H-O interaction, skip all the rest
if (atp1 == "H" and atp2 not in ["O", "H"]) or (atp2 == "H" and atp1 not in ["O", "H"]):
if (atp1 == "H" and atp2 not in H_interactions) or (atp2 == "H" and atp1 not in H_interactions):
continue
energy = self.atmat.loc[self.atmat["atom_pair"]==atom_pair, "energy"].values[0]
sigma = self.atmat.loc[self.atmat["atom_pair"]==atom_pair, "exp_aver"].values[0] / 2**(1/6)
Expand All @@ -258,23 +255,17 @@
return special_nonlocal_dict

def nonlocal_matrix(self):
"""
Calculate the energy values for the atomtype pairs based on the probability values and the p_threshold.
"""
# sc-sc
condition_sc_sc = ~(self.atmat["attype1"].isin(bkbn_attypes) | self.atmat["attype2"].isin(bkbn_attypes))
self.atmat.loc[condition_sc_sc, "energy"] = np.array(self.regall(self.atmat.loc[condition_sc_sc, "probability"].to_numpy(), 1, self.emax, self.pth))
# sc-bkbn
condition_sc_bkbn = (self.atmat["attype1"].isin(bkbn_attypes) & ~self.atmat["attype2"].isin(bkbn_attypes)) | (self.atmat["attype2"].isin(bkbn_attypes) & ~self.atmat["attype1"].isin(bkbn_attypes))
self.atmat.loc[condition_sc_bkbn, "energy"] = np.array(self.regall(self.atmat.loc[condition_sc_bkbn, "probability"].to_numpy(), 1, self.emax_sc_bkbn, self.pth_sc_bkbn))
# bkbn-bkbn
condition_bkbn_bkbn = self.atmat["attype1"].isin(bkbn_attypes) & self.atmat["attype2"].isin(bkbn_attypes)
self.atmat.loc[ condition_bkbn_bkbn, "energy"] = np.array(self.regall(self.atmat.loc[condition_bkbn_bkbn, "probability"].to_numpy(), 1, self.emax_bkbn, self.pth_bkbn))

bkbn_pairs = [ "H_H", "O_O", "N_N", "C_C", "CAH_CAH", "CAH2_CAH2",
"O_N", "O_C", "O_CAH","O_CAH2", "O_H",
"N_C","N_CAH", "N_CAH2",
"C_CAH","C_CAH2",
"CAH_CAH2"]
# check that all bkbn_pairs are in the atmat dataframe
for pair in bkbn_pairs:
if pair not in self.atmat["atom_pair"].values:
raise ValueError(f"Atom pair {pair} not found in atmat dataframe.")

# where bkbn_pairs set probability pth to pth_bkbn and else to pth
self.atmat.loc[self.atmat["atom_pair"].isin(bkbn_pairs), "energy"] = np.array(self.regall(self.atmat.loc[self.atmat["atom_pair"].isin(bkbn_pairs), "probability"].to_numpy(), 1, self.emax_bkbn, self.pth_bkbn))
self.atmat.loc[~self.atmat["atom_pair"].isin(bkbn_pairs), "energy"] = np.array(self.regall(self.atmat.loc[~self.atmat["atom_pair"].isin(bkbn_pairs), "probability"].to_numpy(), 1, self.emax, self.pth))
#self.atmat["energy"] = np.array(self.regall(self.atmat["probability"].to_numpy(), 1, self.emax, self.pth))

self.special_nonlocal_dict = self.define_special_nonlocal_dict()



Binary file added src/multiego/sb_atdhisto_2res.pkl
Binary file not shown.
Binary file added src/multiego/sb_atdhisto_3res.pkl
Binary file not shown.
Loading
Loading