/
data_holder.py
217 lines (143 loc) · 6.8 KB
/
data_holder.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
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
from collections import defaultdict
import peaks
import peak_groups
import chrom
import numpy as np
import savitzky_golay as sg
import parameters
__author__ = 'Tiannan Guo, ETH Zurich 2015'
class NestedDict(defaultdict):
def __init__(self):
super(NestedDict, self).__init__(NestedDict)
def __deepcopy__(self):
return self
class Chromatogram(object):
def __init__(self, rt_list_three_values_csv, i_list_csv):
if rt_list_three_values_csv != 'NA':
rt_list = map(float, peaks.rt_three_values_to_full_list_string(rt_list_three_values_csv).split(','))
i_list = map(float, i_list_csv.split(','))
# i_list_smoothed = smooth_chromatogram_using_Savitzky_Golay(i_list)
self.rt_list = rt_list
self.i_list = i_list
# self.i_list_smoothed = i_list_smoothed
max_peaks, __ = peaks.peakdetect(i_list, rt_list, 2.0, 0.3)
# max_peaks_smoothed, __ = peaks.peakdetect(i_list_smoothed, rt_list, 6.0, 0.3)
if len(max_peaks) > 0:
# max_peaks_smoothed = filter_smoothed_peaks_based_on_raw_peaks(max_peaks, max_peaks_smoothed)
max_peaks_all = filter_peaks_based_on_peak_shape(max_peaks, i_list, rt_list)
self.peak_apex_rt_list = [rt for (rt, i) in max_peaks_all]
self.peak_apex_i_list = [i for (rt, i) in max_peaks_all]
else:
# if no peak found. Most likely there is no signal. Use looser criteria to detect peaks
max_peaks, __ = peaks.peakdetect(i_list, rt_list, 1.0, 0.3)
# max_peaks_smoothed, __ = peaks.peakdetect(i_list_smoothed, rt_list, 1.0, 0.3)
# max_peaks_smoothed = filter_smoothed_peaks_based_on_raw_peaks(max_peaks, max_peaks_smoothed)
max_peaks_all = filter_peaks_based_on_peak_shape(max_peaks, i_list, rt_list)
if len(max_peaks) > 0:
self.peak_apex_rt_list = [rt for (rt, i) in max_peaks_all]
self.peak_apex_i_list = [i for (rt, i) in max_peaks_all]
else:
# if still no peak found, most likely it is an empty chrom.
# use the point in the median value as peak
self.peak_apex_rt_list = [np.median(np.array(rt_list))]
self.peak_apex_i_list = [np.median(np.array(i_list))]
else:
self.rt_list = 'NA'
self.i_list = 'NA'
self.peak_apex_rt_list = 'NA'
self.peak_apex_i_list = 'NA'
def filter_peaks_based_on_peak_shape(max_peaks, i_list, rt_list):
max_peaks_all = []
max_peaks_all = filter_peaks_based_on_peak_shape_worker(max_peaks, i_list, rt_list, max_peaks_all)
if len(max_peaks_all) == 0:
# if no peak is found, find the best peak in the chrom
max_peaks_all = filter_peaks_based_on_peak_shape_worker2(max_peaks, i_list, rt_list, max_peaks_all)
return max_peaks_all
def filter_peaks_based_on_peak_shape_worker2(max_peaks, i_list, rt_list, max_peaks_all):
max_fold_change_score = -1
best_rt = -1
best_i = -1
for rt, i in max_peaks:
rt_left, rt_right = chrom.get_peak_boundary(rt_list, i_list, rt)
i_apex = float(i)
i_left = peak_groups.get_intensity_for_closest_rt(rt_left, rt_list, i_list)
i_right = peak_groups.get_intensity_for_closest_rt(rt_right, rt_list, i_list)
fold_change_left = i_apex / (i_left + 1.0)
fold_change_right = i_apex / (i_right + 1.0)
fold_change_score = fold_change_left * fold_change_right
if fold_change_score > max_fold_change_score:
max_fold_change_score = fold_change_score
best_rt = rt
best_i = i
max_peaks_all.append((best_rt, best_i))
return max_peaks_all
def filter_peaks_based_on_peak_shape_worker(max_peaks, i_list, rt_list, max_peaks_all):
for rt, i in max_peaks:
rt_left, rt_right = chrom.get_peak_boundary(rt_list, i_list, rt)
i_apex = float(i)
i_left = peak_groups.get_intensity_for_closest_rt(rt_left, rt_list, i_list)
i_right = peak_groups.get_intensity_for_closest_rt(rt_right, rt_list, i_list)
fold_change_left = i_apex / (i_left + 1.0)
fold_change_right = i_apex / (i_right + 1.0)
if fold_change_left >= parameters.PEAK_SHAPE_FOLD_VARIATION_CRUDE and fold_change_right >= parameters.PEAK_SHAPE_FOLD_VARIATION_CRUDE:
max_peaks_all.append((rt, i))
return max_peaks_all
def filter_smoothed_peaks_based_on_raw_peaks(peaks, peaks_smoothed):
peaks_smoothed2 = []
for rt, i in peaks_smoothed:
if_select = decide_whether_choose_a_smoothed_rt(rt, [rt for (rt, i) in peaks])
if if_select == 1:
peaks_smoothed2.append((rt, i))
return peaks_smoothed2
def decide_whether_choose_a_smoothed_rt(rt0, rt_list):
if_select = 0
rt_closest_left = find_closest_rt_left(rt0, rt_list)
rt_closest_right = find_closest_rt_right(rt0, rt_list)
if abs(rt_closest_left - rt0) > 10 and abs(rt_closest_right - rt0) > 10:
if_select = 1
return if_select
def find_closest_rt_left(rt0, rt_list):
rt1 = rt_list[0]
rt_dif = 999.999
for rt in rt_list[1:]:
if rt < rt0:
rt_dif2 = rt0 - rt
if rt_dif2 < rt_dif:
rt1 = rt
rt_dif = rt_dif2
return rt1
def find_closest_rt_right(rt0, rt_list):
rt1 = rt_list[0]
rt_dif = 999.999
for rt in rt_list[1:]:
if rt > rt0:
rt_dif2 = rt - rt0
if rt_dif2 < rt_dif:
rt1 = rt
rt_dif = rt_dif2
return rt1
def smooth_chromatogram_using_Savitzky_Golay(i_list):
i_list2 = sg.savitzky_golay(np.array(i_list), 7, 3) # window size 11, polynomial order 3. Optimized for chrom
return i_list2.tolist()
class ReferenceSample(object):
def __init__(self, sample_name, score, peak_rt):
self.sample_name = sample_name
self.score = score
self.peak_rt = peak_rt
self.peak_rt_found = ''
self.peak_rt_left = ''
self.peak_rt_right = ''
def read_peak_boundary(self, peak_rt_left, peak_rt_right):
self.peak_rt_left = peak_rt_left
self.peak_rt_right = peak_rt_right
def read_peak_rt_found(self, rt_found):
self.peak_rt_found = rt_found
class PeakGroup(object):
def __init__(self, chrom_data, tg, sample, rt):
self.rt = rt
(self.matched_fragments, self.matched_fragments_rt,
self.matched_fragments_i,
self.matched_fragments_peak_rt_left,
self.matched_fragments_peak_rt_right) = peak_groups.find_matched_fragments(chrom_data, tg, sample, rt)
self.num_matched_fragments = len(self.matched_fragments)
self.if_ms1_peak = peak_groups.check_if_ms1_peak(chrom_data, tg, sample, rt)