-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv_slicer_th.py
159 lines (138 loc) · 3.95 KB
/
csv_slicer_th.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
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
#%%
import numpy as np
import re
import os, sys
import pandas as pd
import pprint
import matplotlib
matplotlib.use("TkAgg")
from matplotlib import pyplot as plt
from matplotlib import style as styl
import matplotlib.image as mpimg
styl.use('default')
from core.fileop import DirCheck, ListFiles, GetPendingList, GetGrpFLs
#%%
'''
path = '/Volumes/LaCie_DataStorage/xiaochao_wei_STORM imaging/STORM_imaging'
analysis_dir = 'analysis_20190419'
tstorm_dir = 'tstorm'
st_dir = 'spacial_test'
csvdata_dir = 'csvdata_sliced_T_ROI'
nchannel = 2
threshold_photon_c1 = 7000
threshold_photon_c2 = 15000
csv_th_dir = 'csvdata_sliced_T_ROI_th'
cutoff_c1 = 50
cutoff_c2 = 50
csv_th_match_dir = 'csvdata_sliced_T_ROI_th_match'
'''
path = str(sys.argv[1])
analysis_dir = str(sys.argv[2])
tstorm_dir = str(sys.argv[3])
st_dir = str(sys.argv[4])
csvdata_dir = str(sys.argv[5])
nchannel = int(sys.argv[6])
threshold_photon_c1 = int(sys.argv[7])
threshold_photon_c2 = int(sys.argv[8])
csv_th_dir = str(sys.argv[9])
cutoff_c1 = int(sys.argv[10])
cutoff_c2 = int(sys.argv[11])
csv_th_match_dir = str(sys.argv[12])
#%%
# create input path
stormcsv_path = os.path.join(path, analysis_dir, tstorm_dir, csvdata_dir)
print(stormcsv_path)
# exportfile
csv_th_path = os.path.join(path, analysis_dir, tstorm_dir, csv_th_dir)
csv_th_match_path = os.path.join(path, analysis_dir, tstorm_dir, csv_th_match_dir)
dir_check = []
for c in range(nchannel):
dir_check.append(os.path.join(csv_th_path, str(c+1)))
dir_check.append(os.path.join(csv_th_match_path, str(c+1)))
# check dir
DirCheck(dir_check)
# %%
# create grouped filename and file path
colors = {
'wildtype': 'red',
'knockout': 'blue'
}
group = {
'wildtype': 'w',
'knockout': 'k'
}
ippath = {
'stormdata':{
'dir': stormcsv_path,
'ext': '.csv',
}
}
oppath = {
'stormdata_th': {
'dir': csv_th_path,
'ext': '.csv'
}
}
# %%
# create filenamelist
filenamelist = ListFiles(os.path.join(stormcsv_path, str(1)), '.csv')['filelist']
mainfilelist = GetGrpFLs(filenamelist, nchannel, group, ippath, oppath)
pprint.pprint(mainfilelist)
#%%
# export sliced csv
threshold = {
'1': threshold_photon_c1,
'2': threshold_photon_c2,
}
data_list = []
for c in range(nchannel):
for g in group.keys():
for i in range(len(mainfilelist[str(c+1)][g]['filename_ip'])):
# for i in range(10):
filepath = mainfilelist[str(c+1)][g]['stormdata'][i]
# print(filepath)
data = pd.read_csv(filepath, header=0, index_col = False)
'''
data['filename'] = filelist[str(c+1)][group][filedir[0]][i]
data['group'] = group
data['channel'] = str(c+1)
'''
data=data[(data['intensity [photon]'] > threshold[str(c+1)])]
csvpath = mainfilelist[str(c+1)][g]['stormdata_th'][i]
data.to_csv(csvpath, index = False)
# ----------------------------------------------------- #
# remove files by the amount of event
# %%
cutoff = {
'1': cutoff_c1,
'2': cutoff_c2,
}
threshold_filelist = {}
for c in range(nchannel):
filelist_tmp = ListFiles(os.path.join(csv_th_path, str(c+1)), extension = '.csv')['fileabslist']
filelist_th_tmp = []
for i in filelist_tmp:
#print(i)
data_tmp = pd.read_csv(i, header=0, index_col = False)
if data_tmp.shape[0] > cutoff[str(c+1)]:
filelist_th_tmp.append(os.path.basename(i))
threshold_filelist[str(c+1)] = filelist_th_tmp
print(threshold_filelist)
# %%
def compare_intersect(x, y):
return frozenset(x).intersection(y)
c1 = threshold_filelist['1']
c2 = threshold_filelist['2']
print(len(c1))
print(len(c2))
matches = compare_intersect(c1, c2)
print(len(matches))
# %%
print(matches)
# %%
import shutil
for i in matches:
for c in range(nchannel):
ipfilepath = os.path.join(csv_th_path, str(c+1), i)
opfilepath = os.path.join(csv_th_match_path, str(c+1), i)
shutil.copy2(ipfilepath, opfilepath)