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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,14 @@ Options:
--data-type, --process TEXT Name or ID of the data type (--process is deprecated)
--data-directory TEXT Directory you wish to upload
--file TEXT Relative path of the file(s) to upload (optional, can be used multiple times)
--tag TEXT Tag to apply to the dataset (optional, can be used multiple times)
-i, --interactive Gather arguments interactively
--include-hidden Include hidden files in the upload (e.g., files starting with .)
--help Show this message and exit.
```

```bash
$ cirro upload --project "Test Project 1" --name "test" --file "sample1.fastq.gz" --file "sample2.fastq.gz" --data-directory "~/data" --data-type "Paired DNAseq (FASTQ)"
$ cirro upload --project "Test Project 1" --name "test" --file "sample1.fastq.gz" --file "sample2.fastq.gz" --data-directory "~/data" --data-type "Paired DNAseq (FASTQ)" --tag "study-x" --tag "batch: 2"
```

#### Resuming an upload:
Expand Down Expand Up @@ -188,6 +189,7 @@ $ cirro upload --interactive
? What type of files? Illumina Sequencing Run
? What is the name of this dataset? test
? Enter a description of the dataset (optional)
? Enter tags for the dataset, separated by commas (optional) study-x, batch: 2
```

## Python Usage
Expand Down
4 changes: 4 additions & 0 deletions cirro/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ def download(**kwargs):
help='Relative path of the file(s) to upload (optional, can be used multiple times)',
default=[],
multiple=True)
@click.option('--tag',
help='Tag to apply to the dataset (optional, can be used multiple times)',
default=[],
multiple=True)
@click.option('-i', '--interactive',
help='Gather arguments interactively',
is_flag=True, default=False)
Expand Down
5 changes: 3 additions & 2 deletions cirro/cli/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import sys
from pathlib import Path

from cirro_api_client.v1.models import UploadDatasetRequest, Status, Executor
from cirro_api_client.v1.models import UploadDatasetRequest, Status, Executor, Tag

from cirro.cirro_client import CirroApi
from cirro.cli.interactive.auth_args import gather_auth_config
Expand Down Expand Up @@ -93,7 +93,8 @@ def run_ingest(input_params: UploadArguments, interactive=False):
process_id=process.id,
name=input_params['name'],
description=input_params['description'],
expected_files=files
expected_files=files,
tags=[Tag(value=tag) for tag in input_params['tag']]
)

project_id = get_id_from_name(projects, input_params['project'])
Expand Down
13 changes: 13 additions & 0 deletions cirro/cli/interactive/upload_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ def ask_description(input_value: str) -> str:
return answers['description']


def ask_tags(input_value: List[str]) -> List[str]:
tags_prompt = {
'type': 'input',
'name': 'tags',
'message': 'Enter tags for the dataset, separated by commas (optional)',
'default': ', '.join(input_value or [])
}

answers = prompt_wrapper(tags_prompt)
return [tag.strip() for tag in answers['tags'].split(',') if tag.strip()]


def confirm_data_files(data_directory: str, files: List[str]):
stats = get_files_stats([
Path(data_directory) / file
Expand Down Expand Up @@ -174,4 +186,5 @@ def gather_upload_arguments(input_params: UploadArguments, projects: List[Projec
default_name = input_params.get('name') or data_directory_name
input_params['name'] = ask_name(default_name)
input_params['description'] = ask_description(input_params.get('description'))
input_params['tag'] = ask_tags(input_params.get('tag'))
return input_params, files
1 change: 1 addition & 0 deletions cirro/cli/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class UploadArguments(TypedDict):
include_hidden: bool
interactive: bool
file: Optional[list[str]]
tag: Optional[list[str]]
threads: int


Expand Down
75 changes: 75 additions & 0 deletions tests/test_cli_upload.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import tempfile
import unittest
from pathlib import Path
from unittest.mock import MagicMock, patch

from click.testing import CliRunner

from cirro.cli.cli import run
from cirro.cli.interactive.upload_args import ask_tags


def _named(id_, name):
item = MagicMock(id=id_)
item.name = name
return item


class TestUploadTags(unittest.TestCase):
def setUp(self):
self.data_dir = tempfile.TemporaryDirectory()
Path(self.data_dir.name, "sample.fastq.gz").write_text("reads")

self.cirro = MagicMock()
self.cirro.projects.list.return_value = [_named("project-1", "Test Project")]
process = _named("process-1", "Paired DNAseq (FASTQ)")
self.cirro.processes.list.return_value = [process]
self.cirro.processes.get.return_value = process

def tearDown(self):
self.data_dir.cleanup()

def _upload(self, *extra_args):
args = [
"upload",
"--project", "Test Project",
"--data-type", "Paired DNAseq (FASTQ)",
"--name", "test",
"--data-directory", self.data_dir.name,
*extra_args
]
with patch("cirro.cli.controller._init_cirro_client", return_value=self.cirro):
result = CliRunner().invoke(run, args, catch_exceptions=False)
self.assertEqual(result.exit_code, 0, result.output)
return self.cirro.datasets.create.call_args.kwargs["upload_request"]

def test_tags_passed_to_create_request(self):
request = self._upload("--tag", "microbiome-portal: metaphlan", "--tag", "study-x")
tags = request.to_dict()["tags"]
self.assertEqual([tag["value"] for tag in tags], ["microbiome-portal: metaphlan", "study-x"])
# Tags are keyless; `key` is deprecated
self.assertTrue(all("key" not in tag for tag in tags))

def test_no_tags(self):
request = self._upload()
self.assertEqual(request.tags, [])


class TestAskTags(unittest.TestCase):
def _ask(self, answer, input_value=None):
with patch("cirro.cli.interactive.upload_args.prompt_wrapper", return_value={"tags": answer}) as prompt:
tags = ask_tags(input_value)
return tags, prompt.call_args.args[0]["default"]

def test_splits_and_strips(self):
tags, _ = self._ask(" microbiome-portal: wgs-reads , study-x,, ")
self.assertEqual(tags, ["microbiome-portal: wgs-reads", "study-x"])

def test_blank_answer_gives_no_tags(self):
tags, default = self._ask("")
self.assertEqual(tags, [])
self.assertEqual(default, "")

def test_defaults_to_command_line_tags(self):
_, default = self._ask("a, b", input_value=("a", "b"))
self.assertEqual(default, "a, b")
Loading