Skip to content

Commit

Permalink
Ensure directories exist
Browse files Browse the repository at this point in the history
  • Loading branch information
ahdamin committed Dec 13, 2024
1 parent 5e129f9 commit f8c8c04
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion microSALT/utils/referencer.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,40 @@ def resolve_path(path):
path = os.path.abspath(path)
return path

def resolve_config_paths(self, config):
# Ensure all paths in 'folders' are resolved
if 'folders' in config:
for key, value in config['folders'].items():
if isinstance(value, str) and '/' in value:
config['folders'][key] = resolve_path(value)

# Resolve pubmlst credentials_files_path if present
if 'pubmlst' in config and 'credentials_files_path' in config['pubmlst']:
config['pubmlst']['credentials_files_path'] = resolve_path(config['pubmlst']['credentials_files_path'])

return config

def ensure_directories(self):
"""Ensure all required directories are created."""
required_dirs = [
self.config["folders"].get("results"),
self.config["folders"].get("reports"),
self.config["folders"].get("profiles"),
self.config["folders"].get("references"),
self.config["folders"].get("resistances"),
self.config["folders"].get("genomes"),
]
for dir_path in required_dirs:
if dir_path:
resolved_path = resolve_path(dir_path)
os.makedirs(resolved_path, exist_ok=True)
self.logger.info(f"Ensured directory exists: {resolved_path}")


class Referencer:
def __init__(self, config, log, sampleinfo={}, force=False):
self.config = self.resolve_config_paths(config)
self.ensure_directories()
self.logger = log
self.db_access = DB_Manipulator(config, log)
self.updated = list()
Expand Down Expand Up @@ -70,7 +101,7 @@ def __init__(self, config, log, sampleinfo={}, force=False):
self.token, self.secret = get_new_session_token(default_db)

def resolve_config_paths(self, config):
# Resolve all folder paths
# Ensure all paths in 'folders' are resolved
if 'folders' in config:
for key, value in config['folders'].items():
if isinstance(value, str) and '/' in value:
Expand Down Expand Up @@ -125,6 +156,7 @@ def update_refs(self):
def index_db(self, full_dir, suffix):
"""Check for indexation, makeblastdb job if not enough of them."""
reindexation = False
full_dir = resolve_path(full_dir)
files = os.listdir(full_dir)
sufx_files = glob.glob("{}/*{}".format(full_dir, suffix)) # List of source files
for file in sufx_files:
Expand Down

0 comments on commit f8c8c04

Please sign in to comment.