Skip to content

Commit 6cac61a

Browse files
authored
Merge pull request #68 from ens-lgil/main
Update import scripts and optimise REST API
2 parents 3de92fe + 3e1a316 commit 6cac61a

9 files changed

Lines changed: 145 additions & 70 deletions

File tree

imports/applications/parsers/phecode.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import re
2-
from psycopg2.extras import NumericRange
2+
from psycopg.types.range import Range
33

44
class PhecodeFullParser():
55

@@ -31,7 +31,7 @@ def parse_hazard_ratio(self):
3131
if len(values) == 3:
3232
ci_low = values[1].replace('(','')
3333
ci_upper = values[2].replace(')','')
34-
self.score_application['hr_ci'] = NumericRange(lower=float(ci_low), upper=float(ci_upper), bounds='[]')
34+
self.score_application['hr_ci'] = Range(lower=float(ci_low), upper=float(ci_upper), bounds='[]')
3535

3636

3737
def fix_phecode_id(self):

imports/omicspred/models/cohort.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class CohortData(GenericData):
88
def __init__(self,name_short:str,cohort_data:dict):
99
GenericData.__init__(self)
1010
self.name_short = name_short
11+
self.data['name_short'] = name_short
1112
for item in cohort_data.keys():
1213
if cohort_data[item]:
1314
self.data[item] = cohort_data[item]

imports/omicspred/models/dataset.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ def __init__(self, publication:PublicationData, platform:PlatformData, tissue:Ti
2323
self.publication = publication
2424
self.platform = platform
2525
self.tissue = tissue
26+
self.name = name
2627
if name:
27-
self.name = name
2828
self.data['name'] = name
2929
if data_type in self.omics_data_types.keys():
3030
self.data['omics_type'] = self.omics_data_types[data_type]

imports/omicspred/models/performance.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,23 @@ class PerformanceData(GenericData):
1818
ancestries = {
1919
'Ad Mixed American': 'AMR',
2020
'African': 'AFR',
21+
'African American or Afro-Caribbean': 'AFR',
2122
'East Asian': 'EAS',
2223
'European': 'EUR',
2324
'European,Ad Mixed American,African,East Asian,South Asian': 'ALL',
25+
'African American or Afro-Caribbean, East Asian, European, Hispanic or Latin American': 'ALL',
26+
'Hispanic or Latin American': 'AMR',
2427
'South Asian': 'SAS'
2528
}
2629

2730

28-
def __init__(self,score_name,metrics):
31+
def __init__(self):
2932
GenericData.__init__(self)
30-
self.score_name = score_name # Could be removed ?
31-
self.metrics = metrics
33+
self.metrics = []
34+
35+
36+
def add_eval_type(self, eval_type:str):
37+
self.data['eval_type'] = eval_type
3238

3339

3440
def add_score_model(self, score_model:Score):
@@ -48,7 +54,7 @@ def add_metrics(self,metrics_data):
4854
'''
4955
Method adding MetricData objects to the metrics array.
5056
'''
51-
self.metrics = metrics_data
57+
self.metrics.append(metrics_data)
5258
# for metric_type in metric_values.keys():
5359
# metric_val = None
5460
# pvalue_val = None
@@ -67,7 +73,8 @@ def add_metrics(self,metrics_data):
6773
def get_cohort_label(self, sample_model:Sample) -> str:
6874
cohorts = [x.name_short for x in sample_model.cohorts.all()]
6975
cohort_label = '_'.join(sorted(cohorts))
70-
if cohort_label in ['MEC','MESA','UKB_Withheld']:
76+
# if cohort_label in ['MEC','MESA','UKB_Withheld']:
77+
if cohort_label in ['MEC','UKB_Withheld']:
7178
sample_anc = sample_model.ancestry_broad
7279
if sample_anc in self.ancestries.keys():
7380
cohort_label = f'{cohort_label}_{self.ancestries[sample_anc]}'
@@ -77,7 +84,7 @@ def get_cohort_label(self, sample_model:Sample) -> str:
7784
def update_eval_type(self):
7885
eval_type = self.data['eval_type']
7986
eval_type_choices = Performance.eval_type.field.choices
80-
eval_types = {}
87+
eval_types = {}
8188
for choice in eval_type_choices:
8289
eval_types[choice[1]] = choice[0]
8390
if eval_type in eval_types.keys():
@@ -92,7 +99,8 @@ def create_model(self):
9299
'''
93100
try:
94101
with transaction.atomic():
95-
self.update_eval_type()
102+
if self.data['eval_type'] and len(self.data['eval_type']) > 2:
103+
self.update_eval_type()
96104
self.model = Performance(**self.data)
97105
self.model.save()
98106

imports/omicspred/models/sample.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import re
22
from django.db import IntegrityError, transaction
33
from imports.generic_model import GenericData
4+
from imports.omicspred.models.cohort import CohortData
45
from omicspred.models import Sample
56

67
class SampleData(GenericData):
@@ -54,7 +55,10 @@ def create_model(self):
5455
if field == 'cohorts':
5556
# Stored as list of CohortData -> Cohort Model
5657
for cohort_data in val:
57-
cohort = cohort_data.create_model()
58+
if isinstance(cohort_data,CohortData):
59+
cohort = cohort_data.create_model()
60+
else:
61+
cohort = cohort_data
5862
cohorts.append(cohort)
5963
continue
6064
elif field in ['ancestry_broad','ancestry_country','ancestry_free']:

imports/omicspred/models/score.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ def create_model(self):
4343
try:
4444
with transaction.atomic():
4545
self.model = Score()
46-
self.model.set_score_ids(self.next_id_number(Score, 'num'))
46+
id_number = Score.objects.latest('num').pk + 1
47+
# self.model.set_score_ids(self.next_id_number(Score, 'num'))
48+
self.model.set_score_ids(id_number)
4749
for field, val in self.data.items():
4850
setattr(self.model, field, val)
4951
self.model.save()

imports/omicspred/spreadsheets/spreadsheet.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,10 +371,12 @@ def extract_data(self):
371371
metrics.append(metric)
372372
# print(f"==> Metrics: {len(metrics)}")
373373
# Perforance
374-
performance = PerformanceData(score_name, metrics)
374+
# performance = PerformanceData(score_name, metrics)
375+
performance = PerformanceData()
376+
for metric in metrics:
377+
performance.add_metrics(metric)
375378
for p_key, p_val in performance_data.items():
376379
performance.add_data(p_key, p_val)
377-
performance.add_metrics(metrics)
378380
# print(f"==> Performance Metrics: {performance.metrics}")
379381

380382
# Take into account when there are more than 1 performance per score

requirements.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#### Django library ####
22
Django==5.2.8
33
#### Python PostgreSQL library ####
4-
psycopg2-binary==2.9.11
5-
# psycopg-binary==3.2.10
4+
# psycopg2-binary==2.9.11
5+
psycopg[binary]==3.3.2
66
# psycopg==3.2.10
77
#### Django REST API libraries ####
88
djangorestframework==3.16.1

0 commit comments

Comments
 (0)