Skip to content

Commit

Permalink
moved vector operations to their own library and can now calculate sc…
Browse files Browse the repository at this point in the history
…ores for tissue specific expression
  • Loading branch information
proost committed Mar 22, 2016
1 parent b35ddf7 commit e25a21f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
2 changes: 1 addition & 1 deletion planet/models/expression_specificity.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
11 changes: 10 additions & 1 deletion utils/expression.py
Original file line number Diff line number Diff line change
@@ -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)
dot_product = dot_prod(values, vector)

mul_len = norm(values) * norm(vector)

if mul_len != 0:
return dot_product/mul_len
else:
return 0
21 changes: 21 additions & 0 deletions utils/vector.py
Original file line number Diff line number Diff line change
@@ -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]))

0 comments on commit e25a21f

Please sign in to comment.