Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added drop-down functionality to build-schema #375

Merged
merged 3 commits into from
Jan 27, 2025
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Code contributions to the release:
- Added log functionality to build-schema module [#340](https://github.com/BU-ISCIII/relecov-tools/pull/340)
- Updated the metadata_processing field in configuration.json and added the other_preparation_kit, quality_control_metrics and consensus_criteria fields in the json schema [#372](https://github.com/BU-ISCIII/relecov-tools/pull/372)
- Added quality control functionality to read-bioinfo-metadata [#373](https://github.com/BU-ISCIII/relecov-tools/pull/373)
- Added dropdown functionality to build-schema enums [#374](https://github.com/BU-ISCIII/relecov-tools/pull/374)

#### Fixes

Expand Down
54 changes: 54 additions & 0 deletions relecov_tools/build_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import rich.console
import pandas as pd
import os
import openpyxl
import sys
import json
import difflib
Expand All @@ -13,6 +14,7 @@
import relecov_tools.assets.schema_utils.jsonschema_draft
import relecov_tools.assets.schema_utils.metadatalab_template
from relecov_tools.config_json import ConfigJson
from openpyxl.worksheet.datavalidation import DataValidation

log = logging.getLogger(__name__)
stderr = rich.console.Console(
Expand Down Expand Up @@ -668,6 +670,58 @@ def create_metadatalab_excel(self, json_schema):
log.error(f"Error writing to Excel: {e}")
stderr.print(f"[red]Error writing to Excel: {e}")
return None

try:
wb = openpyxl.load_workbook(out_file)
ws_metadata = wb["METADATA_LAB"]

ws_dropdowns = (
wb.create_sheet("DROPDOWNS")
if "DROPDOWNS" not in wb.sheetnames
else wb["DROPDOWNS"]
)

for row in ws_dropdowns.iter_rows():
for cell in row:
cell.value = None

for col_idx, (property_id, enum_values) in enumerate(
zip(df["property_id"], df["enum"]), start=1
):
if isinstance(enum_values, list) and len(enum_values) > 0:
start_row = 1
col_letter = openpyxl.utils.get_column_letter(col_idx)
for row_offset, value in enumerate(
enum_values, start=start_row
):
ws_dropdowns[f"{col_letter}{row_offset}"] = value

dropdown_range_address = f"DROPDOWNS!${col_letter}${start_row}:${col_letter}${start_row + len(enum_values) - 1}"

col_letter_metadata = ws_metadata.cell(
row=4, column=col_idx + 1
).column_letter
dropdown_range_metadata = (
f"{col_letter_metadata}5:{col_letter_metadata}1000"
)
dropdown = DataValidation(
type="list",
formula1=f"{dropdown_range_address}",
allow_blank=True,
)
dropdown.error = "Invalid value"
dropdown.errorTitle = "Invalid entry"
dropdown.prompt = f"Select a value for {property_id}"
dropdown.promptTitle = "Value selection"

ws_metadata.add_data_validation(dropdown)
dropdown.add(dropdown_range_metadata)
ws_dropdowns.sheet_state = "hidden"
wb.save(out_file)
except Exception as e:
log.error(f"Error adding dropdowns: {e}")
stderr.print(f"[red]Error adding dropdowns: {e}")
return None
except Exception as e:
log.error(f"Error in create_metadatalab_excel: {e}")
stderr.print(f"[red]Error in create_metadatalab_excel: {e}")
Expand Down
Loading