forked from wfondrie/mokapot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added mokapot.to_txt(). Now time for tests.
- Loading branch information
Showing
4 changed files
with
153 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
"""Define the public functions for the writers""" | ||
from .txt import to_txt | ||
from .flashlfq import to_flashlfq |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
"""Writer to save results in a tab-delmited format""" | ||
from pathlib import Path | ||
from collections import defaultdict | ||
|
||
import pandas as pd | ||
|
||
|
||
def to_txt(conf, dest_dir=None, file_root=None, sep="\t", decoys=False): | ||
"""Save confidence estimates to delimited text files. | ||
Parameters | ||
---------- | ||
conf : Confidence object or tuple of Confidence objects | ||
One or more :py:class:`~mokapot.confidence.LinearConfidence` objects. | ||
dest_dir : str or None, optional | ||
The directory in which to save the files. `None` will use the current | ||
working directory. | ||
file_root : str or None, optional | ||
An optional prefix for the confidence estimate files. The suffix will | ||
always be "mokapot.{level}.txt" where "{level}" indicates the level at | ||
which confidence estimation was performed (i.e. PSMs, peptides, | ||
proteins). | ||
sep : str, optional | ||
The delimiter to use. | ||
decoys : bool, optional | ||
Save decoys confidence estimates as well? | ||
Returns | ||
------- | ||
list of str | ||
The paths to the saved files. | ||
""" | ||
try: | ||
assert not isinstance(conf, str) | ||
iter(conf) | ||
except TypeError: | ||
conf = [conf] | ||
except AssertionError: | ||
raise ValueError("'conf' should be a Confidence object, not a string.") | ||
|
||
file_base = "mokapot" | ||
if file_root is not None: | ||
file_base = file_root + "." + file_base | ||
if dest_dir is not None: | ||
file_base = Path(dest_dir, file_base) | ||
|
||
results = defaultdict(list) | ||
for res in conf: | ||
for level, qval_list in _get_level_data(res, decoys).items(): | ||
results[level] += qval_list | ||
|
||
out_files = [] | ||
for level, qval_list in results.items(): | ||
out_file = str(file_base) + f".{level}.txt" | ||
pd.concat(qval_list).to_csv(out_file, sep=sep, index=False) | ||
out_files.append(out_file) | ||
|
||
return out_files | ||
|
||
|
||
def _get_level_data(conf, decoys): | ||
"""Return the dataframes for each level. | ||
Parameters | ||
---------- | ||
conf : a Confidence object | ||
A LinearConfidence object. | ||
decoys : bool | ||
Should decoys be included? | ||
Returns | ||
------- | ||
Dict | ||
Each entry contains a level, dataframe pair. | ||
""" | ||
results = defaultdict(list) | ||
for level, qvals in conf.confidence_estimates.items(): | ||
if qvals is None: | ||
continue | ||
|
||
results[level].append(qvals) | ||
|
||
if decoys: | ||
for level, qvals in conf.decoy_confidence_estiamtes.items(): | ||
if qvals is None: | ||
continue | ||
|
||
results[f"decoy.{level}"].append(qvals) | ||
|
||
return results |