From 76c1d9e80cbb7df4236b72c66324e3bdd6dc7791 Mon Sep 17 00:00:00 2001 From: Alok Saldanha Date: Mon, 29 Mar 2021 07:28:16 -0400 Subject: [PATCH] removed .csv suffix from annotation name --- cellxgene_gateway/items/file/fileitem.py | 5 +++-- .../items/file/fileitem_source.py | 9 ++++++++- tests/items/__init__.py | 0 tests/items/file/__init__.py | 0 tests/items/file/test_fileitem_source.py | 20 +++++++++++++++++++ tests/test_cache_entry.py | 2 +- 6 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 tests/items/__init__.py create mode 100644 tests/items/file/__init__.py create mode 100644 tests/items/file/test_fileitem_source.py diff --git a/cellxgene_gateway/items/file/fileitem.py b/cellxgene_gateway/items/file/fileitem.py index 5951b2e..cd5c011 100644 --- a/cellxgene_gateway/items/file/fileitem.py +++ b/cellxgene_gateway/items/file/fileitem.py @@ -18,10 +18,11 @@ class FileItem(Item): The Item superclass expects a 'name' and 'type'. """ - def __init__(self, subpath: str, *args, **kwargs): + def __init__(self, subpath: str, ext: str = "", *args, **kwargs): super().__init__(*args, **kwargs) self.subpath = subpath + self.ext = ext @property def descriptor(self) -> str: - return os.path.join(self.subpath, self.name).strip("/") + return os.path.join(self.subpath, self.name + self.ext).strip("/") diff --git a/cellxgene_gateway/items/file/fileitem_source.py b/cellxgene_gateway/items/file/fileitem_source.py index 073425c..5a23048 100644 --- a/cellxgene_gateway/items/file/fileitem_source.py +++ b/cellxgene_gateway/items/file/fileitem_source.py @@ -150,9 +150,16 @@ class FileItemSource(ItemSource): def make_fileitem_from_path( self, filename, subpath, is_annotation=False, is_shallow=False ) -> FileItem: + if is_annotation and filename.endswith(self.annotation_file_suffix): + name = filename[: -len(self.annotation_file_suffix)] + ext = self.annotation_file_suffix + else: + name = filename + ext = "" item = FileItem( subpath=subpath, - name=filename, + name=name, + ext=ext, type=ItemType.annotation if is_annotation else ItemType.h5ad, ) diff --git a/tests/items/__init__.py b/tests/items/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/items/file/__init__.py b/tests/items/file/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/items/file/test_fileitem_source.py b/tests/items/file/test_fileitem_source.py new file mode 100644 index 0000000..9350690 --- /dev/null +++ b/tests/items/file/test_fileitem_source.py @@ -0,0 +1,20 @@ +import tempfile +import unittest + +from cellxgene_gateway.items.file.fileitem_source import FileItemSource + + +class TestFileItemSource(unittest.TestCase): + def test_make_fileitem_from_path_GIVEN_annotation_file_THEN_name_lacks_csv(self): + source = FileItemSource(tempfile.gettempdir(), "local") + item = source.make_fileitem_from_path( + "customanno.csv", "someh5ad_annotations", True + ) + self.assertEqual(item.name, "customanno") + self.assertEqual(item.descriptor, "someh5ad_annotations/customanno.csv") + + def test_make_fileitem_from_path_GIVEN_h5ad_file_THEN_returns_name(self): + source = FileItemSource(tempfile.gettempdir(), "local") + item = source.make_fileitem_from_path("someanalysis.h5ad", "studydir") + self.assertEqual(item.name, "someanalysis.h5ad") + self.assertEqual(item.descriptor, "studydir/someanalysis.h5ad") diff --git a/tests/test_cache_entry.py b/tests/test_cache_entry.py index 7f076c9..6ad04bd 100644 --- a/tests/test_cache_entry.py +++ b/tests/test_cache_entry.py @@ -10,7 +10,7 @@ from cellxgene_gateway.items.file.fileitem_source import FileItemSource from cellxgene_gateway.items.item import ItemType key = CacheKey( - FileItem("/czi/", "pbmc3k.h5ad", ItemType.h5ad), + FileItem("/czi/", name="pbmc3k.h5ad", type=ItemType.h5ad), FileItemSource("/tmp", "local"), )