Skip to content
Snippets Groups Projects
Commit fe09bec9 authored by Kenzo-Hugo Hillion's avatar Kenzo-Hugo Hillion :recycle:
Browse files

Handle eggnog with multiple functional categories

parent f0fb2d72
No related branches found
No related tags found
2 merge requests!59Prod,!25integration eggnog to catalog
...@@ -25,7 +25,7 @@ class EggNogAdmin(admin.ModelAdmin): ...@@ -25,7 +25,7 @@ class EggNogAdmin(admin.ModelAdmin):
def get_functional_categories(self, obj): def get_functional_categories(self, obj):
if obj.functional_categories.all(): if obj.functional_categories.all():
return ",".join([str(f) for f in obj.functional_categories.all()]) return ", ".join([str(f) for f in obj.functional_categories.all()])
return '-' return '-'
get_functional_categories.short_description = 'Functional categories' get_functional_categories.short_description = 'Functional categories'
......
...@@ -27,12 +27,11 @@ class ImportEggNog(object): ...@@ -27,12 +27,11 @@ class ImportEggNog(object):
all_categories = EggNogFunctionalCategory.objects.all() all_categories = EggNogFunctionalCategory.objects.all()
if not all_categories: if not all_categories:
raise Exception("You need to create Functional categories first.") raise Exception("You need to create Functional categories first.")
self.functional_cat = {cat.category_id: cat for cat in all_categories} self.functional_cat_instances = {cat.category_id: cat for cat in all_categories}
def link_functional_category(self, eggnog_dict): def add_functional_categories(self, eggnog, functional_category_keys):
cat_key = eggnog_dict.get('functional_category', 'S') for key in functional_category_keys:
category = self.functional_cat.get(cat_key) eggnog.functional_categories.add(self.functional_cat_instances[key])
eggnog_dict.update({'functional_category': category})
def load_all(self, test=False): def load_all(self, test=False):
self._build_functional_category_dict() self._build_functional_category_dict()
...@@ -40,8 +39,9 @@ class ImportEggNog(object): ...@@ -40,8 +39,9 @@ class ImportEggNog(object):
with open(self.annotation_file, "r") as file: with open(self.annotation_file, "r") as file:
for line in file: for line in file:
eggnog_dict = self.eggnog_parser.get_dict(line) eggnog_dict = self.eggnog_parser.get_dict(line)
self.link_functional_category(eggnog_dict) functional_category_keys = eggnog_dict.pop('functional_categories') # link later
payload = {k: v for k, v in eggnog_dict.items() if v != ""} payload = {k: v for k, v in eggnog_dict.items() if v != ""}
eggnog = None
try: try:
eggnog = EggNog(**payload) eggnog = EggNog(**payload)
eggnog.full_clean() eggnog.full_clean()
...@@ -66,6 +66,9 @@ class ImportEggNog(object): ...@@ -66,6 +66,9 @@ class ImportEggNog(object):
self.skipped_errors.append(validation_error) self.skipped_errors.append(validation_error)
self.skipped_ids.append(payload.get('function_id')) self.skipped_ids.append(payload.get('function_id'))
self.skipped_count += 1 self.skipped_count += 1
if eggnog is not None:
self.add_functional_categories(eggnog, functional_category_keys)
eggnog.save()
self.processed_count += 1 self.processed_count += 1
if self.processed_count % 1000 == 0: if self.processed_count % 1000 == 0:
logger.info("%s/%s EggNog processed so far...", self.processed_count, self.total_eggnog_nb) logger.info("%s/%s EggNog processed so far...", self.processed_count, self.total_eggnog_nb)
......
...@@ -13,7 +13,7 @@ class EggNogAnnotationLineParser(object): ...@@ -13,7 +13,7 @@ class EggNogAnnotationLineParser(object):
try: try:
elements = line.split('\t') elements = line.split('\t')
return { return {
'functional_category': list(elements[2]), 'functional_categories': list(elements[2]),
'function_id': elements[1], 'function_id': elements[1],
'name': elements[3].rstrip().split('.')[0], 'name': elements[3].rstrip().split('.')[0],
} }
......
...@@ -10,7 +10,7 @@ class TestEggNogAnnotationLineParser(TestCase): ...@@ -10,7 +10,7 @@ class TestEggNogAnnotationLineParser(TestCase):
expected_dict = { expected_dict = {
'function_id': "28H54", 'function_id': "28H54",
'name': "translational termination", 'name': "translational termination",
'functional_category': ["K"] 'functional_categories': ["K"]
} }
test_dict = EggNogAnnotationLineParser.get_dict(ko_line) test_dict = EggNogAnnotationLineParser.get_dict(ko_line)
self.assertDictEqual(test_dict, expected_dict) self.assertDictEqual(test_dict, expected_dict)
...@@ -20,7 +20,7 @@ class TestEggNogAnnotationLineParser(TestCase): ...@@ -20,7 +20,7 @@ class TestEggNogAnnotationLineParser(TestCase):
expected_dict = { expected_dict = {
'function_id': "28H50", 'function_id': "28H50",
'name': "", 'name': "",
'functional_category': ["S"] 'functional_categories': ["S"]
} }
test_dict = EggNogAnnotationLineParser.get_dict(ko_line) test_dict = EggNogAnnotationLineParser.get_dict(ko_line)
self.assertDictEqual(test_dict, expected_dict) self.assertDictEqual(test_dict, expected_dict)
...@@ -30,7 +30,7 @@ class TestEggNogAnnotationLineParser(TestCase): ...@@ -30,7 +30,7 @@ class TestEggNogAnnotationLineParser(TestCase):
expected_dict = { expected_dict = {
'function_id': "28H50", 'function_id': "28H50",
'name': "Glucose-responsive transcription factor that regulates expression of several glucose transporter (HXT) genes in response to glucose", # noqa 'name': "Glucose-responsive transcription factor that regulates expression of several glucose transporter (HXT) genes in response to glucose", # noqa
'functional_category': ["S"] 'functional_categories': ["S"]
} }
test_dict = EggNogAnnotationLineParser.get_dict(ko_line) test_dict = EggNogAnnotationLineParser.get_dict(ko_line)
self.assertDictEqual(test_dict, expected_dict) self.assertDictEqual(test_dict, expected_dict)
...@@ -40,7 +40,7 @@ class TestEggNogAnnotationLineParser(TestCase): ...@@ -40,7 +40,7 @@ class TestEggNogAnnotationLineParser(TestCase):
expected_dict = { expected_dict = {
'function_id': "28H54", 'function_id': "28H54",
'name': "translational termination", 'name': "translational termination",
'functional_category': ["K", "S"] 'functional_categories': ["K", "S"]
} }
test_dict = EggNogAnnotationLineParser.get_dict(ko_line) test_dict = EggNogAnnotationLineParser.get_dict(ko_line)
self.assertDictEqual(test_dict, expected_dict) self.assertDictEqual(test_dict, expected_dict)
......
...@@ -5,7 +5,13 @@ import os ...@@ -5,7 +5,13 @@ import os
import sys import sys
from itertools import islice from itertools import islice
from bioapi import MetageneDBCatalogFunctionAPI, MetageneDBCatalogGeneAPI, MetageneDBCatalogTaxonomyAPI from bioapi import (
MetageneDBCatalogFunctionAPI,
MetageneDBCatalogGeneAPI,
MetageneDBCatalogTaxonomyAPI,
MetageneDBCatalogKeggOrthologyAPI,
MetageneDBCatalogEggNogAPI
)
from requests.exceptions import HTTPError from requests.exceptions import HTTPError
from slugify import slugify from slugify import slugify
...@@ -19,6 +25,8 @@ class ImportIGCGenes(object): ...@@ -19,6 +25,8 @@ class ImportIGCGenes(object):
METAGENEDB_GENE_API = MetageneDBCatalogGeneAPI METAGENEDB_GENE_API = MetageneDBCatalogGeneAPI
METAGENEDB_TAXONOMY_API = MetageneDBCatalogTaxonomyAPI METAGENEDB_TAXONOMY_API = MetageneDBCatalogTaxonomyAPI
METAGENEDB_FUNCTION_API = MetageneDBCatalogFunctionAPI METAGENEDB_FUNCTION_API = MetageneDBCatalogFunctionAPI
METAGENEDB_KEGG_API = MetageneDBCatalogKeggOrthologyAPI
METAGENEDB_EGGNOG_API = MetageneDBCatalogEggNogAPI
PHYLUM_COL = 'taxo_phylum' PHYLUM_COL = 'taxo_phylum'
GENUS_COL = 'taxo_genus' GENUS_COL = 'taxo_genus'
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment