Skip to content

Commit

Permalink
Fix #2, Small fixes & apply migrations in docker
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiaskoenig committed Jun 29, 2018
1 parent 91bf282 commit 1714c5e
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 8 deletions.
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ services:
build: ./
command: >
bash -c "python wait_for_postgres.py &&
./manage.py makemigrations &&
./manage.py migrate &&
./manage.py runserver 0.0.0.0:8000"
volumes:
Expand Down
19 changes: 13 additions & 6 deletions pkdb_app/data_management/create_master.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
"""
Creates json files in master folder
"""
import os
import csv
import sys
Expand All @@ -7,7 +10,7 @@
import json

BASEPATH = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)), '../'))
Master = os.path.join(BASEPATH,"Master")
Master = os.path.join(BASEPATH, "Master")
if BASEPATH not in sys.path:
sys.path.append(BASEPATH)

Expand All @@ -17,12 +20,12 @@
PHARMACOKINETICSPATH = os.path.join(DATABASEPATH, "caffeine", "Pharmacokinetics.tsv")
INTERVENTIONSPATH = os.path.join(DATABASEPATH, "caffeine", "Interventions.tsv")
DOSINGPATH = os.path.join(DATABASEPATH, "caffeine", "Dosing.tsv")
MASTERPATH = os.path.join(DATABASEPATH, "Master")
MASTERPATH = os.path.join(DATABASEPATH, "Master")
STUDIESMASTERPATH = os.path.join(MASTERPATH, "Studies")


def extract_studies(path):
reader = csv.DictReader(open(path),delimiter='\t')
reader = csv.DictReader(open(path), delimiter='\t')
for line in reader:
yield dict(line)

Expand All @@ -34,7 +37,7 @@ def pmid_to_int(d):

def add_study_sid(d):
sid = str(d["study"])
yield {**d ,'sid':sid}
yield {**d , 'sid': sid}


def add_study_path(d):
Expand Down Expand Up @@ -73,14 +76,18 @@ def create_json(d):
return {'json': json_dict, 'study_path' : d["study_path"]}


#save
def save_json(d):
json_file = os.path.join(d["study_path"],"data.json")
with open(json_file, 'w') as fp:
json.dump(d['json'],fp, indent=4)


def get_graph(**options):
""" Bonobo execution graph.
:param options:
:return:
"""
graph = bonobo.Graph()
graph.add_chain(
extract_studies(STUDIESPATH),
Expand All @@ -102,4 +109,4 @@ def get_services(**options):
if __name__ == '__main__':
parser = bonobo.get_argument_parser()
with bonobo.parse_args(parser) as options:
bonobo.run(get_graph(**options), services=get_services(**options))
bonobo.run(get_graph(**options), services=get_services(**options))
6 changes: 6 additions & 0 deletions pkdb_app/data_management/create_master_json.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
"""
TODO: check if still needed and remove
"""


import os
import sys
import bonobo
Expand Down
10 changes: 8 additions & 2 deletions pkdb_app/data_management/fill_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,23 @@
client = coreapi.Client()
document = client.get("http://0.0.0.0:8000/")


def get_study_json_path():
for root, dirs, files in os.walk(STUDIESMASTERPATH, topdown=False):
if "data.json" in files:
yield os.path.join(root,'data.json')
yield os.path.join(root, 'data.json')


def open_json(d):
with open(d) as f:
json_dict = json.loads(f.read())
return json_dict


def upload_study(json_study):
client.action(document, ["studies", "create"], params=json_study)


def get_graph(**options):
graph = bonobo.Graph()
#add studies
Expand All @@ -30,10 +34,12 @@ def get_graph(**options):
)
return graph


def get_services(**options):
return {}


if __name__ == '__main__':
parser = bonobo.get_argument_parser()
with bonobo.parse_args(parser) as options:
bonobo.run(get_graph(**options), services=get_services(**options))
bonobo.run(get_graph(**options), services=get_services(**options))
32 changes: 32 additions & 0 deletions pkdb_app/studies/migrations/0004_auto_20180629_1437.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Generated by Django 2.0.6 on 2018-06-29 14:37

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('studies', '0003_auto_20180619_0834'),
]

operations = [
migrations.RemoveField(
model_name='study',
name='sid',
),
migrations.AddField(
model_name='study',
name='abstract',
field=models.TextField(blank=True, null=True),
),
migrations.AlterField(
model_name='study',
name='authors',
field=models.ManyToManyField(blank=True, related_name='authors', to='studies.Author'),
),
migrations.AlterField(
model_name='study',
name='title',
field=models.TextField(),
),
]
10 changes: 10 additions & 0 deletions pkdb_app/studies/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
"""
Django model for Study.
"""
from django.db import models
from .utils import CHAR_MAX_LENGTH
from pkdb_app.behaviours import Sidable, Describable, Commentable
Expand All @@ -12,12 +15,19 @@ def __str__(self):
return '%s %s' % (self.first_name, self.last_name)

class Study(Commentable, Describable, models.Model):
""" Single clinical study.
Mainly reported as a single publication.
"""


title = models.TextField()
pmid = models.CharField(max_length=CHAR_MAX_LENGTH)
abstract = models.TextField(blank=True, null=True)
file = models.FileField(upload_to="study", null=True, blank=True)
authors = models.ManyToManyField(Author, blank=True, related_name='authors')


class Intervention(Sidable, Commentable, Describable, models.Model):
type = models.IntegerField(choices=INTERVENTION_CHOICES)
study = models.ForeignKey(Study, null=True, blank=True, on_delete=True)
Expand Down

0 comments on commit 1714c5e

Please sign in to comment.