Skip to content

Commit

Permalink
Merge branch 'master' into M_to_A_add_message
Browse files Browse the repository at this point in the history
Needed master to test on stage.
  • Loading branch information
RasmusBurge-CG committed Oct 10, 2022
2 parents 87f7a17 + 6123129 commit 4ccea60
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .bumpversion.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 6.4.0
current_version = 6.5.0
commit = True
tag = True
tag_name = v{new_version}
Expand Down
29 changes: 24 additions & 5 deletions cg_lims/EPPs/udf/set/set_barcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def get_barcode_set_udf(
artifacts: List[Artifact],
artifact_udf: str,
container_type: str,
measurement: bool,
):

"""Assigning barcode to a barcode udf.
Expand All @@ -31,6 +32,14 @@ def get_barcode_set_udf(
failed_samples = []

for artifact in artifacts:
if measurement:
if len(artifact.samples) > 1:
LOG.info(f"Sample {str(artifact.id)} is a pool, and will be excluded")

else:
measurement_artifact = artifact
artifact = artifact.input_artifact_list()[0]

try:
art_container_type = artifact.container.type.name

Expand All @@ -51,9 +60,16 @@ def get_barcode_set_udf(
# Sample has the correct "container_type".
else:
barcode = get_barcode(artifact)
artifact.udf[artifact_udf] = barcode
artifact.put()
assigned_artifacts += 1

if measurement:
measurement_artifact.udf[artifact_udf] = barcode
measurement_artifact.put()
assigned_artifacts += 1

else:
artifact.udf[artifact_udf] = barcode
artifact.put()
assigned_artifacts += 1

# Collects failed samples.
except:
Expand All @@ -74,24 +90,27 @@ def get_barcode_set_udf(
@click.command()
@options.artifact_udf(help="The name of the barcode udf.")
@options.input()
@options.measurement()
@options.container_type()
@click.pass_context
def assign_barcode(
ctx: click.Context,
artifact_udf: str,
input: bool,
measurement: bool,
container_type: str,
):
"""Assigned barcode to UDF"""

LOG.info(f"Running {ctx.command_path} with params: {ctx.params}")
process = ctx.obj["process"]
artifacts = get_artifacts(process=process, input=input)
artifacts = get_artifacts(process=process, input=input, measurement=measurement)
try:
get_barcode_set_udf(
artifacts=artifacts,
artifact_udf=artifact_udf,
container_type=container_type
container_type=container_type,
measurement=measurement,
)
message = "Barcodes were successfully generated."
LOG.info(message)
Expand Down
2 changes: 1 addition & 1 deletion cg_lims/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "6.4.0"
__version__ = "6.5.0"
71 changes: 64 additions & 7 deletions tests/EPPs/udf/set/test_set_barcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from tests.conftest import server


def test_with_tube(lims):
def test_tube_barcode(lims):
# GIVEN four artifacts, only two of them ACC9553A3PA1
# and ACC9621A7PA1 have container type "Tube" and are the
# only ones to be have assigned a barcode udf.
Expand All @@ -27,7 +27,7 @@ def test_with_tube(lims):
del artifact.udf[barcode_udf]

# WHEN running get_barcode_set_udf
get_barcode_set_udf(artifacts=artifacts, artifact_udf=barcode_udf, container_type=container)
get_barcode_set_udf(artifacts=artifacts, artifact_udf=barcode_udf, container_type=container, measurement=False)

# THEN only tubes should get barcodes and be correct.
for artifact in artifacts:
Expand Down Expand Up @@ -57,7 +57,7 @@ def test_plate_barcode(lims):

# WHEN running get_barcode_set_udf
with pytest.raises(MissingValueError) as error_message:
get_barcode_set_udf(artifacts=artifacts, artifact_udf=barcode_udf, container_type=container)
get_barcode_set_udf(artifacts=artifacts, artifact_udf=barcode_udf, container_type=container, measurement=False)

# THEN InvalidValueError exception should be raised.
# Because plate barcode has no specific barcode.
Expand All @@ -81,7 +81,7 @@ def test_pool_barcode(lims):
exit()

# WHEN running function get_barcode_set_udf.
get_barcode_set_udf(artifacts=artifacts, artifact_udf=barcode_udf, container_type=container)
get_barcode_set_udf(artifacts=artifacts, artifact_udf=barcode_udf, container_type=container, measurement=False)

# THEN correct barcode should be assigned.
for artifact in artifacts:
Expand All @@ -108,7 +108,7 @@ def test_no_container_type(lims):
del artifact.udf[barcode_udf]

# WHEN running get_barcode_set_udf
get_barcode_set_udf(artifacts=artifacts, artifact_udf=barcode_udf, container_type="")
get_barcode_set_udf(artifacts=artifacts, artifact_udf=barcode_udf, container_type="", measurement=False)

# THEN all artifacts should get barcodes and be correct.
for artifact in artifacts:
Expand All @@ -125,7 +125,7 @@ def test_invalid_value(lims):

# WHEN running function get_barcode_set_udf.
with pytest.raises(InvalidValueError) as error_message:
get_barcode_set_udf(artifacts=artifact, artifact_udf=barcode_udf, container_type=container)
get_barcode_set_udf(artifacts=artifact, artifact_udf=barcode_udf, container_type=container, measurement=False)

# THEN InvalidValueError should be triggered.
assert (
Expand All @@ -150,7 +150,7 @@ def test_missing_value(lims):

# WHEN running function get_barcode_set_udf.
with pytest.raises(MissingValueError) as error_message:
get_barcode_set_udf(artifacts=artifacts, artifact_udf=barcode_udf, container_type=container)
get_barcode_set_udf(artifacts=artifacts, artifact_udf=barcode_udf, container_type=container, measurement=False)

# THEN no barcodes should be assigned and barcode_udf should not
# exists.
Expand All @@ -161,3 +161,60 @@ def test_missing_value(lims):

with pytest.raises(KeyError):
artifact.udf[barcode_udf]


def test_container_on_measurement(lims):
# GIVEN measurements with different containers.
barcode_udf = 'Output Container Barcode'
container = 'Tube'
server("reception_control_wgs")
process = Process(lims, id="24-349794")
measurements = get_artifacts(process=process, measurement=True)

for measurement in measurements:
try:
del measurement.udf[barcode_udf]
except:
continue

# WHEN running get_barcode_set_udf
get_barcode_set_udf(artifacts=measurements, artifact_udf=barcode_udf, container_type=container, measurement=True)

# THEN barcodes should be assigned on measurement level only to samples with correct container.
for measurement in measurements:
if measurement.samples[0].artifact.container.type.name != container:
with pytest.raises(KeyError):
measurement.udf[barcode_udf]

else:
barcode = get_barcode(measurement.samples[0].artifact)
assert measurement.udf[barcode_udf] == barcode


def test_missing_value_on_measurement(lims):
# GIVEN measurements with different containers.
barcode_udf = 'Output Container Barcode'
container = 'foobar'
server("reception_control_wgs")
process = Process(lims, id="24-349794")
measurements = get_artifacts(process=process, measurement=True)

for measurement in measurements:
try:
del measurement.udf[barcode_udf]
except:
continue

# WHEN running function get_barcode_set_udf.
with pytest.raises(MissingValueError) as error_message:
get_barcode_set_udf(artifacts=measurements, artifact_udf=barcode_udf, container_type=container, measurement=True)

# THEN no barcodes should be assigned and barcode_udf should not
# exists.
assert (
f"No barcode assigned. Check parameters."
in error_message.value.message
)

with pytest.raises(KeyError):
measurement.udf[barcode_udf]

0 comments on commit 4ccea60

Please sign in to comment.