From d3b3fc38263430346d8b9f21067e041fe81c9420 Mon Sep 17 00:00:00 2001 From: skchronicles Date: Fri, 1 Dec 2023 09:30:32 -0700 Subject: [PATCH] Feat: Add auto-filter to excel output --- VERSION | 2 +- workflow/rules/paired-end.smk | 2 ++ workflow/scripts/files2spreadsheet.py | 33 +++++++++++++++++++++++---- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/VERSION b/VERSION index f0bb29e..3a3cd8c 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.3.0 +1.3.1 diff --git a/workflow/rules/paired-end.smk b/workflow/rules/paired-end.smk index 6992165..0ab9801 100644 --- a/workflow/rules/paired-end.smk +++ b/workflow/rules/paired-end.smk @@ -780,6 +780,7 @@ rule blast_metaspades_xlsx: # of aligning the metaspades # contigs against NCBI viral db {params.script} \\ + --add-auto-filters \\ --rm-suffix "{params.extension}" \\ --input {output.tsv} {input.blasts} \\ --output {output.excel} @@ -888,6 +889,7 @@ rule blast_megahit_xlsx: # of aligning the megahit # contigs against NCBI viral db {params.script} \\ + --add-auto-filters \\ --rm-suffix "{params.extension}" \\ --input {output.tsv} {input.blasts} \\ --output {output.excel} diff --git a/workflow/scripts/files2spreadsheet.py b/workflow/scripts/files2spreadsheet.py index a38618b..4667598 100755 --- a/workflow/scripts/files2spreadsheet.py +++ b/workflow/scripts/files2spreadsheet.py @@ -12,7 +12,7 @@ # Script metadata __author__ = "Skyler Kuhn" -__version__ = "v0.1.0" +__version__ = "v0.2.0" _help = textwrap.dedent( """./file2spreadsheet.py: @@ -20,6 +20,7 @@ @Usage: $ ./file2spreadsheet.py [-h] [--version] \\ + [--add-auto-filters] \\ [--rm-suffix RM_SUFFIX] \\ [--comment-symbol COMMENT_SYMBOL] \\ --input FILE_1 [FILE_2 ...] \\ @@ -63,6 +64,12 @@ this character will be skipped. By default, all lines of the file will be included and nothing is skipped. + + -a, --add-auto-filters + Adds auto-filters to all columns + in a worksheet. Excel auto-filters + apply drop-down filters/selectors + to the column headers in a sheet. --h, --help Shows this help message and exits. @@ -154,6 +161,17 @@ def parse_arguments(): help = argparse.SUPPRESS ) + # Applies auto-filters + # to column headers + parser.add_argument( + '-a', + '--add-auto-filters', + required=False, + default=False, + action = 'store_true', + help = argparse.SUPPRESS + ) + # Add custom help message parser.add_argument( '-h', '--help', @@ -266,7 +284,7 @@ def csv(filename, subset=[], skip='#', **kwargs): return pd.read_csv(filename, comment=skip, **kwargs) -def excel_writer(files, spreadsheet, skip_comments=None, remove_suffix = ''): +def excel_writer(files, spreadsheet, skip_comments=None, remove_suffix = '', add_auto_filters=False): """Takes a list of files and creates one excel spreadsheet. Each file will becomes a sheet in the spreadsheet where the name of the sheet is the basename of the file with the extension @@ -317,6 +335,8 @@ def excel_writer(files, spreadsheet, skip_comments=None, remove_suffix = ''): ) ) + 2 # adding a little extra space worksheet.set_column(idx, idx, max_len) # set column width + if add_auto_filters: + worksheet.autofilter(0, 0, df.shape[0], df.shape[1]) def main(): @@ -335,16 +355,21 @@ def main(): # X tab/worksheet name rm_suffix = args.rm_suffix + # Apply auto filters to + # column headers in a sheet + auto_filter = args.add_auto_filters + # Create XLSX file from the list # of input files excel_writer( files=inputs, spreadsheet=output, skip_comments=comment_char, - remove_suffix=rm_suffix + remove_suffix=rm_suffix, + add_auto_filters=auto_filter ) if __name__ == '__main__': # Call main method - main() \ No newline at end of file + main()