Skip to content

Commit

Permalink
Do not hardcode list of available catalogues
Browse files Browse the repository at this point in the history
  • Loading branch information
fplazaonate committed Mar 29, 2024
1 parent d2d871f commit 24f5f7a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
25 changes: 20 additions & 5 deletions meteor/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@
from time import time
import tarfile
import json
from typing import ClassVar


@dataclass
class Downloader(Session):
"""Download and prepare catalogues"""

CONFIG_DATA_FILE: ClassVar[Path] = Path("data/zenodo.json")
TEST_CATALOGUE: ClassVar[str] = "test"

meteor: type[Component]
choice: str
taxonomy: bool
Expand All @@ -36,14 +40,25 @@ class Downloader(Session):
catalogues_config: dict = field(default_factory=dict)
start_time: float = field(default_factory=float)

def __post_init__(self) -> None:
@staticmethod
def load_catalogues_config() -> dict:
try:
config_data = importlib.resources.files("meteor") / "data/zenodo.json"
config_data = importlib.resources.files("meteor") / str(Downloader.CONFIG_DATA_FILE)
with importlib.resources.as_file(config_data) as configuration_path:
with configuration_path.open("rt", encoding="UTF-8") as config:
self.catalogues_config = json.load(config)
return json.load(config)
except AssertionError:
logging.error("The file zenodo.json is missing in meteor source")
logging.error("The file %s is missing in meteor source", Downloader.CONFIG_DATA_FILE.name)

@staticmethod
def get_available_catalogues() -> list[str]:
catalogues_config = Downloader.load_catalogues_config()
available_catalogues = list(catalogues_config.keys())
available_catalogues.remove(Downloader.TEST_CATALOGUE)
return available_catalogues

def __post_init__(self) -> None:
self.catalogues_config = Downloader.load_catalogues_config()
self.meteor.ref_dir.mkdir(exist_ok=True, parents=True)
if self.taxonomy:
self.data_type = "taxonomy_info"
Expand Down Expand Up @@ -111,7 +126,7 @@ def execute(self) -> None:
)
urlretrieve(url, filename=catalogue, reporthook=self.show_progress)
print(flush=True)
if self.choice == "test":
if self.choice == Downloader.TEST_CATALOGUE:
for sample in self.catalogues_config[self.choice]["samples"]:
logging.info(f"Download {sample} fastq file")
url_fastq = self.catalogues_config[self.choice]["samples"][sample][
Expand Down
13 changes: 1 addition & 12 deletions meteor/meteor.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,18 +148,7 @@ def get_arguments() -> Namespace: # pragma: no cover
dest="user_choice",
type=str,
required=True,
choices=[
"cat_gut",
"chicken_caecal",
"dog_gut",
"human_gut",
"human_oral",
"human_skin",
"mouse_gut",
"rabbit_gut",
"rat_gut",
"pig_gut",
],
choices=Downloader.get_available_catalogues(),
help="Select the catalogue to download.",
)
download_parser.add_argument(
Expand Down

0 comments on commit 24f5f7a

Please sign in to comment.