Skip to content
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

Dist measure#17 #38

Merged
merged 6 commits into from
May 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions prowler/databases.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,8 @@ def merger(self,
self.merged.dropna(inplace=True)

def profilize(self,
reference_species):
reference_species,
method="pairwise"):
"""
Append databases.merged with Profiles Similarity Score and/or string
representation of the phylogenetic profiles.
Expand All @@ -445,13 +446,18 @@ def profilize(self,
reference_species: list of str
Species list compared to contained in the orthogroup. Basis for the
profiles construction.
method: str, default <pairwise>
Distance measure to use in Profiles Similarity Score calculation.
"""
if method != "pairwise":
self.dtypes[self.PSS] = "float32"
self.merged[self.PROF_A] = self.merged[self.ORGS_A].apply(lambda x:
_Profile(x, reference_species))
self.merged[self.PROF_Q] = self.merged[self.ORGS_Q].apply(lambda x:
_Profile(x, reference_species))
self.merged[self.PSS] = self.merged.apply(lambda x:
x[self.PROF_Q].calculate_pss(x[self.PROF_A]),
x[self.PROF_Q].calculate_pss(x[self.PROF_A],
method=method),
axis=1).astype(self.dtypes[self.PSS])
self.merged = self.merged.astype({k: v for k, v in self.dtypes.items()
if k in self.merged.columns})
23 changes: 21 additions & 2 deletions prowler/profiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from prowler.errors import *
import numpy as np
import pandas as pd
from scipy.spatial import distance as dist


class Profile:
Expand Down Expand Up @@ -139,7 +140,8 @@ def to_series(self,

def calculate_pss(self,
profile,
ignore=None):
ignore=None,
method="pairwise"):
"""
Calculate Profiles Similarity Score.
"""
Expand All @@ -157,7 +159,24 @@ def calculate_pss(self,
del prof_2.profile[prof_2.query.index(i)]
except IndexError:
raise ProfileError("Element to ignore not in profile")
return sum(a == b for a, b in zip(prof_1.profile, prof_2.profile))
if method == "pairwise":
return sum(a == b for a, b in zip(prof_1.profile, prof_2.profile))
elif method == "jaccard":
return dist.jaccard(prof_1.profile, prof_2.profile)
elif method == "yule":
return dist.yule(prof_1.profile, prof_2.profile)
elif method == "dice":
return dist.dice(prof_1.profile, prof_2.profile)
elif method == "hamming":
return dist.hamming(prof_1.profile, prof_2.profile)
elif method == "kulsinski":
return dist.kulsinski(prof_1.profile, prof_2.profile)
elif method == "rogerstanimoto":
return dist.rogerstanimoto(prof_1.profile, prof_2.profile)
elif method == "russellrao":
return dist.russellrao(prof_1.profile, prof_2.profile)
elif method == "sokalmichener":
return dist.sokalmichener(prof_1.profile, prof_2.profile)

def get_present(self):
"""
Expand Down
2 changes: 1 addition & 1 deletion prowler_py27.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ dependencies:
- readline=7.0=0
- requests=2.18.4=py27_1
- scandir=1.6=py27_0
- scipy=1.0.0=py27_blas_openblas_201
- scipy=1.1.0=py27_blas_openblas_200
- seaborn=0.8.1=py27_0
- setuptools=38.4.0=py27_0
- simplegeneric=0.8.1=py27_0
Expand Down
1 change: 1 addition & 0 deletions prowler_py36.yml
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,4 @@ dependencies:
- libstdcxx-ng=7.2.0=hdf63c60_3
- mkl=2018.0.2=1
- numpy=1.14.2=py36hdbf6ddf_1
- scipy=1.1.0=py36hfc37229_0
Binary file added test_data/ProfIntTests/ref_nwrk_dice.pickle
Binary file not shown.
Binary file added test_data/ProfIntTests/ref_nwrk_hamming.pickle
Binary file not shown.
Binary file added test_data/ProfIntTests/ref_nwrk_jaccard.pickle
Binary file not shown.
Binary file added test_data/ProfIntTests/ref_nwrk_kulsinski.pickle
Binary file not shown.
Binary file added test_data/ProfIntTests/ref_nwrk_pairwise.pickle
Binary file not shown.
Binary file not shown.
Binary file added test_data/ProfIntTests/ref_nwrk_russellrao.pickle
Binary file not shown.
Binary file not shown.
62 changes: 52 additions & 10 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,14 @@ def setUp(self):
Sets up class level attributes for the tests.
"""
self.profint = databases.ProfInt()
self.methods = ["pairwise",
"jaccard",
"dice",
"hamming",
"kulsinski",
"rogerstanimoto",
"russellrao",
"sokalmichener"]
self.ref_merged = pd.read_pickle("test_data/ProfIntTests/ref_merged.pickle")
self.ref_profilized_nwrk = pd.read_pickle("test_data/StatsTests/ref_nwrk.pickle").reset_index(drop=True)
self.reference_species = self.ref_profilized_nwrk[self.profint.PROF_Q].iloc[0].query
Expand Down Expand Up @@ -358,6 +366,19 @@ def test_profilize(self):
pd.testing.assert_series_equal(self.ref_profilized_nwrk[self.profint.PROF_Q].apply(lambda x: sorted(x.get_absent())),
self.profint.merged[self.profint.PROF_Q].apply(lambda x: sorted(x.get_absent())))

def test_pss(self):
for method in self.methods:
self.ref_df = pd.read_pickle("test_data/ProfIntTests/ref_nwrk_{}.pickle".format(method))
self.profint.merged = self.test_non_profilized_nwrk
self.profint.profilize(self.reference_species, method=method)
pd.testing.assert_series_equal(self.ref_df[self.profint.PSS],
self.profint.merged[self.profint.PSS])
# print(method)
# self.profint.merged = self.test_non_profilized_nwrk
# self.profint.profilize(self.reference_species, method=method)
# print(self.profint.merged)
# self.profint.merged.to_pickle("test_data/ProfIntTests/ref_nwrk_{}.pickle".format(method))


class ProfileTests(unittest.TestCase):
"""
Expand All @@ -368,9 +389,10 @@ def setUp(self):
Sets up class level attributes for the tests.
"""
self.ref_query = list("acdfhiklostuz")
self.ref_queries = [self.ref_query,
"aaaaaaaaaaaaa",
"bbbbbbbbbbbbb"]
self.ref_queries = [self.ref_query[:10],
list('!@hjlnrtwy'),
list('acdfhi@#$%'),
list("qoadzv!@#$")]
self.ref_reference = list("bcefghijklmnprstuwxy")
self.ref_bound = [("a", False),
("c", True),
Expand All @@ -391,11 +413,28 @@ def setUp(self):
self.alt_neg_sing = "#"
self.ref_profile = "-+-+++++-+++-"
self.ref_alt_profile = "#$#$$$$$#$$$#"
self.ref_pss = [13, 4, 9]
self.methods = ["pairwise",
"jaccard",
"dice",
"hamming",
"kulsinski",
"rogerstanimoto",
"russellrao",
"sokalmichener"]
self.ref_pss = [[10, 7, 5, 3],
[0.0, 0.3333333333333333, 0.625, 1.0],
[0.0, 0.2, 0.45454545454545453, 1.0],
[0.0, 0.3, 0.5, 0.7],
[0.3, 0.5384615384615384, 0.8, 1.0],
[0.0, 0.46153846153846156, 0.6666666666666666, 0.8235294117647058],
[0.3, 0.4, 0.7, 1.0],
[0.0, 0.46153846153846156, 0.6666666666666666, 0.8235294117647058]]
self.ref_ignore_elements = ["a", "c", "f"]
self.ref_pss_ignore = 10
self.test_profile = profiles.Profile(reference=self.ref_reference,
query=self.ref_query)
self.test_pss_methods_profile = profiles.Profile(reference=self.ref_reference,
query=self.ref_query[:10])

def test__convert(self):
"""
Expand Down Expand Up @@ -469,12 +508,15 @@ def test_to_series(self):

def test_calculate_pss(self):
"""
Test if Profiles Similarity Score (PSS) is properly calculated.
"""
for query, pss in zip(self.ref_queries, self.ref_pss):
self.assertEqual(self.test_profile.calculate_pss(profiles.Profile(reference=self.ref_reference,
query=query)),
pss)
Test if Profiles Similarity Score (PSS) is properly calculated with
jaccard distance measure.
"""
for method, pss_grp in zip(self.methods, self.ref_pss):
for query, pss in zip(self.ref_queries, pss_grp):
self.assertEqual(self.test_pss_methods_profile.calculate_pss(profiles.Profile(reference=self.ref_reference,
query=query),
method=method),
pss)

def test_calculate_pss_ignore(self):
"""
Expand Down