Skip to content

Commit

Permalink
Update Saphyr concentration inputs (#491)(patch)
Browse files Browse the repository at this point in the history
### Changed
- Made the number of concentration replicates flexible
- Added a new click option
  • Loading branch information
idalindegaard authored Mar 28, 2024
1 parent 3066128 commit 5bda02d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
21 changes: 10 additions & 11 deletions cg_lims/EPPs/udf/calculate/calculate_saphyr_concentration.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@

import click
import numpy as np
from cg_lims import options
from cg_lims.exceptions import LimsError, MissingUDFsError
from cg_lims.get.artifacts import get_artifacts
from genologics.entities import Artifact

LOG = logging.getLogger(__name__)


def get_concentrations(artifact: Artifact) -> List[float]:
"""Returns a list of all concentration replicates called
Concentration 1 (ng/ul), Concentration 2 (ng/ul) and Concentration 3 (ng/ul) of an artifact."""
udf_names = ["Concentration 1 (ng/ul)", "Concentration 2 (ng/ul)", "Concentration 3 (ng/ul)"]
def get_concentrations(artifact: Artifact, udf_names: List[str]) -> List[float]:
"""Returns a list of all concentration replicates of an artifact."""
concentrations = []
for name in udf_names:
concentrations.append(artifact.udf.get(name))
Expand All @@ -35,11 +34,11 @@ def calculate_cv(concentrations: List[float]) -> float:
return coefficient_variation


def set_average_and_cv(artifact: Artifact) -> None:
def set_average_and_cv(artifact: Artifact, udf_names: List[str]) -> None:
"""Calls on the previous functions get_concentration, calculate_average_concentration and calculate_cv
and updates the UDFs Average concentration (ng/ul) and Coefficient of variation (CV) with the calculated values
"""
concentrations = get_concentrations(artifact=artifact)
concentrations = get_concentrations(artifact=artifact, udf_names=udf_names)
average_concentration = calculate_average_concentration(concentrations=concentrations)
coefficient_variation = calculate_cv(concentrations=concentrations)

Expand All @@ -48,10 +47,9 @@ def set_average_and_cv(artifact: Artifact) -> None:
artifact.put()


def validate_udf_values(artifact: Artifact) -> bool:
def validate_udf_values(artifact: Artifact, udf_names: List[str]) -> bool:
"""A function checking whether a concentration in the list of concentrations for each artifact has a negative/no/zero value.
Then the function returns the output as 'False' and logs all those sample IDs in the EPP log"""
udf_names = ["Concentration 1 (ng/ul)", "Concentration 2 (ng/ul)", "Concentration 3 (ng/ul)"]
output = True
for name in udf_names:
if not artifact.udf.get(name) or artifact.udf.get(name) < 0:
Expand All @@ -63,8 +61,9 @@ def validate_udf_values(artifact: Artifact) -> bool:


@click.command()
@options.concentration_replicates()
@click.pass_context
def calculate_saphyr_concentration(ctx) -> None:
def calculate_saphyr_concentration(ctx, concentration_udf: List[str]) -> None:
"""Calculates and sets the average concentration and coefficient of variance based on three given concentrations.
Returns a message if this worked well, and if there were negative/no/zero concentration values, there's an error message for this
"""
Expand All @@ -76,10 +75,10 @@ def calculate_saphyr_concentration(ctx) -> None:
artifacts: List[Artifact] = get_artifacts(process=process, measurement=True)
failed_samples = 0
for artifact in artifacts:
if not validate_udf_values(artifact=artifact):
if not validate_udf_values(artifact=artifact, udf_names=concentration_udf):
failed_samples += 1
continue
set_average_and_cv(artifact=artifact)
set_average_and_cv(artifact=artifact, udf_names=concentration_udf)
if failed_samples:
raise MissingUDFsError(
f"{failed_samples} samples have invalid concentration values (<= 0). See log for more information."
Expand Down
10 changes: 10 additions & 0 deletions cg_lims/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,16 @@ def artifact_udfs(help: str = "Artifact udfs.") -> click.option:
)


def concentration_replicates(help: str = "Udf name for concentration replicates.") -> click.option:
return click.option(
"-cr",
"--concentration-udf",
required=True,
multiple=True,
help=help,
)


def source_artifact_udfs(help: str = "Artifact udfs.") -> click.option:
return click.option(
"-sau",
Expand Down

0 comments on commit 5bda02d

Please sign in to comment.