protxml - parsing of ProteinProphet output files¶
Summary¶
protXML is the output format of the ProteinProphet software. It contains information about identified proteins and their statistical significance.
This module provides minimalistic infrastructure for access to data stored in
protXML files. The central class is ProtXML
, which
reads protein entries and related information and saves them into
Python dicts.
Data access¶
ProtXML
- a class representing a single protXML file. Other data access functions use this class internally.
read()
- iterate through peptide-spectrum matches in a protXML file. Calling the function is synonymous to instantiating theProtXML
class.
chain()
- read multiple files at once.
chain.from_iterable()
- read multiple files at once, using an iterable of files.
DataFrame()
- read protXML files into apandas.DataFrame
.
Target-decoy approach¶
filter()
- filter protein groups from a chain of protXML files to a specific FDR using TDA.
filter.chain()
- chain a series of filters applied independently to several files.
filter.chain.from_iterable()
- chain a series of filters applied independently to an iterable of files.
filter_df()
- filter protXML files and return apandas.DataFrame
.
fdr()
- estimate the false discovery rate of a set of protein groups using the target-decoy approach.
qvalues()
- get an array of scores and q values for protein groups using the target-decoy approach.
is_decoy()
- determine whether a protein group is decoy or not. This function may not suit your use case.
Dependencies¶
This module requres lxml
.
- pyteomics.protxml.chain(*sources, **kwargs)¶
Chain
ProtXML
for several sources into a single iterable. Positional arguments should be sources like file names or file objects. Keyword arguments are passed to theProtXML
function.- Parameters:
sources (
Iterable
) – Sources for creating new sequences from, such as paths or file-like objectskwargs (
Mapping
) – Additional arguments used to instantiate each sequence
- chain.from_iterable(files, **kwargs)¶
Chain
read()
for several files. Keyword arguments are passed to theread()
function.- Parameters:
files – Iterable of file names or file objects.
- pyteomics.protxml.filter(*args, **kwargs)¶
Read args and yield only the PSMs that form a set with estimated false discovery rate (FDR) not exceeding fdr.
Requires
numpy
and, optionally,pandas
.- Parameters:
args (positional) – Files to read PSMs from. All positional arguments are treated as files. The rest of the arguments must be named.
fdr (float, keyword only, 0 <= fdr <= 1) – Desired FDR level.
key (callable / array-like / iterable / str, keyword only, optional) –
A function used for sorting of PSMs. Should accept exactly one argument (PSM) and return a number (the smaller the better). The default is a function that tries to extract e-value from the PSM.
Warning
The default function may not work with your files, because format flavours are diverse.
reverse (bool, keyword only, optional) – If
True
, then PSMs are sorted in descending order, i.e. the value of the key function is higher for better PSMs. Default isFalse
.is_decoy (callable / array-like / iterable / str, keyword only, optional) –
A function used to determine if the PSM is decoy or not. Should accept exactly one argument (PSM) and return a truthy value if the PSM should be considered decoy.
Warning
The default function may not work with your files, because format flavours are diverse.
decoy_prefix (str, optional) – If the default is_decoy function works for you, this parameter specifies which protein name prefix to use to detect decoy matches. If you provide your own is_decoy, or if you specify decoy_suffix, this parameter has no effect. Default is “DECOY_”.
decoy_suffix (str, optional) – If the default is_decoy function works for you, this parameter specifies which protein name suffix to use to detect decoy matches. If you provide your own is_decoy, this parameter has no effect. Mutually exclusive with decoy_prefix.
remove_decoy (bool, keyword only, optional) –
Defines whether decoy matches should be removed from the output. Default is
True
.Note
If set to
False
, then by default the decoy PSMs will be taken into account when estimating FDR. Refer to the documentation offdr()
for math; basically, if remove_decoy isTrue
, then formula 1 is used to control output FDR, otherwise it’s formula 2. This can be changed by overriding the formula argument.formula (int, keyword only, optional) – Can be either 1 or 2, defines which formula should be used for FDR estimation. Default is 1 if remove_decoy is
True
, else 2 (seefdr()
for definitions).ratio (float, keyword only, optional) – The size ratio between the decoy and target databases. Default is 1. In theory, the “size” of the database is the number of theoretical peptides eligible for assignment to spectra that are produced by in silico cleavage of that database.
correction (int or float, keyword only, optional) –
Possible values are 0, 1 and 2, or floating point numbers between 0 and 1.
0 (default): no correction;
1: enable “+1” correction. This accounts for the probability that a false positive scores better than the first excluded decoy PSM;
2: this also corrects that probability for finite size of the sample, so the correction will be slightly less than “+1”.
If a floating point number is given, then instead of the expectation value for the number of false PSMs, the confidence value is used. The value of correction is then interpreted as desired confidence level. E.g., if correction=0.95, then the calculated q-values do not exceed the “real” q-values with 95% probability.
See this paper for further explanation.
pep (callable / array-like / iterable / str, keyword only, optional) –
If callable, a function used to determine the posterior error probability (PEP). Should accept exactly one argument (PSM) and return a float. If array-like, should contain float values for all given PSMs. If string, it is used as a field name (PSMs must be in a record array or a
DataFrame
).Note
If this parameter is given, then PEP values will be used to calculate q-values. Otherwise, decoy PSMs will be used instead. This option conflicts with: is_decoy, remove_decoy, formula, ratio, correction. key can still be provided. Without key, PSMs will be sorted by PEP.
full_output (bool, keyword only, optional) –
If
True
, then an array of PSM objects is returned. Otherwise, an iterator / context manager object is returned, and the files are parsed twice. This saves some RAM, but is ~2x slower. Default isTrue
.Note
The name for the parameter comes from the fact that it is internally passed to
qvalues()
.q_label (str, optional) – Field name for q-value in the output. Default is
'q'
.score_label (str, optional) – Field name for score in the output. Default is
'score'
.decoy_label (str, optional) – Field name for the decoy flag in the output. Default is
'is decoy'
.pep_label (str, optional) – Field name for PEP in the output. Default is
'PEP'
.**kwargs (passed to the
chain()
function.)
- Returns:
out
- Return type:
iterator or
numpy.ndarray
orpandas.DataFrame
- filter.chain(*files, **kwargs)¶
Chain
filter()
for several files. Positional arguments should be file names or file objects. Keyword arguments are passed to thefilter()
function.
- filter.chain.from_iterable(*files, **kwargs)¶
Chain
filter()
for several files. Keyword arguments are passed to thefilter()
function.- Parameters:
files – Iterable of file names or file objects.
- pyteomics.protxml.fdr(psms=None, formula=1, is_decoy=None, ratio=1, correction=0, pep=None, decoy_prefix='DECOY_', decoy_suffix=None)¶
Estimate FDR of a data set using TDA or given PEP values. Two formulas can be used. The first one (default) is:
\[FDR = \frac{N_{decoy}}{N_{target} * ratio}\]The second formula is:
\[FDR = \frac{N_{decoy} * (1 + \frac{1}{ratio})}{N_{total}}\]Note
This function is less versatile than
qvalues()
. To obtain FDR, you can callqvalues()
and take the last q-value. This function can be used (with correction = 0 or 1) whennumpy
is not available.- Parameters:
psms (iterable, optional) – An iterable of PSMs, e.g. as returned by
read()
. Not needed if is_decoy is an iterable.formula (int, optional) – Can be either 1 or 2, defines which formula should be used for FDR estimation. Default is 1.
is_decoy (callable, iterable, or str, optional) –
If callable, should accept exactly one argument (PSM) and return a truthy value if the PSM is considered decoy. Default is
is_decoy()
. If array-like, should contain float values for all given PSMs. If string, it is used as a field name (PSMs must be in a record array or apandas.DataFrame
).Warning
The default function may not work with your files, because format flavours are diverse.
decoy_prefix (str, optional) – If the default is_decoy function works for you, this parameter specifies which protein name prefix to use to detect decoy matches. If you provide your own is_decoy, or if you specify decoy_suffix, this parameter has no effect. Default is “DECOY_”.
decoy_suffix (str, optional) – If the default is_decoy function works for you, this parameter specifies which protein name suffix to use to detect decoy matches. If you provide your own is_decoy, this parameter has no effect. Mutually exclusive with decoy_prefix.
pep (callable, iterable, or str, optional) –
If callable, a function used to determine the posterior error probability (PEP). Should accept exactly one argument (PSM) and return a float. If array-like, should contain float values for all given PSMs. If string, it is used as a field name (PSMs must be in a record array or a
pandas.DataFrame
).Note
If this parameter is given, then PEP values will be used to calculate FDR. Otherwise, decoy PSMs will be used instead. This option conflicts with: is_decoy, formula, ratio, correction.
ratio (float, optional) – The size ratio between the decoy and target databases. Default is 1. In theory, the “size” of the database is the number of theoretical peptides eligible for assignment to spectra that are produced by in silico cleavage of that database.
correction (int or float, optional) –
Possible values are 0, 1 and 2, or floating point numbers between 0 and 1.
0 (default): no correction;
1: enable “+1” correction. This accounts for the probability that a false positive scores better than the first excluded decoy PSM;
2: this also corrects that probability for finite size of the sample, so the correction will be slightly less than “+1”.
If a floating point number is given, then instead of the expectation value for the number of false PSMs, the confidence value is used. The value of correction is then interpreted as desired confidence level. E.g., if correction=0.95, then the calculated q-values do not exceed the “real” q-values with 95% probability.
See this paper for further explanation.
Note
Requires
numpy
, if correction is a float or 2.Note
Correction is only needed if the PSM set at hand was obtained using TDA filtering based on decoy counting (as done by using
filter()
without correction).
- Returns:
out – The estimation of FDR, (roughly) between 0 and 1.
- Return type:
- pyteomics.protxml.qvalues(*args, **kwargs)¶
Read args and return a NumPy array with scores and q-values. q-values are calculated either using TDA or based on provided values of PEP.
Requires
numpy
(and optionallypandas
).- Parameters:
args (positional) – Files to read PSMs from. All positional arguments are treated as files. The rest of the arguments must be named.
key (callable / array-like / iterable / str, keyword only, optional) –
If callable, a function used for sorting of PSMs. Should accept exactly one argument (PSM) and return a number (the smaller the better). If array-like, should contain scores for all given PSMs. If string, it is used as a field name (PSMs must be in a record array or a
DataFrame
).Warning
The default function may not work with your files, because format flavours are diverse.
reverse (bool, keyword only, optional) – If
True
, then PSMs are sorted in descending order, i.e. the value of the key function is higher for better PSMs. Default isFalse
.is_decoy (callable / array-like / iterable / str, keyword only, optional) –
If callable, a function used to determine if the PSM is decoy or not. Should accept exactly one argument (PSM) and return a truthy value if the PSM should be considered decoy. If array-like, should contain boolean values for all given PSMs. If string, it is used as a field name (PSMs must be in a record array or a
DataFrame
).Warning
The default function may not work with your files, because format flavours are diverse.
decoy_prefix (str, optional) – If the default is_decoy function works for you, this parameter specifies which protein name prefix to use to detect decoy matches. If you provide your own is_decoy, or if you specify decoy_suffix, this parameter has no effect. Default is “DECOY_”.
decoy_suffix (str, optional) – If the default is_decoy function works for you, this parameter specifies which protein name suffix to use to detect decoy matches. If you provide your own is_decoy, this parameter has no effect. Mutually exclusive with decoy_prefix.
pep (callable / array-like / iterable / str, keyword only, optional) –
If callable, a function used to determine the posterior error probability (PEP). Should accept exactly one argument (PSM) and return a float. If array-like, should contain float values for all given PSMs. If string, it is used as a field name (PSMs must be in a record array or a
DataFrame
).Note
If this parameter is given, then PEP values will be used to calculate q-values. Otherwise, decoy PSMs will be used instead. This option conflicts with: is_decoy, remove_decoy, formula, ratio, correction. key can still be provided. Without key, PSMs will be sorted by PEP.
remove_decoy (bool, keyword only, optional) –
Defines whether decoy matches should be removed from the output. Default is
False
.Note
If set to
False
, then by default the decoy PSMs will be taken into account when estimating FDR. Refer to the documentation offdr()
for math; basically, if remove_decoy isTrue
, then formula 1 is used to control output FDR, otherwise it’s formula 2. This can be changed by overriding the formula argument.formula (int, keyword only, optional) – Can be either 1 or 2, defines which formula should be used for FDR estimation. Default is 1 if remove_decoy is
True
, else 2 (seefdr()
for definitions).ratio (float, keyword only, optional) – The size ratio between the decoy and target databases. Default is 1. In theory, the “size” of the database is the number of theoretical peptides eligible for assignment to spectra that are produced by in silico cleavage of that database.
correction (int or float, keyword only, optional) –
Possible values are 0, 1 and 2, or floating point numbers between 0 and 1.
0 (default): no correction;
1: enable “+1” correction. This accounts for the probability that a false positive scores better than the first excluded decoy PSM;
2: this also corrects that probability for finite size of the sample, so the correction will be slightly less than “+1”.
If a floating point number is given, then instead of the expectation value for the number of false PSMs, the confidence value is used. The value of correction is then interpreted as desired confidence level. E.g., if correction=0.95, then the calculated q-values do not exceed the “real” q-values with 95% probability.
See this paper for further explanation.
q_label (str, optional) – Field name for q-value in the output. Default is
'q'
.score_label (str, optional) – Field name for score in the output. Default is
'score'
.decoy_label (str, optional) – Field name for the decoy flag in the output. Default is
'is decoy'
.pep_label (str, optional) – Field name for PEP in the output. Default is
'PEP'
.full_output (bool, keyword only, optional) – If
True
, then the returned array has PSM objects along with scores and q-values. Default isFalse
.**kwargs (passed to the
chain()
function.)
- Returns:
out – A sorted array of records with the following fields:
’score’:
np.float64
’is decoy’:
np.bool_
’q’:
np.float64
’psm’:
np.object_
(if full_output isTrue
)
- Return type:
numpy.ndarray
- pyteomics.protxml.DataFrame(*args, **kwargs)[source]¶
Read protXML output files into a
pandas.DataFrame
.Note
Rows in the DataFrame correspond to individual proteins, not protein groups.
Requires
pandas
.- Parameters:
sep (str or None, keyword only, optional) – Some values related to protein groups are variable-length lists. If sep is a
str
, they will be packed into single string using this delimiter. If sep isNone
, they are kept as lists. Default isNone
.pd_kwargs (dict, optional) – Keyword arguments passed to the
pandas.DataFrame
constructor.*args – Passed to
chain()
.**kwargs – Passed to
chain()
.
- Returns:
out
- Return type:
pandas.DataFrame
- class pyteomics.protxml.ProtXML(source, read_schema=False, iterative=True, build_id_cache=False, use_index=None, *args, **kwargs)[source]¶
Bases:
MultiProcessingXML
Parser class for protXML files.
- __init__(source, read_schema=False, iterative=True, build_id_cache=False, use_index=None, *args, **kwargs)¶
Create an indexed XML parser object.
- Parameters:
source (str or file) – File name or file-like object corresponding to an XML file.
read_schema (bool, optional) – Defines whether schema file referenced in the file header should be used to extract information about value conversion. Default is
False
.iterative (bool, optional) – Defines whether an
ElementTree
object should be constructed and stored on the instance or if iterative parsing should be used instead. Iterative parsing keeps the memory usage low for large XML files. Default isTrue
.use_index (bool, optional) – Defines whether an index of byte offsets needs to be created for elements listed in indexed_tags. This is useful for random access to spectra in mzML or elements of mzIdentML files, or for iterative parsing of mzIdentML with
retrieve_refs=True
. IfTrue
, build_id_cache is ignored. IfFalse
, the object acts exactly likeXML
. Default isTrue
.indexed_tags (container of bytes, optional) – If use_index is
True
, elements listed in this parameter will be indexed. Empty set by default.
- build_byte_index()¶
Build up an index of offsets for elements.
- Returns:
out
- Return type:
- build_id_cache()¶
Construct a cache for each element in the document, indexed by id attribute
- build_tree()¶
Build and store the
ElementTree
instance for the underlying file
- clear_id_cache()¶
Clear the element ID cache
- clear_tree()¶
Remove the saved
ElementTree
.
- get_by_id(elem_id, id_key=None, element_type=None, **kwargs)¶
Retrieve the requested entity by its id. If the entity is a spectrum described in the offset index, it will be retrieved by immediately seeking to the starting position of the entry, otherwise falling back to parsing from the start of the file.
- iterfind(path, **kwargs)¶
Parse the XML and yield info on elements with specified local name or by specified “XPath”.
- Parameters:
path (str) – Element name or XPath-like expression. The path is very close to full XPath syntax, but local names should be used for all elements in the path. They will be substituted with local-name() checks, up to the (first) predicate. The path can be absolute or “free”. Please don’t specify namespaces.
**kwargs (passed to
self._get_info_smart()
.)
- Returns:
out
- Return type:
iterator
- map(target=None, processes=-1, args=None, kwargs=None, **_kwargs)¶
Execute the
target
function over entries of this object across up toprocesses
processes.Results will be returned out of order.
- Parameters:
target (
Callable
, optional) – The function to execute over each entry. It will be given a single object yielded by the wrapped iterator as well as all of the values inargs
andkwargs
processes (int, optional) – The number of worker processes to use. If 0 or negative, defaults to the number of available CPUs. This parameter can also be set at reader creation.
args (
Sequence
, optional) – Additional positional arguments to be passed to the target functionkwargs (
Mapping
, optional) – Additional keyword arguments to be passed to the target function**_kwargs – Additional keyword arguments to be passed to the target function
- Yields:
object – The work item returned by the target function.
- reset()¶
Resets the iterator to its initial state.
- pyteomics.protxml.filter_df(*args, **kwargs)[source]¶
Read protXML files or DataFrames and return a
DataFrame
with filtered PSMs. Positional arguments can be protXML files or DataFrames.Note
Rows in the DataFrame correspond to individual proteins, not protein groups.
Requires
pandas
.- Parameters:
key (str / iterable / callable, keyword only, optional) – Default is ‘probability’.
is_decoy (str / iterable / callable, keyword only, optional) – Default is to check that “protein_name” starts with ‘DECOY_’.
reverse (bool, keyword only, optional) – Should be
True
if higher score is better. Default isTrue
(because the default key is ‘probability’).*args – Passed to
auxiliary.filter()
and/orDataFrame()
.**kwargs – Passed to
auxiliary.filter()
and/orDataFrame()
.
- Returns:
out
- Return type:
pandas.DataFrame
- pyteomics.protxml.is_decoy(pg, prefix='DECOY_')¶
Determine if a protein group should be considered decoy.
This function checks that all protein names in a group start with prefix. You may need to provide your own function for correct filtering and FDR estimation.
- pyteomics.protxml.read(source, read_schema=False, iterative=True, **kwargs)[source]¶
Parse source and iterate through protein groups.
- Parameters:
source (str or file) – A path to a target protXML file or the file object itself.
read_schema (bool, optional) – If
True
, attempt to extract information from the XML schema mentioned in the protXML header. Otherwise, use default parameters. Not recommended without Internet connection or if you don’t like to get the related warnings.iterative (bool, optional) – Defines whether iterative parsing should be used. It helps reduce memory usage at almost the same parsing speed. Default is
True
.
- Returns:
out – An iterator over dicts with protein group properties.
- Return type: