forked from junlabucsd/mm3
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcombine_tracks_from_chtc.py
executable file
·86 lines (70 loc) · 2.7 KB
/
combine_tracks_from_chtc.py
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
#!/usr/bin/env python3
# import modules
import sys
import os
import inspect
import glob
import argparse
import skimage
from skimage import measure, io
from pprint import pprint # for human readable file output
try:
import cPickle as pickle
except:
import pickle
# user modules
# realpath() will make your script run, even if you symlink it
cmd_folder = os.path.realpath(os.path.abspath(
os.path.split(inspect.getfile(inspect.currentframe()))[0]))
if cmd_folder not in sys.path:
sys.path.insert(0, cmd_folder)
# This makes python look for modules in ./external_lib
cmd_subfolder = os.path.realpath(os.path.abspath(
os.path.join(os.path.split(inspect.getfile(
inspect.currentframe()))[0], "external_lib")))
if cmd_subfolder not in sys.path:
sys.path.insert(0, cmd_subfolder)
import mm3_helpers as mm3
#%%
# when using this script as a function and not as a library the following will execute
if __name__ == "__main__":
# set switches and parameters
parser = argparse.ArgumentParser(
prog='python combine_tracks_from_chtc.py',
description='CHTC saves a separate track file for each fov/peak. Here we combine them.'
)
parser.add_argument(
'-f',
'--paramfile',
type=str,
required=True,
help='Yaml file containing parameters.'
)
namespace = parser.parse_args()
# Load the project parameters file
mm3.information('Loading experiment parameters.')
if namespace.paramfile:
param_file_path = namespace.paramfile
else:
mm3.warning('No param file specified. Using 100X template.')
param_file_path = 'yaml_templates/params_SJ110_100X.yaml'
p = mm3.init_mm3_helpers(param_file_path) # initialized the helper library
# Get file names
fnames = glob.glob(os.path.join(p['cell_dir'], "{}*_tracks.pkl".format(p['experiment_name'])))
### Now prune and save the data.
mm3.information("Reading cell data from each file and combining into one.")
tracks = {}
for fname in fnames:
with open(fname, 'rb') as cell_file:
cell_data = pickle.load(cell_file)
os.remove(fname)
tracks.update(cell_data)
with open(p['cell_dir'] + '/all_cells.pkl', 'wb') as cell_file:
pickle.dump(tracks, cell_file, protocol=pickle.HIGHEST_PROTOCOL)
if os.path.isfile(os.path.join(p['cell_dir'], 'complete_cells.pkl')):
os.remove(os.path.join(p['cell_dir'], 'complete_cells.pkl'))
os.symlink(
os.path.join(p['cell_dir'], 'all_cells.pkl'),
os.path.join(p['cell_dir'], 'complete_cells.pkl')
)
mm3.information("Finished curating and saving cell data.")