-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from monarch-initiative/apply-mappings
Apply mappings
- Loading branch information
Showing
6 changed files
with
95 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import numpy as np | ||
from pandas.core.frame import DataFrame | ||
|
||
|
||
def apply_mappings(edges: DataFrame, mapping: DataFrame): | ||
|
||
mapping_dict = mapping.set_index('subject_id')['object_id'] | ||
|
||
edges['original_subject'] = edges['subject'] | ||
edges['subject'].replace(mapping_dict, inplace=True) | ||
edges['original_subject'] = np.where(edges.subject == edges.original_subject, None, edges.original_subject) | ||
|
||
edges['original_object'] = edges['object'] | ||
edges['object'].replace(mapping_dict, inplace=True) | ||
edges['original_object'] = np.where(edges.object == edges.original_object, None, edges.original_object) | ||
|
||
return edges |
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
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,46 @@ | ||
import pytest | ||
from tests.test_utils import string_df | ||
from cat_merge.mapping_utils import apply_mappings | ||
|
||
@pytest.fixture | ||
def edges(): | ||
edges = u"""\ | ||
id subject object | ||
uuid:1 Gene:1 Disease:1 | ||
uuid:2 XGene:2 Disease:2 | ||
uuid:3 Gene:2 XDisease:3 | ||
uuid:4 XGene:3 XDisease:4 | ||
""" | ||
return string_df(edges) | ||
|
||
|
||
@pytest.fixture | ||
def mapping(): | ||
mapping = u"""\ | ||
subject_id object_id | ||
XGene:1 Gene:1 | ||
XGene:2 Gene:2 | ||
XGene:3 Gene:3 | ||
XDisease:1 Disease:1 | ||
XDisease:2 Disease:2 | ||
XDisease:3 Disease:3 | ||
XDisease:4 Disease:4 | ||
""" | ||
return string_df(mapping, index_column_is_id=False) | ||
|
||
|
||
def test_apply_mappings(edges, mapping): | ||
mapped_edges = apply_mappings(edges, mapping) | ||
|
||
assert mapped_edges.loc['uuid:3']['subject'] == 'Gene:2' | ||
assert mapped_edges.loc['uuid:3']['object'] == 'Disease:3' | ||
assert mapped_edges.loc['uuid:4']['subject'] == 'Gene:3' | ||
assert mapped_edges.loc['uuid:4']['object'] == 'Disease:4' | ||
|
||
|
||
def test_original_subject_and_object(edges, mapping): | ||
mapped_edges = apply_mappings(edges, mapping) | ||
|
||
assert mapped_edges.loc['uuid:2']['original_subject'] == 'XGene:2' | ||
assert mapped_edges.loc['uuid:3']['original_object'] == 'XDisease:3' | ||
assert mapped_edges.loc['uuid:4']['original_object'] == 'XDisease:4' |