Commit c576a345 authored by Cage, Gregory's avatar Cage, Gregory
Browse files

Refactor data stores and expose all classes on package level

parent 3f645b05
Loading
Loading
Loading
Loading
Loading
+5 −1
Original line number Diff line number Diff line
import importlib.metadata

from .data_store import Datastore
from .dataset import Dataset, DatasetCollection, upload_datasets
from .nova import Nova
from .parameters import Parameters
from .tool import Tool

__all__ = ["Nova"]
__all__ = ["Nova", "Datastore", "Dataset", "DatasetCollection", "upload_datasets", "Tool", "Parameters"]

__version__ = importlib.metadata.version("nova-galaxy")
+6 −2
Original line number Diff line number Diff line
@@ -7,8 +7,12 @@ if TYPE_CHECKING:


class Datastore:
    """Groups tool outputs together."""
    """Groups tool outputs together.

    def __init__(self, name: str, nova_instance: "Nova"):
    The constructor is not intended for external use. Use nova.galaxy.Nova.create_data_store() instead.
    """

    def __init__(self, name: str, nova_instance: "Nova", history_id: str):
        self.name = name
        self.nova = nova_instance
        self.history_id = history_id
+9 −4
Original line number Diff line number Diff line
@@ -64,6 +64,8 @@ class Dataset(AbstractData):

    def __init__(self, path: str):
        self.path = path
        self.id: str
        self.store: Datastore

    def upload(self, store: Datastore) -> None:
        galaxy_instance = store.nova.galaxy_instance
@@ -72,7 +74,6 @@ class Dataset(AbstractData):
        dataset_id = galaxy_instance.tools.upload_file(path=self.path, history_id=history_id)
        self.id = dataset_id["outputs"][0]["id"]
        self.store = store
        if self.id:
        dataset_client.wait_for_dataset(self.id)

    def download(self, local_path: str) -> None:
@@ -80,7 +81,8 @@ class Dataset(AbstractData):
        if self.store and self.id:
            dataset_client = DatasetClient(self.store.nova.galaxy_instance)
            dataset_client.download_dataset(self.id, use_default_filename=False, file_path=local_path)
            dataset_client.wait_for_dataset(self.id)
        else:
            raise Exception("Dataset is not present in Galaxy.")


class DatasetCollection(AbstractData):
@@ -88,6 +90,8 @@ class DatasetCollection(AbstractData):

    def __init__(self, path: str):
        self.path = path
        self.id: str
        self.store: Datastore

    def upload(self, store: Datastore) -> None:
        """Will need to handle this differently than single datasets."""
@@ -98,7 +102,8 @@ class DatasetCollection(AbstractData):
        if self.store and self.id:
            dataset_client = DatasetCollectionClient(self.store.nova.galaxy_instance)
            dataset_client.download_dataset_collection(self.id, file_path=local_path)
            dataset_client.wait_for_dataset_collection(self.id)
        else:
            raise Exception("Dataset collection is not present in Galaxy.")


def upload_datasets(store: Datastore, datasets: Dict[str, AbstractData]) -> Dict[str, str]:
+11 −3
Original line number Diff line number Diff line
@@ -41,10 +41,10 @@ class Nova:
        Args:
            galaxy_url (Optional[str]): URL of the Galaxy instance.
            galaxy_key (Optional[str]): API key for the Galaxy instance.
            namespace (str): Namespace for Galaxy histories.
        """
        self.galaxy_url = galaxy_url
        self.galaxy_api_key = galaxy_key
        self.galaxy_instance: galaxy.GalaxyInstance

    def connect(self) -> None:
        """
@@ -64,5 +64,13 @@ class Nova:

    def create_data_store(self, name: str) -> Datastore:
        """Creates a datastore with the given name."""
        self.galaxy_instance.histories.create_history(name=name)["id"]
        return Datastore(name, self)
        histories = self.galaxy_instance.histories.get_histories(name=name)
        if len(histories) > 0:
            return Datastore(name, self, histories[0]["id"])
        history_id = self.galaxy_instance.histories.create_history(name=name)["id"]
        return Datastore(name, self, history_id)

    def remove_data_store(self, name: str) -> None:
        """Permanently deletes the data store with the given name."""
        history = self.galaxy_instance.histories.get_histories(name=name)[0]["id"]
        self.galaxy_instance.histories.delete_history(history_id=history, purge=True)
+3 −3
Original line number Diff line number Diff line
@@ -35,8 +35,6 @@ class Tool(AbstractWork):
        """Runs this tool in a blocking manner and returns a map of the output datasets and collections."""
        outputs: Dict[Any, AbstractData] = {}
        galaxy_instance = data_store.nova.galaxy_instance
        history_id = galaxy_instance.histories.get_histories(name=data_store.name)[0]["id"]

        datasets_to_upload = {}

        # Set Tool Inputs
@@ -52,7 +50,9 @@ class Tool(AbstractWork):
            tool_inputs.set_dataset_param(param, val)

        # Run tool and wait for job to finish
        results = galaxy_instance.tools.run_tool(history_id=history_id, tool_id=self.id, tool_inputs=tool_inputs)
        results = galaxy_instance.tools.run_tool(
            history_id=data_store.history_id, tool_id=self.id, tool_inputs=tool_inputs
        )

        for job in results["jobs"]:
            galaxy_instance.jobs.wait_for_job(job_id=job["id"])