diff --git a/src/sourmash/index.py b/src/sourmash/index.py index ca9477db5b..7b7484bc11 100644 --- a/src/sourmash/index.py +++ b/src/sourmash/index.py @@ -882,37 +882,59 @@ def sigloc_iter(): return cls(manifest) @classmethod - def load_from_path(cls, pathname, force=False): - """ - Create a MultiIndex from a path (filename or directory). - - Note: this only uses LinearIndex.load(...), so will only load - signature JSON files. - """ + def load_from_directory(cls, pathname, force=False): from .sourmash_args import traverse_find_sigs - if not os.path.exists(pathname): # CTB consider changing to isdir... - raise ValueError(f"'{pathname}' must exist.") index_list = [] source_list = [] - for thisfile in traverse_find_sigs([pathname], yield_all_files=force): + + traversal = traverse_find_sigs([pathname], yield_all_files=force) + for thisfile in traversal: try: idx = LinearIndex.load(thisfile) + index_list.append(idx) - if idx: - index_list.append(idx) - source_list.append(thisfile) + relpath = os.path.relpath(thisfile, pathname) + source_list.append(relpath) except (IOError, sourmash.exceptions.SourmashError): if force: continue # ignore error else: raise # stop loading! + # did we load anything? if not, error if not index_list: raise ValueError(f"no signatures to load under directory '{pathname}'") return cls.load(index_list, source_list) + @classmethod + def load_from_path(cls, pathname, force=False): + """ + Create a MultiIndex from a path (filename or directory). + + Note: this only uses LinearIndex.load(...), so will only load + signature JSON files. + """ + if not os.path.exists(pathname): + raise ValueError(f"'{pathname}' must exist.") + + if os.path.isdir(pathname): # traverse + return cls.load_from_directory(pathname, force=force) + else: # load as a .sig/JSON file + index_list = [] + source_list = [] + try: + idx = LinearIndex.load(pathname) + index_list = [idx] + source_list = [pathname] + except (IOError, sourmash.exceptions.SourmashError): + if not force: + raise ValueError(f"no signatures to load from '{pathname}'") + return None + + return cls.load(index_list, source_list) + @classmethod def load_from_pathlist(cls, filename): """Create a MultiIndex from all files listed in a text file.