IsoSpecPy 2.2.3__cp313-cp313-win32.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
IsoSpecPy/IsoSpecPy.py ADDED
@@ -0,0 +1,827 @@
1
+ # -*- coding: utf-8 -*-
2
+ #
3
+ # Copyright (C) 2015-2020 Mateusz Łącki and Michał Startek.
4
+ #
5
+ # This file is part of IsoSpec.
6
+ #
7
+ # IsoSpec is free software: you can redistribute it and/or modify
8
+ # it under the terms of the Simplified ("2-clause") BSD licence.
9
+ #
10
+ # IsoSpec is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
+ #
14
+ # You should have received a copy of the Simplified BSD Licence
15
+ # along with IsoSpec. If not, see <https://opensource.org/licenses/BSD-2-Clause>.
16
+ #
17
+
18
+ from .isoFFI import isoFFI
19
+ import re
20
+ import types
21
+ from . import PeriodicTbl
22
+ from .confs_passthrough import ConfsPassthrough
23
+ from collections import namedtuple, OrderedDict
24
+ import math
25
+
26
+ try:
27
+ xrange
28
+ except NameError:
29
+ xrange = range
30
+
31
+ regex_pattern = re.compile('([A-Z][a-z]?)(-?[0-9]*)')
32
+ ParsedFormula = namedtuple('ParsedFormula', 'atomCounts masses probs elems')
33
+
34
+
35
+
36
+
37
+ def ParseFormula(formula):
38
+ """Parse a chemical formula.
39
+
40
+ Args:
41
+ formula (str): a chemical formula, e.g. "C2H6O1" or "C2H6O"
42
+
43
+ Returns:
44
+ A tuple containing element symbols and atomCounts of elements in the parsed formula.
45
+ """
46
+ global regex_pattern
47
+
48
+ ret = OrderedDict()
49
+
50
+ last = 0
51
+ for match in re.finditer(regex_pattern, formula):
52
+ elem, cnt = match.groups()
53
+ if elem in ret:
54
+ raise ValueError("""Invalid formula: {} (repeating element: "{}")""".format(formula, elem))
55
+ ret[elem] = int(cnt) if cnt != '' else 1
56
+ if last!=match.start():
57
+ raise ValueError("""Invalid formula: {} (garbage inside: "{}")""".format(formula, formula[last:match.start()]))
58
+ if elem not in PeriodicTbl.symbol_to_masses:
59
+ raise ValueError("""Invalid formula: {} (unknown element symbol: "{}")""".format(formula, elem))
60
+ last = match.end()
61
+
62
+ if len(formula) != last:
63
+ raise ValueError('''Invalid formula: {} (trailing garbage: "{}")'''.format(formula, formula[last:]))
64
+
65
+ if len(ret) == 0:
66
+ raise ValueError("Invalid formula (empty)")
67
+
68
+ return ret
69
+
70
+ fasta_parsing_space = isoFFI.ffi.new("int[6]")
71
+
72
+ def ParseFASTA(fasta):
73
+ if isinstance(fasta, str):
74
+ fasta = fasta.encode("ascii")
75
+ isoFFI.clib.parse_fasta_c(fasta, fasta_parsing_space)
76
+ elements = list("CHNOS")
77
+ if fasta_parsing_space[5] > 0:
78
+ elements.append("Se")
79
+ od = OrderedDict()
80
+ for i in range(len(elements)):
81
+ od[elements[i]] = fasta_parsing_space[i]
82
+ return od
83
+
84
+
85
+ def IsoParamsFromDict(formula, use_nominal_masses = False):
86
+ """Produces a set of IsoSpec parameters from a chemical formula.
87
+
88
+ Args:
89
+ formula (dict): a parsed chemical formula, e.g. {"C": 2, "H": 6, "O": 1}
90
+ use_nominal_masses (boolean): use masses of elements rounded to integer numbers (nominal masses)
91
+
92
+ Returns:
93
+ ParsedFormula: a tuple containing atomCounts, masses and marginal probabilities of elements in the parsed formula.
94
+ """
95
+
96
+ symbols, atomCounts = [], []
97
+ for symbol, atomCount in formula.items():
98
+ symbols.append(symbol)
99
+ atomCounts.append(atomCount)
100
+
101
+ try:
102
+ if use_nominal_masses:
103
+ masses = [PeriodicTbl.symbol_to_massNo[s] for s in symbols]
104
+ else:
105
+ masses = [PeriodicTbl.symbol_to_masses[s] for s in symbols]
106
+ probs = [PeriodicTbl.symbol_to_probs[s] for s in symbols]
107
+ except KeyError:
108
+ raise ValueError("Invalid formula")
109
+
110
+ return ParsedFormula(atomCounts, masses, probs, symbols)
111
+
112
+
113
+
114
+ class Iso(object):
115
+ """Virtual class representing an isotopic distribution."""
116
+ def __init__(self, formula="",
117
+ get_confs=False,
118
+ atomCounts=None,
119
+ isotopeMasses=None,
120
+ isotopeProbabilities=None,
121
+ use_nominal_masses = False,
122
+ fasta = "",
123
+ charge = 1.0):
124
+ """Initialize Iso.
125
+
126
+ Args:
127
+ formula (str): a chemical formula, e.g. "C2H6O1" or "C2H6O".
128
+ get_confs (boolean): should we report counts of isotopologues?
129
+ atomCounts (list): a list of atom counts (alternative to 'formula').
130
+ isotopeMasses (list): a list of lists of masses of elements with counts in 'atomCounts'.
131
+ isotopeProbabilities (list): a list of lists of probabilities of elements with counts in 'atomCounts'.
132
+ use_nominal_masses (boolean): should the masses be rounded to the closest integer values.
133
+ charge (float): charge state of the molecule: all masses will be divided by this value to obtain the m/z values.
134
+ """
135
+
136
+ self.iso = None
137
+
138
+ if len(fasta) > 0:
139
+ molecule = ParseFASTA(fasta)
140
+ else:
141
+ molecule = OrderedDict()
142
+
143
+ if len(formula) > 0:
144
+ if isinstance(formula, dict):
145
+ df = formula
146
+ else:
147
+ df = ParseFormula(formula)
148
+ for symbol, count in df.items():
149
+ molecule[symbol] = molecule.get(symbol, 0) + count
150
+
151
+ for sym, cnt in molecule.items():
152
+ if cnt < 0:
153
+ raise Exception("Negative count of element " + sym + ": " + str(cnt))
154
+
155
+ if len(molecule) == 0 and not all([atomCounts, isotopeMasses, isotopeProbabilities]):
156
+ raise Exception("Either formula, fasta or ALL of: atomCounts, isotopeMasses, isotopeProbabilities must not be None")
157
+
158
+ if len(molecule) > 0:
159
+ self.atomCounts, self.isotopeMasses, self.isotopeProbabilities, _ = IsoParamsFromDict(molecule, use_nominal_masses = use_nominal_masses)
160
+ else:
161
+ self.atomCounts, self.isotopeMasses, self.isotopeProbabilities = [], [], []
162
+
163
+ if not (atomCounts is None):
164
+ self.atomCounts.extend(atomCounts)
165
+
166
+ if not (isotopeMasses is None):
167
+ self.isotopeMasses.extend(isotopeMasses)
168
+
169
+ if not (isotopeProbabilities is None):
170
+ self.isotopeProbabilities.extend(isotopeProbabilities)
171
+
172
+ for sublist in self.isotopeProbabilities:
173
+ for prob in sublist:
174
+ if not (0.0 < prob <= 1.0):
175
+ raise ValueError("All isotope probabilities p must fulfill: 0.0 < p <= 1.0")
176
+
177
+ self.isotopeNumbers = tuple(map(len, self.isotopeMasses))
178
+ assert self.isotopeNumbers == tuple(map(len, self.isotopeProbabilities))
179
+ assert len(self.atomCounts) == len(self.isotopeNumbers) == len(self.isotopeProbabilities)
180
+
181
+ self.dimNumber = len(self.isotopeNumbers)
182
+
183
+ self.get_confs = get_confs
184
+ self.ffi = isoFFI.clib
185
+
186
+ offsets = []
187
+
188
+ if get_confs:
189
+ i = 0
190
+ for j in xrange(self.dimNumber):
191
+ newl = []
192
+ for k in xrange(self.isotopeNumbers[j]):
193
+ newl.append(i)
194
+ i += 1
195
+ offsets.append(tuple(newl))
196
+ self.offsets = tuple(offsets)
197
+
198
+ self.iso = self.ffi.setupIso(self.dimNumber, self.isotopeNumbers,
199
+ self.atomCounts,
200
+ [i/charge for s in self.isotopeMasses for i in s],
201
+ [i for s in self.isotopeProbabilities for i in s])
202
+
203
+ def __del__(self):
204
+ try:
205
+ if self.iso is not None:
206
+ self.ffi.deleteIso(self.iso)
207
+ self.iso = None
208
+ except AttributeError:
209
+ pass
210
+
211
+ def getLightestPeakMass(self):
212
+ """Get the lightest peak in the isotopic distribution."""
213
+ return self.ffi.getLightestPeakMassIso(self.iso)
214
+
215
+ def getHeaviestPeakMass(self):
216
+ """Get the heaviest peak in the isotopic distribution."""
217
+ return self.ffi.getHeaviestPeakMassIso(self.iso)
218
+
219
+ def getMonoisotopicPeakMass(self):
220
+ """Get the monoisotopic mass of the peak."""
221
+ return self.ffi.getMonoisotopicPeakMassIso(self.iso)
222
+
223
+ def getModeLProb(self):
224
+ """Get the log probability of the most probable peak(s) in the isotopic distribution."""
225
+ return self.ffi.getModeLProbIso(self.iso)
226
+
227
+ def getModeMass(self):
228
+ """Get the mass of the most probable peak.
229
+
230
+ If there are more, return only the mass of one of them."""
231
+ return self.ffi.getModeMassIso(self.iso)
232
+
233
+ def getTheoreticalAverageMass(self):
234
+ return self.ffi.getTheoreticalAverageMassIso(self.iso)
235
+
236
+ def variance(self):
237
+ return self.ffi.getIsoVariance(self.iso)
238
+
239
+ def stddev(self):
240
+ return self.ffi.getIsoStddev(self.iso)
241
+
242
+ def getMarginalLogSizeEstimates(self, prob):
243
+ cbuf = isoFFI.clib.getMarginalLogSizeEstimates(self.iso, prob)
244
+ ret = list(isoFFI.ffi.cast('double[' + str(self.dimNumber) + ']', cbuf))
245
+ isoFFI.clib.freeReleasedArray(cbuf)
246
+ return ret
247
+
248
+ def parse_conf(self, cptr, starting_with = 0):
249
+ return tuple(tuple(cptr[i+starting_with] for i in o) for o in self.offsets)
250
+
251
+ def _get_parse_conf_fun(self):
252
+ # Can't just use the above function as lambda, as the entire class instance will be in closure
253
+ offsets = self.offsets
254
+ def pc(cptr, starting_with = 0):
255
+ return tuple(tuple(cptr[i+starting_with] for i in o) for o in offsets)
256
+ return pc
257
+
258
+
259
+ class IsoDistribution(object):
260
+ """Isotopoic distribution with precomputed vector of masses and probabilities."""
261
+ def np_masses(self):
262
+ """Return computed masses as a numpy array."""
263
+ try:
264
+ import numpy as np
265
+ except ImportError:
266
+ raise Exception(e.msg + "\nThis requires numpy to be installed.")
267
+ return np.frombuffer(isoFFI.ffi.buffer(self.masses))
268
+
269
+ def np_probs(self):
270
+ """Return computed probabilities as a numpy array."""
271
+ try:
272
+ import numpy as np
273
+ except ImportError as e:
274
+ raise Exception(e.msg + "\nThis requires numpy to be installed.")
275
+ return np.frombuffer(isoFFI.ffi.buffer(self.probs))
276
+
277
+ def __iter__(self):
278
+ if hasattr(self, "confs") and self.confs is not None:
279
+ for i in xrange(self.size):
280
+ yield(self.masses[i], self.probs[i], self.confs[i])
281
+ else:
282
+ for i in xrange(self.size):
283
+ yield (self.masses[i], self.probs[i])
284
+
285
+ def __getitem__(self, idx):
286
+ try:
287
+ return (self.masses[idx], self.probs[idx], self.confs[idx])
288
+ except (AttributeError, TypeError):
289
+ return (self.masses[idx], self.probs[idx])
290
+
291
+ def __len__(self):
292
+ """Get the number of calculated peaks."""
293
+ return self.size
294
+
295
+ def _get_conf(self, idx):
296
+ return self.parse_conf(self.raw_confs, starting_with = self.sum_isotope_numbers * idx)
297
+
298
+ def __del__(self):
299
+ pass
300
+
301
+ def __init__(self, cobject = None, probs = None, masses = None, get_confs = False, iso = None):
302
+ self.mass_sorted = False
303
+ self.prob_sorted = False
304
+ self._total_prob = float('nan')
305
+
306
+ if cobject is not None:
307
+ self.size = isoFFI.clib.confs_noFixedEnvelope(cobject)
308
+
309
+ def wrap(typename, what, attrname, mult = 1):
310
+ if what is not None:
311
+ x = isoFFI.ffi.gc(isoFFI.ffi.cast(typename + '[' + str(self.size*mult) + ']', what), isoFFI.clib.freeReleasedArray)
312
+ setattr(self, attrname, x)
313
+
314
+ wrap("double", isoFFI.clib.massesFixedEnvelope(cobject), "masses")
315
+ wrap("double", isoFFI.clib.probsFixedEnvelope(cobject), "probs")
316
+
317
+ if get_confs:
318
+ # Must also be a subclass of Iso...
319
+ self.sum_isotope_numbers = sum(iso.isotopeNumbers)
320
+ wrap("int", isoFFI.clib.confsFixedEnvelope(cobject), "raw_confs", mult = self.sum_isotope_numbers)
321
+ self.confs = ConfsPassthrough(lambda idx: self._get_conf(idx), self.size)
322
+ self.parse_conf = iso._get_parse_conf_fun()
323
+
324
+ elif probs is not None or masses is not None:
325
+ assert probs is not None and masses is not None
326
+ assert len(probs) == len(masses)
327
+ self.size = len(probs)
328
+ type_str = "double["+str(self.size)+"]"
329
+ self.probs = isoFFI.ffi.new(type_str, probs)
330
+ self.masses = isoFFI.ffi.new(type_str, masses)
331
+ elif cobject == probs == masses == get_confs == iso == None:
332
+ self.size = 0
333
+ type_str = "double["+str(self.size)+"]"
334
+ self.probs = isoFFI.ffi.new(type_str, [])
335
+ self.masses = isoFFI.ffi.new(type_str, [])
336
+ self._total_prob = 0.0
337
+ self.mass_sorted = True
338
+ self.prob_sorted = True
339
+ else:
340
+ raise RuntimeError("Invalid arguments for IsoDistribution constructor")
341
+
342
+ def _get_cobject(self):
343
+ return isoFFI.clib.setupFixedEnvelope(self.masses, self.probs, len(self.masses), self.mass_sorted, self.prob_sorted, self._total_prob)
344
+
345
+ def copy(self):
346
+ x = self._get_cobject()
347
+ c = isoFFI.clib.copyFixedEnvelope(x)
348
+ isoFFI.clib.deleteFixedEnvelope(x, True)
349
+ ret = IsoDistribution(cobject = c)
350
+ ret._total_prob = self._total_prob
351
+ ret.mass_sorted = self.mass_sorted
352
+ ret.prob_sorted = self.prob_sorted
353
+ isoFFI.clib.deleteFixedEnvelope(c, False)
354
+ return ret
355
+
356
+ def __add__(self, other):
357
+ x = self._get_cobject()
358
+ y = other._get_cobject()
359
+ cobject = isoFFI.clib.addEnvelopes(x, y)
360
+ isoFFI.clib.deleteFixedEnvelope(x, True)
361
+ isoFFI.clib.deleteFixedEnvelope(y, True)
362
+ ret = IsoDistribution(cobject = cobject)
363
+ isoFFI.clib.deleteFixedEnvelope(cobject, False)
364
+ return ret
365
+
366
+ def __mul__(self, other):
367
+ x = self._get_cobject()
368
+ y = other._get_cobject()
369
+ cobject = isoFFI.clib.convolveEnvelopes(x, y)
370
+ isoFFI.clib.deleteFixedEnvelope(x, True)
371
+ isoFFI.clib.deleteFixedEnvelope(y, True)
372
+ ret = IsoDistribution(cobject = cobject)
373
+ isoFFI.clib.deleteFixedEnvelope(cobject, False)
374
+ return ret
375
+
376
+ def total_prob(self):
377
+ if math.isnan(self._total_prob):
378
+ co = self._get_cobject()
379
+ self._total_prob = isoFFI.clib.getTotalProbOfEnvelope(co)
380
+ isoFFI.clib.deleteFixedEnvelope(co, True)
381
+ return self._total_prob
382
+
383
+ def normalize(self):
384
+ co = self._get_cobject()
385
+ isoFFI.clib.normalizeEnvelope(co)
386
+ isoFFI.clib.deleteFixedEnvelope(co, True)
387
+ self._total_prob = 1.0
388
+
389
+ def normalized(self):
390
+ ret = self.copy()
391
+ ret.normalize()
392
+ return ret
393
+
394
+ def add_mass(self, d_mass):
395
+ isoFFI.clib.array_add(self.masses, self.size, d_mass)
396
+
397
+ def mul_mass(self, d_mass):
398
+ isoFFI.clib.array_mul(self.masses, self.size, d_mass)
399
+
400
+ def add_mul_mass(self, add, mul):
401
+ isoFFI.clib.array_fma(self.masses, self.size, mul, add*mul)
402
+
403
+ def mul_add_mass(self, mul, add):
404
+ isoFFI.clib.array_fma(self.masses, self.size, mul, add)
405
+
406
+ def scale(self, factor):
407
+ '''Multiplies the pribabilities of spectrum by factor. Works in place.'''
408
+ co = self._get_cobject()
409
+ isoFFI.clib.scaleEnvelope(co, factor)
410
+ isoFFI.clib.deleteFixedEnvelope(co, True)
411
+ self._total_prob *= factor
412
+
413
+ def scaled(self, factor):
414
+ '''Returns a copy of the spectrum where each probability was multiplied by factor.'''
415
+ ret = self.copy()
416
+ ret.scale(factor)
417
+ return ret
418
+
419
+ def resample(self, ionic_current, beta_bias=1.0):
420
+ co = self._get_cobject()
421
+ isoFFI.clib.resampleEnvelope(co, ionic_current, beta_bias)
422
+ isoFFI.clib.deleteFixedEnvelope(co, True)
423
+ self._total_prob = float(ionic_current)
424
+ self.prob_sorted = False
425
+
426
+ def sort_by_prob(self):
427
+ if not self.prob_sorted:
428
+ co = self._get_cobject()
429
+ isoFFI.clib.sortEnvelopeByProb(co)
430
+ isoFFI.clib.deleteFixedEnvelope(co, True)
431
+ self.mass_sorted = False
432
+ self.prob_sorted = True
433
+
434
+ def sort_by_mass(self):
435
+ if not self.mass_sorted:
436
+ co = self._get_cobject()
437
+ isoFFI.clib.sortEnvelopeByMass(co)
438
+ isoFFI.clib.deleteFixedEnvelope(co, True)
439
+ self.mass_sorted = True
440
+ self.prob_sorted = False
441
+
442
+ def _recalculate_everything(self):
443
+ self._total_prob = float('nan')
444
+ self.mass_sorted = False
445
+ self.prob_sorted = False
446
+
447
+ def empiric_average_mass(self):
448
+ co = self._get_cobject()
449
+ ret = isoFFI.clib.empiricAverageMass(co)
450
+ isoFFI.clib.deleteFixedEnvelope(co, True)
451
+ return ret
452
+
453
+ def empiric_variance(self):
454
+ co = self._get_cobject()
455
+ ret = isoFFI.clib.empiricVariance(co)
456
+ isoFFI.clib.deleteFixedEnvelope(co, True)
457
+ return ret
458
+
459
+ def empiric_stddev(self):
460
+ co = self._get_cobject()
461
+ ret = isoFFI.clib.empiricStddev(co)
462
+ isoFFI.clib.deleteFixedEnvelope(co, True)
463
+ return ret
464
+
465
+ def wassersteinDistance(self, other):
466
+ x = self._get_cobject()
467
+ y = other._get_cobject()
468
+ ret = isoFFI.clib.wassersteinDistance(x, y)
469
+ isoFFI.clib.deleteFixedEnvelope(x, True)
470
+ isoFFI.clib.deleteFixedEnvelope(y, True)
471
+ self.mass_sorted = True
472
+ self.prob_sorted = False
473
+ other.mass_sorted = True
474
+ other.prob_sorted = False
475
+ if math.isnan(ret):
476
+ raise ValueError("Both spectra must be normalized before Wasserstein distance can be computed.")
477
+ return ret
478
+
479
+ def orientedWassersteinDistance(self, other):
480
+ x = self._get_cobject()
481
+ y = other._get_cobject()
482
+ ret = isoFFI.clib.orientedWassersteinDistance(x, y)
483
+ isoFFI.clib.deleteFixedEnvelope(x, True)
484
+ isoFFI.clib.deleteFixedEnvelope(y, True)
485
+ self.mass_sorted = True
486
+ self.prob_sorted = False
487
+ other.mass_sorted = True
488
+ other.prob_sorted = False
489
+ if math.isnan(ret):
490
+ raise ValueError("Both spectra must be normalized before Wasserstein distance can be computed.")
491
+ return ret
492
+
493
+ def abyssalWassersteinDistance(self, other, abyss_depth, other_scale = 1.0):
494
+ x = self._get_cobject()
495
+ y = other._get_cobject()
496
+ ret = isoFFI.clib.abyssalWassersteinDistance(x, y, abyss_depth, other_scale)
497
+ isoFFI.clib.deleteFixedEnvelope(x, True)
498
+ isoFFI.clib.deleteFixedEnvelope(y, True)
499
+ self.mass_sorted = True
500
+ self.prob_sorted = False
501
+ other.mass_sorted = True
502
+ other.prob_sorted = False
503
+ return ret
504
+
505
+ def abyssalWassersteinDistanceGrad(self, others, scales, grad, abyss_depth_exp, abyss_depth_the):
506
+ assert len(others) + 1 == len(scales) == len(grad)
507
+ cobjs = [self._get_cobject()]
508
+ cobjs.extend(other._get_cobject() for other in others)
509
+ ret = isoFFI.clib.abyssalWassersteinDistanceGrad(cobjs, scales, grad, len(others), abyss_depth_exp, abyss_depth_the)
510
+ for cobj in cobjs:
511
+ isoFFI.clib.deleteFixedEnvelope(cobj, True)
512
+ self.mass_sorted = True
513
+ self.prob_sorted = False
514
+ for other in others:
515
+ other.mass_sorted = True
516
+ other.prob_sorted = False
517
+ return ret
518
+
519
+ def wassersteinMatch(self, other, flow_dist, other_scale = 1.0):
520
+ x = self._get_cobject()
521
+ y = other._get_cobject()
522
+ ret = isoFFI.clib.wassersteinMatch(x, y, flow_dist, other_scale)
523
+ isoFFI.clib.deleteFixedEnvelope(x, True)
524
+ isoFFI.clib.deleteFixedEnvelope(y, True)
525
+ self.mass_sorted = True
526
+ self.prob_sorted = False
527
+ other.mass_sorted = True
528
+ other.prob_sorted = False
529
+ return (ret.res1, ret.res2, ret.flow)
530
+
531
+ def binned(self, width = 1.0, middle = 0.0):
532
+ co = self._get_cobject()
533
+ cbo = isoFFI.clib.binnedEnvelope(co, width, middle)
534
+ isoFFI.clib.deleteFixedEnvelope(co, True)
535
+ ret = IsoDistribution(cobject = cbo)
536
+ isoFFI.clib.deleteFixedEnvelope(cbo, False)
537
+ self.mass_sorted = True
538
+ self.prob_sorted = False
539
+ ret.mass_sorted = True
540
+ ret.prob_sorted = False
541
+ return ret
542
+
543
+
544
+ @staticmethod
545
+ def LinearCombination(envelopes, intensities):
546
+ envelope_objs = [x._get_cobject() for x in envelopes]
547
+ if type(intensities) != list:
548
+ intensities = list(intensities)
549
+ cobject = isoFFI.clib.linearCombination(envelope_objs, intensities, len(envelope_objs))
550
+ for x in envelope_objs:
551
+ isoFFI.clib.deleteFixedEnvelope(x, True)
552
+ ret = IsoDistribution(cobject = cobject)
553
+ isoFFI.clib.deleteFixedEnvelope(cobject, False)
554
+ return ret
555
+
556
+
557
+ def plot(self, plot_type = "bars", show = True, **matplotlib_args):
558
+ """Plot the isotopic distribution.
559
+
560
+ Args:
561
+ **matplotlib_args: arguments for matplotlib plot.
562
+ """
563
+ try:
564
+ from matplotlib import pyplot as plt
565
+ except ImportError as e:
566
+ raise ImportError(str(e) + "\nPlotting spectra requires matplotlib to be installed.")
567
+ if plot_type == "bars":
568
+ if "linewidth" not in matplotlib_args:
569
+ matplotlib_args['linewidth'] = 1.0
570
+ plt.vlines(list(self.masses), [0], list(self.probs), **matplotlib_args)
571
+ elif plot_type == "profile":
572
+ self.sort_by_mass()
573
+ plt.plot(list(self.masses), list(self.probs), **matplotlib_args)
574
+ plt.xlabel("Mass (Da)")
575
+ plt.ylabel("Intensity (relative)")
576
+ if show:
577
+ plt.show()
578
+
579
+
580
+
581
+
582
+ def IsoThreshold(threshold,
583
+ formula="",
584
+ absolute=False,
585
+ get_confs=False,
586
+ **kwargs):
587
+ """Initialize the IsoDistribution isotopic distribution by threshold.
588
+
589
+ Args:
590
+ threshold (float): value of the absolute or relative threshold.
591
+ formula (str): a chemical formula, e.g. "C2H6O1" or "C2H6O".
592
+ absolute (boolean): should we report peaks with probabilities above an absolute probability threshold, or above a relative threshold amounting to a given proportion of the most probable peak?
593
+ get_confs (boolean): should we report counts of isotopologues?
594
+ **kwds: named arguments to IsoSpectrum.
595
+ """
596
+ iso = Iso(formula = formula, get_confs = get_confs, **kwargs)
597
+ tabulator = isoFFI.clib.setupThresholdFixedEnvelope(iso.iso, threshold, absolute, get_confs)
598
+ ido = IsoDistribution(cobject = tabulator, get_confs = get_confs, iso = iso)
599
+ isoFFI.clib.deleteFixedEnvelope(tabulator, False)
600
+ return ido
601
+
602
+
603
+ def IsoTotalProb(prob_to_cover,
604
+ formula="",
605
+ get_minimal_pset=True,
606
+ get_confs=False,
607
+ **kwargs):
608
+ """Initialize the IsoDistribution isotopic distribution by total probability.
609
+
610
+ Args:
611
+ prob_to_cover (float): minimal total probability of the reported peaks.
612
+ formula (str): a chemical formula, e.g. "C2H6O1" or "C2H6O".
613
+ get_minimal_pset (boolean): should we trim the last calculated layer of isotopologues so that the reported result is as small as possible?
614
+ get_confs (boolean): should we report the counts of isotopologues?
615
+ **kwargs: named arguments to the superclass.
616
+ """
617
+ iso = Iso(formula=formula, get_confs=get_confs, **kwargs)
618
+ tabulator = isoFFI.clib.setupTotalProbFixedEnvelope(iso.iso, prob_to_cover, get_minimal_pset, get_confs)
619
+ ido = IsoDistribution(cobject = tabulator, get_confs = get_confs, iso = iso)
620
+ isoFFI.clib.deleteFixedEnvelope(tabulator, False)
621
+ return ido
622
+
623
+
624
+ def IsoStochastic(no_molecules,
625
+ formula="",
626
+ precision=0.9999,
627
+ beta_bias=5.0,
628
+ get_confs=False,
629
+ **kwargs):
630
+ """Initialize the IsoDistribution isotopic distribution by total probability.
631
+
632
+ Args:
633
+ no_molecules (uint): ionic current in instrument
634
+ formula (str): a chemical formula, e.g. "C2H6O1" or "C2H6O".
635
+ precision (float): passed to IsoTotalProbGenerator. Between 0.0 and 1.0.
636
+ beta_bias (float, nonnegative): fiddling with this parameter does not change the result, but might make computations slightly faster (or likely, much, much slower is you screw it up...)
637
+ get_confs (boolean): should we report the counts of isotopologues?
638
+ **kwargs: named arguments to the superclass.
639
+ """
640
+ iso = Iso(formula=formula, get_confs=get_confs, **kwargs)
641
+ tabulator = isoFFI.clib.setupStochasticFixedEnvelope(iso.iso, no_molecules, precision, beta_bias, get_confs)
642
+ ido = IsoDistribution(cobject = tabulator, get_confs = get_confs, iso = iso)
643
+ isoFFI.clib.deleteFixedEnvelope(tabulator, False)
644
+ return ido
645
+
646
+
647
+ def IsoBinned(bin_width,
648
+ formula="",
649
+ target_total_prob=0.9999,
650
+ bin_middle=0.0,
651
+ **kwargs):
652
+ """Initialize the IsoDistribution isotopic distribution by total probability.
653
+
654
+ Args:
655
+ no_molecules (uint): ionic current in instrument
656
+ formula (str): a chemical formula, e.g. "C2H6O1" or "C2H6O".
657
+ precision (float): passed to IsoTotalProbGenerator. Between 0.0 and 1.0.
658
+ beta_bias (float, nonnegative): fiddling with this parameter does not change the result, but might make computations slightly faster (or likely, much, much slower is you screw it up...)
659
+ get_confs (boolean): should we report the counts of isotopologues?
660
+ **kwargs: named arguments to the superclass.
661
+ """
662
+ iso = Iso(formula=formula, get_confs=False, **kwargs)
663
+ tabulator = isoFFI.clib.setupBinnedFixedEnvelope(iso.iso, target_total_prob, bin_width, bin_middle)
664
+ ido = IsoDistribution(cobject = tabulator, get_confs = False, iso = iso)
665
+ isoFFI.clib.deleteFixedEnvelope(tabulator, False)
666
+ return ido
667
+
668
+
669
+ class IsoGenerator(Iso):
670
+ """Virtual class alowing memory-efficient iteration over the isotopic distribution.
671
+
672
+ This iterator will stop only after enumerating all isotopologues.
673
+ """
674
+ def __init__(self, formula="", get_confs=False, **kwargs):
675
+ """Initialize the IsoGenerator.
676
+
677
+ Args:
678
+ formula (str): a chemical formula, e.g. "C2H6O1" or "C2H6O".
679
+ get_confs (boolean): should we report the counts of isotopologues?
680
+ **kwargs: named arguments to the superclass.
681
+ """
682
+ self.cgen = None
683
+ super(IsoGenerator, self).__init__(formula=formula, get_confs=get_confs, **kwargs)
684
+ self.conf_space = isoFFI.ffi.new("int[" + str(sum(self.isotopeNumbers)) + "]")
685
+ self.firstuse = True
686
+
687
+ def __iter__(self):
688
+ if not self.firstuse:
689
+ raise NotImplementedError("Multiple iterations through the same IsoGenerator object are not supported. Either create a new (identical) generator for a second loop-through, or use one of the non-generator classes, which do support being re-used.")
690
+ self.firstuse = False
691
+ cgen = self.cgen
692
+ if self.get_confs:
693
+ while self.advancer(cgen):
694
+ self.conf_getter(cgen, self.conf_space)
695
+ yield (self.mass_getter(cgen), self.xprob_getter(cgen), self.parse_conf(self.conf_space))
696
+ else:
697
+ while self.advancer(cgen):
698
+ yield (self.mass_getter(cgen), self.xprob_getter(cgen))
699
+
700
+ def __del__(self):
701
+ super(IsoGenerator, self).__del__()
702
+
703
+
704
+ class IsoThresholdGenerator(IsoGenerator):
705
+ """Class alowing memory-efficient iteration over the isotopic distribution up till some probability threshold.
706
+
707
+ This iterator will stop only after reaching a probability threshold.
708
+ """
709
+ def __init__(self, threshold, formula="", absolute=False, get_confs=False, reorder_marginals = True, **kwargs):
710
+ """Initialize IsoThresholdGenerator.
711
+
712
+ Args:
713
+ formula (str): a chemical formula, e.g. "C2H6O1" or "C2H6O".
714
+ absolute (boolean): should we report peaks with probabilities above an absolute probability threshold, or above a relative threshold amounting to a given proportion of the most probable peak?
715
+ get_confs (boolean): should we report the counts of isotopologues?
716
+ **kwargs: named arguments to the superclass.
717
+ """
718
+ super(IsoThresholdGenerator, self).__init__(formula=formula, get_confs=get_confs, **kwargs)
719
+ self.threshold = threshold
720
+ self.absolute = absolute
721
+ self.cgen = self.ffi.setupIsoThresholdGenerator(self.iso,
722
+ threshold,
723
+ absolute,
724
+ 1000,
725
+ 1000,
726
+ reorder_marginals)
727
+ self.advancer = self.ffi.advanceToNextConfigurationIsoThresholdGenerator
728
+ self.xprob_getter = self.ffi.probIsoThresholdGenerator
729
+ self.mass_getter = self.ffi.massIsoThresholdGenerator
730
+ self.conf_getter = self.ffi.get_conf_signatureIsoThresholdGenerator
731
+
732
+ def __del__(self):
733
+ """Destructor."""
734
+ try:
735
+ if self.cgen is not None:
736
+ self.ffi.deleteIsoThresholdGenerator(self.cgen)
737
+ self.cgen = None
738
+ except AttributeError:
739
+ pass
740
+ super(IsoThresholdGenerator, self).__del__()
741
+
742
+
743
+ class IsoLayeredGenerator(IsoGenerator):
744
+ """Class alowing memory-efficient iteration over the isotopic distribution up till some joint probability of the reported peaks."""
745
+ def __init__(self, formula="", get_confs=False, reorder_marginals = True, t_prob_hint = 0.99, **kwargs):
746
+ """Initialize IsoThresholdGenerator.
747
+
748
+ Args:
749
+ formula (str): a chemical formula, e.g. "C2H6O1" or "C2H6O".
750
+ absolute (boolean): should we report peaks with probabilities above an absolute probability threshold, or above a relative threshold amounting to a given proportion of the most probable peak?
751
+ get_confs (boolean): should we report the counts of isotopologues?
752
+ **kwargs: named arguments to the superclass.
753
+ """
754
+ super(IsoLayeredGenerator, self).__init__(formula=formula, get_confs=get_confs, **kwargs)
755
+ self.cgen = self.ffi.setupIsoLayeredGenerator(self.iso, 1000, 1000, reorder_marginals, t_prob_hint)
756
+ self.advancer = self.ffi.advanceToNextConfigurationIsoLayeredGenerator
757
+ self.xprob_getter = self.ffi.probIsoLayeredGenerator
758
+ self.mass_getter = self.ffi.massIsoLayeredGenerator
759
+ self.conf_getter = self.ffi.get_conf_signatureIsoLayeredGenerator
760
+
761
+ def __del__(self):
762
+ try:
763
+ if self.cgen is not None:
764
+ self.ffi.deleteIsoLayeredGenerator(self.cgen)
765
+ self.cgen = None
766
+ except AttributeError:
767
+ pass
768
+ super(IsoLayeredGenerator, self).__del__()
769
+
770
+ class IsoOrderedGenerator(IsoGenerator):
771
+ """Class representing an isotopic distribution with peaks ordered by descending probability.
772
+
773
+ This generator return probilities ordered with descending probability.
774
+ It it not optimal to do so, but it might be useful.
775
+
776
+ WARNING! This algorithm work in O(N*log(N)) vs O(N) of the threshold and layered algorithms.
777
+ Also, the order of descending probability will most likely not reflect the order of ascending masses.
778
+ """
779
+ def __init__(self, formula="", get_confs=False, **kwargs):
780
+ """Initialize IsoOrderedGenerator.
781
+
782
+ Args:
783
+ formula (str): a chemical formula, e.g. "C2H6O1" or "C2H6O".
784
+ get_confs (boolean): should we report the counts of isotopologues?
785
+ **kwargs: named arguments to the superclass.
786
+ """
787
+ super(IsoOrderedGenerator, self).__init__(formula=formula, get_confs=get_confs, **kwargs)
788
+ self.cgen = self.ffi.setupIsoOrderedGenerator(self.iso, 1000, 1000)
789
+ self.advancer = self.ffi.advanceToNextConfigurationIsoOrderedGenerator
790
+ self.xprob_getter = self.ffi.probIsoOrderedGenerator
791
+ self.mass_getter = self.ffi.massIsoOrderedGenerator
792
+ self.conf_getter = self.ffi.get_conf_signatureIsoOrderedGenerator
793
+
794
+ def __del__(self):
795
+ try:
796
+ if self.cgen is not None:
797
+ self.ffi.deleteIsoLayeredGenerator(self.cgen)
798
+ self.cgen = None
799
+ except AttributeError:
800
+ pass
801
+ super(IsoOrderedGenerator, self).__del__()
802
+
803
+
804
+ class IsoStochasticGenerator(IsoGenerator):
805
+ def __init__(self, no_molecules, formula="", precision=0.9999, beta_bias = 1.0, get_confs=False, **kwargs):
806
+ super(IsoStochasticGenerator, self).__init__(formula=formula, get_confs=get_confs, **kwargs)
807
+ self.threshold = precision
808
+ self.no_molecules = no_molecules
809
+ self.cgen = self.ffi.setupIsoStochasticGenerator(self.iso,
810
+ no_molecules,
811
+ precision,
812
+ beta_bias)
813
+ self.advancer = self.ffi.advanceToNextConfigurationIsoStochasticGenerator
814
+ self.xprob_getter = self.ffi.probIsoStochasticGenerator
815
+ self.mass_getter = self.ffi.massIsoStochasticGenerator
816
+ self.conf_getter = self.ffi.get_conf_signatureIsoStochasticGenerator
817
+
818
+ def __del__(self):
819
+ """Destructor."""
820
+ try:
821
+ if self.cgen is not None:
822
+ self.ffi.deleteIsoStochasticGenerator(self.cgen)
823
+ self.cgen = None
824
+ except AttributeError:
825
+ pass
826
+ super(IsoStochasticGenerator, self).__del__()
827
+