Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
expression specificity is loaded into the database
  • Loading branch information
proost committed Mar 23, 2016
1 parent e25a21f commit f336cbc
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
34 changes: 30 additions & 4 deletions planet/models/expression_specificity.py
Expand Up @@ -21,25 +21,51 @@ def calculate_specificities(species_id, description):
new_method = ExpressionSpecificityMethod()
new_method.species_id = species_id
new_method.description = description
conditions = []

profiles = db.engine.execute(db.select([ExpressionProfile.__table__.c.id, ExpressionProfile.__table__.c.profile]).
where(ExpressionProfile.__table__.c.species_id == species_id)
).fetchall()

for profile_id, profile in profiles:
profile_data = json.loads(profile)
profile_mean = {k: mean(v) for k, v in profile_data['data'].items()}
for condition in profile_data['order']:
if condition not in conditions:
conditions.append(condition)

new_method.conditions = json.dumps(conditions)

db.session.add(new_method)
db.session.commit()

specificities = []

for profile_id, profile in profiles:
profile_data = json.loads(profile)
profile_means = {k: mean(v) for k, v in profile_data['data'].items()}

print(profile_id, profile_mean)
for condition in profile_data['order']:
score = expression_specificity(condition, profile_mean)
score = expression_specificity(condition, profile_means)
new_specificity = {
'profile_id': profile_id,
'condition': condition,
'score': score,
'method_id': new_method.id,
}

specificities.append(new_specificity)
if len(specificities) > 400:
db.engine.execute(ExpressionSpecificity.__table__.insert(), specificities)
specificities = []

db.engine.execute(ExpressionSpecificity.__table__.insert(), specificities)


class ExpressionSpecificity(db.Model):
__tablename__ = 'expression_specificity'

id = db.Column(db.Integer, primary_key=True)
profile = db.Column(db.Integer, db.ForeignKey('expression_profiles.id'), index=True)
profile_id = db.Column(db.Integer, db.ForeignKey('expression_profiles.id'), index=True)
condition = db.Column(db.String(255), index=True)
score = db.Column(db.Float, index=True)
method_id = db.Column(db.Integer, db.ForeignKey('expression_specificity_method.id'), index=True)
3 changes: 2 additions & 1 deletion utils/vector.py
@@ -1,5 +1,6 @@
from math import sqrt


def dot_prod(a, b):
"""
Calculates the dot product of two lists with values
Expand All @@ -18,4 +19,4 @@ def norm(a):
:param a: list of values
:return: the Frobenius norm
"""
return sqrt(sum([abs(i)**2 for i in a]))
return sqrt(sum([i**2 for i in a]))

0 comments on commit f336cbc

Please sign in to comment.