Skip to content

Commit

Permalink
Merge pull request #102 from romgrk/experiments-api
Browse files Browse the repository at this point in the history
Experiments API
  • Loading branch information
zxenia committed Mar 30, 2020
2 parents 564080a + 52109c1 commit 061c43e
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 11 deletions.
20 changes: 20 additions & 0 deletions chord_metadata_service/experiments/api_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from rest_framework import viewsets
from rest_framework.settings import api_settings
from .serializers import ExperimentSerializer
from .models import Experiment
from chord_metadata_service.restapi.pagination import LargeResultsSetPagination


class ExperimentViewSet(viewsets.ModelViewSet):
"""
get:
Return a list of all existing experiments
post:
Create a new experiment
"""

queryset = Experiment.objects.all().order_by("id")
serializer_class = ExperimentSerializer
pagination_class = LargeResultsSetPagination
renderer_classes = tuple(api_settings.DEFAULT_RENDERER_CLASSES)
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Generated by Django 2.2.11 on 2020-03-27 17:28

import chord_metadata_service.restapi.validators
import django.contrib.postgres.fields.jsonb
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('experiments', '0001_initial'),
]

operations = [
migrations.AlterField(
model_name='experiment',
name='experiment_ontology',
field=django.contrib.postgres.fields.jsonb.JSONField(help_text='(Ontology: OBI) links to experiment ontology information.', null=True, validators=[chord_metadata_service.restapi.validators.JsonSchemaValidator({'$id': 'ONTOLOGY_CLASS_LIST', '$schema': 'http://json-schema.org/draft-07/schema#', 'description': 'Ontology class list', 'items': {'$id': 'ONTOLOGY_CLASS', '$schema': 'http://json-schema.org/draft-07/schema#', 'additionalProperties': False, 'description': 'todo', 'properties': {'id': {'description': 'CURIE style identifier.', 'type': 'string'}, 'label': {'description': 'Human-readable class name.', 'type': 'string'}}, 'required': ['id', 'label'], 'title': 'Ontology class schema', 'type': 'object'}, 'title': 'Ontology class list', 'type': 'array'})]),
),
migrations.AlterField(
model_name='experiment',
name='molecule',
field=models.CharField(choices=[('total RNA', 'total RNA'), ('polyA RNA', 'polyA RNA'), ('cytoplasmic RNA', 'cytoplasmic RNA'), ('nuclear RNA', 'nuclear RNA'), ('small RNA', 'small RNA'), ('genomic DNA', 'genomic DNA'), ('protein', 'protein'), ('other', 'other')], help_text='(Controlled Vocabulary) The type of molecule that was extracted from the biological material. Include one of the following: total RNA, polyA RNA, cytoplasmic RNA, nuclear RNA, small RNA, genomic DNA, protein, or other.', max_length=20, null=True),
),
migrations.AlterField(
model_name='experiment',
name='molecule_ontology',
field=django.contrib.postgres.fields.jsonb.JSONField(help_text='(Ontology: SO) links to molecule ontology information.', null=True, validators=[chord_metadata_service.restapi.validators.JsonSchemaValidator({'$id': 'ONTOLOGY_CLASS_LIST', '$schema': 'http://json-schema.org/draft-07/schema#', 'description': 'Ontology class list', 'items': {'$id': 'ONTOLOGY_CLASS', '$schema': 'http://json-schema.org/draft-07/schema#', 'additionalProperties': False, 'description': 'todo', 'properties': {'id': {'description': 'CURIE style identifier.', 'type': 'string'}, 'label': {'description': 'Human-readable class name.', 'type': 'string'}}, 'required': ['id', 'label'], 'title': 'Ontology class schema', 'type': 'object'}, 'title': 'Ontology class list', 'type': 'array'})]),
),
migrations.AlterField(
model_name='experiment',
name='other_fields',
field=django.contrib.postgres.fields.jsonb.JSONField(help_text='The other fields for the experiment', null=True, validators=[chord_metadata_service.restapi.validators.JsonSchemaValidator({'$id': 'KEY_VALUE_OBJECT', '$schema': 'http://json-schema.org/draft-07/schema#', 'additionalProperties': False, 'description': 'The schema represents a key-value object.', 'patternProperties': {'^.*$': {'type': 'string'}}, 'title': 'Key-value object', 'type': 'object'})]),
),
migrations.AlterField(
model_name='experiment',
name='reference_registry_id',
field=models.CharField(help_text='The IHEC EpiRR ID for this dataset, only for IHEC Reference Epigenome datasets. Otherwise leave empty.', max_length=30, null=True),
),
]
19 changes: 8 additions & 11 deletions chord_metadata_service/experiments/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@
from django.contrib.postgres.fields import JSONField, ArrayField
from chord_metadata_service.restapi.models import IndexableMixin
from chord_metadata_service.restapi.description_utils import rec_help
from chord_metadata_service.restapi.validators import JsonSchemaValidator
from chord_metadata_service.restapi.validators import ontologyListValidator, keyValueValidator
from chord_metadata_service.restapi.schemas import ONTOLOGY_CLASS_LIST, KEY_VALUE_OBJECT
import chord_metadata_service.experiments.descriptions as d

ontologyListValidator = JsonSchemaValidator(ONTOLOGY_CLASS_LIST)
keyValueValidator = JsonSchemaValidator(KEY_VALUE_OBJECT)

class Experiment(models.Model, IndexableMixin):
""" Class to store Experiment information """

Expand Down Expand Up @@ -41,16 +38,16 @@ class Experiment(models.Model, IndexableMixin):

id = CharField(primary_key=True, max_length=200, help_text=rec_help(d.EXPERIMENT, 'id'))

reference_registry_id = CharField(max_length=30, null=True, blank=True, help_text=rec_help(d.EXPERIMENT, 'reference_registry_id'))
reference_registry_id = CharField(max_length=30, null=True, help_text=rec_help(d.EXPERIMENT, 'reference_registry_id'))
qc_flags = ArrayField(CharField(max_length=100, help_text=rec_help(d.EXPERIMENT, 'qc_flags')), null=True, default=list)
experiment_type = CharField(max_length=30, null=False, blank=False, help_text=rec_help(d.EXPERIMENT, 'experiment_type'))
experiment_ontology = JSONField(null=True, blank=True, validators=[ontologyListValidator], help_text=rec_help(d.EXPERIMENT, 'experiment_ontology'))
molecule_ontology = JSONField(null=True, blank=True, validators=[ontologyListValidator], help_text=rec_help(d.EXPERIMENT, 'molecule_ontology'))
molecule = CharField(choices=MOLECULE, max_length=20, null=True, blank=True, help_text=rec_help(d.EXPERIMENT, 'molecule'))
experiment_type = CharField(max_length=30, help_text=rec_help(d.EXPERIMENT, 'experiment_type'))
experiment_ontology = JSONField(null=True, validators=[ontologyListValidator], help_text=rec_help(d.EXPERIMENT, 'experiment_ontology'))
molecule_ontology = JSONField(null=True, validators=[ontologyListValidator], help_text=rec_help(d.EXPERIMENT, 'molecule_ontology'))
molecule = CharField(choices=MOLECULE, max_length=20, null=True, help_text=rec_help(d.EXPERIMENT, 'molecule'))

library_strategy = CharField(choices=LIBRARY_STRATEGY, max_length=25, null=False, blank=False, help_text=rec_help(d.EXPERIMENT, 'library_strategy'))
library_strategy = CharField(choices=LIBRARY_STRATEGY, max_length=25, help_text=rec_help(d.EXPERIMENT, 'library_strategy'))

other_fields = JSONField(blank=True, null=True, validators=[keyValueValidator], help_text=rec_help(d.EXPERIMENT, 'other_fields'))
other_fields = JSONField(null=True, validators=[keyValueValidator], help_text=rec_help(d.EXPERIMENT, 'other_fields'))

def __str__(self):
return str(self.id)
11 changes: 11 additions & 0 deletions chord_metadata_service/experiments/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from rest_framework import serializers
from chord_metadata_service.restapi.schemas import ONTOLOGY_CLASS, AGE_OR_AGE_RANGE
from chord_metadata_service.restapi.validators import JsonSchemaValidator, ontologyListValidator, keyValueValidator
from chord_metadata_service.restapi.serializers import GenericSerializer
from .models import Experiment


class ExperimentSerializer(GenericSerializer):
class Meta:
model = Experiment
fields = '__all__'
4 changes: 4 additions & 0 deletions chord_metadata_service/restapi/urls.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.urls import path, include
from rest_framework import routers
from chord_metadata_service.chord import api_views as chord_views
from chord_metadata_service.experiments import api_views as experiment_views
from chord_metadata_service.patients import api_views as individual_views
from chord_metadata_service.phenopackets import api_views as phenopacket_views

Expand All @@ -10,6 +11,9 @@

router = routers.DefaultRouter(trailing_slash=False)

# Experiments app urls
router.register(r'experiments', experiment_views.ExperimentViewSet)

# Patients app urls
router.register(r'individuals', individual_views.IndividualViewSet)

Expand Down
6 changes: 6 additions & 0 deletions chord_metadata_service/restapi/validators.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from rest_framework import serializers
from jsonschema import Draft7Validator
from chord_metadata_service.restapi.schemas import ONTOLOGY_CLASS, ONTOLOGY_CLASS_LIST, KEY_VALUE_OBJECT, AGE_OR_AGE_RANGE

class JsonSchemaValidator(object):
""" Custom class based validator to validate against Json schema for JSONField """
Expand All @@ -22,3 +23,8 @@ def deconstruct(self):
[self.schema],
{}
)

ontologyValidator = JsonSchemaValidator(ONTOLOGY_CLASS)
ontologyListValidator = JsonSchemaValidator(ONTOLOGY_CLASS_LIST)
keyValueValidator = JsonSchemaValidator(KEY_VALUE_OBJECT)
ageOrAgeRangeValidator = JsonSchemaValidator(AGE_OR_AGE_RANGE)

0 comments on commit 061c43e

Please sign in to comment.