-
Notifications
You must be signed in to change notification settings - Fork 3
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
Add unit tests for the outputaccumulator post processing. #422
Open
mo-sameh
wants to merge
3
commits into
development
Choose a base branch
from
add-outputaccumlator-unittests
base: development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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 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,13 +1,14 @@ | ||
import os | ||
import shutil | ||
import tempfile | ||
|
||
import numpy as np | ||
import pandas as pd | ||
from alphabase.spectral_library.base import SpecLibBase | ||
from conftest import mock_fragment_df, mock_precursor_df | ||
from alphabase.spectral_library.flat import SpecLibFlat | ||
from conftest import mock_fragment_correlation_df, mock_fragment_df, mock_precursor_df | ||
|
||
from alphadia import outputtransform | ||
from alphadia.outputaccumulator import ms2_quality_control | ||
from alphadia.workflow.base import QUANT_FOLDER_NAME | ||
|
||
|
||
|
@@ -136,7 +137,6 @@ def test_complete_output_accumulation(): | |
== number_of_unique_precursors | ||
), f"{len(np.unique(built_lib.precursor_df['precursor_idx']))} != {number_of_unique_precursors}" | ||
|
||
shutil.rmtree(temp_folder) | ||
|
||
|
||
def test_selection_of_precursors(): | ||
|
@@ -178,7 +178,6 @@ def test_selection_of_precursors(): | |
f"{selected_probas} != {target_kept_probas}", | ||
) | ||
|
||
shutil.rmtree(temp_folder) | ||
|
||
|
||
def test_keep_top_constraint(): | ||
|
@@ -210,7 +209,6 @@ def test_keep_top_constraint(): | |
<= keep_top | ||
), f"{len(built_lib.precursor_df[built_lib.precursor_df['precursor_idx'] == precursor_idx])} != {keep_top}" | ||
|
||
shutil.rmtree(temp_folder) | ||
|
||
|
||
def test_default_column_assignment(): | ||
|
@@ -247,3 +245,64 @@ def test_default_column_assignment(): | |
assert built_lib.precursor_df[f"{col}"].equals( | ||
built_lib.precursor_df[f"{col}_library"] | ||
), f"{col} != {col}_library" | ||
|
||
def test_non_nan_fragments(): | ||
""" | ||
Test that the accumulated fragments data frame has no nan values | ||
""" | ||
# Given: | ||
config, temp_folder, raw_folders, psm_dfs, fragment_dfs = prepare_input_data() | ||
keep_top = 2 | ||
config["transfer_library"]["top_k_samples"] = keep_top | ||
|
||
# When: | ||
output = outputtransform.SearchPlanOutput(config, temp_folder) | ||
_ = output.build_transfer_library(raw_folders, save=True) | ||
built_lib = SpecLibBase() | ||
built_lib.load_hdf( | ||
os.path.join(temp_folder, f"{output.TRANSFER_OUTPUT}.hdf"), load_mod_seq=True | ||
) | ||
|
||
# Then: The fragment dataframe should have no nan values | ||
assert not built_lib.fragment_intensity_df.isnull().values.any(), "There are nan values in the fragment dataframe" | ||
|
||
|
||
def test_use_for_ms2(): | ||
""" | ||
Test that the ms2 quality control is correctly applied by checking the use_for_ms2 column in the precursor_df | ||
""" | ||
# Given: | ||
psm_flat_df = mock_precursor_df(n_precursor=100, with_decoy=True) | ||
fragment_flat_df = mock_fragment_df(n_precursor=100, n_fragments=10) | ||
psm_flat_df = psm_flat_df.sort_values(by="precursor_idx") | ||
fragment_flat_df = fragment_flat_df.sort_values(by="precursor_idx") | ||
psm_flat_df["flat_frag_start_idx"] = np.arange(0, len(psm_flat_df) * 10, 10) | ||
psm_flat_df["flat_frag_stop_idx"] = np.arange(0, len(psm_flat_df) * 10, 10) + 9 | ||
psm_flat_df['nAA'] =psm_flat_df.sequence.str.len().astype(np.int32) | ||
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. please install the pre-commit hook :-) 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. This is strange, I already have them installed and ran it locally and all checks were passed. |
||
fragment_flat_df["loss_type"] = 0 | ||
flat_spec_lib = SpecLibFlat() | ||
flat_spec_lib._precursor_df = psm_flat_df | ||
flat_spec_lib._fragment_df = fragment_flat_df | ||
# TODO: to_SpecLibBase will be deprecated and this should be adapted to use to_speclib_base | ||
spec_lib = flat_spec_lib.to_SpecLibBase() | ||
mo-sameh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
fragment_correlation_base_df = mock_fragment_correlation_df(spec_lib.fragment_intensity_df) | ||
spec_lib._fragment_correlation_df = fragment_correlation_base_df | ||
precursor_correlation_cutoff = 0.5 | ||
fragment_correlation_ratio = 0.75 | ||
|
||
base_precursor_df = spec_lib.precursor_df.copy() | ||
base_fragment_df = spec_lib.fragment_intensity_df.copy() | ||
# When: | ||
ms2_quality_control(spec_lib, precursor_correlation_cutoff, fragment_correlation_ratio) | ||
|
||
# Then: The use_for_ms2 column should be correctly assigned for precursors with median fragment correlation above precursor_correlation_cutoff | ||
target_use_for_ms2 = [] | ||
for frag_start,frag_stop in zip(base_precursor_df["frag_start_idx"],base_precursor_df["frag_stop_idx"]): | ||
frag_corr = fragment_correlation_base_df.iloc[frag_start:frag_stop].values | ||
frag_intensities = base_fragment_df.iloc[frag_start:frag_stop].values | ||
# median corr of non zero intensities | ||
frag_corr = frag_corr[frag_intensities>0] | ||
median_frag_corr = np.median(frag_corr) if len(frag_corr) > 0 else 0 | ||
target_use_for_ms2.append(median_frag_corr > precursor_correlation_cutoff) | ||
|
||
np.testing.assert_array_equal(spec_lib.precursor_df["use_for_ms2"].values, target_use_for_ms2) |
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.
could we use a fixed seed here to make the tests reproducible?
if not, it would be good to print out the generated date, otherwise debugging tests will be a nightmare
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.
currently mocking the precursor_df, fragment_df and fragment correlation are completely random.
@GeorgWa do you think we can fix the seed for all of them ?