CIMA 1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- CIMA/__init__.py +17 -0
- CIMA/assessment/Images_Assessment.py +441 -0
- CIMA/assessment/__init__.py +14 -0
- CIMA/detection/__init__.py +14 -0
- CIMA/detection/beads_identification.py +108 -0
- CIMA/detection/clusters.py +992 -0
- CIMA/maps/DensityProprieties.py +257 -0
- CIMA/maps/EMMap.py +2871 -0
- CIMA/maps/MapFeatures.py +494 -0
- CIMA/maps/MapParser.py +203 -0
- CIMA/maps/MapSpatialFeatures.py +122 -0
- CIMA/maps/ScoringFunctions.py +2366 -0
- CIMA/maps/StructureBlurrer.py +1014 -0
- CIMA/maps/VQ.py +185 -0
- CIMA/maps/__init__.py +13 -0
- CIMA/maps/maptomap.py +129 -0
- CIMA/parsers/ParserCSV.py +386 -0
- CIMA/parsers/Parserh5py.py +156 -0
- CIMA/parsers/__init__.py +13 -0
- CIMA/patches/HomologSeparator.py +95 -0
- CIMA/patches/NuclearMask.py +62 -0
- CIMA/patches/SegmentReconstruction.py +443 -0
- CIMA/patches/__init__.py +13 -0
- CIMA/segments/SegmentConvex.py +117 -0
- CIMA/segments/SegmentFeatures.py +287 -0
- CIMA/segments/SegmentGaussian.py +218 -0
- CIMA/segments/SegmentInfo.py +299 -0
- CIMA/segments/SegmentInfoH5.py +182 -0
- CIMA/segments/SegmentInfoXYZ.py +358 -0
- CIMA/segments/SegmentSpatialFeatures.py +92 -0
- CIMA/segments/__init__.py +15 -0
- CIMA/utils/StatsFunction.py +185 -0
- CIMA/utils/Vector.py +457 -0
- CIMA/utils/Visualization.py +406 -0
- CIMA/utils/WalkFeatures.py +454 -0
- CIMA/utils/WritePDB.py +156 -0
- CIMA/utils/__init__.py +13 -0
- CIMA-1.0.dist-info/LICENCE +28 -0
- CIMA-1.0.dist-info/METADATA +11 -0
- CIMA-1.0.dist-info/RECORD +42 -0
- CIMA-1.0.dist-info/WHEEL +5 -0
- CIMA-1.0.dist-info/top_level.txt +1 -0
CIMA/__init__.py
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
#===============================================================================
|
|
2
|
+
# This file is part of CIMA.
|
|
3
|
+
#
|
|
4
|
+
# CIMA is a software designed to help the user in the manipulation
|
|
5
|
+
# and analyses of genomic super resolution localisation data.
|
|
6
|
+
#
|
|
7
|
+
# Copyright 2019-2023
|
|
8
|
+
#
|
|
9
|
+
# Authors: Irene Farabella
|
|
10
|
+
#
|
|
11
|
+
#
|
|
12
|
+
#
|
|
13
|
+
#===============================================================================
|
|
14
|
+
|
|
15
|
+
"""
|
|
16
|
+
.. include:: ../../README.md
|
|
17
|
+
"""
|
|
@@ -0,0 +1,441 @@
|
|
|
1
|
+
#===============================================================================
|
|
2
|
+
# This file is part of CIMA.
|
|
3
|
+
#
|
|
4
|
+
# CIMA is a software designed to help the user in the manipulation
|
|
5
|
+
# and analyses of genomic super resolution localisation data.
|
|
6
|
+
#
|
|
7
|
+
# Copyright 2019-2023
|
|
8
|
+
#
|
|
9
|
+
# Authors: Irene Farabella
|
|
10
|
+
#
|
|
11
|
+
#
|
|
12
|
+
#
|
|
13
|
+
#===============================================================================
|
|
14
|
+
|
|
15
|
+
__docformat__ = "markdown"
|
|
16
|
+
|
|
17
|
+
from math import pi
|
|
18
|
+
from random import randrange
|
|
19
|
+
import CIMA.utils.Vector as Vector
|
|
20
|
+
import numpy as np
|
|
21
|
+
from numpy import ndarray,array,append,matrix
|
|
22
|
+
import os
|
|
23
|
+
import sys
|
|
24
|
+
import pandas as pd
|
|
25
|
+
from CIMA.parsers import ParserCSV as Parser
|
|
26
|
+
from CIMA.segments import SegmentGaussian as SG
|
|
27
|
+
from CIMA.maps import DensityProprieties as DS
|
|
28
|
+
from CIMA.maps import MapFeatures as MF
|
|
29
|
+
from CIMA.utils import WalkFeatures as WF
|
|
30
|
+
from CIMA.segments import SegmentInfo as SI
|
|
31
|
+
|
|
32
|
+
from CIMA.maps.ScoringFunctions import ScoringFunctions
|
|
33
|
+
from CIMA.segments.SegmentInfo import Segment
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
scorer = ScoringFunctions()
|
|
37
|
+
|
|
38
|
+
TB=SG.TransformBlurrer()
|
|
39
|
+
|
|
40
|
+
#saturation Plot
|
|
41
|
+
def Saturation_tofinal(endstructure: Segment,prec_mean=0.,mode="unit",maxframes=250,interval=5,ccc_mode=1.,getcom=False, selection_mode='frames', random_groups_num=50):
|
|
42
|
+
"""
|
|
43
|
+
Calculates the saturation plot by evaluating the cross-correlation coefficient (CCC) between a reference structure and
|
|
44
|
+
cumulative subsets of its frames or random groups, allowing the assessment of structural saturation over time or random groups.
|
|
45
|
+
|
|
46
|
+
Args:
|
|
47
|
+
* endstructure (Segment): The reference structure used for CCC calculations.
|
|
48
|
+
* prec_mean (float, optional): Precision value used in Gaussian blurring. Defaults to 0.0.
|
|
49
|
+
* mode (str, optional): Mode for Gaussian blurring ('unit' or other available modes). Defaults to "unit".
|
|
50
|
+
* maxframes (int, optional): Maximum number of frames to process. Defaults to 250.
|
|
51
|
+
* interval (int, optional): Number of frames in each chunk for CCC calculation. Defaults to 5.
|
|
52
|
+
* ccc_mode (float, optional): Mode for calculating CCC between reference and partial structures. Defaults to 1.0.
|
|
53
|
+
* getcom (bool, optional): If True, includes the center of mass (COM) of partial structures in the output. Defaults to False.
|
|
54
|
+
* selection_mode (str, optional): Mode of selection for frames ('frames' or 'random'). Defaults to 'frames'.
|
|
55
|
+
* random_groups_num (int, optional): Number of random groups used if `selection_mode` is 'random'. Defaults to 50.
|
|
56
|
+
|
|
57
|
+
Returns:
|
|
58
|
+
* list: A list of scores, where each score is a list containing:
|
|
59
|
+
- Chunk ID (int)
|
|
60
|
+
- CCC value (float)
|
|
61
|
+
- Number of atoms in the partial structure (int)
|
|
62
|
+
- (Optional) COM of the partial structure (if `getcom` is True)
|
|
63
|
+
"""
|
|
64
|
+
if(selection_mode == 'frames'):
|
|
65
|
+
maxframes=int(maxframes)
|
|
66
|
+
interval=int(interval)
|
|
67
|
+
frames=list(range(0,int(maxframes)))
|
|
68
|
+
frames_chucks = [frames[x:x+interval] for x in range(0, len(frames),interval)]
|
|
69
|
+
#print (frames_chucks)
|
|
70
|
+
#tmp=[]
|
|
71
|
+
scores=[]
|
|
72
|
+
tmp0=[]
|
|
73
|
+
strcture_list_chucks=[endstructure.getFrames(fr) for C, fr in enumerate(frames_chucks) if endstructure.getFrames(fr)!= None ]
|
|
74
|
+
elif(selection_mode == 'random'):
|
|
75
|
+
labels = np.random.choice(np.arange(0,random_groups_num), len(endstructure), replace=True)
|
|
76
|
+
strcture_list_chucks=[endstructure.copyWithNewContent(subdf) for n, subdf in endstructure.atomList.groupby(labels) if len(subdf)>0 ]
|
|
77
|
+
else:
|
|
78
|
+
raise ValueError('Selection mode not implemented')
|
|
79
|
+
#print (strcture_list_chucks)
|
|
80
|
+
C = 0
|
|
81
|
+
ncc = []
|
|
82
|
+
tmp = []
|
|
83
|
+
scores = []
|
|
84
|
+
#for fr in frames_chucks[0:1]:
|
|
85
|
+
# tmp0_s = endstructure.getFrames(fr)
|
|
86
|
+
|
|
87
|
+
# Run per cicle and add them upon convergence
|
|
88
|
+
for i in range(0,len(strcture_list_chucks)):
|
|
89
|
+
cc = strcture_list_chucks[i]
|
|
90
|
+
if cc:
|
|
91
|
+
if (i==0):
|
|
92
|
+
acc = strcture_list_chucks[0]
|
|
93
|
+
else:
|
|
94
|
+
acc.atomList = pd.concat([acc.atomList,cc.atomList])
|
|
95
|
+
acc.copyWithNewContent(acc.atomList)
|
|
96
|
+
C+=1
|
|
97
|
+
tmp_map = TB.SR_gaussian_blur(acc, prec_mean, sigma_coeff=1.,mode=mode)
|
|
98
|
+
endstructuremap = TB.SR_gaussian_blur(endstructure, prec_mean, sigma_coeff=1.,mode=mode)
|
|
99
|
+
cth = 0.5
|
|
100
|
+
cth2 = 0.5
|
|
101
|
+
map_s1g,map_s2g = MF._match_grid(endstructuremap,tmp_map)
|
|
102
|
+
ccctmp,ovtmp = scorer.CCC_map(map_s1g,map_s2g,map_target_threshold=cth,map_probe_threshold=cth2,mode=ccc_mode)
|
|
103
|
+
# ccctmp = scorer.CCC_simple(map_s1g, map_s2g, map1_thr=cth,map2_thr=cth2,mode=ccc_mode)
|
|
104
|
+
if getcom==True:
|
|
105
|
+
COM2=tmp_map._get_com_threshold(cth2)
|
|
106
|
+
scores.append([C,ccctmp,len(cc.atomList),COM2])
|
|
107
|
+
else:
|
|
108
|
+
scores.append([C,ccctmp,len(cc.atomList)])
|
|
109
|
+
return scores
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def _Saturation_incremental(endstructure: Segment,prec_mean=0.,mode="unit",maxframes=250,interval=5,ccc_mode=1.):
|
|
114
|
+
"""
|
|
115
|
+
Arguments:
|
|
116
|
+
|
|
117
|
+
*endstructure*
|
|
118
|
+
an input structure that serves as the reference structure.
|
|
119
|
+
*prec_mean*
|
|
120
|
+
the precision value for the Gaussian blur function used in the function.
|
|
121
|
+
*maxframes*
|
|
122
|
+
the maximum number of frames to use for calculating the saturation.
|
|
123
|
+
*interval*
|
|
124
|
+
the number of locations to use in each chunk for calculating the saturation.
|
|
125
|
+
*ccc_mode*
|
|
126
|
+
the mode for calculating the cross-correlation coefficient (CCC) between the reference structure and the partial structures.
|
|
127
|
+
*getcom*
|
|
128
|
+
a Boolean parameter indicating whether to include the center of mass (COM) of the partial structures in the output.
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
Return:
|
|
132
|
+
|
|
133
|
+
An array of 3-list (Id, score, number of localisation)
|
|
134
|
+
The function then returns a list of scores, where each score corresponds to a chunk of frames and includes the CCC value and the number of atoms in the partial structure.
|
|
135
|
+
If getcom is True, the output also includes the COM of the partial structure.
|
|
136
|
+
"""
|
|
137
|
+
maxframes=int(maxframes)
|
|
138
|
+
interval=int(interval)
|
|
139
|
+
frames=list(range(0,int(maxframes)))
|
|
140
|
+
frames_chucks = [frames[x:x+interval] for x in range(0, len(frames),interval)]
|
|
141
|
+
tmp=[]
|
|
142
|
+
scores=[]
|
|
143
|
+
tmp_ref=[]
|
|
144
|
+
for fr in frames_chucks[0:1]:
|
|
145
|
+
tmp_ref = endstructure.getFrames(fr)
|
|
146
|
+
|
|
147
|
+
if tmp_ref is None:
|
|
148
|
+
tmp_ref=False
|
|
149
|
+
else:
|
|
150
|
+
tmp_ref=tmp_ref
|
|
151
|
+
|
|
152
|
+
for C, fr in enumerate(frames_chucks):
|
|
153
|
+
C+=1
|
|
154
|
+
tmp = endstructure.getFrames(fr)
|
|
155
|
+
if tmp is None:
|
|
156
|
+
scores.append([C,0,0])
|
|
157
|
+
else:
|
|
158
|
+
tmp_str=tmp
|
|
159
|
+
#print(tmp_ref)
|
|
160
|
+
tmp_map=TB.SR_gaussian_blur(tmp_str, prec_mean, sigma_coeff=1.,mode=mode)
|
|
161
|
+
if tmp_ref==False:
|
|
162
|
+
scores.append([C,0,0])
|
|
163
|
+
else:
|
|
164
|
+
endstructuremap=TB.SR_gaussian_blur(tmp_ref, prec_mean, sigma_coeff=1.,mode=mode)
|
|
165
|
+
#cth=DS.calculate_map_threshold_SR(endstructuremap)
|
|
166
|
+
#cth2=DS.calculate_map_threshold_SR(tmp_map)
|
|
167
|
+
cth=0.5
|
|
168
|
+
cth2=0.5
|
|
169
|
+
|
|
170
|
+
map_s1g,map_s2g=MF._match_grid(endstructuremap,tmp_map)
|
|
171
|
+
ccctmp,ovtmp=scorer.CCC_map(map_s1g,map_s2g,map_target_threshold=cth,map_probe_threshold=cth2,mode=ccc_mode)
|
|
172
|
+
scores.append([C,ccctmp,len(tmp_str.atomList)])
|
|
173
|
+
tmp_ref=tmp_str
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
return scores
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
def Assessment_Chrom_Precision(StructureObj,chrin,nuclei="",date="",hom="",pathin="",writeout=True):
|
|
181
|
+
|
|
182
|
+
"""
|
|
183
|
+
Assesses chromatin structure precision over time and outputs summary data.
|
|
184
|
+
|
|
185
|
+
This function calculates the mean positional precision of chromatin data across timepoints
|
|
186
|
+
in a given structure. It then compiles this information into a summary table and optionally
|
|
187
|
+
writes the data to a file.
|
|
188
|
+
|
|
189
|
+
Args:
|
|
190
|
+
* StructureObj (Segment): The structure object containing chromatin data.
|
|
191
|
+
* chrin (str): Identifier for the chromatin input structure.
|
|
192
|
+
* nuclei (str, optional): Label for the output file. Defaults to an empty string.
|
|
193
|
+
* date (str, optional): Identifier for the date of the experiment. Defaults to an empty string.
|
|
194
|
+
* hom (str, optional): Identifier for homology data in the experiment. Defaults to an empty string.
|
|
195
|
+
* pathin (str, optional): Directory path for saving the output file. Defaults to an empty string.
|
|
196
|
+
* writeout (bool, optional): If True, writes the assessment data to a file. Defaults to True.
|
|
197
|
+
|
|
198
|
+
Returns:
|
|
199
|
+
* list: A list containing assessment data. Each entry includes:
|
|
200
|
+
- Chromatin ID (`chrin`)
|
|
201
|
+
- Timepoint label
|
|
202
|
+
- Nuclei label
|
|
203
|
+
- Date of experiment
|
|
204
|
+
- Homology identifier
|
|
205
|
+
- Mean precision values (x, y, z)
|
|
206
|
+
- Number of localizations at the timepoint
|
|
207
|
+
- Total number of timepoints
|
|
208
|
+
"""
|
|
209
|
+
|
|
210
|
+
table_out=""
|
|
211
|
+
listout=[]
|
|
212
|
+
timechrin=StructureObj.split_into_time()
|
|
213
|
+
prec_mean_ch=StructureObj.GetMeanPrecision()
|
|
214
|
+
nlocchrin=len(StructureObj.atomList)
|
|
215
|
+
timepoint_list=[]
|
|
216
|
+
for t1 in range(len(timechrin)):
|
|
217
|
+
lab1=timechrin[t1].atomList.iloc[0].timepoint
|
|
218
|
+
prec_mean_time=timechrin[t1].GetMeanPrecision()
|
|
219
|
+
nloctime=len(timechrin[t1].atomList)
|
|
220
|
+
listouttmp=[chrin,lab1,nuclei,date,hom,prec_mean_time[0],prec_mean_time[1],prec_mean_time[2],nloctime,0]
|
|
221
|
+
listout.append(listouttmp)
|
|
222
|
+
#timepoint_list.append(lab1)
|
|
223
|
+
table_out+="%s,%s,%s,%s,%s,%s,%s,%s\n"%(date,hom,lab1,prec_mean_time[0],prec_mean_time[1],prec_mean_time[2],nloctime,0)
|
|
224
|
+
|
|
225
|
+
table_out+="%s,%s,%s,%s,%s,%s,%s,%s\n"%(date,hom,0,prec_mean_ch[0],prec_mean_ch[1],prec_mean_ch[2],nlocchrin,len(timechrin))
|
|
226
|
+
|
|
227
|
+
listout.append([chrin,0,nuclei,date,hom,prec_mean_ch[0],prec_mean_ch[1],prec_mean_ch[2],nlocchrin,len(timechrin)])
|
|
228
|
+
|
|
229
|
+
if writeout==True:
|
|
230
|
+
import os
|
|
231
|
+
FOUT=pathin+"Assessment/"+"multi_assessment_precision_chr%s_%s_%s_%s.txt"%(chrin,date,nuclei,hom)
|
|
232
|
+
if not os.path.isdir(pathin+"Assessment/"):
|
|
233
|
+
os.mkdir(pathin+"Assessment/")
|
|
234
|
+
fout=open(FOUT,"w")
|
|
235
|
+
fout.write("Date,hom,Segment,pmeanx,pmeany,pmeanz,nloc,ntime\n")
|
|
236
|
+
fout.write(table_out)
|
|
237
|
+
return listout
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
# def _Saturation_tofinal_cycle_incremental_marcius(endstructure,prec_mean=0.,mode="unit",ccc_mode=1.,merge=1):#,getcom=False):
|
|
242
|
+
# """
|
|
243
|
+
# Arguments:
|
|
244
|
+
|
|
245
|
+
# *endstructure*
|
|
246
|
+
# an input structure that serves as the reference structure.
|
|
247
|
+
# *prec_mean*
|
|
248
|
+
# the precision value for the Gaussian blur function used in the function.
|
|
249
|
+
# *mode*
|
|
250
|
+
# a string indicating the mode for the Gaussian blur function used in the function.
|
|
251
|
+
# *ccc_mode*
|
|
252
|
+
# the mode for calculating the cross-correlation coefficient (CCC) between the reference structure and the partial structures.
|
|
253
|
+
# *getcom*
|
|
254
|
+
# a Boolean parameter indicating whether to include the center of mass (COM) of the partial structures in the output.
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
# Return:
|
|
258
|
+
# An array of 3-list (Id, score, number of localisation)
|
|
259
|
+
# The list includes the cycle number, CCC score, number of atoms in the cycle, and optionally, the center of mass for the cycle.
|
|
260
|
+
# """
|
|
261
|
+
# cicli = endstructure.split_into_cycle()
|
|
262
|
+
# C = 0
|
|
263
|
+
# ncc = []
|
|
264
|
+
# tmp = []
|
|
265
|
+
# scores = []
|
|
266
|
+
# # Run per cicle and add them upon convergence
|
|
267
|
+
# for i in range(0,len(cicli)):
|
|
268
|
+
# cc = cicli[i]
|
|
269
|
+
# if (i==0):
|
|
270
|
+
# acc = cicli[0]
|
|
271
|
+
# else:
|
|
272
|
+
# acc.atomList = pd.concat([acc.atomList,cc.atomList])
|
|
273
|
+
# acc.copyWithNewContent(acc.atomList)
|
|
274
|
+
# C+=1
|
|
275
|
+
# tmp_map = TB.SR_gaussian_blur(acc, prec_mean, sigma_coeff=1.,mode=mode)
|
|
276
|
+
# endstructuremap = TB.SR_gaussian_blur(endstructure, prec_mean, sigma_coeff=1.,mode=mode)
|
|
277
|
+
# #print (acc)
|
|
278
|
+
# #print (tmp_map)
|
|
279
|
+
# #print (endstructuremap)
|
|
280
|
+
# cth = 0.5
|
|
281
|
+
# cth2 = 0.5
|
|
282
|
+
# map_s1g,map_s2g = MF._match_grid(endstructuremap,tmp_map)
|
|
283
|
+
# ccctmp,ovtmp = scorer.CCC_map(map_s1g,map_s2g,map_target_threshold=cth,map_probe_threshold=cth2,mode=ccc_mode)
|
|
284
|
+
# scores.append([C,ccctmp,len(cc.atomList)])
|
|
285
|
+
# return scores
|
|
286
|
+
|
|
287
|
+
def Cycle_assessment(endstructure,prec_mean=0.,mode="unit",ccc_mode=1.,merge=1):#,getcom=False):
|
|
288
|
+
"""
|
|
289
|
+
Performs cycle-based assessment of a structure using cross-correlation coefficient (CCC) calculations.
|
|
290
|
+
|
|
291
|
+
This function evaluates the similarity between each cycle in the input structure (`endstructure`)
|
|
292
|
+
and the overall structure by calculating the CCC. Each cycle is processed individually to compute
|
|
293
|
+
the CCC score relative to the full structure, allowing for an assessment of structural changes over cycles.
|
|
294
|
+
|
|
295
|
+
Args:
|
|
296
|
+
* endstructure (Segment): The reference structure used for CCC calculations.
|
|
297
|
+
* prec_mean (float, optional): Precision value used in Gaussian blurring. Defaults to 0.0.
|
|
298
|
+
* mode (str, optional): Mode for Gaussian blurring ('unit' or other available modes). Defaults to "unit".
|
|
299
|
+
* ccc_mode (float, optional): Mode for calculating CCC between reference and cycle structures. Defaults to 1.0.
|
|
300
|
+
* merge (int, optional): Reserved for future extensions to merge cycles, currently unused.
|
|
301
|
+
|
|
302
|
+
Returns:
|
|
303
|
+
* list: A list of lists, where each inner list contains:
|
|
304
|
+
- Cycle number (int)
|
|
305
|
+
- CCC score (float)
|
|
306
|
+
- Number of atoms in the cycle (int)
|
|
307
|
+
"""
|
|
308
|
+
|
|
309
|
+
|
|
310
|
+
C=0
|
|
311
|
+
tmp=[]
|
|
312
|
+
scores=[]
|
|
313
|
+
cycles=endstructure.split_into_cycle()
|
|
314
|
+
#print (len(cicli))
|
|
315
|
+
for cc in cycles:
|
|
316
|
+
C+=1
|
|
317
|
+
tmp_map=TB.SR_gaussian_blur(cc, prec_mean, sigma_coeff=1.,mode=mode)
|
|
318
|
+
endstructuremap=TB.SR_gaussian_blur(endstructure, prec_mean, sigma_coeff=1.,mode=mode)
|
|
319
|
+
#cth=DS.calculate_map_threshold_SR(endstructuremap)
|
|
320
|
+
cth=0.5
|
|
321
|
+
cth2=0.5
|
|
322
|
+
#cth2=DS.calculate_map_threshold_SR(tmp_map)
|
|
323
|
+
map_s1g,map_s2g=MF._match_grid(endstructuremap,tmp_map)
|
|
324
|
+
ccctmp,ovtmp=scorer.CCC_map(map_s1g,map_s2g,map_target_threshold=cth,map_probe_threshold=cth2,mode=ccc_mode)
|
|
325
|
+
scores.append([C,ccctmp,len(cc.atomList)])
|
|
326
|
+
return scores
|
|
327
|
+
|
|
328
|
+
def _Halfdataset_ccc(endstructure,prec_mean=45.,mode="unit",ccc_mode=1.):
|
|
329
|
+
"""
|
|
330
|
+
Arguments:
|
|
331
|
+
|
|
332
|
+
*endstructure*
|
|
333
|
+
an input structure that serves as the reference structure.
|
|
334
|
+
*prec_mean*
|
|
335
|
+
the precision value for the Gaussian blur function used in the function.
|
|
336
|
+
*maxframes*
|
|
337
|
+
the maximum number of frames to use for calculating the saturation.
|
|
338
|
+
*interval*
|
|
339
|
+
the number of locations to use in each chunk for calculating the saturation.
|
|
340
|
+
*ccc_mode*
|
|
341
|
+
the mode for calculating the cross-correlation coefficient (CCC) between the reference structure and the partial structures.
|
|
342
|
+
*getcom*
|
|
343
|
+
a Boolean parameter indicating whether to include the center of mass (COM) of the partial structures in the output.
|
|
344
|
+
|
|
345
|
+
Return:
|
|
346
|
+
|
|
347
|
+
list of scores, where each score corresponds to a chunk of frames and includes the CCC value and the number of atoms in the partial structure.
|
|
348
|
+
If getcom is True, the output also includes the COM of the partial structure.
|
|
349
|
+
|
|
350
|
+
Id, score, number of localisation
|
|
351
|
+
"""
|
|
352
|
+
df=endstructure.atomList
|
|
353
|
+
shuffled_df = df.sample(frac=1)
|
|
354
|
+
splitted_df = np.array_split(shuffled_df, 2)
|
|
355
|
+
#print (len(splitted_df))
|
|
356
|
+
str1=endstructure.copy()
|
|
357
|
+
str2=endstructure.copy()
|
|
358
|
+
#print (str1,str2)
|
|
359
|
+
str1=str1.copyWithNewContent(splitted_df[0])
|
|
360
|
+
str2=str2.copyWithNewContent(splitted_df[1])
|
|
361
|
+
#print (str1,str2)
|
|
362
|
+
tmp_map1=TB.SR_gaussian_blur(str1, prec_mean, sigma_coeff=1.,mode=mode)
|
|
363
|
+
tmp_map2=TB.SR_gaussian_blur(str2, prec_mean, sigma_coeff=1.,mode=mode)
|
|
364
|
+
tmp_map1,tmp_map2=MF._match_grid(tmp_map1,tmp_map2)
|
|
365
|
+
ccctmp,ovtmp=scorer.CCC_map(tmp_map1,tmp_map2,map_target_threshold=0.5,map_probe_threshold=0.5,mode=ccc_mode)
|
|
366
|
+
return [ccctmp,str1.GetMeanPrecision()[2],str2.GetMeanPrecision()[2]]
|
|
367
|
+
|
|
368
|
+
|
|
369
|
+
def Saturation_experimentpseudotime(endstructure: Segment,numberoflocations=100,prec_mean=0.,mode="unit",ccc_mode=1.,getcom=False):
|
|
370
|
+
"""
|
|
371
|
+
Calculates saturation over experimental pseudotime by evaluating the cross-correlation coefficient (CCC)
|
|
372
|
+
between the reference structure and cumulative subsets of frames, allowing for an assessment of structural
|
|
373
|
+
stability over pseudotime.
|
|
374
|
+
|
|
375
|
+
Args:
|
|
376
|
+
* endstructure (Segment): The reference structure used for CCC calculations.
|
|
377
|
+
* numberoflocations (int, optional): The number of locations to use in each chunk for CCC calculation. Defaults to 100.
|
|
378
|
+
* prec_mean (float, optional): Precision value used in Gaussian blurring. Defaults to 0.0.
|
|
379
|
+
* mode (str, optional): Mode for Gaussian blurring ('unit' or other available modes). Defaults to "unit".
|
|
380
|
+
* ccc_mode (float, optional): Mode for calculating CCC between reference and partial structures. Defaults to 1.0.
|
|
381
|
+
* getcom (bool, optional): If True, includes the center of mass (COM) of partial structures in the output. Defaults to False.
|
|
382
|
+
|
|
383
|
+
Returns:
|
|
384
|
+
* list: A list of lists, where each inner list contains:
|
|
385
|
+
- Pseudotime step (int)
|
|
386
|
+
- CCC score (float)
|
|
387
|
+
- Number of localizations in the chunk (int)
|
|
388
|
+
- (Optional) COM of the partial structure (if `getcom` is True)
|
|
389
|
+
"""
|
|
390
|
+
|
|
391
|
+
StrObjOrdered=endstructure.OrderbyExperimenttime()
|
|
392
|
+
#print (StrObjOrdered)
|
|
393
|
+
maxframes=len(StrObjOrdered.atomList)
|
|
394
|
+
maxframes=int(maxframes)
|
|
395
|
+
interval=int(numberoflocations)
|
|
396
|
+
frames=list(range(0,int(maxframes)))
|
|
397
|
+
frames_chucks = [frames[x:x+interval] for x in range(0, len(frames),interval)]
|
|
398
|
+
#print (frames_chucks)
|
|
399
|
+
C=0
|
|
400
|
+
tmp=[]
|
|
401
|
+
scores=[]
|
|
402
|
+
tmp_ref=[]
|
|
403
|
+
for fr in frames_chucks[0:1]:
|
|
404
|
+
countx=0
|
|
405
|
+
tmp_ref = endstructure.getInRange([f-1 for f in fr])
|
|
406
|
+
|
|
407
|
+
if tmp_ref is None:
|
|
408
|
+
tmp_ref=False
|
|
409
|
+
|
|
410
|
+
for C, fr in enumerate(frames_chucks):
|
|
411
|
+
C+=1
|
|
412
|
+
local_fr_parcial=[]
|
|
413
|
+
countx=0
|
|
414
|
+
tmp = endstructure.getInRange([f-1 for f in fr])
|
|
415
|
+
if tmp is None:
|
|
416
|
+
scores.append([C,0,0])
|
|
417
|
+
else:
|
|
418
|
+
tmp_str=tmp
|
|
419
|
+
print(tmp_ref,tmp_str)
|
|
420
|
+
tmp_map=TB.SR_gaussian_blur(tmp_str, prec_mean, sigma_coeff=1.,mode=mode)
|
|
421
|
+
if tmp_ref==False:
|
|
422
|
+
scores.append([C,0,0])
|
|
423
|
+
else:
|
|
424
|
+
endstructuremap=TB.SR_gaussian_blur(tmp_ref, prec_mean, sigma_coeff=1.,mode=mode)
|
|
425
|
+
#cth=DS.calculate_map_threshold_SR(endstructuremap)
|
|
426
|
+
#cth2=DS.calculate_map_threshold_SR(tmp_map)
|
|
427
|
+
cth=0.5
|
|
428
|
+
cth2=0.5
|
|
429
|
+
|
|
430
|
+
map_s1g,map_s2g=MF._match_grid(endstructuremap,tmp_map)
|
|
431
|
+
ccctmp,ovtmp=scorer.CCC_map(map_s1g,map_s2g,map_target_threshold=cth,map_probe_threshold=cth2,mode=ccc_mode)
|
|
432
|
+
scores.append([C,ccctmp,len(tmp_str.atomList)])
|
|
433
|
+
tmp_ref=tmp_str
|
|
434
|
+
|
|
435
|
+
|
|
436
|
+
return scores
|
|
437
|
+
|
|
438
|
+
|
|
439
|
+
def _split(a, n):
|
|
440
|
+
k, m = divmod(len(a), n)
|
|
441
|
+
return list((a[i*k+min(i, m):(i+1)*k+min(i+1, m)] for i in range(n)))
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
|
|
2
|
+
#===============================================================================
|
|
3
|
+
# This file is part of CIMA.
|
|
4
|
+
#
|
|
5
|
+
# CIMA is a software designed to help the user in the manipulation
|
|
6
|
+
# and analyses of genomic super resolution localisation data.
|
|
7
|
+
#
|
|
8
|
+
# Copyright 2019-2023
|
|
9
|
+
#
|
|
10
|
+
# Authors: Irene Farabella
|
|
11
|
+
#
|
|
12
|
+
#
|
|
13
|
+
#
|
|
14
|
+
#===============================================================================
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
|
|
2
|
+
#===============================================================================
|
|
3
|
+
# This file is part of CIMA.
|
|
4
|
+
#
|
|
5
|
+
# CIMA is a software designed to help the user in the manipulation
|
|
6
|
+
# and analyses of genomic super resolution localisation data.
|
|
7
|
+
#
|
|
8
|
+
# Copyright 2019-2023
|
|
9
|
+
#
|
|
10
|
+
# Authors: Irene Farabella
|
|
11
|
+
#
|
|
12
|
+
#
|
|
13
|
+
#
|
|
14
|
+
#===============================================================================
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
#===============================================================================
|
|
2
|
+
# This file is part of CIMA.
|
|
3
|
+
#
|
|
4
|
+
# CIMA is a software designed to help the user in the manipulation
|
|
5
|
+
# and analyses of genomic super resolution localisation data.
|
|
6
|
+
#
|
|
7
|
+
# Copyright 2019-2023
|
|
8
|
+
#
|
|
9
|
+
# Authors: Irene Farabella
|
|
10
|
+
#
|
|
11
|
+
#
|
|
12
|
+
#
|
|
13
|
+
#===============================================================================
|
|
14
|
+
|
|
15
|
+
__docformat__ = "markdown"
|
|
16
|
+
|
|
17
|
+
import numpy as np
|
|
18
|
+
import pandas as pd
|
|
19
|
+
import matplotlib.pyplot as plt
|
|
20
|
+
import seaborn as sns
|
|
21
|
+
from sklearn.neighbors import NearestNeighbors
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class BeadsFinder():
|
|
25
|
+
def __init__(self):
|
|
26
|
+
pass
|
|
27
|
+
|
|
28
|
+
def fit(self, seg, n_jobs=1, radius=40, min_persistence=0.75):
|
|
29
|
+
'''
|
|
30
|
+
Arguments:
|
|
31
|
+
* seg: Segment object with at least x,y,z,cycle,zstep,frame columns
|
|
32
|
+
* n_jobs: number of cpus used to compute distances
|
|
33
|
+
* radius: radius inside of which to search for neighbors
|
|
34
|
+
* min_persistence: ratio of frames inside of which there should be
|
|
35
|
+
a neighbor so that the localization be classified as bead-originated
|
|
36
|
+
|
|
37
|
+
Attributes:
|
|
38
|
+
* debeaded_segment: the segment cleaned of beads is saved here
|
|
39
|
+
* labels_: 0/1 numpy array of same length as seg, where 0 indicates localizations coming from beads
|
|
40
|
+
* graphs_df: contains the intermidiate data computed during fitting
|
|
41
|
+
|
|
42
|
+
This class marks as beads those localizations which have a localization
|
|
43
|
+
distant from them less than the specified radius in at least the specified
|
|
44
|
+
percentage of frames of the current zstep and cycle.
|
|
45
|
+
This approach works only if the localization have already been drift corrected.
|
|
46
|
+
'''
|
|
47
|
+
|
|
48
|
+
df = seg.atomList
|
|
49
|
+
bead_labels = []
|
|
50
|
+
|
|
51
|
+
self.graphs_df = pd.DataFrame(columns=['cycle', 'zstep', 'graph', 'shrinked_graph', 'coords', 'persistence'])
|
|
52
|
+
for c in df['cycle'].unique():
|
|
53
|
+
for z in df['zstep'].unique():
|
|
54
|
+
subdf = df.loc[(df['cycle']==c) & (df['zstep']==z)]
|
|
55
|
+
if(len(subdf)<2):
|
|
56
|
+
continue
|
|
57
|
+
subdf = subdf.sort_values('frame', kind='mergesort')
|
|
58
|
+
where_splits = np.where(np.diff(subdf.frame.values)>0)[0]+1
|
|
59
|
+
coords = subdf[['x','y','z']].values
|
|
60
|
+
graph = NearestNeighbors(radius=radius, n_jobs=n_jobs).fit(coords).radius_neighbors_graph(mode='connectivity').toarray()
|
|
61
|
+
subgs = np.split(graph, where_splits, axis=1)
|
|
62
|
+
shrinked_graph = np.column_stack([s.sum(axis=1)>0 for s in subgs]).astype('int')
|
|
63
|
+
persistence = (shrinked_graph>0).sum(axis=1).flatten()/subdf['frame'].max()
|
|
64
|
+
local_where_bead = persistence>min_persistence
|
|
65
|
+
bead_labels.extend(subdf.index.values[local_where_bead])
|
|
66
|
+
# bead_labels.extend(subdf.index.values[local_where_bead_extended])
|
|
67
|
+
|
|
68
|
+
self.graphs_df.loc[len(self.graphs_df)] = {'cycle': c,
|
|
69
|
+
'zstep': z,
|
|
70
|
+
'graph': graph,
|
|
71
|
+
'shrinked_graph': shrinked_graph,
|
|
72
|
+
'coords': coords,
|
|
73
|
+
'persistence': persistence
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
self.persistence = np.concatenate(self.graphs_df['persistence'].values)
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
labs = np.ones(len(df), dtype='int')
|
|
80
|
+
labs[bead_labels]=0
|
|
81
|
+
|
|
82
|
+
self.labels_ = labs
|
|
83
|
+
|
|
84
|
+
self.debeaded_segment = seg.copyWithNewContent(seg.atomList[self.labels_==1])
|
|
85
|
+
|
|
86
|
+
return self
|
|
87
|
+
|
|
88
|
+
def show(self):
|
|
89
|
+
'''
|
|
90
|
+
Displays a representation of the connection matrices,
|
|
91
|
+
first by considering all localizations and then considering one localization per frame.
|
|
92
|
+
Then it also displays the persistence of each localization.
|
|
93
|
+
'''
|
|
94
|
+
for i,row in self.graphs_df.iterrows():
|
|
95
|
+
plt.imshow(row['graph'], cmap='gray')
|
|
96
|
+
plt.title('Connections between localizations')
|
|
97
|
+
plt.ylabel('All localizations')
|
|
98
|
+
plt.xlabel('All localizations')
|
|
99
|
+
plt.show()
|
|
100
|
+
plt.imshow(row['shrinked_graph'], cmap='gray')
|
|
101
|
+
plt.title('Connections between localizations considering one localization per frame')
|
|
102
|
+
plt.ylabel('All localizations')
|
|
103
|
+
plt.xlabel('Frames')
|
|
104
|
+
plt.show()
|
|
105
|
+
sns.scatterplot(x=row['coords'][:,0], y=row['persistence'], alpha=0.1)
|
|
106
|
+
plt.axhline(0.75, ls='--')
|
|
107
|
+
plt.title('cycle ' + str(row['cycle']) + ' zstep '+ str(row['zstep']))
|
|
108
|
+
plt.show()
|