Skip to content

Commit

Permalink
make manifest internal_location relative for MultiIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
ctb committed Jul 3, 2021
1 parent f7e755f commit 5c3f5fc
Showing 1 changed file with 35 additions and 13 deletions.
48 changes: 35 additions & 13 deletions src/sourmash/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 5c3f5fc

Please sign in to comment.