-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserializers.py
178 lines (136 loc) · 6.59 KB
/
serializers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
from rest_framework import serializers
from genes.serializers import GeneGEMWithType
from user_files.models_choices import MoleculeType
from .models import Biomarker, MRNAIdentifier, MethylationIdentifier, CNAIdentifier, MiRNAIdentifier, MoleculeIdentifier
from tags.serializers import TagSerializer
from drf_writable_nested import WritableNestedModelSerializer
class MoleculeIdentifierSerializer(serializers.Serializer):
"""
Serializer for all the types of molecules (MRNAIdentifier, MiRNAIdentifier, CNAIdentifier, or
MethylationIdentifier). Gets their ID and identifier only.
"""
id = serializers.IntegerField()
identifier = serializers.CharField()
type = serializers.SerializerMethodField(method_name='get_type')
@staticmethod
def get_type(instance: MoleculeIdentifier) -> MoleculeType:
if isinstance(instance, MRNAIdentifier):
return MoleculeType.MRNA
if isinstance(instance, MiRNAIdentifier):
return MoleculeType.MIRNA
if isinstance(instance, CNAIdentifier):
return MoleculeType.CNA
return MoleculeType.METHYLATION
class MRNAIdentifierSerializer(serializers.ModelSerializer):
"""MRNAIdentifier serializer"""
class Meta:
model = MRNAIdentifier
exclude = ['biomarker']
class MiRNAIdentifierSerializer(serializers.ModelSerializer):
"""MiRNAIdentifier serializer"""
class Meta:
model = MiRNAIdentifier
exclude = ['biomarker']
class CNAIdentifierSerializer(serializers.ModelSerializer):
"""CNAIdentifier serializer"""
class Meta:
model = CNAIdentifier
exclude = ['biomarker']
class MethylationIdentifierSerializer(serializers.ModelSerializer):
"""MethylationIdentifier serializer"""
class Meta:
model = MethylationIdentifier
exclude = ['biomarker']
class BiomarkerSimpleSerializer(WritableNestedModelSerializer):
"""Biomarker model serializer without the molecules (useful to list Biomarkers)."""
number_of_mrnas = serializers.SerializerMethodField(method_name='get_number_of_mrnas')
number_of_mirnas = serializers.SerializerMethodField(method_name='get_number_of_mirnas')
number_of_cnas = serializers.SerializerMethodField(method_name='get_number_of_cnas')
number_of_methylations = serializers.SerializerMethodField(method_name='get_number_of_methylations')
has_fs_experiment = serializers.SerializerMethodField(method_name='get_has_fs_experiment')
origin = serializers.IntegerField(required=False)
state = serializers.IntegerField(required=False)
tag = TagSerializer(required=False)
class Meta:
model = Biomarker
exclude = ['user']
@staticmethod
def get_number_of_mrnas(ins: Biomarker) -> int:
"""Gets the number of genes in this Biomarker"""
return ins.number_of_mrnas
@staticmethod
def get_number_of_mirnas(ins: Biomarker) -> int:
"""Gets the number of miRNAs in this Biomarker"""
return ins.number_of_mirnas
@staticmethod
def get_number_of_cnas(ins: Biomarker) -> int:
"""Gets the number of CNAs in this Biomarker"""
return ins.number_of_cnas
@staticmethod
def get_number_of_methylations(ins: Biomarker) -> int:
"""Gets the number of Methylations in this Biomarker"""
return ins.number_of_methylations
@staticmethod
def get_has_fs_experiment(ins: Biomarker) -> bool:
"""Gets if the current Biomarker was created from a Feature Selection experiment"""
return ins.has_fs_experiment
class BiomarkerSimpleUpdateSerializer(serializers.ModelSerializer):
"""
This serializer is used to update only the name and description of the Biomarker model to prevent updating all
the molecules when it's not necessary.
"""
class Meta:
model = Biomarker
fields = ['name', 'description']
class BiomarkerSerializer(WritableNestedModelSerializer):
"""Biomarker model serializer (useful to create/edit a Biomarker)."""
number_of_mrnas = serializers.SerializerMethodField(method_name='get_number_of_mrnas')
number_of_mirnas = serializers.SerializerMethodField(method_name='get_number_of_mirnas')
number_of_cnas = serializers.SerializerMethodField(method_name='get_number_of_cnas')
number_of_methylations = serializers.SerializerMethodField(method_name='get_number_of_methylations')
has_fs_experiment = serializers.SerializerMethodField(method_name='get_has_fs_experiment')
was_already_used = serializers.SerializerMethodField(method_name='get_was_already_used')
mrnas = MRNAIdentifierSerializer(many=True, required=False)
mirnas = MiRNAIdentifierSerializer(many=True, required=False)
cnas = CNAIdentifierSerializer(many=True, required=False)
methylations = MethylationIdentifierSerializer(many=True, required=False)
origin = serializers.IntegerField(required=False)
state = serializers.IntegerField(required=False)
tag = TagSerializer(required=False)
class Meta:
model = Biomarker
exclude = ['user']
@staticmethod
def get_number_of_mrnas(ins: Biomarker) -> int:
"""Gets the number of genes in this Biomarker"""
return ins.number_of_mrnas
@staticmethod
def get_number_of_mirnas(ins: Biomarker) -> int:
"""Gets the number of miRNAs in this Biomarker"""
return ins.number_of_mirnas
@staticmethod
def get_number_of_cnas(ins: Biomarker) -> int:
"""Gets the number of CNAs in this Biomarker"""
return ins.number_of_cnas
@staticmethod
def get_number_of_methylations(ins: Biomarker) -> int:
"""Gets the number of Methylations in this Biomarker"""
return ins.number_of_methylations
@staticmethod
def get_has_fs_experiment(ins: Biomarker) -> bool:
"""Gets if the current Biomarker was created from a Feature Selection experiment"""
return ins.has_fs_experiment
@staticmethod
def get_was_already_used(ins: Biomarker) -> bool:
"""
Returns True if this Biomarker was used for an Inference experiment, Statistical Validation or Trained Model.
This avoids the user to edit a Biomarker that was already used and generate inconsistencies.
"""
return ins.was_already_used
class BiomarkerFromCorrelationAnalysisSerializer(serializers.Serializer):
"""
Serializer for BiomarkerFromCorrelationAnalysis, including correlation analysis ID, gene GEM list, and correlation threshold.
"""
correlation_analysis_id = serializers.IntegerField()
gene_gem_list = GeneGEMWithType(many=True, required=False)
correlation_threshold = serializers.IntegerField(required=False)