genesets route for local server (#2079)

* first cut at GET /genesets route

* update existing tests to match code changes

* more GET /genesets and initial tests

* add missing test fixture

* geneset validation accepts OTA format

* genesets route: better error handling, more tests

* lint
This commit is contained in:
Bruce Martin
2021-02-26 17:53:07 -08:00
committed by GitHub
parent 09466a5c32
commit f3a3820ffa
18 changed files with 813 additions and 82 deletions
+77 -3
View File
@@ -3,19 +3,34 @@ from abc import ABCMeta, abstractmethod
import fastobo
import fsspec
from local_server.common.errors import OntologyLoadFailure
from local_server.common.errors import OntologyLoadFailure, DisabledFeatureError
from local_server.common.utils.type_conversion_utils import get_schema_type_hint_of_array
class Annotations(metaclass=ABCMeta):
""" baseclass for annotations, including ontologies"""
""" baseclass for annotations, including ontologies and genesets"""
""" our default ontology is the PURL for the Cell Ontology.
See http://www.obofoundry.org/ontology/cl.html """
DefaultOnotology = "http://purl.obolibrary.org/obo/cl.obo"
def __init__(self):
def __init__(self, config={}):
self.ontology_data = None
self.config = config
def user_annotations_enabled(self):
return self.config.get("user-annotations", False)
def genesets_save_enabled(self):
return self.config.get("genesets-save", False)
def check_user_annotations_enabled(self):
if not self.user_annotations_enabled():
raise DisabledFeatureError("User annotations are disabled.")
def check_genesets_save_enabled(self):
if not self.genesets_save_enabled():
raise DisabledFeatureError("User genesets save is disabled.")
def load_ontology(self, path):
"""Load and parse ontologies - currently support OBO files only."""
@@ -64,7 +79,66 @@ class Annotations(metaclass=ABCMeta):
"""Write the labels (df) to a persistent storage such that it can later be read"""
pass
@abstractmethod
def read_genesets(self, data_adaptor):
"""Return the genesets from persistent storage """
pass
@abstractmethod
def write_genesets(self, gs, data_adaptor):
"""Write the genesets (gs) to a persistent storage such that it can later be read"""
pass
@abstractmethod
def update_parameters(self, parameters, data_adaptor):
"""Update configuration parameters that describe information about the annotations feature"""
pass
Genesets_Header = [
"geneset_name",
"geneset_description",
"gene_symbol",
"gene_description",
]
@staticmethod
def genesets_to_csv(genesets):
"""
Convert the internal genesets format (returned by read_geneset) into
the simple Tidy CSV.
"""
from io import StringIO
import csv
if type(genesets) == dict:
genesets = genesets.values()
with StringIO() as sio:
writer = csv.writer(sio, dialect='excel')
writer.writerow(Annotations.Genesets_Header)
for geneset in genesets:
# genes may be empty, in which case we skip the geneset entirely
genes = geneset["genes"]
if not genes:
writer.writerow([geneset["geneset_name"], geneset.get("geneset_description", ""), "", ""])
else:
writer.writerows(
[
[
geneset["geneset_name"],
geneset.get("geneset_description", ""),
gene["gene_symbol"],
gene.get("gene_description", ""),
]
for gene in genes
]
)
return sio.getvalue()
@staticmethod
def genesets_to_response(genesets):
"""
Convert the internal genesets format (returned by read_geneset) into
the dict expected by the JSON REST API
"""
return list(genesets.values())