mirror of
https://github.com/Novartis/cellxgene-gateway.git
synced 2026-10-09 16:10:56 +08:00
#14 enabled new annotations
Due to issue with opening annotation files with "-" in the name, this doesn't quite work.
This commit is contained in:
@@ -13,37 +13,81 @@ from flask_api import status
|
||||
|
||||
from cellxgene_gateway import env
|
||||
from cellxgene_gateway.cellxgene_exception import CellxgeneException
|
||||
from cellxgene_gateway.dir_util import make_h5ad
|
||||
from cellxgene_gateway.cache_key import CacheKey
|
||||
|
||||
|
||||
def get_dataset(path):
|
||||
def get_key(path):
|
||||
if path == "/" or path == "":
|
||||
raise CellxgeneException(
|
||||
"No matching dataset found.", status.HTTP_404_NOT_FOUND
|
||||
)
|
||||
|
||||
trimmed = path[:-1] if path[-1] == "/" else path
|
||||
|
||||
try:
|
||||
get_file_path(trimmed)
|
||||
return trimmed
|
||||
# valid paths come in three forms:
|
||||
if trimmed.endswith('.h5ad') and data_file_exists(trimmed):
|
||||
# 1) somedir/dataset.h5ad: a dataset
|
||||
return CacheKey(trimmed, trimmed, None)
|
||||
elif trimmed.endswith('.csv') and data_file_exists(trimmed):
|
||||
|
||||
# 2) somedir/dataset_annotations/saldaal1-T5HMVBNV.csv : an actual annotaitons file.
|
||||
annotations_dir = os.path.split(trimmed)[0]
|
||||
dataset = make_h5ad(annotations_dir)
|
||||
if data_file_exists(dataset):
|
||||
return CacheKey(trimmed, dataset, trimmed)
|
||||
elif trimmed.endswith('_annotations') and data_dir_exists(trimmed):
|
||||
# 3) somedir/dataset_annotations: an annotation directory. The corresponding h5ad must exist, but the directory may not.
|
||||
dataset = make_h5ad(trimmed)
|
||||
if data_file_exists(dataset):
|
||||
return CacheKey(trimmed, dataset, '')
|
||||
except CellxgeneException:
|
||||
split = os.path.split(trimmed)
|
||||
return get_dataset(split[0])
|
||||
pass
|
||||
split = os.path.split(trimmed)
|
||||
return get_key(split[0])
|
||||
|
||||
|
||||
def validate_path(file_path):
|
||||
def validate_exists(file_path):
|
||||
if not os.path.exists(file_path):
|
||||
raise CellxgeneException(
|
||||
"File does not exist: " + file_path, status.HTTP_400_BAD_REQUEST
|
||||
)
|
||||
|
||||
def validate_is_file(file_path):
|
||||
validate_exists(file_path)
|
||||
if not os.path.isfile(file_path):
|
||||
raise CellxgeneException(
|
||||
"Path is not file: " + file_path, status.HTTP_400_BAD_REQUEST
|
||||
)
|
||||
return
|
||||
def validate_is_dir(file_path):
|
||||
validate_exists(file_path)
|
||||
if not os.path.isdir(file_path):
|
||||
raise CellxgeneException(
|
||||
"Path is not dir: " + file_path, status.HTTP_400_BAD_REQUEST
|
||||
)
|
||||
return
|
||||
|
||||
|
||||
def get_file_path(dataset):
|
||||
def data_file_exists(dataset):
|
||||
file_path = os.path.join(env.cellxgene_data, dataset)
|
||||
validate_path(file_path)
|
||||
validate_is_file(file_path)
|
||||
return True
|
||||
def data_dir_exists(dataset):
|
||||
file_path = os.path.join(env.cellxgene_data, dataset)
|
||||
validate_is_dir(file_path)
|
||||
return True
|
||||
|
||||
def get_file_path(key):
|
||||
dataset = key.dataset
|
||||
file_path = os.path.join(env.cellxgene_data, dataset)
|
||||
validate_is_file(file_path)
|
||||
return file_path
|
||||
|
||||
def get_annotation_file_path(key):
|
||||
print(f"getting annotaiton_file_path for {key.annotation_file}")
|
||||
if key.annotation_file is None:
|
||||
return None
|
||||
if key.annotation_file == '':
|
||||
return ''
|
||||
file_path = os.path.join(env.cellxgene_data, key.annotation_file)
|
||||
print(f"getting annotaiton_file_path for {key}, file_path {file_path}")
|
||||
validate_is_file(file_path)
|
||||
return file_path
|
||||
|
||||
Reference in New Issue
Block a user