-
Notifications
You must be signed in to change notification settings - Fork 0
/
viewmodels.py
executable file
·243 lines (185 loc) · 7.94 KB
/
viewmodels.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
from PyQt4.QtGui import QItemDelegate, QComboBox
from PyQt4.QtCore import QObject, SIGNAL, SLOT, pyqtSignal, pyqtSlot
from models import BinSizeRecord, Ion, Range, ExperimentInfo, Analysis, Isotope
import json
import datetime
import itertools
from collections import namedtuple
from string import ascii_uppercase
import numpy as np
WorkingPlotRecord = namedtuple('WorkingPlotRecord', 'm2cs bin_size analyses ions')
MethodsRecord = namedtuple('MethodsRecord', 'methods m2cs bin_size')
MRRecord = namedtuple('MRRecord', 'analyses metadata')
AnalysesRecord = namedtuple('AnalysesRecord', 'analyses methods')
class MethodsViewModel(QObject):
def __init__(self):
super(MethodsViewModel, self).__init__(None)
self._record = MethodsRecord(
methods={},
m2cs=(),
bin_size=BinSizeRecord(1, 0, 1),
)
@pyqtSlot(dict)
def on_methods_updated(self, new_methods):
self._record = self._record._replace(methods=new_methods)
@pyqtSlot(tuple)
def on_m2cs_updated(self, new_m2cs):
self._record = self._record._replace(m2cs=new_m2cs)
@pyqtSlot(BinSizeRecord)
def on_bin_size_updated(self, new_bin_size):
self._record = self._record._replace(bin_size=new_bin_size)
def run_method_for_ion(self, new_ion, method_name):
module = self._record.methods[method_name]
method_to_call = getattr(module, method_name.lower())
required_inputs = module.required_inputs()
inputs = self._send_inputs(required_inputs, new_ion)
start, end = method_to_call(*inputs)
return Range(start, end)
def _send_inputs(self, required_inputs, ion):
input_reference={
'suggested_m2c': ion.mass_to_charge,
'abundance': ion.isotope.abundance,
'bin_size': self._record.bin_size.value,
'm2cs': self._record.m2cs,
}
inputs = []
for _input in required_inputs:
if _input in input_reference:
inputs.append(input_reference[_input])
return inputs
class ExportViewModel(QObject):
export_error = pyqtSignal()
updated = pyqtSignal()
def __init__(self):
super(ExportViewModel, self).__init__(None)
self._record = MRRecord(
analyses=(),
metadata=()
)
@pyqtSlot(tuple)
def on_metadata_updated(self, new_metadata):
self._record = self._record._replace(metadata=new_metadata)
@pyqtSlot(dict)
def on_analyses_updated(self, new_analyses):
self._record = self._record._replace(analyses=new_analyses)
def check_manual_analyses_have_reasons(self):
for ion, analysis in self._record.analyses.items():
if analysis.method == 'Manual' and analysis.reason == None:
self.export_error.emit()
return False
return True
def export_analyses_to_mrfile(self, filename):
if self._record:
if self.check_manual_analyses_have_reasons():
record = self._to_json(self._record)
with open(filename, mode='w', encoding='utf-8') as f:
json.dump(record, f, indent=2)
def _to_json(self, record):
analyses_list = []
analyses_list.append([
record.metadata.ID,
record.metadata.description
])
for ion, analysis in record.analyses.items():
analyses_list.append(ion.name)
analyses_list.append({
'Ion': [ion.isotope.element, ion.isotope.number, ion.isotope.mass, ion.isotope.abundance, ion.charge_state],
'Method': analysis.method,
'Range': [analysis.range.start, analysis.range.end],
'Reason': analysis.reason,
'Color': analysis.color,
})
analyses_list.append(str(datetime.datetime.today())) #ISO 8601 format
return analyses_list
def import_analyses_from_mrfile(self, mrfile):
with open(mrfile, 'r', encoding='utf-8') as f:
contents = json.load(f)
new_analyses, new_metadata = self._from_json(contents)
return new_analyses, new_metadata
def _from_json(self, contents):
new_analyses = {}
new_metadata = None
for entry in contents:
if isinstance(entry, list):
new_metadata = ExperimentInfo(entry[0], entry[1])
if isinstance(entry, dict):
element, number, mass, abundance, charge_state = entry.get('Ion')
method_name = entry.get('Method')
start, end = entry.get('Range')
reason = entry.get('Reason')
color = entry.get('Color')
_range = Range(start, end)
new_analyses.update({Ion(Isotope(element, number, mass, abundance), charge_state): Analysis(method_name, _range, reason, color)})
return new_analyses, new_metadata
def export_analyses_to_rngfile(self, filename):
record = self._record
existent_color_mapping = {}
for ion in record.analyses.keys():
existent_color_mapping.update({ion.isotope.element: record.analyses[ion].color})
line_1 = str(record.metadata.ID)
line_2 = str(len(existent_color_mapping)) + ' ' + str(len(record.analyses))
elements = []
ranges = []
middle_block = ''
for element, color in existent_color_mapping.items():
middle_block += str(element) + '\n' + str(element) + ' ' + str(color[0]) + ' ' + str(color[1]) + ' ' + str(color[2]) + '\n'
elements.append(element)
for ion, analysis in record.analyses.items():
ranges.append(str(analysis.range.start) + ' ' + str(analysis.range.end))
final_block_header = '------------------------- ' + ' '.join(name for name in elements)
final_block_contents = ''
index = 0
def zerolistmaker(n):
listofzeros = []
listofzeros = [0] * n
return listofzeros
for _range in ranges:
zeros = zerolistmaker(len(record.analyses))
zeros[index] = 1
final_block_contents += '. ' + _range + ' ' + ' '.join(str(i) for i in zeros) + '\n'
index += 1
with open(filename, 'w') as f:
contents = line_1 + '\n' + line_2 + '\n' + middle_block + final_block_header + '\n' + final_block_contents
f.write(contents)
class WorkingPlotViewModel(QObject):
updated = pyqtSignal(WorkingPlotRecord)
def __init__(self):
super(WorkingPlotViewModel, self).__init__(None)
self._record = WorkingPlotRecord(
m2cs=(),
bin_size=BinSizeRecord(1, 0, 1),
analyses={},
ions=(),
)
@pyqtSlot(tuple)
def on_m2cs_updated(self, new_m2cs):
self._record = self._record._replace(m2cs=new_m2cs)
self.updated.emit(self._record)
@pyqtSlot(BinSizeRecord)
def on_bin_size_updated(self, new_bin_size):
self._record = self._record._replace(bin_size=new_bin_size)
self.updated.emit(self._record)
@pyqtSlot(tuple)
def on_analyses_updated(self, new_analyses):
self._record = self._record._replace(analyses=new_analyses)
self.updated.emit(self._record)
@pyqtSlot(tuple)
def on_ions_updated(self, new_ions):
self._record = self._record._replace(ions=new_ions)
self.updated.emit(self._record)
class AnalysesViewModel(QObject):
updated = pyqtSignal(AnalysesRecord)
def __init__(self):
super(AnalysesViewModel, self).__init__(None)
self._record = AnalysesRecord(
analyses = {},
methods = (),
)
@pyqtSlot(dict)
def on_analyses_updated(self, new_analyses):
self._record = self._record._replace(analyses=new_analyses)
self.updated.emit(self._record)
@pyqtSlot(tuple)
def on_methods_updated(self, new_methods):
self._record = self._record._replace(methods=new_methods)
self.updated.emit(self._record)