Mass2Motif Module¶
Mass2Motif
¶
Mass2Motif
¶
Mass2Motif(
frag_mz: array,
frag_intensities: array,
loss_mz: array,
loss_intensities: array,
metadata: Optional[dict] = None,
metadata_harmonization: bool = True,
)
Container for a collection of peaks, losses and metadata.
Spectrum peaks are stored as :class:~matchms.Fragments
object which can be
addressed calling spectrum.peaks
and contains m/z values and the respective
peak intensities.
Spectrum metadata is stored as :class:~matchms.Metadata
object which can be
addressed by spectrum.metadata
.
Code example
.. testcode::
import numpy as np
from matchms import Scores, Spectrum
from matchms.similarity import CosineGreedy
spectrum = Spectrum(mz=np.array([100, 150, 200.]),
intensities=np.array([0.7, 0.2, 0.1]),
metadata={"id": 'spectrum1',
"precursor_mz": 222.333,
"peak_comments": {200.: "the peak at 200 m/z"}})
print(spectrum)
print(spectrum.peaks.mz[0])
print(spectrum.peaks.intensities[0])
print(spectrum.get('id'))
print(spectrum.peak_comments.get(200))
Should output
.. testoutput::
Spectrum(precursor m/z=222.33, 3 fragments between 100.0 and 200.0)
100.0
0.7
spectrum1
the peak at 200 m/z
Attributes¶
peaks: ~matchms.Fragments.Fragments Peaks of spectrum losses: ~matchms.Fragments.Fragments or None Losses of spectrum, the difference between the precursor and all peaks.
Can be filled with
.. code-block ::
from matchms import Fragments
spectrum.losess = Fragments(mz=np.array([50.]), intensities=np.array([0.1]))
metadata: dict Dict of metadata with for example the scan number of precursor m/z.
Parameters¶
mz Array of m/z for the peaks intensities Array of intensities for the peaks metadata Dictionary with for example the scan number of precursor m/z. metadata_harmonization : bool, optional Set to False if default metadata filters should not be applied. The default is True.
Source code in MS2LDA/Mass2Motif.py
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 |
|
__hash__
¶
__hash__()
Return a integer hash which is computed from both metadata (see .metadata_hash() method) and spectrum peaks (see .spectrum_hash() method).
Source code in MS2LDA/Mass2Motif.py
107 108 109 110 111 112 |
|
clone
¶
clone()
Return a deepcopy of the spectrum instance.
Source code in MS2LDA/Mass2Motif.py
138 139 140 141 142 143 144 145 146 147 148 |
|
get
¶
get(key: str, default=None)
Retrieve value from :attr:metadata
dict. Shorthand for
.. code-block:: python
val = self.metadata[key]
Source code in MS2LDA/Mass2Motif.py
179 180 181 182 183 184 185 186 187 |
|
metadata_dict
¶
metadata_dict(export_style: str = 'matchms') -> dict
Convert spectrum metadata to Python dictionary.
Parameters¶
export_style: Converts the keys to the required export style. One of ["matchms", "massbank", "nist", "riken", "gnps"]. Default is "matchms"
Source code in MS2LDA/Mass2Motif.py
216 217 218 219 220 221 222 223 224 225 |
|
metadata_hash
¶
metadata_hash()
Return a (truncated) sha256-based hash which is generated based on the spectrum metadata. Spectra with same metadata results in same metadata_hash.
Source code in MS2LDA/Mass2Motif.py
132 133 134 135 136 |
|
plot
¶
plot(figsize=(8, 6), dpi=200, **kwargs)
Plot to visually inspect a spectrum run spectrum.plot()
.. figure:: ../_static/spectrum-plot-example.png :width: 450 :alt: spectrum plotting function
Example of a spectrum plotted using ``spectrum.plot()`` ..
Source code in MS2LDA/Mass2Motif.py
150 151 152 153 154 155 156 157 158 159 160 161 |
|
plot_against
¶
plot_against(other_spectrum, figsize=(8, 6), dpi=200, **spectrum_kws)
Compare two spectra visually in a mirror plot.
To visually compare the peaks of two spectra run
spectrum.plot_against(other_spectrum)
.. figure:: ../_static/spectrum-mirror-plot-example.png :width: 450 :alt: spectrum mirror plot function
Example of a mirror plot comparing two spectra ``spectrum.plot_against()`` ..
Source code in MS2LDA/Mass2Motif.py
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
|
set
¶
set(key: str, value)
Set value in :attr:metadata
dict. Shorthand for
.. code-block:: python
self.metadata[key] = val
Source code in MS2LDA/Mass2Motif.py
189 190 191 192 193 194 195 196 197 198 |
|
spectrum_hash
¶
spectrum_hash()
Return a (truncated) sha256-based hash which is generated based on the spectrum peaks (mz:intensity pairs). Spectra with same peaks will results in same spectrum_hash.
Source code in MS2LDA/Mass2Motif.py
126 127 128 129 130 |
|
to_dict
¶
to_dict(export_style: str = 'matchms') -> dict
Return a dictionary representation of a spectrum.
Parameters¶
export_style: Converts the keys to the required export style. One of ["matchms", "massbank", "nist", "riken", "gnps"]. Default is "matchms"
Source code in MS2LDA/Mass2Motif.py
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
|
update_peak_comments_mz_tolerance
classmethod
¶
update_peak_comments_mz_tolerance(mz_tolerance: float)
Change current peak comment m/z tolerance to mz_tolerance.
Source code in MS2LDA/Mass2Motif.py
265 266 267 268 |
|
Mass2MotifDocument
¶
Mass2MotifDocument
¶
Mass2MotifDocument(spectrum, n_decimals: int = 2)
Bases: SpectrumDocument
Create documents from spectra.
Every peak (and loss) positions (m/z value) will be converted into a string "word". The entire list of all peak words forms a spectrum document. Peak words have the form "peak@100.32" (for n_decimals=2), and losses have the format "loss@100.32". Peaks with identical resulting strings will not be merged, hence same words can exist multiple times in a document (e.g. peaks at 100.31 and 100.29 would lead to two words "peak@100.3" when using n_decimals=1).
For example:
.. testcode::
import numpy as np
from matchms import Spectrum
from spec2vec import SpectrumDocument
spectrum = Spectrum(mz=np.array([100.0, 150.0, 200.51]),
intensities=np.array([0.7, 0.2, 0.1]),
metadata={'compound_name': 'substance1'})
spectrum_document = SpectrumDocument(spectrum, n_decimals=1)
print(spectrum_document.words)
print(spectrum_document.peaks.mz)
print(spectrum_document.get("compound_name"))
Should output
.. testoutput::
['peak@100.0', 'peak@150.0', 'peak@200.5']
[100. 150. 200.51]
substance1
Parameters¶
spectrum: SpectrumType Input spectrum. n_decimals Peak positions are converted to strings with n_decimal decimals. The default is 2, which would convert a peak at 100.387 into the word "peak@100.39".
Source code in MS2LDA/Mass2MotifDocument.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
|
get
¶
get(key: str, default=None)
Retrieve value from Spectrum metadata dict. Shorthand for
.. code-block:: python
val = self._obj.metadata[key]
Source code in MS2LDA/Mass2MotifDocument.py
90 91 92 93 94 95 96 97 98 99 100 101 |
|