-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.py
More file actions
324 lines (229 loc) · 9.75 KB
/
tools.py
File metadata and controls
324 lines (229 loc) · 9.75 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Dec 8 11:47:08 2021
@author: Luciano A. Masullo
"""
import numpy as np
import pandas as pd
import h5py
import os
def angle(p1x, p1y, p2x, p2y):
angle = np.zeros(len(p1x))
for i in range(len(p1x)):
p1 = np.array([p1x[i], p1y[i]])
p2 = np.array([p2x[i], p2y[i]])
"""
if p1y[i] > p2y[i]:
p3x = p1x[i] + 1
p3y = p1y[i]
p3 = np.array([p3x, p3y])
v0 = p3 - p1
v1 = p2 - p1
else:
p3x = p2x[i] + 1
p3y = p2y[i]
p3 = np.array([p3x, p3y])
v0 = p3 - p2
v1 = p2 - p2
"""
p3x = p1x[i] + 1
p3y = p1y[i]
p3 = np.array([p3x, p3y])
v0 = p3 - p1
v1 = p2 - p1
angle[i] = np.degrees(np.math.atan2(np.linalg.det([v0,v1]),np.dot(v0,v1)))
return angle
def picasso_hdf5(df, hdf5_fname, hdf5_oldname, path):
"""
This function recieves a pandas data frame coming from a Picasso .hdf5
file but that has been modified somehow (e.g. coordinates rotated,
groups edited, etc) and formats it to be compatible with Picasso.
It also creates the necessary .yaml file.
It is meant to be used in a folder that contains the original Picasso
.hdf5 file and the .yaml file.
- df: pandas data frame object
- hdf5_fname: the desired filename for the Picasso-compatible .hdf5 file
- hdf5_oldname: name of the original Picasso file that was modified
- path: the absolute path containing the path to the file's folder
Note: include ".hdf5" in the file names
Warning: the .yaml file is basically a copy of the old one. If important
information should be added to the new .yaml file this function should be
modified
"""
labels = list(df.keys())
df_picasso = df.reindex(columns=labels, fill_value=1)
locs = df_picasso.to_records(index = False)
# Saving data
hf = h5py.File(path + hdf5_fname, 'w')
hf.create_dataset('locs', data=locs)
hf.close()
# YAML saver
yaml_oldname = path + hdf5_oldname.replace('.hdf5', '.yaml')
yaml_newname = path + hdf5_fname.replace('.hdf5', '.yaml')
yaml_file_info = open(yaml_oldname, 'r')
yaml_file_data = yaml_file_info.read()
yaml_newfile = open(yaml_newname, 'w')
yaml_newfile.write(yaml_file_data)
yaml_newfile.close()
print('New Picasso-compatible .hdf5 file and .yaml file successfully created.')
def get_resi_locs(files, K):
"""
input:
files: must be a list of strings containing the file names by channel
K: number of localizations to be averaged to obtain each resi
localization
output:
data: a data frame with the localizations with one extra property,
the 'subset'
resi_locs: a data frame with the resi localizations obtained by
averaging in the subsets
"""
data = {}
resi_locs = {}
for i, file in enumerate(files):
data[str(i)] = pd.read_hdf(file, key='locs') # read each channel (file)
data[str(i)].rename(columns={'group': 'cluster_id'}, inplace=True)
for key in data.keys():
# initalize a list with the 'subset' property
subsetslist = [-1]*data[key].shape[0] # -1 is the label for localizations not assigned to any subset
data[key]['subset'] = subsetslist
cluster_id_set = list(set(list(data[key]['cluster_id'])))
for _ , cluster_id in enumerate(cluster_id_set):
print(cluster_id)
cluster = data[key].loc[data[key]['cluster_id'] == cluster_id] # get the table of cluster i
indexes = cluster.index # get the (general) indexes of the localizations in this cluster
nlocs = cluster.shape[0] # get the number of localizations in cluster i
nsubsets = int(nlocs/K) # get number of subsets, given K
for j in range(nsubsets):
# random choice of size K
subsets_id = np.random.choice(indexes, size=K, replace=False)
indexes = [i for i in indexes if i not in subsets_id] # remove already chosen indexes
data[key].loc[subsets_id, 'subset'] = j # assign a subset label
grouped_locs = data[key].groupby(['cluster_id', 'subset']) # group localizations by cluster_id and subset
resi_locs[key] = grouped_locs.mean().reset_index() # calculate mean by cluster_id and subset and obtain resi data
return resi_locs, data
def simulate_data(fname, sites, locs_per_site, σ_dnapaint, plot=False):
"""
input:
sites: array with the coordinates of the docking sites (in nm)
locs_per_site: number of localizations per site
σ_dnapaint: DNA-PAINT precision in nm
output:
it writes a file with the simulated data
"""
cov = [[σ_dnapaint**2, 0], [0, σ_dnapaint**2]] # create covariance matrix
locs = locs_per_site # number of localizations per docking site
# generate simulated origami data
data = np.zeros((sites.shape[0], locs, 2))
xlist = []
ylist = []
clusterlist = []
for i, site in enumerate(sites):
data[i, :, :] = np.random.multivariate_normal(site, cov, locs)
xlist += list(data[i, :, 0])
ylist += list(data[i, :, 1])
clusterlist += list(np.array((np.ones(data.shape[1])*i + 1),
dtype=int))
d = {'x': xlist, 'y': ylist, 'cluster_id': clusterlist}
df = pd.DataFrame(d)
df.to_hdf(fname, key='locs', mode='w')
if plot: # plot whole origami (histogram)
import matplotlib.pyplot as plt
fig1, ax1 = plt.subplots()
ax1.set_xlabel('x (nm)')
ax1.set_xlim([-50, 50])
ax1.set_ylim([-50, 50])
ax1.set_ylabel('y (nm)')
bins = np.arange(-50, 50, 0.2)
hist, xbins, ybins = np.histogram2d(data[:, :, 0].flatten(),
data[:, :, 1].flatten(),
bins=bins)
extent = [-50, 50, -50, 50]
ax1.imshow(hist.T, interpolation='none', origin='lower',
cmap='hot',
extent=extent)
ax1.set_aspect('equal')
return "Simulated data successfully generated"
def rigid_transform_3D(A, B):
"""
source: https://github.com/nghiaho12/rigid_transform_3D/blob/master/rigid_transform_3D.py
Given two sets of 3D points and their correspondence the algorithm will return a least square optimal
rigid transform (also known as Euclidean) between the two sets. The transform solves for 3D rotation and 3D translation, no scaling.
rigid transformation in 3D: R A + t = B
Input: expects 3xN matrix of points
Returns R,t
R = 3x3 rotation matrix
t = 3x1 column vector
"""
assert A.shape == B.shape
num_rows, num_cols = A.shape
if num_rows != 3:
raise Exception(f"matrix A is not 3xN, it is {num_rows}x{num_cols}")
num_rows, num_cols = B.shape
if num_rows != 3:
raise Exception(f"matrix B is not 3xN, it is {num_rows}x{num_cols}")
# find mean row wise
centroid_A = np.mean(A, axis=1)
centroid_B = np.mean(B, axis=1)
# ensure centroids are 3x1
centroid_A = centroid_A.reshape(-1, 1)
centroid_B = centroid_B.reshape(-1, 1)
# subtract mean
Am = A - centroid_A
Bm = B - centroid_B
H = Am @ np.transpose(Bm)
# sanity check
#if linalg.matrix_rank(H) < 3:
# raise ValueError("rank of H = {}, expecting 3".format(linalg.matrix_rank(H)))
# find rotation
U, S, Vt = np.linalg.svd(H)
R = Vt.T @ U.T
# special reflection case
if np.linalg.det(R) < 0:
print("det(R) < R, reflection detected!, correcting for it ...")
Vt[2,:] *= -1
R = Vt.T @ U.T
t = -R @ centroid_A + centroid_B
return R, t
def rigid_transform_2D(A, B):
"""
source: https://github.com/nghiaho12/rigid_transform_3D/blob/master/rigid_transform_3D.py
Given two sets of 2D points and their correspondence the algorithm will return a least square optimal
rigid transform (also known as Euclidean) between the two sets. The transform solves for 2D rotation and 2D translation, no scaling.
rigid transformation in 3D: R A + t = B
Input: expects 3xN matrix of points
Returns R,t
R = 3x3 rotation matrix
t = 3x1 column vector
"""
assert A.shape == B.shape
num_rows, num_cols = A.shape
if num_rows != 2:
raise Exception(f"matrix A is not 2xN, it is {num_rows}x{num_cols}")
num_rows, num_cols = B.shape
if num_rows != 2:
raise Exception(f"matrix B is not 2xN, it is {num_rows}x{num_cols}")
# find mean row wise
centroid_A = np.mean(A, axis=1)
centroid_B = np.mean(B, axis=1)
# ensure centroids are 3x1
centroid_A = centroid_A.reshape(-1, 1)
centroid_B = centroid_B.reshape(-1, 1)
# subtract mean
Am = A - centroid_A
Bm = B - centroid_B
H = Am @ np.transpose(Bm)
# sanity check
#if linalg.matrix_rank(H) < 3:
# raise ValueError("rank of H = {}, expecting 3".format(linalg.matrix_rank(H)))
# find rotation
U, S, Vt = np.linalg.svd(H)
R = Vt.T @ U.T
# special reflection case
if np.linalg.det(R) < 0:
print("det(R) < R, reflection detected!, correcting for it ...")
Vt[2,:] *= -1
R = Vt.T @ U.T
t = -R @ centroid_A + centroid_B
return R, t