-
Notifications
You must be signed in to change notification settings - Fork 1
add diann file support and tests #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
59d52f6
add diann file support and tests
ltatka 27cce5b
add tqdm to install_requires
ltatka 803558d
add loguru to install_requires
ltatka 1beb214
remove new line and unnecessary import
ltatka 610e362
add doc for expected columns and datatype check
ltatka 909f8e8
add new line at end of file
ltatka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,2 @@ | ||
| """The parsers""" | ||
| from .tabular import read_encyclopedia, read_metamorpheus | ||
| from .tabular import read_encyclopedia, read_metamorpheus, read_diann |
This file contains hidden or 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,5 +1,6 @@ | ||
| """Parse tabular result files from common tools""" | ||
| import pandas as pd | ||
| import numpy as np | ||
|
|
||
|
|
||
| def read_encyclopedia(proteins_txt: str) -> pd.DataFrame: | ||
|
|
@@ -52,3 +53,60 @@ def read_metamorpheus(proteins_txt: str) -> pd.DataFrame: | |
| .fillna(0) | ||
| ) | ||
| return proteins | ||
|
|
||
| def read_diann(proteins_tsv: str) -> pd.DataFrame: | ||
| """ | ||
| Reads a DIANN-generated TSV file containing protein information, processes | ||
| it, and returns a cleaned Pandas DataFrame with relevant data. | ||
|
|
||
| The function: | ||
| - Extracts the first protein accession from the "Protein.Ids" column to use | ||
| as the DataFrame index. | ||
| - Renames the index axis to "Protein". | ||
| - Drops unnecessary metadata columns. | ||
|
|
||
| Args: | ||
| proteins_tsv (str): Path to the DIANN-generated TSV file. | ||
| Expected columns: | ||
| 'Protein.Group', | ||
| 'Protein.Ids', | ||
| 'Protein.Names', | ||
| 'Genes', | ||
| 'First.Protein.Description', | ||
| <several MSR columns> | ||
|
|
||
|
|
||
| Returns: | ||
| pd.DataFrame: A DataFrame with the processed protein data, indexed by | ||
| the first protein accession. | ||
| The returned DataFrame has the "Protein.Ids" column as the | ||
| index and all columns are the MSR columns. | ||
| """ | ||
| proteins = pd.read_table(proteins_tsv) | ||
| accessions = proteins["Protein.Ids"].str.split(";").str[0] | ||
|
ltatka marked this conversation as resolved.
|
||
|
|
||
| proteins = proteins.set_index(accessions) | ||
| proteins = proteins.rename_axis("Protein", axis="index") | ||
| proteins = proteins.drop( | ||
| columns=[ | ||
| "Protein.Group", | ||
| "Protein.Ids", | ||
| "Protein.Names", | ||
| "Genes", | ||
| "First.Protein.Description", | ||
| ] | ||
| ) | ||
|
|
||
| # Check data types | ||
| # (if loading from S3, default types are 'O' | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can pandas read from s3? |
||
| if proteins.index.dtype not in ["O", "category", "str"]: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why did you go with this instead of trying to cast to a number and fail if it cannot? |
||
| raise ValueError( | ||
| f"Protein index is incorrect type: {proteins.index.dtype}" | ||
| ) | ||
| if not all( | ||
| np.issubdtype(dtype, np.floating) or dtype == "O" | ||
| for dtype in proteins.dtypes | ||
| ): | ||
| raise ValueError("Non-numeric columns present") | ||
|
|
||
| return proteins | ||
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -29,6 +29,8 @@ install_requires = | |
| numba | ||
| seaborn | ||
| biopython | ||
| tqdm | ||
| loguru | ||
|
|
||
| [options.extras_require] | ||
| docs = | ||
|
|
||
This file contains hidden or 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,27 @@ | ||
| import pandas as pd | ||
| from io import StringIO | ||
| from pandas.testing import assert_frame_equal | ||
|
|
||
| from gopher.parsers.tabular import read_diann | ||
|
|
||
| def test_read_diann_removes_metadata_and_sets_index(): | ||
| # Simulated DIANN output | ||
| mock_data = StringIO( | ||
| """Protein.Group\tProtein.Ids\tProtein.Names\tGenes\tFirst.Protein.Description\tIntensity.Sample1\tIntensity.Sample2 | ||
| PG1\tP12345;P67890\tProtein A\tGENE1\tDescription A\t1000\t2000 | ||
| PG2\tP23456\tProtein B\tGENE2\tDescription B\t1500\t2500 | ||
| """ | ||
| ) | ||
|
|
||
| # Expected DataFrame | ||
| expected = pd.DataFrame( | ||
| { | ||
| "Intensity.Sample1": [1000, 1500], | ||
| "Intensity.Sample2": [2000, 2500], | ||
| }, | ||
| index=["P12345", "P23456"] | ||
| ) | ||
| expected.index.name = "Protein" | ||
|
|
||
| result = read_diann(mock_data) | ||
| assert_frame_equal(result, expected) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: rename to
read_diann_pg_mator something that denotes which file is supportedThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have no objections to this. The reason I did it this way was to be consistent with the current parser function names, which are
read_encyclopediaandread_metamorpheus. Do you think those function names should also be updated?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like the difference there is that (as far as I can recall ...) those guys only return one matrix (I might be wrong though)