From b29fd40b6c00d10905f34a31e7fa36a34ecfcc4d Mon Sep 17 00:00:00 2001 From: Nathan Thorpe Date: Thu, 12 Nov 2026 13:16:00 -0800 Subject: [PATCH 1/4] base copy function --- cirro/services/dataset.py | 42 +++++++++++++++++++++++++++++++++++++-- poetry.lock | 20 +++++++++---------- pyproject.toml | 2 +- 3 files changed, 51 insertions(+), 13 deletions(-) diff --git a/cirro/services/dataset.py b/cirro/services/dataset.py index 85c379e..ce1e658 100644 --- a/cirro/services/dataset.py +++ b/cirro/services/dataset.py @@ -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 @@ -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 diff --git a/poetry.lock b/poetry.lock index 505b8e2..2091018 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1,4 +1,4 @@ -# This file is automatically @generated by Poetry 2.1.2 and should not be changed by hand. +# This file is automatically @generated by Poetry 2.5.1 and should not be changed by hand. [[package]] name = "aiobotocore" @@ -423,7 +423,7 @@ files = [ [package.dependencies] jmespath = ">=0.7.1,<2.0.0" python-dateutil = ">=2.1,<3.0.0" -urllib3 = ">=1.25.4,<2.2.0 || >2.2.0,<3" +urllib3 = ">=1.25.4,!=2.2.0,<3" [package.extras] crt = ["awscrt (==0.36.0)"] @@ -779,14 +779,14 @@ files = [ [[package]] name = "cirro-api-client" -version = "1.8.0" +version = "1.9.0" description = "A client library for accessing Cirro" optional = false python-versions = "<4.0,>=3.11" groups = ["main"] files = [ - {file = "cirro_api_client-1.8.0-py3-none-any.whl", hash = "sha256:8c214ee7a3222d1ce9131d6382925619f7f447b421efa2b1e7527f2955eeb9fb"}, - {file = "cirro_api_client-1.8.0.tar.gz", hash = "sha256:9e7b3b48b1071f7b6d9bbf962faabfc6aff9d5ee9af6699b4b790e6d7bd1b017"}, + {file = "cirro_api_client-1.9.0-py3-none-any.whl", hash = "sha256:4ef61fbb3bc371d77d5034fcc00ec49c00a873fe51749589760fd1e719d5ef13"}, + {file = "cirro_api_client-1.9.0.tar.gz", hash = "sha256:8fb0ebef1226adc9874ffa46e81f8ea159101bfb6db5d4c648ae154b51444cb7"}, ] [package.dependencies] @@ -1548,7 +1548,7 @@ files = [ [package.dependencies] attrs = ">=22.2.0" -jsonschema-specifications = ">=2023.03.6" +jsonschema-specifications = ">=2023.3.6" referencing = ">=0.28.4" rpds-py = ">=0.25.0" @@ -4028,7 +4028,7 @@ files = [ [package.dependencies] aiobotocore = ">=2.19.0,<4.0.0" -aiohttp = ">=3.9.0,<4.0.0a0 || >4.0.0a0,<4.0.0a1 || >4.0.0a1" +aiohttp = ">=3.9.0,!=4.0.0a0,!=4.0.0a1" fsspec = ">=2026.7.0,<2026.7.1" [[package]] @@ -4044,10 +4044,10 @@ files = [ ] [package.dependencies] -botocore = ">=1.37.4,<2.0a.0" +botocore = ">=1.37.4,<2.0a0" [package.extras] -crt = ["botocore[crt] (>=1.37.4,<2.0a.0)"] +crt = ["botocore[crt] (>=1.37.4,<2.0a0)"] [[package]] name = "shellingham" @@ -4662,4 +4662,4 @@ wdl = ["miniwdl"] [metadata] lock-version = "2.1" python-versions = ">3.11.0,<4.0" -content-hash = "1e9e4c155dc3d0e8b6250934ce71044e606ca0e3b8c5f4fa798e2a56251bad59" +content-hash = "c967953e3f45685843c586961bfae4e2e20b1c4276411320fd9247b8fa299e4b" diff --git a/pyproject.toml b/pyproject.toml index 015a9bf..fe5b6ef 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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.0" click = "^8.5.0" boto3 = "~=1.43" questionary = "^2.0.1" From 1fe1a049002eb51ab9ad6e0815e5f0afff1a94e4 Mon Sep 17 00:00:00 2001 From: Nathan Thorpe Date: Thu, 12 Nov 2026 13:21:43 -0800 Subject: [PATCH 2/4] sdk method --- cirro/sdk/dataset.py | 10 +++- cirro/sdk/project.py | 132 +++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 137 insertions(+), 5 deletions(-) diff --git a/cirro/sdk/dataset.py b/cirro/sdk/dataset.py index 98e1323..33a7239 100644 --- a/cirro/sdk/dataset.py +++ b/cirro/sdk/dataset.py @@ -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 @@ -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: """ diff --git a/cirro/sdk/project.py b/cirro/sdk/project.py index 458bde0..5d71bfc 100644 --- a/cirro/sdk/project.py +++ b/cirro/sdk/project.py @@ -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 @@ -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. @@ -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 From 74c780c4e1c981b10a7fab7f28bbfd9df5bb0406 Mon Sep 17 00:00:00 2001 From: Nathan Thorpe Date: Fri, 13 Nov 2026 14:04:42 -0800 Subject: [PATCH 3/4] bump --- poetry.lock | 20 ++++++++++---------- pyproject.toml | 2 +- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/poetry.lock b/poetry.lock index 2091018..cf21356 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1,4 +1,4 @@ -# This file is automatically @generated by Poetry 2.5.1 and should not be changed by hand. +# This file is automatically @generated by Poetry 2.1.2 and should not be changed by hand. [[package]] name = "aiobotocore" @@ -423,7 +423,7 @@ files = [ [package.dependencies] jmespath = ">=0.7.1,<2.0.0" python-dateutil = ">=2.1,<3.0.0" -urllib3 = ">=1.25.4,!=2.2.0,<3" +urllib3 = ">=1.25.4,<2.2.0 || >2.2.0,<3" [package.extras] crt = ["awscrt (==0.36.0)"] @@ -779,14 +779,14 @@ files = [ [[package]] name = "cirro-api-client" -version = "1.9.0" +version = "1.9.1" description = "A client library for accessing Cirro" optional = false python-versions = "<4.0,>=3.11" groups = ["main"] files = [ - {file = "cirro_api_client-1.9.0-py3-none-any.whl", hash = "sha256:4ef61fbb3bc371d77d5034fcc00ec49c00a873fe51749589760fd1e719d5ef13"}, - {file = "cirro_api_client-1.9.0.tar.gz", hash = "sha256:8fb0ebef1226adc9874ffa46e81f8ea159101bfb6db5d4c648ae154b51444cb7"}, + {file = "cirro_api_client-1.9.1-py3-none-any.whl", hash = "sha256:f439dc9d1afa7ae04ddf4dca8b836ecd9826a5a95616135938319cd72dc507a3"}, + {file = "cirro_api_client-1.9.1.tar.gz", hash = "sha256:63b0b8f73c7989bff264c5e0cdc35308f3c21472aa19a9d81e450f85bb01f78b"}, ] [package.dependencies] @@ -1548,7 +1548,7 @@ files = [ [package.dependencies] attrs = ">=22.2.0" -jsonschema-specifications = ">=2023.3.6" +jsonschema-specifications = ">=2023.03.6" referencing = ">=0.28.4" rpds-py = ">=0.25.0" @@ -4028,7 +4028,7 @@ files = [ [package.dependencies] aiobotocore = ">=2.19.0,<4.0.0" -aiohttp = ">=3.9.0,!=4.0.0a0,!=4.0.0a1" +aiohttp = ">=3.9.0,<4.0.0a0 || >4.0.0a0,<4.0.0a1 || >4.0.0a1" fsspec = ">=2026.7.0,<2026.7.1" [[package]] @@ -4044,10 +4044,10 @@ files = [ ] [package.dependencies] -botocore = ">=1.37.4,<2.0a0" +botocore = ">=1.37.4,<2.0a.0" [package.extras] -crt = ["botocore[crt] (>=1.37.4,<2.0a0)"] +crt = ["botocore[crt] (>=1.37.4,<2.0a.0)"] [[package]] name = "shellingham" @@ -4662,4 +4662,4 @@ wdl = ["miniwdl"] [metadata] lock-version = "2.1" python-versions = ">3.11.0,<4.0" -content-hash = "c967953e3f45685843c586961bfae4e2e20b1c4276411320fd9247b8fa299e4b" +content-hash = "209e875fe361bcd9973b7a31af6c4375bc354da946b397f0c014ab352409c104" diff --git a/pyproject.toml b/pyproject.toml index fe5b6ef..ee0b548 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -11,7 +11,7 @@ packages = [{include = "cirro"}] [tool.poetry.dependencies] python = ">3.11.0,<4.0" attrs = ">=21.3.0" -cirro_api_client = "~1.9.0" +cirro_api_client = "~1.9.1" click = "^8.5.0" boto3 = "~=1.43" questionary = "^2.0.1" From 5cd46498566b2bc31e7d729592261828f497798b Mon Sep 17 00:00:00 2001 From: Nathan Thorpe Date: Fri, 13 Nov 2026 18:10:17 -0800 Subject: [PATCH 4/4] bump version --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index ee0b548..8b60541 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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 "] license = "MIT"