Repository navigation
Expand file tree
/
Copy pathutils.py
More file actions
178 lines (137 loc) · 5.23 KB
/
Copy pathutils.py
File metadata and controls
178 lines (137 loc) · 5.23 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import os
import json
from tqdm.autonotebook import tqdm
from rdkit.Chem import AllChem
from rdkit.Chem.rdMolDescriptors import GetMorganFingerprint
from rdkit import DataStructs
from rdkit.SimDivFilters.rdSimDivPickers import MaxMinPicker
import numpy as np
import numpy.matlib
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns
from IPython.display import SVG, display, HTML
import cairosvg
LATEX_TABLE = r'''\documentclass{{standalone}}
\usepackage{{booktabs}}
\usepackage{{multirow}}
\usepackage{{graphicx}}
\usepackage{{xcolor,colortbl}}
\begin{{document}}
{}
\end{{document}}
'''
def get_curve_elbow(values):
"""https://stackoverflow.com/questions/2018178/finding-the-best-trade-off-point-on-a-curve """
n = len(values)
allCoord = np.vstack((range(n), values)).T
first_v = allCoord[0]
line_vec = allCoord[-1] - allCoord[0]
line_vec_norm = line_vec / np.sqrt(np.sum(line_vec**2))
vec_from_first = allCoord - first_v
scalar_product = np.sum(
vec_from_first * np.matlib.repmat(line_vec_norm, n, 1), axis=1)
vec_from_first_parallel = np.outer(scalar_product, line_vec_norm)
vec_to_line = vec_from_first - vec_from_first_parallel
dist_to_line = np.sqrt(np.sum(vec_to_line ** 2, axis=1))
elbow_index = np.argmax(dist_to_line)
return elbow_index
def display_svg(svg):
return display(SVG(svg))
def save_latextable(df, adir, name):
"""ddataframe to latex"""
filename = os.path.join(adir, '{}.tex'.format(name))
a_str = df.to_latex(multicolumn=True, multirow=True, escape=False)
with open(filename, 'w') as a_file:
a_file.write(LATEX_TABLE.format(a_str))
def save_molgrid(svg, adir, name):
filename = os.path.join(adir, '{}.{}'.format(name, 'svg'))
with open(filename, 'w') as afile:
afile.write(svg)
filename = os.path.join(adir, '{}.{}'.format(name, 'png'))
cairosvg.svg2png(bytestring=svg, write_to=filename)
def discrete_colormap(colors):
return mpl.colors.ListedColormap(colors)
def save_figure(adir, name):
for ext in ['png', 'svg']:
filename = os.path.join(adir, '{}.{}'.format(name, ext))
plt.savefig(filename, dpi=300, bbox_inches='tight', transparent=True)
def header_str(a_str, n=80):
"""Returns a string formatted as a header."""
return '{{:=^{:d}}}'.format(n).format(' ' + a_str + ' ')
def header_html(astr, level=1):
html_code = '<h{}>{}</h{}>'.format(level, astr, level)
return display(HTML(html_code))
def header_legend(label, label_space=2, **legend_kwargs):
fmt = '{{:<{:d}s}}'.format(label_space) + ' ({:>5s}, {:>5s}, {:>5s})'
header = fmt.format(label, 'n', 'mean', 'std')
ax = plt.gca()
handles, labels = ax.get_legend_handles_labels()
ex_labels = [header]
ex_handles = [mpl.patches.Rectangle(
(0, 0), 0, 0, alpha=0.0) for i in ex_labels]
if 'prop' not in legend_kwargs.keys():
legend_kwargs['prop'] = {'family': 'monospace', 'size': 'x-small'}
return plt.legend(handles=ex_handles + handles, labels=ex_labels + labels, **legend_kwargs)
def legend_stats_label(label, y, label_space=2):
fmt = '{{:<{:d}s}}'.format(label_space) + ' ({:>5s}, {:>5s}, {:>5s})'
n = str(len(y))
mu = '{:.1f}'.format(np.mean(y))
std = '{:.1f}'.format(np.std(y))
return fmt.format(label, n, mu, std)
def plot_settings():
"""Plot settings"""
sns.set_style("white")
sns.set_style('ticks')
sns.set_context("paper", font_scale=2.25)
sns.set_palette(sns.color_palette('bright'))
# matplotlib stuff
plt_params = {
'figure.figsize': (10, 8),
'lines.linewidth': 3,
'axes.linewidth': 2.5,
'savefig.dpi': 300,
'xtick.major.width': 2.5,
'ytick.major.width': 2.5,
'xtick.minor.width': 1,
'ytick.minor.width': 1,
'legend.framealpha': 1.0,
'figure.dpi': 86
}
mpl.rcParams.update(plt_params)
return
def isosmiles(smiles):
"""Convert smiles to isosmiles."""
mol = AllChem.MolFromSmiles(smiles)
new_smi = AllChem.MolToSmiles(mol, isomericSmiles=True, canonical=True)
return new_smi
def get_all_jsons(path, suffixes=None):
"""Retrieve jsons from a given path and suffixes."""
files = []
json_files = [f for f in os.listdir(path) if f.endswith('.json')]
if suffixes:
for filename in json_files:
suffix = filename.split('_')[-1].replace('.json', '')
if suffix in suffixes:
files.append(filename)
else:
files = json_files
return files
def read_json_data(files, path, data_fn, ):
"""Read jsons with a function and return list of information."""
data = []
for file in tqdm(files):
filename = os.path.join(path, file)
with open(filename) as afile:
sample_dict = json.load(afile)
sample_dict['filename'] = file
data.append(data_fn(sample_dict))
return data
def diverse_mols_indexes(mol_list, n_pick, radius=4, seed=42):
fps = [GetMorganFingerprint(mol, radius) for mol in mol_list]
picker = MaxMinPicker()
n_fps = len(fps)
def fp_distance(i, j): return 1 - \
DataStructs.DiceSimilarity(fps[i], fps[j])
indexes = picker.LazyPick(fp_distance, n_fps, n_pick, seed=seed)
return indexes