Skip to content

Commit

Permalink
FIX config, start #116, refactor FileService
Browse files Browse the repository at this point in the history
  • Loading branch information
eboileau committed Jul 12, 2024
1 parent 895bfdd commit 3e81a5f
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 14 deletions.
3 changes: 2 additions & 1 deletion server/migrations/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from scimodom.database.database import Base
import scimodom.database.models # noqa
from scimodom.config import get_config
from scimodom.config import set_config_from_environment, get_config

# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
Expand All @@ -33,6 +33,7 @@


def get_database_url() -> str:
set_config_from_environment()
return get_config().DATABASE_URI


Expand Down
4 changes: 2 additions & 2 deletions server/src/scimodom/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ class _Config(Config):
NOTIFICATION_ADDRESS = get_required_parameter("NOTIFICATION_ADDRESS")
HTTP_PUBLIC_URL = get_required_parameter("HTTP_PUBLIC_URL")

FLASK_DEBUG = eval(os.getenv("FLASK_DEBUG", Config.FLASK_DEBUG))
FLASK_DEBUG = eval(os.getenv("FLASK_DEBUG", str(Config.FLASK_DEBUG)))
SESSION_COOKIE_SAMESITE = os.getenv("SESSION_COOKIE_SAMESITE")
SESSION_COOKIE_SECURE = eval(
os.getenv("SESSION_COOKIE_SECURE", Config.SESSION_COOKIE_SECURE)
os.getenv("SESSION_COOKIE_SECURE", str(Config.SESSION_COOKIE_SECURE))
)

JWT_SECRET_KEY = SECRET_KEY
Expand Down
2 changes: 1 addition & 1 deletion server/src/scimodom/services/annotation/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def get_release_path(self, annotation: Annotation) -> Path:
).scalar_one()
organism_name = "_".join(organism_name.lower().split()).capitalize()
assembly_name = self._assembly_service.get_name_for_version(annotation.taxa_id)
path = self._file_service.get_annotation_dir()
path = self._file_service.get_annotation_parent_dir()
return Path(path, organism_name, assembly_name, str(annotation.release))

def _release_exists(self, annotation_id) -> bool:
Expand Down
2 changes: 1 addition & 1 deletion server/src/scimodom/services/annotation/gtrnadb.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def _patch_annotation(self, annotation_file: Path, seqids: list[str]):

# TODO
def _update_annotation(self, domain, fasta_file):
annotation_path = self._file_service.get_annotation_dir()
annotation_path = self._file_service.get_annotation_parent_dir()
model_file = Path(annotation_path, domain).with_suffix(".cm").as_posix()
sprinzl_file = Path(annotation_path, domain).with_suffix(".txt").as_posix()
self._external_service.get_sprinzl_mapping(model_file, fasta_file, sprinzl_file)
Expand Down
37 changes: 30 additions & 7 deletions server/src/scimodom/services/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,16 @@ def __init__(
self._import_path = import_path

for path in [
data_path,
temp_path,
upload_path,
self._get_gene_cache_dir(),
self.get_annotation_dir(),
import_path,
self.get_project_metadata_dir(),
self._get_project_request_dir(),
self.get_annotation_parent_dir(),
self.get_assembly_parent_dir(),
self._get_gene_cache_dir(),
self.get_bam_files_parent_dir(),
]:
makedirs(path, exist_ok=True)

Expand All @@ -83,7 +88,7 @@ def open_import_file(self, name: str):

# annotation

def get_annotation_dir(self) -> Path:
def get_annotation_parent_dir(self) -> Path:
"""Construct parent path to annotation files.
:returns: Path to annotation
Expand Down Expand Up @@ -190,6 +195,14 @@ def _get_project_request_dir(self):

# Assembly

def get_assembly_parent_dir(self) -> Path:
"""Construct parent path to assembly files.
:returns: Path to assembly
:rtype: Path
"""
return Path(self._data_path, "assembly")

def get_assembly_file_path(
self,
taxa_id: int,
Expand Down Expand Up @@ -301,8 +314,7 @@ def _get_dir_name_from_organism(organism) -> str:
def _get_assembly_dir(self, taxa_id: int, assembly_name: str) -> Path:
organism = self._get_organism_from_taxa_id(taxa_id)
return Path(
self._data_path,
"assembly",
self.get_assembly_parent_dir(),
self._get_dir_name_from_organism(organism),
assembly_name,
)
Expand Down Expand Up @@ -378,6 +390,14 @@ def get_temp_path(self) -> str:

# BAM file

def get_bam_files_parent_dir(self):
"""Construct parent path to BAM files.
:returns: Path to BAM files
:rtype: Path
"""
return Path(self._data_path, "bam_files")

def create_or_update_bam_file(
self,
dataset: Dataset,
Expand Down Expand Up @@ -419,11 +439,14 @@ def _update_bam_file(self, bam_file, data_stream, max_size):

def _get_bam_file_tmp_path(self, bam_file):
return join(
join(self._data_path, "bam_files"), f"tmp.{bam_file.storage_file_name}"
self.get_bam_files_parent_dir().as_posix(),
f"tmp.{bam_file.storage_file_name}",
)

def _get_bam_file_path(self, bam_file):
return join(join(self._data_path, "bam_files"), bam_file.storage_file_name)
return join(
self.get_bam_files_parent_dir().as_posix(), bam_file.storage_file_name
)

@staticmethod
def _handle_upload_error(exception, path):
Expand Down
2 changes: 1 addition & 1 deletion server/tests/integration/services/test_file_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def test_count_lines(Session, tmp_path):

def test_get_annotation_path(Session, tmp_path):
service = _get_file_service(Session, tmp_path)
assert service.get_annotation_dir() == Path(tmp_path, "t_data", "annotation")
assert service.get_annotation_parent_dir() == Path(tmp_path, "t_data", "annotation")


def test_update_gene_cache(Session, tmp_path):
Expand Down
2 changes: 1 addition & 1 deletion server/tests/unit/services/test_ensembl_annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def get_assembly_file_path(
return Path(f"/data/assembly/{taxa_id}/{file_type.value}")

@staticmethod
def get_annotation_dir():
def get_annotation_parent_dir():
return "/data/annotation"


Expand Down

0 comments on commit 3e81a5f

Please sign in to comment.