1
1
# coding: utf-8
2
2
# !/usr/bin/env python3
3
3
4
- from cellh5 import cellh5 , cellh5write
4
+ import cellh5 , cellh5write
5
5
import multiprocessing as mp
6
6
import numpy as np
7
7
import pandas as pd
@@ -219,18 +219,21 @@ def getkts2polesStats(spotsInFrame):
219
219
# return (xs, ys)
220
220
221
221
def getSpotGeos (poleSpots , ktSpots , radius = 0.25 ):
222
- spots = poleSpots [[pos_x , pos_y ]].join (ktSpots [[pos_x , pos_y ]], how = "outer" , lsuffix = poleSuffix , rsuffix = ktSuffix )
223
- geos = pd .DataFrame (columns = ["phase" , "poleNb" , "ktNb" , "poleCenter_X" ,"poleCenter_Y" ,\
224
- "ktCenter_X" ,"ktCenter_Y" , "poleCenter2KtCenter_X" , "poleCenter2KtCenter_Y" , \
225
- "ktXpos_std" , "ktYpos_std" , "kts2ktCenter_mean" ,"kts2ktCenter_std" , "kts2ktCenter_max" ,"kts2ktCenter_min" , \
226
- "convexHull_area" ,
227
- "kts2poleCenter_mean" ,"kts2poleCenter_std" , "kts2poleCenter_max" ,"kts2poleCenter_min" , \
228
- "kts2poles_mean" , "kts2poles_std" , "kts2poles_max" , "kts2poles_min" , \
229
- "kt2Sp_mean" , "kt2Sp_std" , "kt2Sp_max" , "kt2Sp_min" , \
230
- "proj2SpCenter_mean" , "proj2SpCenter_std" ,"proj2SpCenter_max" ,"proj2SpCenter_min" ])
231
- for f in spots .index .levels [0 ]:
232
- geos .loc [f ] = computeGeometries (spots .loc [f ])
233
- return geos
222
+ if poleSpots is not None and ktSpots is not None :
223
+ spots = poleSpots [[pos_x , pos_y ]].join (ktSpots [[pos_x , pos_y ]], how = "outer" , lsuffix = poleSuffix , rsuffix = ktSuffix )
224
+ geos = pd .DataFrame (columns = ["phase" , "poleNb" , "ktNb" , "poleCenter_X" ,"poleCenter_Y" ,\
225
+ "ktCenter_X" ,"ktCenter_Y" , "poleCenter2KtCenter_X" , "poleCenter2KtCenter_Y" , \
226
+ "ktXpos_std" , "ktYpos_std" , "kts2ktCenter_mean" ,"kts2ktCenter_std" , "kts2ktCenter_max" ,"kts2ktCenter_min" , \
227
+ "convexHull_area" ,
228
+ "kts2poleCenter_mean" ,"kts2poleCenter_std" , "kts2poleCenter_max" ,"kts2poleCenter_min" , \
229
+ "kts2poles_mean" , "kts2poles_std" , "kts2poles_max" , "kts2poles_min" , \
230
+ "kt2Sp_mean" , "kt2Sp_std" , "kt2Sp_max" , "kt2Sp_min" , \
231
+ "proj2SpCenter_mean" , "proj2SpCenter_std" ,"proj2SpCenter_max" ,"proj2SpCenter_min" ])
232
+ for f in spots .index .levels [0 ]:
233
+ geos .loc [f ] = computeGeometries (spots .loc [f ])
234
+ return geos
235
+ else :
236
+ return
234
237
235
238
def getDotProjectionOnSp (sp1x , sp1y , sp2x , sp2y , dotx , doty ):
236
239
px = sp2x - sp1x
@@ -287,6 +290,19 @@ def computeGeometries(spotsInFrame):
287
290
# plt.show()
288
291
return [phase ] + params
289
292
293
+ def isInMitosis (cellNb , features_dir , channel ):
294
+ csvPath = features_dir + str (cellNb ) + '_' + channel + '.csv'
295
+ if path .lexists (csvPath ):
296
+ oneCell = pd .read_csv (csvPath )
297
+ spLens = oneCell ['SpLength' ]
298
+ if len (spLens [spLens > 0 ]) > 0 :
299
+ return str (cellNb ) + "_" + channel , spLens
300
+ else :
301
+ return
302
+ else :
303
+ return
304
+
305
+
290
306
if __name__ == '__main__' :
291
307
args = set_attributes_from_cmd_line ()
292
308
baseDir = args .baseDir
@@ -321,17 +337,25 @@ def computeGeometries(spotsInFrame):
321
337
cellRois = pd .read_csv (baseDir + path .sep + seg + posPrefix + 'Results.csv' )
322
338
cellNbs = getAllCellNumbers (features_dir )
323
339
tasks = []
324
- for cellNb in cellNbs :
325
- tasks .append ((features_dir , cellNb , - 4 , minSegLen , channel ))
326
- results = [pool .apply_async (getMitoticElongation , t ) for t in tasks ]
340
+ isStatic = True
341
+ if isStatic :
342
+ for cellNb in cellNbs :
343
+ tasks .append ((cellNb , features_dir , channel ))
344
+ results = [pool .apply_async (isInMitosis , t ) for t in tasks ]
345
+ else :
346
+ for cellNb in cellNbs :
347
+ tasks .append ((features_dir , cellNb , - 4 , minSegLen , channel ))
348
+ results = [pool .apply_async (getMitoticElongation , t ) for t in tasks ]
327
349
elongationRegions = dict ()
328
350
for result in results :
329
351
res = result .get ()
330
352
if res is None :
331
353
continue
332
354
elongationRegions [res [0 ]] = res [1 ]
333
-
334
- timePoints = analyse_each_cell (pool , minSegLen , elongationRegions , cellRois , mitosisDir )
355
+ if isStatic :
356
+ timePoints = None
357
+ else :
358
+ timePoints = analyse_each_cell (pool , minSegLen , elongationRegions , cellRois , mitosisDir )
335
359
if ch5 :
336
360
description = ("cell" , "cell_shape_features" )
337
361
with cellh5write .CH5FileWriter (mitosisDir + "mitosisAnalysis.ch5" ) as cfw :
@@ -372,7 +396,10 @@ def computeGeometries(spotsInFrame):
372
396
for c in chs :
373
397
##### save features data#######
374
398
curId = str (cellNb ) + "_" + c
375
- features = pd .read_csv (features_dir + curId + ".csv" )
399
+ try :
400
+ features = pd .read_csv (features_dir + curId + ".csv" )
401
+ except :
402
+ continue
376
403
# cow_feature = cpw.add_region_object(c + '_features')
377
404
# regObjs.append(cow_feature)
378
405
# for t in features['Frame']:
@@ -387,33 +414,37 @@ def computeGeometries(spotsInFrame):
387
414
pd .DataFrame ({"Frame" :features .index , "mitoRegion" :np .empty (len (features .index ))})
388
415
, on = 'Frame' , how = 'outer' )
389
416
changePoints = pd .DataFrame ({"Frame" :features .index ,"ChangePoints" :np .empty (len (features .index ))})
390
- for chPoiInd in timePoints .loc [str (cellNb )].values :
391
- changePoints .loc [chPoiInd ] = [1 , chPoiInd ]
417
+ if timePoints != None :
418
+ for chPoiInd in timePoints .loc [str (cellNb )].values :
419
+ changePoints .loc [chPoiInd ] = [1 , chPoiInd ]
392
420
features = pd .merge (features , changePoints , on = 'Frame' , how = 'outer' )
393
421
featuresOfCurrentCell [c ] = features
394
422
##### save spot data#######
395
423
spotsData = TMxml2dflib .getAllSpots (fluoDir + spots + str (cellNb ) + "_" + c + ".xml" )
396
- del spotsData ["name" ]
397
- cow_spot = cpw .add_region_object (c + '_spot' )
398
- for t in spotsData .index .levels [0 ]:
399
- cow_spot .write (t = t , object_labels = spotsData .loc [t ]['ID' ])
400
- cow_spot .finalize ()
401
-
402
- regObjs .append (cow_spot )
403
-
404
- cfew_spot_features = cpw .add_object_feature_matrix (object_name = c + '_spot' ,
405
- feature_name = c + '_spot_features' , n_features = len (spotsData .columns ),
406
- dtype = np .float16 )
407
- cfew_spot_features .write (spotsData .astype (np .float16 ))
408
- cfew_spot_features .finalize ()
409
-
410
- spotsOfCurrentCell [c ] = spotsData
411
- cfewSpotMats .append (cfew_spot_features )
424
+ print (spotsData .shape [0 ])
425
+ if spotsData .shape [0 ] > 1 :
426
+ spotsData .drop ('name' , axis = 1 , inplace = True )
427
+ cow_spot = cpw .add_region_object (c + '_spot' )
428
+ for t in spotsData .index .levels [0 ]:
429
+ cow_spot .write (t = t , object_labels = spotsData .loc [t ]['ID' ])
430
+ cow_spot .finalize ()
431
+
432
+ regObjs .append (cow_spot )
433
+
434
+ cfew_spot_features = cpw .add_object_feature_matrix (object_name = c + '_spot' ,
435
+ feature_name = c + '_spot_features' , n_features = len (spotsData .columns ),
436
+ dtype = np .float16 )
437
+ cfew_spot_features .write (spotsData .astype (np .float16 ))
438
+ cfew_spot_features .finalize ()
439
+
440
+ spotsOfCurrentCell [c ] = spotsData
441
+ cfewSpotMats .append (cfew_spot_features )
412
442
geos = getSpotGeos (spotsOfCurrentCell [channel ], spotsOfCurrentCell ["GFP" ])
413
- geos ['Frame' ] = list (geos .index )
414
- features = pd .merge (featuresOfCurrentCell [channel ], geos , on = 'Frame' , how = 'outer' )
415
- features .set_index ('Frame' , inplace = True )
416
- features .sort_index (inplace = True )
443
+ if geos is not None :
444
+ geos ['Frame' ] = list (geos .index )
445
+ features = pd .merge (featuresOfCurrentCell [channel ], geos , on = 'Frame' , how = 'outer' )
446
+ features .set_index ('Frame' , inplace = True )
447
+ features .sort_index (inplace = True )
417
448
cfewFeatureMats = list ()
418
449
for c in chs :
419
450
cfew_features = cpw .add_object_feature_matrix (object_name = c + '_features' ,
0 commit comments