Commit b1fac06e authored by Kenzo-Hugo Hillion's avatar Kenzo-Hugo Hillion
Browse files

add property to Taxonomy to retrieve one line taxonomy description

parent 8c35c9e0
Pipeline #37427 failed with stages
in 17 seconds
......@@ -40,6 +40,8 @@ class DbGenerator:
rank=rank,
parent=getattr(self, "last_tax", None)
)
else:
self.last_tax = models.Taxonomy.objects.get(tax_id=desc['tax_id'])
self.created_ids.add(desc['tax_id'])
self.last_tax.build_hierarchy()
......
......@@ -11,6 +11,7 @@ from metagenedb.apps.catalog.models import (
from metagenedb.apps.catalog.management.commands.compute_stats import (
ComputeStatistics, ComputeCounts, ComputeGeneLength, ComputeTaxonomyRepartition, ComputeTaxonomyPresence
)
from metagenedb.apps.catalog.management.commands.compute_stats import clean_db as clean_db_stats
logging.basicConfig(format='[%(asctime)s] %(levelname)s:%(name)s:%(message)s')
logger = logging.getLogger()
......@@ -37,7 +38,7 @@ def create_genes_db():
def compute_stats():
ComputeStatistics('all').clean_db()
clean_db_stats()
for gene_source in ['all', 'virgo', 'igc']:
ComputeCounts(gene_source).all()
ComputeGeneLength(gene_source).all()
......
from django.db import models
from django.contrib.postgres.fields import JSONField
class Statistics(models.Model):
......@@ -8,7 +7,7 @@ class Statistics(models.Model):
"""
stats_id = models.SlugField(max_length=400, db_index=True, unique=True)
body = JSONField()
body = models.JSONField()
class Meta:
verbose_name_plural = "Statistics"
from django.db import models
from django.contrib.postgres.fields import JSONField
class Taxonomy(models.Model):
......@@ -52,7 +51,7 @@ class Taxonomy(models.Model):
on_delete=models.SET_NULL,
null=True, blank=True,
)
hierarchy = JSONField(null=True)
hierarchy = models.JSONField(null=True)
def __str__(self):
return f"{self.name}"
......@@ -73,6 +72,32 @@ class Taxonomy(models.Model):
self.save()
return hierarchy
def _compute_one_line_detailed_taxonomy(self) -> str:
default_item = {
'name': ''
}
if self.hierarchy is None:
self.build_hierarchy()
if self.hierarchy.get('species', None) is None:
s = ''
else:
s = self.hierarchy.get('species')['name'].split()[-1]
return "k__{k}; p__{p}; c__{c}; o__{o}; f__{f}; g__{g}; s__{s}".format(
k=self.hierarchy.get('kingdom', self.hierarchy.get('superkingdom', default_item))['name'],
p=self.hierarchy.get('phylum', default_item)['name'],
c=self.hierarchy.get('class', default_item)['name'],
o=self.hierarchy.get('order', default_item)['name'],
f=self.hierarchy.get('family', default_item)['name'],
g=self.hierarchy.get('genus', default_item)['name'],
s=s
)
@property
def one_line_detailed_taxonomy(self) -> str:
if getattr(self, '_one_line_detailed_taxonomy', None) is None:
self._one_line_detailed_taxonomy = self._compute_one_line_detailed_taxonomy()
return self._one_line_detailed_taxonomy
class Meta:
verbose_name_plural = "Taxonomy"
ordering = ['-tax_id']
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment