Skip to content

Commit

Permalink
New Femtopulse csv file EPP (#569) (minor)
Browse files Browse the repository at this point in the history
### Added

- New EPP "Create Femtopulse Run File"
  • Loading branch information
idalindegaard authored Jan 7, 2025
1 parent 2a0eb15 commit e0538f5
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 2 deletions.
4 changes: 2 additions & 2 deletions cg_lims/EPPs/files/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
import click
from cg_lims.EPPs.files.barcode_tubes import make_barcode_csv
from cg_lims.EPPs.files.csv_for_kapa_truble_shooting.csv_for_kapa_debug import trouble_shoot_kapa

# commands
from cg_lims.EPPs.files.femtopulse_csv import make_femtopulse_csv
from cg_lims.EPPs.files.file_to_udf import csv_well_to_udf
from cg_lims.EPPs.files.hamilton.base import hamilton
from cg_lims.EPPs.files.ont_json_to_udf import parse_ont_report
Expand Down Expand Up @@ -33,5 +32,6 @@ def files(ctx):
files.add_command(create_sample_sheet)
files.add_command(parse_run_parameters)
files.add_command(parse_ont_report)
files.add_command(make_femtopulse_csv)
files.add_command(create_smrtlink_sample_setup)
files.add_command(create_smrtlink_run_design)
109 changes: 109 additions & 0 deletions cg_lims/EPPs/files/femtopulse_csv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import logging
import sys
from pathlib import Path
from typing import List

import click
import pandas as pd
from cg_lims import options
from cg_lims.exceptions import InvalidValueError, LimsError
from cg_lims.get.artifacts import get_artifacts
from cg_lims.get.fields import get_artifact_well
from genologics.lims import Artifact

LOG = logging.getLogger(__name__)


WELL_POSITIONS = [f"A{i}" for i in range(1, 13)] # List with well positions A1-A12
SAMPLE_NAMES = [""] * len(WELL_POSITIONS) # List with twelve empty positions for sample names
DATAFRAME = pd.DataFrame(
{"well positions": WELL_POSITIONS, "sample names": SAMPLE_NAMES}
) # Dataframe with well positions and sample names


def get_sample_artifact_name(artifact: Artifact):

artifact_name: str = artifact.samples[0].name

return artifact_name


def get_data_and_write(artifacts: List[Artifact], file: str):
"""Make a csv file for a Femtopulse run start with three columns:
one numbered 1-12, one with the sample position/well for the run and
a column with the sample name or ladder (in the 12th position)."""

failed_samples: list = []

for artifact in artifacts:

artifact_name: str = get_sample_artifact_name(artifact=artifact)

# Fetch sample well in format 'A1'
artifact_well: str = get_artifact_well(artifact=artifact)

# Checks that the sample well matches with one in the WELL_POSITIONS list (A1-A11)
# and adds the sample name to the SAMPLE_NAMES list for that position
if artifact_well in DATAFRAME["well positions"].values:
if artifact_well == DATAFRAME["well positions"].iloc[-1]:
failed_samples.append(
{
"artifact_name": artifact_name,
"parsed_well": artifact_well,
"error": "This position is reserved for the ladder.",
}
)
else:
DATAFRAME.loc[DATAFRAME["well positions"] == artifact_well, "sample names"] = (
artifact_name
)
else:
failed_samples.append(
{
"artifact_name": artifact_name,
"parsed_well": artifact_well,
"error": "This position is not possible for the run.",
}
)

# Prints out error message(s)
if failed_samples:
all_errors = ""
for sample in failed_samples:
error_message: str = (
f"Sample {sample['artifact_name']} in position {sample['parsed_well']}: {sample['error']}"
)
all_errors = all_errors + " " + error_message
raise InvalidValueError(f"Errors found: {all_errors}")

# The ladder will always be in well A12
DATAFRAME["sample names"].iloc[-1] = "ladder"

# Create the csv file
DATAFRAME.index = range(1, len(DATAFRAME) + 1)
DATAFRAME.to_csv(Path(file), index=True, header=False)


@click.command()
@options.file_placeholder()
@options.measurement()
@click.pass_context
def make_femtopulse_csv(
ctx: click.Context,
file: str,
measurement: bool,
):
"""Script to make a csv file for a Femtopulse run"""

LOG.info(f"Running {ctx.command_path} with params: {ctx.params}")
process = ctx.obj["process"]
artifacts = get_artifacts(process=process, measurement=measurement)

try:
get_data_and_write(
artifacts=artifacts,
file=f"{file}_femtopulse.csv",
)
click.echo("The file was successfully generated.")
except LimsError as e:
sys.exit(e.message)

0 comments on commit e0538f5

Please sign in to comment.