Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion cirro/sdk/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from cirro_api_client.v1.errors import CirroException, UnexpectedStatus
from cirro_api_client.v1.models import Dataset, DatasetDetail, ProcessDetail, \
Status, Tag, ArtifactType, NamedItem, ValidateFileRequirementsRequest, \
CostResponse
CostResponse, DatasetCopyOrigin

from cirro.cirro_client import CirroApi
from cirro.config import Constants
Expand Down Expand Up @@ -204,6 +204,14 @@ def source_datasets(self) -> List['DataPortalDataset']:
for dataset_id in self.source_dataset_ids
]

@property
def copied_from(self) -> List[DatasetCopyOrigin]:
"""
The datasets (or S3 locations) this dataset copied its files from,
if it was created with `cirro.sdk.project.DataPortalProject.copy_dataset`
"""
return self._get_detail().copied_from or []

@property
def params(self) -> dict:
"""
Expand Down
132 changes: 128 additions & 4 deletions cirro/sdk/project.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import uuid
from functools import cache
from time import sleep
from typing import List, TYPE_CHECKING, Union
from typing import Any, List, TYPE_CHECKING, Union

if TYPE_CHECKING:
from pandas import DataFrame

from cirro_api_client.v1.models import Project, UploadDatasetRequest, Dataset, Sample, Tag, Status, \
TableSheetInput, ViewSheetInput, DashboardInput
TableSheetInput, ViewSheetInput, DashboardInput, CopyDatasetRequest, FileMapping

from cirro.cirro_client import CirroApi
from cirro.file_utils import get_files_in_directory
from cirro.models.cirro_uri import format_dataset_uri
from cirro.sdk.asset import DataPortalAssets, DataPortalAsset
from cirro.sdk.dashboard import DataPortalDashboard, DataPortalDashboards, DATA_STUDIO_TYPE, \
RECORD_SCHEMA_VERSION
from cirro.sdk.dataset import DataPortalDataset, DataPortalDatasets
from cirro.sdk.exceptions import DataPortalAssetNotFound, DataPortalInputError
from cirro.sdk.file import DataPortalFile
from cirro.sdk.helpers import parse_process_name_or_id
from cirro.sdk.process import DataPortalProcess
from cirro.sdk.reference import DataPortalReference, DataPortalReferences
Expand All @@ -24,6 +26,21 @@
from cirro.services.service_helpers import list_all_datasets


def _to_file_mapping(source: Any) -> FileMapping:
"""Normalize a source given to `DataPortalProject.copy_dataset` into what to copy"""
if isinstance(source, FileMapping):
return source
if isinstance(source, DataPortalDataset):
# The whole data folder of the dataset
return FileMapping(src=format_dataset_uri(source.project_id, source.id, ''))
if isinstance(source, DataPortalFile):
return FileMapping(src=source.cirro_uri)
if isinstance(source, str):
return FileMapping(src=source)
raise DataPortalInputError(f"Cannot copy from '{source}': "
"expected a dataset, file, FileMapping, or Cirro / S3 URI")


class DataPortalProject(DataPortalAsset):
"""
Projects in the Data Portal contain collections of Datasets.
Expand Down Expand Up @@ -444,11 +461,118 @@ def upload_dataset(
files=files
)

# Return the dataset which was created, which might take a second to update
return self._get_created_dataset(create_response.id)

def copy_dataset(
self,
sources: List[Union[DataPortalDataset, DataPortalFile, FileMapping, str]],
name: str = None,
description: str = None,
process: Union[DataPortalProcess, str] = None,
tags: List[str] = None,
run_details_from: Union[DataPortalDataset, str] = None,
source_dataset_ids: List[str] = None,
) -> DataPortalDataset:
"""
Create a new dataset in this project by copying files and folders from
other datasets (in this project, or shared with it) or from S3.

Use it to copy a dataset from another project, take a subset of a
dataset's files, or combine several datasets into one.

The copy runs in the background: the dataset which is returned will
have the status `RUNNING` until all files have been copied.

Args:
sources (list): What to copy. Each item is one of
a `cirro.sdk.dataset.DataPortalDataset` (all of its data files),
a `cirro.sdk.file.DataPortalFile` (a single data file),
a Cirro URI (see `cirro.models.cirro_uri.format_dataset_uri`)
or S3 URI of a file or folder (folders end in `/`),
or a `cirro_api_client.v1.models.FileMapping`, which also sets
the destination path within the new dataset.
Files may come from at most 40 datasets.
name (str): Name of the new dataset. Required unless every file
comes from one dataset, in which case it defaults to that dataset's name.
description (str): Description of the new dataset
(defaults to that of the source dataset, when there is only one)
process (str | DataPortalProcess): Data type of the new dataset, referenced by name, ID, or object.
Defaults to that of the source datasets if they all share one.
Required when no file comes from a dataset.
tags (List[str]): Optional list of tags to apply to the dataset
(defaults to those of the source dataset, when there is only one)
run_details_from (DataPortalDataset | str): Dataset (or its Cirro URI) to also copy
the run details (params, reports, logs) from. Must be one of the datasets being copied from.
source_dataset_ids (List[str]): IDs of the datasets in this project that the new dataset
derives from (its lineage). Defaults to those of the source datasets.

Returns:
`cirro.sdk.dataset.DataPortalDataset`: the newly created dataset.

Raises:
DataPortalInputError: if no sources are provided, or one is of an unsupported type.

```python
from cirro import DataPortal
from cirro_api_client.v1.models import FileMapping

portal = DataPortal()
source = portal.get_dataset(project="Source Project", dataset="Sequencing Run 1")
project = portal.get_project_by_name("Destination Project")

# Copy a whole dataset, along with its run details
copied = project.copy_dataset([source], run_details_from=source)

# Combine a file, a folder in S3, and a renamed file into one dataset
combined = project.copy_dataset(
[
source.get_file("data/sample1_R1.fastq.gz"),
"s3://bucket/folder/",
FileMapping(src=source.get_file("data/sample1_R2.fastq.gz").cirro_uri,
dest="renamed/sample1_R2.fastq.gz")
],
name="Combined dataset",
process="Paired DNAseq (FASTQ)"
)
```
"""
if not sources:
raise DataPortalInputError("Must provide at least one source to copy from")
# A single source would otherwise be iterated over (character by character, for a string)
if isinstance(sources, (DataPortalDataset, DataPortalFile, FileMapping, str)):
sources = [sources]

if process is not None:
process = parse_process_name_or_id(process, self._client).id

if isinstance(run_details_from, DataPortalDataset):
run_details_from = format_dataset_uri(run_details_from.project_id, run_details_from.id)

# Normalize into Tag object
if tags is not None:
tags = [Tag(value=value) for value in tags]

# Anything left as None falls back to defaults
copy_request = CopyDatasetRequest(
files=[_to_file_mapping(source) for source in sources],
run_details_from=run_details_from,
name=name,
description=description,
tags=tags,
process_id=process,
source_dataset_ids=source_dataset_ids,
)

create_response = self._client.datasets.copy(project_id=self.id, copy_request=copy_request)

return self._get_created_dataset(create_response.id)

def _get_created_dataset(self, dataset_id: str) -> DataPortalDataset:
"""Return a dataset which was just created, which might take a second to update"""
max_attempts = 5
for attempt in range(max_attempts):
try:
return self.get_dataset_by_id(create_response.id)
return self.get_dataset_by_id(dataset_id)
except DataPortalAssetNotFound as e:
if attempt == max_attempts - 1:
raise e
Expand Down
42 changes: 40 additions & 2 deletions cirro/services/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
from typing import List, Optional, Union, Dict

from cirro_api_client.v1.api.datasets import get_datasets, get_dataset, import_public_dataset, upload_dataset, \
update_dataset, delete_dataset, get_dataset_manifest
update_dataset, delete_dataset, get_dataset_manifest, copy_dataset
from cirro_api_client.v1.api.sharing import get_shared_datasets
from cirro_api_client.v1.models import ImportDataRequest, UploadDatasetRequest, UpdateDatasetRequest, Dataset, \
DatasetDetail, CreateResponse, UploadDatasetCreateResponse, FileEntry
DatasetDetail, CreateResponse, UploadDatasetCreateResponse, FileEntry, CopyDatasetRequest
from cirro.file_utils import is_hidden_file
from cirro.models.assets import DatasetAssets, Artifact
from cirro.models.dataset import DatasetValidationResponse
Expand Down Expand Up @@ -101,6 +101,44 @@ def import_public(self, project_id: str, import_request: ImportDataRequest) -> C
"""
return import_public_dataset.sync(project_id=project_id, client=self._api_client, body=import_request)

def copy(self, project_id: str, copy_request: CopyDatasetRequest) -> CreateResponse:
"""
Creates a dataset in this project by copying files and folders from
datasets or from S3 buckets the project can read.

This can be used to copy a whole dataset, a subset of its files, or to combine several datasets into one.
The copy runs in the background; the new dataset is available once its status is `COMPLETED`.

Args:
project_id (str): ID of the Project to create the dataset in
copy_request (cirro_api_client.v1.models.CopyDatasetRequest):

Returns:
ID of the created dataset

```python
from cirro_api_client.v1.models import CopyDatasetRequest, FileMapping
from cirro.cirro_client import CirroApi
from cirro.models.cirro_uri import format_dataset_uri

cirro = CirroApi()
request = CopyDatasetRequest(
name="Copied dataset",
files=[
# A whole dataset (the trailing slash denotes a folder)
FileMapping(src=format_dataset_uri("source-project-id", "dataset-id", "")),
# A single file, renamed
FileMapping(src=format_dataset_uri("source-project-id", "other-dataset-id", "sample1_R1.fastq.gz"),
dest="renamed/sample1_R1.fastq.gz"),
# A folder in S3
FileMapping(src="s3://bucket/folder/", dest="from_s3/")
]
)
cirro.datasets.copy("project-id", request)
```
"""
return copy_dataset.sync(project_id=project_id, client=self._api_client, body=copy_request)

def create(self, project_id: str, upload_request: UploadDatasetRequest) -> UploadDatasetCreateResponse:
"""
Registers a dataset in Cirro, which can subsequently have files uploaded to it
Expand Down
8 changes: 4 additions & 4 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "cirro"
version = "1.17.0"
version = "1.17.1"
description = "CLI tool and SDK for interacting with the Cirro platform"
authors = ["Cirro Bio <support@cirro.bio>"]
license = "MIT"
Expand All @@ -11,7 +11,7 @@ packages = [{include = "cirro"}]
[tool.poetry.dependencies]
python = ">3.11.0,<4.0"
attrs = ">=21.3.0"
cirro_api_client = "~1.8.0"
cirro_api_client = "~1.9.1"
click = "^8.5.0"
boto3 = "~=1.43"
questionary = "^2.0.1"
Expand Down
Loading