Skip to content

Commit 417ffe0

Browse files
authored
add download dir and template arguments to generic idc program (#138)
1 parent 6ab7564 commit 417ffe0

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

idc_index/cli.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,18 @@ def download_from_manifest(
296296
"generic_argument",
297297
type=str,
298298
)
299+
@click.option(
300+
"--download-dir",
301+
required=False,
302+
type=click.Path(),
303+
help="Path to the directory to download the files to.",
304+
)
305+
@click.option(
306+
"--dir-template",
307+
type=str,
308+
default=IDCClient.DOWNLOAD_HIERARCHY_DEFAULT,
309+
help="Download directory hierarchy template. This variable defines the folder hierarchy for the organizing the downloaded files in downloadDirectory. Defaults to index.DOWNLOAD_HIERARCHY_DEFAULT set to %collection_id/%PatientID/%StudyInstanceUID/%Modality_%SeriesInstanceUID. The template string can be built using a combination of selected metadata attributes (PatientID, collection_id, Modality, StudyInstanceUID, SeriesInstanceUID) that must be prefixed by '%'. The following special characters can be used as separators: '-' (hyphen), '/' (slash for subdirectories), '_' (underscore). When set to empty string (\"\") all files will be downloaded to the download directory with no subdirectories.",
310+
)
299311
@click.option(
300312
"--log-level",
301313
type=click.Choice(
@@ -304,7 +316,7 @@ def download_from_manifest(
304316
default="info",
305317
help="Set the logging level for the CLI module.",
306318
)
307-
def download(generic_argument, log_level):
319+
def download(generic_argument, download_dir, dir_template, log_level):
308320
"""Download content given the input parameter.
309321
310322
Determine whether the input parameter corresponds to a file manifest or a list of collection_id, PatientID, StudyInstanceUID, or SeriesInstanceUID values, and download the corresponding files into the current directory. Default parameters will be used for organizing the downloaded files into folder hierarchy. Use `download_from_selection()` and `download_from_manifest()` functions if granular control over the download process is needed.
@@ -316,12 +328,17 @@ def download(generic_argument, log_level):
316328

317329
logger_cli.info(f"Downloading from IDC {client.get_idc_version()} index")
318330

319-
download_dir = Path.cwd()
331+
if download_dir:
332+
download_dir = Path(download_dir)
333+
else:
334+
download_dir = Path.cwd()
320335

321336
if Path(generic_argument).is_file():
322337
# Parse the input parameters and pass them to IDC
323338
logger_cli.info("Detected manifest file, downloading from manifest.")
324-
client.download_from_manifest(generic_argument, downloadDir=download_dir)
339+
client.download_from_manifest(
340+
generic_argument, downloadDir=download_dir, dirTemplate=dir_template
341+
)
325342
# this is not a file manifest
326343
else:
327344
# Split the input string and filter out any empty values
@@ -344,7 +361,11 @@ def check_and_download(column_name, item_ids, download_dir, kwarg_name):
344361
)
345362
logger_cli.info(f"Identified matching {column_name}: {matched_ids}")
346363
client.download_from_selection(
347-
**{kwarg_name: matched_ids, "downloadDir": download_dir}
364+
**{
365+
kwarg_name: matched_ids,
366+
"downloadDir": download_dir,
367+
"dirTemplate": dir_template,
368+
}
348369
)
349370
return True
350371

0 commit comments

Comments
 (0)