Application API¶
The EMERGEN-DB API is built from the Django REST framework whose official website is available here.
How to add an API?¶
Step 1 - Create a serializer in the serializers.py
Step 2 - Create a view in
view.py
Step 3 - Create a url to access the API in the
ulrs.py
;Step 4 - Test the API in the
test.py
file. Many examples are already available in the file.
A complete example is available on the official documentation and APIs are already available in the application. They can help you in the structure to follow.
Key files of the « API » application¶
serializers.py¶
The EMERGEN-DB API is built from the Django REST framework. To format the results returned by the API, serializers are created whose official documentation is here :
- class apps.api.serializers.CountByTechSerializer(*args, **kwargs)¶
Count by technology serializer
- class Meta¶
Fields return by serializer
- model¶
alias de
apps.collector.models.result
- get_Count(res)¶
The function get_Count returns the count of objects based on certain conditions, depending on the user’s role and group membership.
- Paramètres
res – The parameter res is a dictionary that contains the key « technologie_sequencage ». The value of this key is used to filter the result objects in the database
- Renvoie
The code is returning the count of objects in the « result » model that meet certain conditions. If the user is a superuser, data analyst, or manager, it returns the count of objects where the « technologie_sequencage » field matches the value provided in the « res » parameter. If the user is not one of those roles, it returns the count of objects where the «
- class apps.api.serializers.CountByVarSerializer(*args, **kwargs)¶
Count by variants serializer
- class Meta¶
Fields return by serializer
- model¶
alias de
apps.collector.models.result
- get_Count(res)¶
The function get_Count returns the count of objects based on certain conditions, depending on the user’s role and the provided result name.
- Paramètres
res – The parameter « res » is a dictionary that contains the name of the result
- Renvoie
The code is returning the count of objects that meet the specified conditions. If the user is a superuser, data analyst, or manager, it returns the count of objects where the « resultat__name » field matches the value provided in the « res » parameter. If the user is not one of those roles, it filters the objects based on the user’s group membership and returns the count
- get_Resultat(res)¶
The function get_Resultat checks the user’s role and returns a result based on their permissions.
- Paramètres
res – The parameter res is a dictionary that contains the result of some operation. It is assumed to have a key called « res » which holds the actual result value
- Renvoie
The code is returning the value of res[« res »].
- class apps.api.serializers.departementSerializer(*args, **kwargs)¶
Departement serializer
- class Meta¶
Fields return by serializer
- model¶
alias de
apps.stats.models.departement
- get_count_var(departement)¶
The function get_count_var returns the count of objects based on certain conditions related to the user’s role and department.
- Paramètres
departement – The departement parameter represents a department object. It is used to filter the results based on the code of the department
- Renvoie
the count of objects in the « result » model that meet certain conditions. The conditions depend on the user’s role and the department code. If the user is a superuser, data analyst, or manager, the count is based on the department code only. If the user is not one of those roles, the count is based on the department code and the user’s group membership
- class apps.api.serializers.experimentSerializer(*args, **kwargs)¶
-
- get_Team(experiment)¶
The function returns the name of the team associated with a given experiment.
- Paramètres
experiment – The « experiment » parameter is an object that represents an experiment
- Renvoie
The code is returning the name of the team associated with the given experiment.
- class apps.api.serializers.regionSerializer(*args, **kwargs)¶
Region serializer
- class Meta¶
Fields return by serializer
- model¶
alias de
apps.stats.models.region
- get_count_var(region)¶
The function get_count_var returns the count of result objects based on the region parameter and the user’s permissions.
- Paramètres
region – The « region » parameter is an object that represents a specific region. It likely has a « code » attribute that is used in the filtering of the queryset
- Renvoie
the count of objects in the « result » model that meet certain conditions. The conditions depend on the user’s role and the region code. If the user is a superuser, data analyst, or manager, the count is based on the « departement_preleveur__region__code » field. If the user is not one of those roles, the count is based
- class apps.api.serializers.resultsSerializer(*args, **kwargs)¶
Result serializer
- class Meta¶
Fields return by serializer
- model¶
alias de
apps.collector.models.result
- get_Labo_sequenceur(result)¶
The function « get_Labo_sequenceur » returns the name of the labo_sequenceur attribute of the given result object.
- Paramètres
result – The parameter « result » is an object that contains information about a labo sequenceur
- Renvoie
the name of the labo_sequenceur attribute of the result object.
- get_Resultat(result)¶
The function get_Resultat returns the variant name from the auto_result field of a queryset element.
- Paramètres
result – A queryset element
- Renvoie
the Variant name from the auto_result field of the result queryset element.
- get_dep_prelev(result)¶
The function « get_dep_prelev » returns the code of the department of the prelevor if it exists, otherwise it returns None.
- Paramètres
result – The parameter « result » is an object that contains information about a prelevement (sampling). It likely has attributes such as « departement_preleveur » which represents the department from which the sampling was taken
- Renvoie
The code is returning the code of the department if the result has a non-null value for the « departement_preleveur » attribute. If the « departement_preleveur » attribute is null, it returns None.
tasks.py¶
- exception apps.api.tasks.TaskFailure¶
- apps.api.tasks.create_tar_gz(typeheader, seq, res, listNotIn, task_ID, folderName, tarName)¶
The create_tar_gz function generates a tar.gz file containing fasta sequences and their corresponding metadata.
- Paramètres
typeheader – The typeheader parameter is a string that represents the type of header to be generated for each sequence record
seq – A list of sequence records. Each record should have a « sequence » attribute
res – The res parameter is a list of results. It is used in the else block of the code to iterate over both the seq and res lists simultaneously. Each record in seq is paired with the corresponding resu in res using
listNotIn – A list of UIDs that are not found in the sequence data
task_ID – The task ID is a unique identifier for the task being performed. It is used to create a directory and name the files associated with the task
folderName – The name of the folder where the files will be stored
tarName – The tarName parameter is the name of the resulting tar.gz file that will be generated
- apps.api.tasks.generate_header(typeheader, record, res)¶
The function generate_header generates the appropriate header based on the given typeheader parameter.
- Paramètres
typeheader – The parameter « typeheader » is a string that specifies the type of header to generate. It can have three possible values: « Result_ID », « Sequence_ID », or « Human_readable »
record – The record parameter is an object that contains information about a specific record. It likely has attributes such as result_id, seq_id, and others that are used to generate the appropriate header
res – The parameter « res » is an object that contains information about a result. It has the following attributes:
- Renvoie
the appropriate header based on the value of the « typeheader » parameter.
ulrs.py¶
The urls.py
file contains :