API Reference¶
PyMassSpec extension for searching mass spectra using NIST’s Mass Spectrum Search Engine.
base¶
Base class for other PyMassSpec NIST Search classes.
- class NISTBase(name='', cas='---')[source]¶
Bases:
objectBase class for other PyMassSpec NIST Search classes.
- Parameters
Methods:
__eq__(other)Return
self == other.__str__()Return
str(self).from_dict(dictionary)Construct an object from a dictionary.
from_json(json_data)Construct an object from json data.
from_pynist(pynist_dict)Create an object from the raw data returned by the C extension.
to_dict()Convert the object to a dictionary.
to_json()Convert the object to json.
Attributes:
- classmethod from_json(json_data)[source]¶
Construct an object from json data.
- Parameters
json_data (
str)
- classmethod from_pynist(pynist_dict)[source]¶
Create an object from the raw data returned by the C extension.
docker_engine¶
Search engine for Linux and other platforms supporting Docker.
Classes:
|
Search engine for Linux and other platforms supporting Docker. |
Functions:
|
Parse json data into a list of SearchResult objects. |
|
Parse json data into a list of (SearchResult, ReferenceData) tuples. |
|
Decorator to ensure that functions do not run after the class has been uninitialised. |
- class Engine(lib_path, lib_type=1, work_dir=None, debug=False)[source]¶
Bases:
objectSearch engine for Linux and other platforms supporting Docker.
The first time the engine is initialized it will download the latest version of the docker image automatically. This can also be performed manually, such as to upgrade to the latest version, with the following command:
docker pull domdfcoding/pywine-pyms-nistThe engine must be uninitialized when no longer required to shut down the underlying docker container. This is achieved with the
uninit()method. Alternatively, this class can be used as a contextmanager to automatically uninitialize the engine upon leaving thewithblock:with pyms_nist_search.Engine( FULL_PATH_TO_MAIN_LIBRARY, pyms_nist_search.NISTMS_MAIN_LIB, FULL_PATH_TO_WORK_DIR, ) as search: search.full_spectrum_search(ms, n_hits=5)
Changed in version 0.6.0: Added context manager support.
- Parameters
lib_path (
Union[str,Path,PathLike,Sequence[Tuple[Union[str,Path,PathLike],int]]]) – The path to the mass spectral library.lib_type (
int) – The type of library. One ofNISTMS_MAIN_LIB,NISTMS_USER_LIB,NISTMS_REP_LIB. Default1.work_dir (
Union[str,Path,PathLike,None]) – The path to the working directory. DefaultNone.
Methods:
cas_search(cas)Search for a compound by CAS number.
full_search_with_ref_data(mass_spec[, n_hits])Perform a Full Spectrum Search of the mass spectral library, including reference data.
full_spectrum_search(mass_spec[, n_hits])Perform a Full Spectrum Search of the mass spectral library.
Returns the active librararies, as their (zero-based) indices in the output of :meth:~.WinEngine.get_lib_names()`.
Returns the list of library names currently in use.
get_reference_data(spec_loc)Get reference data from the library for the compound at the given location.
spectrum_search(mass_spec[, n_hits])Perform a Quick Spectrum Search of the mass spectral library.
uninit()Uninitialize the Search Engine.
Attributes:
The name (and label) of the docker image to use.
- static cas_search(cas)[source]¶
Search for a compound by CAS number.
Note
This function does not appear to work with user libraries converted using LIB2NIST.
- Parameters
cas (
str)- Return type
- Returns
List of results for CAS number (usually just one result).
- full_search_with_ref_data(mass_spec, n_hits=5)[source]¶
Perform a Full Spectrum Search of the mass spectral library, including reference data.
- Parameters
mass_spec (
MassSpectrum) – The mass spectrum to search against the library.n_hits (
int) – The number of hits to return. Default5.
- Return type
- Returns
List of tuples containing possible identities for the mass spectrum, and the reference data.
- full_spectrum_search(mass_spec, n_hits=5)[source]¶
Perform a Full Spectrum Search of the mass spectral library.
- Parameters
mass_spec (
MassSpectrum) – The mass spectrum to search against the library.n_hits (
int) – The number of hits to return. Default5.
- Return type
- Returns
List of possible identities for the mass spectrum.
- get_active_libs()[source]¶
Returns the active librararies, as their (zero-based) indices in the output of :meth:~.WinEngine.get_lib_names()`.
- get_reference_data(spec_loc)[source]¶
Get reference data from the library for the compound at the given location.
- Parameters
spec_loc (
int)- Return type
- image_name = 'domdfcoding/pywine-pyms-nist:latest'¶
Type:
strThe name (and label) of the docker image to use.
New in version 0.8.0.
- spectrum_search(mass_spec, n_hits=5)[source]¶
Perform a Quick Spectrum Search of the mass spectral library.
- Parameters
mass_spec (
MassSpectrum) – The mass spectrum to search against the library.n_hits (
int) – The number of hits to return. Default5.
- Return type
- Returns
List of possible identities for the mass spectrum.
- hit_list_from_json(json_data)[source]¶
Parse json data into a list of SearchResult objects.
- Parameters
json_data (
str) – str- Return type
reference_data¶
Class to store reference data from NIST MS Search.
- class ReferenceData(name='', cas='---', nist_no=0, id='', mw=0.0, formula='', contributor='', mass_spec=None, synonyms=None, exact_mass=None, lib_idx=0)[source]¶
Bases:
NISTBaseClass to store reference data from NIST MS Search.
- Parameters
name (
str) – The name of the compound. Default''.cas (
Union[str,int]) – The CAS number of the compound. Default'---'.formula (
str) – The formula of the compound. Default''.contributor (
str) – The contributor to the library. Default''.mass_spec (
Optional[MassSpectrum]) – The reference mass spectrum. DefaultNone.synonyms (
Optional[Sequence[str]]) – List of synonyms for the compound. DefaultNone.lib_idx (
int) – The (zero-based) index of the library the result was found in (seeget_lib_names()). Default0.
Methods:
__repr__()Return a string representation of the
ReferenceData.from_jcamp(file_name[, ignore_warnings])Create a ReferenceData object from a JCAMP-DX file.
from_json(json_data)Construct an object from JSON data.
from_mona_dict(mona_data)Construct an object from Massbank of North America json data that has been loaded into a dictionary.
from_pynist(pynist_dict)Create a
ReferenceDataobject from the raw data returned by the C extension.to_dict()Convert the object to a dictionary.
to_json()Convert the object to JSON.
to_msp()Returns the ReferenceData object as an MSP file similar to that produced by NIST MS Search's export function.
Attributes:
The name of the contributor to the library.
The exact mass of the compound (not used).
The formula of the compound.
The ID of the compound.
The (zero-based) index of the library the result was found in (see
get_lib_names()).The mass spectrum of the compound.
The molecular weight of the compound.
The NIST number of the compund.
A list of synonyms for the compound.
- __repr__()[source]¶
Return a string representation of the
ReferenceData.- Return type
- classmethod from_jcamp(file_name, ignore_warnings=True)[source]¶
Create a ReferenceData object from a JCAMP-DX file.
- classmethod from_json(json_data)[source]¶
Construct an object from JSON data.
- Parameters
json_data (
str)- Return type
- classmethod from_mona_dict(mona_data)[source]¶
Construct an object from Massbank of North America json data that has been loaded into a dictionary.
- Parameters
mona_data (
Dict) – dict- Return type
- classmethod from_pynist(pynist_dict)[source]¶
Create a
ReferenceDataobject from the raw data returned by the C extension.- Parameters
- Return type
- property lib_idx¶
The (zero-based) index of the library the result was found in (see
get_lib_names()).- Return type
- property mass_spec¶
The mass spectrum of the compound.
- Return type
search_result¶
Class to store search results from NIST MS Search.
- class SearchResult(name='', cas='---', match_factor=0, reverse_match_factor=0, hit_prob=0.0, spec_loc=0, lib_idx=0)[source]¶
Bases:
NISTBaseClass to store search results from NIST MS Search.
- Parameters
name (
str) – The name of the compound. Default''.cas (
Union[str,int]) – The CAS number of the compound. Default'---'.match_factor (
float) – Default0.reverse_match_factor (
float) – Default0.hit_prob (
float) – Default0.0.spec_loc (
float) – The location of the reference spectrum in the library. Default0.lib_idx (
int) – The (zero-based) index of the library the result was found in (seeget_lib_names()). Default0.
Methods:
from_pynist(pynist_dict)Create a
SearchResultobject from the raw data returned by the C extension.to_dict()Convert the object to a dictionary.
Attributes:
Returns the probability of the hit being the compound responsible for the mass spectrum.
The (zero-based) index of the library the result was found in (see
get_lib_names()).Returns a score (out of 1000) representing the similarity of the searched mass spectrum to the search result.
A score (out of 1000) representing the similarity of the searched mass spectrum to the search result, but ignoring any peaks that are in the searched mass spectrum but not in the library spectrum.
The location of the reference spectrum in the library.
- classmethod from_pynist(pynist_dict)[source]¶
Create a
SearchResultobject from the raw data returned by the C extension.- Parameters
- Return type
- property hit_prob¶
Returns the probability of the hit being the compound responsible for the mass spectrum.
- Return type
- property lib_idx¶
The (zero-based) index of the library the result was found in (see
get_lib_names()).- Return type
- property match_factor¶
Returns a score (out of 1000) representing the similarity of the searched mass spectrum to the search result.
- Return type
- property reverse_match_factor¶
A score (out of 1000) representing the similarity of the searched mass spectrum to the search result, but ignoring any peaks that are in the searched mass spectrum but not in the library spectrum.
- Return type
- property spec_loc¶
The location of the reference spectrum in the library.
This can then be searched using the
get_reference_data()method of the search engine to obtain the reference data.- Return type
utils¶
General utilities.
Functions:
|
Given the path to a mass spectral library, returns the library name (the final path component). |
|
Convert a pyms.Spectrum.MassSpectrum object into a string. |
|
Takes a list of Unicode character codes and converts them to characters, taking into account the special codes used by the NIST DLL. |
- lib_name_from_path(lib_path)[source]¶
Given the path to a mass spectral library, returns the library name (the final path component).
- Return type
- pack(mass_spec, top=20)[source]¶
Convert a pyms.Spectrum.MassSpectrum object into a string.
Adapted from https://sourceforge.net/projects/mzapi-live/
- Parameters
mass_spec (
MassSpectrum)top (
int) – The number of largest peaks to identify. Default20.
- Return type
win_engine¶
Search engine for Windows systems.
- class Engine(lib_path, lib_type=1, work_dir=None, debug=False)[source]¶
Bases:
objectSearch engine for Windows systems.
- Parameters
lib_path (
Union[str,Path,PathLike,Sequence[Tuple[Union[str,Path,PathLike],int]]]) – The path to the mass spectral library.lib_type (
int) – The type of library. One ofNISTMS_MAIN_LIB,NISTMS_USER_LIB,NISTMS_REP_LIB. Default1.work_dir (
Union[str,Path,PathLike,None]) – The path to the working directory. DefaultNone.
Methods:
cas_search(cas)Search for a compound by CAS number.
full_search_with_ref_data(mass_spec[, n_hits])Perform a Full Spectrum Search of the mass spectral library, including reference data.
full_spectrum_search(mass_spec[, n_hits])Perform a Full Spectrum Search of the mass spectral library.
Returns the active librararies, as their (zero-based) indices in the output of
get_lib_names().Returns the list of library names currently in use.
get_reference_data(spec_loc)Get reference data from the library for the compound at the given location.
spectrum_search(mass_spec[, n_hits])Perform a Quick Spectrum Search of the mass spectral library.
uninit()Uninitialize the Search Engine.
- static cas_search(cas)[source]¶
Search for a compound by CAS number.
Note
This function does not appear to work with user libraries converted using LIB2NIST.
- Parameters
cas (
str)- Return type
- Returns
List of results for CAS number (usually just one result).
- full_search_with_ref_data(mass_spec, n_hits=5)[source]¶
Perform a Full Spectrum Search of the mass spectral library, including reference data.
- Parameters
mass_spec (
MassSpectrum) – The mass spectrum to search against the library.n_hits (
int) – The number of hits to return. Default5.
- Return type
- Returns
List of tuples containing possible identities for the mass spectrum, and the reference data
- full_spectrum_search(mass_spec, n_hits=5)[source]¶
Perform a Full Spectrum Search of the mass spectral library.
- Parameters
mass_spec (
MassSpectrum) – The mass spectrum to search against the library.n_hits (
int) – The number of hits to return. Default5.
- Return type
- Returns
List of possible identities for the mass spectrum.
- static get_active_libs()[source]¶
Returns the active librararies, as their (zero-based) indices in the output of
get_lib_names().
- static get_reference_data(spec_loc)[source]¶
Get reference data from the library for the compound at the given location.
- Parameters
spec_loc (
int)- Return type
- static spectrum_search(mass_spec, n_hits=5)[source]¶
Perform a Quick Spectrum Search of the mass spectral library.
- Parameters
mass_spec (
MassSpectrum) – The mass spectrum to search against the library.n_hits (
int) – The number of hits to return. Default5.
- Return type
- Returns
List of possible identities for the mass spectrum.