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

Handle eggnog with multiple functional categories

parent f0fb2d72
......@@ -25,7 +25,7 @@ class EggNogAdmin(admin.ModelAdmin):
def get_functional_categories(self, obj):
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 '-'
get_functional_categories.short_description = 'Functional categories'
......
......@@ -27,12 +27,11 @@ class ImportEggNog(object):
all_categories = EggNogFunctionalCategory.objects.all()
if not all_categories:
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):
cat_key = eggnog_dict.get('functional_category', 'S')
category = self.functional_cat.get(cat_key)
eggnog_dict.update({'functional_category': category})
def add_functional_categories(self, eggnog, functional_category_keys):
for key in functional_category_keys:
eggnog.functional_categories.add(self.functional_cat_instances[key])
def load_all(self, test=False):
self._build_functional_category_dict()
......@@ -40,8 +39,9 @@ class ImportEggNog(object):
with open(self.annotation_file, "r") as file:
for line in file:
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 != ""}
eggnog = None
try:
eggnog = EggNog(**payload)
eggnog.full_clean()
......@@ -66,6 +66,9 @@ class ImportEggNog(object):
self.skipped_errors.append(validation_error)
self.skipped_ids.append(payload.get('function_id'))
self.skipped_count += 1
if eggnog is not None:
self.add_functional_categories(eggnog, functional_category_keys)
eggnog.save()
self.processed_count += 1
if self.processed_count % 1000 == 0:
logger.info("%s/%s EggNog processed so far...", self.processed_count, self.total_eggnog_nb)
......
......@@ -13,7 +13,7 @@ class EggNogAnnotationLineParser(object):
try:
elements = line.split('\t')
return {
'functional_category': list(elements[2]),
'functional_categories': list(elements[2]),
'function_id': elements[1],
'name': elements[3].rstrip().split('.')[0],
}
......
......@@ -10,7 +10,7 @@ class TestEggNogAnnotationLineParser(TestCase):
expected_dict = {
'function_id': "28H54",
'name': "translational termination",
'functional_category': ["K"]
'functional_categories': ["K"]
}
test_dict = EggNogAnnotationLineParser.get_dict(ko_line)
self.assertDictEqual(test_dict, expected_dict)
......@@ -20,7 +20,7 @@ class TestEggNogAnnotationLineParser(TestCase):
expected_dict = {
'function_id': "28H50",
'name': "",
'functional_category': ["S"]
'functional_categories': ["S"]
}
test_dict = EggNogAnnotationLineParser.get_dict(ko_line)
self.assertDictEqual(test_dict, expected_dict)
......@@ -30,7 +30,7 @@ class TestEggNogAnnotationLineParser(TestCase):
expected_dict = {
'function_id': "28H50",
'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)
self.assertDictEqual(test_dict, expected_dict)
......@@ -40,7 +40,7 @@ class TestEggNogAnnotationLineParser(TestCase):
expected_dict = {
'function_id': "28H54",
'name': "translational termination",
'functional_category': ["K", "S"]
'functional_categories': ["K", "S"]
}
test_dict = EggNogAnnotationLineParser.get_dict(ko_line)
self.assertDictEqual(test_dict, expected_dict)
......
......@@ -5,7 +5,13 @@ import os
import sys
from itertools import islice
from bioapi import MetageneDBCatalogFunctionAPI, MetageneDBCatalogGeneAPI, MetageneDBCatalogTaxonomyAPI
from bioapi import (
MetageneDBCatalogFunctionAPI,
MetageneDBCatalogGeneAPI,
MetageneDBCatalogTaxonomyAPI,
MetageneDBCatalogKeggOrthologyAPI,
MetageneDBCatalogEggNogAPI
)
from requests.exceptions import HTTPError
from slugify import slugify
......@@ -19,6 +25,8 @@ class ImportIGCGenes(object):
METAGENEDB_GENE_API = MetageneDBCatalogGeneAPI
METAGENEDB_TAXONOMY_API = MetageneDBCatalogTaxonomyAPI
METAGENEDB_FUNCTION_API = MetageneDBCatalogFunctionAPI
METAGENEDB_KEGG_API = MetageneDBCatalogKeggOrthologyAPI
METAGENEDB_EGGNOG_API = MetageneDBCatalogEggNogAPI
PHYLUM_COL = 'taxo_phylum'
GENUS_COL = 'taxo_genus'
......
Supports Markdown
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