Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
implemented background substraction fixed bug search results
  • Loading branch information
proost committed Apr 12, 2016
1 parent ccdd996 commit c473710
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 22 deletions.
6 changes: 6 additions & 0 deletions README.md
Expand Up @@ -20,6 +20,12 @@ Below are the instructions to set up a new PlaNet version.

[Run website](docs/run_website.md)

Case Studies
------------

How to generate data for case studies is described [here](docs/case_studies.md). These can only be started after the
database is completed.

Instructions for Developers
---------------------------

Expand Down
23 changes: 14 additions & 9 deletions case_study/entropy_specificity.py
Expand Up @@ -2,21 +2,26 @@
from planet.models.expression_profiles import ExpressionProfile
from planet.models.expression_specificity import ExpressionSpecificity, ExpressionSpecificityMethod

from sqlalchemy.orm import joinedload
from sys import argv

app = create_app('config')

with app.app_context():
profiles = ExpressionProfile.query.all()
methods = ExpressionSpecificityMethod.query.all()
def write_entropy_specificity(method_id, filename):
app = create_app('config')

with open("./data/entropy_specificity_full.txt", "w") as f_out:
for m in methods:
with app.app_context():
profiles = ExpressionProfile.query.all()
m = ExpressionSpecificityMethod.query.get(method_id)

with open(filename, "w") as f_out:
for p in profiles:
if m.species_id == p.species_id:
s = p.specificities.filter(ExpressionSpecificity.method_id == m.id).order_by(ExpressionSpecificity.score.desc()).first()
s = p.specificities.filter(ExpressionSpecificity.method_id == method_id).order_by(ExpressionSpecificity.score.desc()).first()
if s is not None:
spm = s.score
method = s.method.description
condition = s.condition
print(p.id, p.probe, p.sequence.name if p.sequence is not None else None, p.species.name, p.entropy, spm, condition, method, sep='\t', file=f_out)
print(p.id, p.probe, p.sequence.name if p.sequence is not None else None, p.species.name, p.entropy, spm, condition, method, sep='\t', file=f_out)


if __name__ == "__main__":
write_entropy_specificity(int(argv[1]), argv[2])
17 changes: 10 additions & 7 deletions case_study/random_specificity.py
Expand Up @@ -8,11 +8,11 @@

import json
from statistics import mean, stdev
from random import lognormvariate
from random import normalvariate
from sys import argv


def write_random_spms(species_id, filename):
def write_random_spms(species_id, filename, substract_background=False):
app = create_app('config')

with app.app_context():
Expand All @@ -24,23 +24,26 @@ def write_random_spms(species_id, filename):
profile_data = json.loads(p.profile)

values = [float(sum(v)) for k, v in profile_data['data'].items()]

if substract_background:
minimum = min(values)
for i in range(len(values)):
values[i] -= minimum

mean_value = mean(values)
sd = stdev(values)

for _ in range(10):
random_values = []

while len(random_values) < len(values):
r = lognormvariate(mean_value, sd)
r = normalvariate(mean_value, sd)
if r >= 0:
random_values.append(r)

rand_mean = mean(random_values)
rand_sd = stdev(random_values)

rand_max_spm = max_spm({c: v for c, v in enumerate(random_values)})['score']

print(rand_max_spm, file=f)

if __name__ == "__main__":
write_random_spms(int(argv[1]), argv[2])
write_random_spms(int(argv[1]), argv[2], substract_background=True)
Empty file added docs/case_studies.md
Empty file.
14 changes: 11 additions & 3 deletions planet/models/expression_specificity.py
Expand Up @@ -17,7 +17,7 @@ class ExpressionSpecificityMethod(db.Model):
specificities = db.relationship('ExpressionSpecificity', backref='method', lazy='dynamic')

@staticmethod
def calculate_specificities(species_id, description):
def calculate_specificities(species_id, description, remove_background=False):
"""
Function that calculates condition specificities for each profile. No grouping is applied, each condition is
used as is
Expand All @@ -42,10 +42,10 @@ def calculate_specificities(species_id, description):

# convert list into dictionary and run function
conditions_dict = {k: k for k in conditions}
ExpressionSpecificityMethod.calculate_tissue_specificities(species_id, description, conditions_dict)
ExpressionSpecificityMethod.calculate_tissue_specificities(species_id, description, conditions_dict, remove_background=remove_background)

@staticmethod
def calculate_tissue_specificities(species_id, description, condition_to_tissue):
def calculate_tissue_specificities(species_id, description, condition_to_tissue, remove_background=False):
"""
Function calculates tissue specific genes based on the expression conditions. A dict is required to link
specific conditions to the correct tissues. This also allows conditions to be excluded in case they are
Expand Down Expand Up @@ -89,6 +89,14 @@ def calculate_tissue_specificities(species_id, description, condition_to_tissue)

profile_means[t] = total_sum/count if count != 0 else 0

# substract minimum value to remove background
# experimental code !
if remove_background:
minimum = min([v for k,v in profile_means.items()])

for k in profile_means.keys():
profile_means[k] -= minimum

# determine spm score for each condition
profile_specificities = []

Expand Down
3 changes: 1 addition & 2 deletions planet/templates/search_results.html
Expand Up @@ -82,7 +82,6 @@ <h1>Search results for: <strong>{{ keyword }}</strong></h1>
{% endif %}

{% if profiles %}
<h2></h2>
<div class="col-md-4 col-sm-12">
<div class="panel panel-default">
<div class="panel-heading">Found <strong>{{ profiles|length }}</strong> expression profiles</div>
Expand All @@ -92,7 +91,7 @@ <h2></h2>
<tbody>
{% for p in profiles %}
<tr><td><a href="{{ url_for('expression_profile.expression_profile_view', profile_id=p.id)}}">{{ p.probe }}</a>
{% if p.gene.name %}: {{ p.gene.name }} {% endif %}</td></tr>
{% if p.sequence %}: {{ p.sequence.name }} {% endif %}</td></tr>
{% endfor %}
</tbody>
</table>
Expand Down
7 changes: 6 additions & 1 deletion utils/expression.py
Expand Up @@ -12,9 +12,14 @@ def expression_specificity(condition, profile):
return dot_product/mul_len if mul_len != 0 else 0


def max_spm(profile):
def max_spm(profile, substract_background=False):
conditions = [k for k, v in profile.items()]

if substract_background:
minimum = min(list(profile.values()))
for k in profile.keys():
profile[k] -= minimum

spm_values = [{'condition': c, 'score': expression_specificity(c, profile)} for c in conditions]

# sort spm_values high to low
Expand Down

0 comments on commit c473710

Please sign in to comment.