From e25a21fb861de80b46a8a6c513eeaabc4303ef05 Mon Sep 17 00:00:00 2001 From: sepro Date: Tue, 22 Mar 2016 15:41:53 +0100 Subject: [PATCH] moved vector operations to their own library and can now calculate scores for tissue specific expression --- planet/models/expression_specificity.py | 2 +- utils/expression.py | 11 ++++++++++- utils/vector.py | 21 +++++++++++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 utils/vector.py diff --git a/planet/models/expression_specificity.py b/planet/models/expression_specificity.py index 883a3ad..0f89023 100644 --- a/planet/models/expression_specificity.py +++ b/planet/models/expression_specificity.py @@ -32,7 +32,7 @@ def calculate_specificities(species_id, description): print(profile_id, profile_mean) for condition in profile_data['order']: - expression_specificity(condition, profile_mean) + score = expression_specificity(condition, profile_mean) class ExpressionSpecificity(db.Model): diff --git a/utils/expression.py b/utils/expression.py index b38d38b..5dacd9f 100644 --- a/utils/expression.py +++ b/utils/expression.py @@ -1,6 +1,15 @@ +from utils.vector import dot_prod, norm + def expression_specificity(condition, profile): values = [v for k, v in profile.items()] vector = [v if k == condition else 0 for k, v in profile.items()] - print(values, vector) \ No newline at end of file + dot_product = dot_prod(values, vector) + + mul_len = norm(values) * norm(vector) + + if mul_len != 0: + return dot_product/mul_len + else: + return 0 diff --git a/utils/vector.py b/utils/vector.py new file mode 100644 index 0000000..f299621 --- /dev/null +++ b/utils/vector.py @@ -0,0 +1,21 @@ +from math import sqrt + +def dot_prod(a, b): + """ + Calculates the dot product of two lists with values + + :param a: first list + :param b: second list + :return: dot product (a . b) + """ + return sum([i*j for (i, j) in zip(a, b)]) + + +def norm(a): + """ + Calculates the Frobenius norm for a list of values + + :param a: list of values + :return: the Frobenius norm + """ + return sqrt(sum([abs(i)**2 for i in a])) \ No newline at end of file