Skip to content

Commit

Permalink
Merge pull request #118 from c3g/rearrange-schemas-validators
Browse files Browse the repository at this point in the history
Rearrange schemas and validators
  • Loading branch information
zxenia committed May 13, 2020
2 parents a41084c + 607a27b commit 61dc5f8
Show file tree
Hide file tree
Showing 23 changed files with 878 additions and 287 deletions.
16 changes: 16 additions & 0 deletions chord_metadata_service/experiments/api_views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
from rest_framework import viewsets
from rest_framework.settings import api_settings
from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import AllowAny
from rest_framework.response import Response

from .serializers import ExperimentSerializer
from .models import Experiment
from .schemas import EXPERIMENT_SCHEMA
from chord_metadata_service.restapi.pagination import LargeResultsSetPagination


Expand All @@ -18,3 +23,14 @@ class ExperimentViewSet(viewsets.ModelViewSet):
serializer_class = ExperimentSerializer
pagination_class = LargeResultsSetPagination
renderer_classes = tuple(api_settings.DEFAULT_RENDERER_CLASSES)


@api_view(["GET"])
@permission_classes([AllowAny])
def get_experiment_schema(_request):
"""
get:
Experiment schema
"""
return Response(EXPERIMENT_SCHEMA)

2 changes: 1 addition & 1 deletion chord_metadata_service/experiments/descriptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

"other_fields": "The other fields for the experiment",

"biosample": "Biosamples on which this experiment was done",
"biosample": "Biosample on which this experiment was done",
"individual": "Donor on which this experiment was done",

**EXTRA_PROPERTIES
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Generated by Django 2.2.12 on 2020-05-13 18:01

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


class Migration(migrations.Migration):

dependencies = [
('experiments', '0004_auto_20200401_1445'),
]

operations = [
migrations.AlterField(
model_name='experiment',
name='biosample',
field=models.ForeignKey(blank=True, help_text='Biosample on which this experiment was done', null=True, on_delete=django.db.models.deletion.SET_NULL, to='phenopackets.Biosample'),
),
migrations.AlterField(
model_name='experiment',
name='experiment_ontology',
field=django.contrib.postgres.fields.jsonb.JSONField(blank=True, help_text='(Ontology: OBI) links to experiment ontology information.', null=True, validators=[chord_metadata_service.restapi.validators.JsonSchemaValidator({'$id': 'chord_metadata_service:ontology_class_list_schema', '$schema': 'http://json-schema.org/draft-07/schema#', 'description': 'Ontology class list', 'items': {'$id': 'chord_metadata_service:ontology_class_schema', '$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'}, formats=None)]),
),
migrations.AlterField(
model_name='experiment',
name='molecule_ontology',
field=django.contrib.postgres.fields.jsonb.JSONField(blank=True, help_text='(Ontology: SO) links to molecule ontology information.', null=True, validators=[chord_metadata_service.restapi.validators.JsonSchemaValidator({'$id': 'chord_metadata_service:ontology_class_list_schema', '$schema': 'http://json-schema.org/draft-07/schema#', 'description': 'Ontology class list', 'items': {'$id': 'chord_metadata_service:ontology_class_schema', '$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'}, formats=None)]),
),
migrations.AlterField(
model_name='experiment',
name='other_fields',
field=django.contrib.postgres.fields.jsonb.JSONField(blank=True, help_text='The other fields for the experiment', null=True, validators=[chord_metadata_service.restapi.validators.JsonSchemaValidator({'$id': 'chord_metadata_service:key_value_object_schema', '$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'}, formats=None)]),
),
]
29 changes: 19 additions & 10 deletions chord_metadata_service/experiments/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,31 @@ 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, blank=True, 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, blank=True, default=list)
reference_registry_id = CharField(max_length=30, blank=True, 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, blank=True, default=list)
experiment_type = CharField(max_length=30, help_text=rec_help(d.EXPERIMENT, 'experiment_type'))
experiment_ontology = JSONField(blank=True, null=True, validators=[ontology_list_validator], help_text=rec_help(d.EXPERIMENT, 'experiment_ontology'))
molecule_ontology = JSONField(blank=True, null=True, validators=[ontology_list_validator], help_text=rec_help(d.EXPERIMENT, 'molecule_ontology'))
molecule = CharField(choices=MOLECULE, max_length=20, blank=True, null=True, help_text=rec_help(d.EXPERIMENT, 'molecule'))
library_strategy = CharField(choices=LIBRARY_STRATEGY, max_length=25, help_text=rec_help(d.EXPERIMENT, 'library_strategy'))
experiment_ontology = JSONField(blank=True, null=True, validators=[ontology_list_validator],
help_text=rec_help(d.EXPERIMENT, 'experiment_ontology'))
molecule_ontology = JSONField(blank=True, null=True, validators=[ontology_list_validator],
help_text=rec_help(d.EXPERIMENT, 'molecule_ontology'))
molecule = CharField(choices=MOLECULE, max_length=20, blank=True, null=True,
help_text=rec_help(d.EXPERIMENT, 'molecule'))
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=[key_value_validator], help_text=rec_help(d.EXPERIMENT, 'other_fields'))
other_fields = JSONField(blank=True, null=True, validators=[key_value_validator],
help_text=rec_help(d.EXPERIMENT, 'other_fields'))

biosample = models.ForeignKey(Biosample, on_delete=models.SET_NULL, blank=True, null=True, help_text=rec_help(d.EXPERIMENT, 'biosample'))
individual = models.ForeignKey(Individual, on_delete=models.SET_NULL, blank=True, null=True, help_text=rec_help(d.EXPERIMENT, 'individual'))
biosample = models.ForeignKey(Biosample, on_delete=models.SET_NULL, blank=True, null=True,
help_text=rec_help(d.EXPERIMENT, 'biosample'))
individual = models.ForeignKey(Individual, on_delete=models.SET_NULL, blank=True, null=True,
help_text=rec_help(d.EXPERIMENT, 'individual'))

def clean(self):
if not (self.biosample or self.individual):
raise ValidationError('Either Biosamples or Individual must be specified')
raise ValidationError('Either Biosample or Individual must be specified')

def __str__(self):
return str(self.id)
57 changes: 57 additions & 0 deletions chord_metadata_service/experiments/schemas.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from .descriptions import EXPERIMENT
from chord_metadata_service.restapi.description_utils import describe_schema
from chord_metadata_service.restapi.schemas import ONTOLOGY_CLASS_LIST, KEY_VALUE_OBJECT


EXPERIMENT_SCHEMA = describe_schema({
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "chord_metadata_service:experiment_schema",
"title": "Experiment schema",
"description": "Schema for describing an experiment.",
"type": "object",
"properties": {
"id": {
"type": "string"
},
"reference_registry_id": {
"type": "string"
},
"qc_flags": {
"type": "array",
"items": {
"type": "string"
}
},
"experiment_type": {
"type": "string"
},
"experiment_ontology": ONTOLOGY_CLASS_LIST,
"molecule": {
"type": "string"
},
"molecule_ontology": ONTOLOGY_CLASS_LIST,
"library_strategy": {
"type": "string",
"enum": [
"DNase-Hypersensitivity",
"ATAC-seq",
"NOME-Seq",
"Bisulfite-Seq",
"MeDIP-Seq",
"MRE-Seq",
"ChIP-Seq",
"RNA-Seq",
"miRNA-Seq",
"WGS"
]
},
"other_fields": KEY_VALUE_OBJECT,
"biosample": {
"type": "string"
},
"individual": {
"type": "string"
}
},
"required": ["id", "experiment_type", "library_strategy"]
}, EXPERIMENT)
15 changes: 15 additions & 0 deletions chord_metadata_service/mcode/api_views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
from rest_framework import viewsets
from rest_framework.settings import api_settings
from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import AllowAny
from rest_framework.response import Response
from .serializers import *
from .schemas import MCODE_SCHEMA
from .models import *
from .serializers import *
from chord_metadata_service.restapi.api_renderers import PhenopacketsRenderer
Expand Down Expand Up @@ -54,3 +59,13 @@ class MedicationStatementViewSet(McodeModelViewSet):
class MCodePacketViewSet(McodeModelViewSet):
queryset = MCodePacket.objects.all()
serializer_class = MCodePacketSerializer


@api_view(["GET"])
@permission_classes([AllowAny])
def get_mcode_schema(_request):
"""
get:
Mcodepacket schema
"""
return Response(MCODE_SCHEMA)

0 comments on commit 61dc5f8

Please sign in to comment.