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 get_samples function to retrieve the acronym #1166

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
62 changes: 56 additions & 6 deletions mxcubecore/HardwareObjects/ICATLIMS.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from mxcubecore.model.lims_session import (
Lims,
LimsSessionManager,
SampleSheet,
Session,
)

Expand Down Expand Up @@ -127,7 +128,10 @@ def get_samples(self, lims_name):
self.session_manager.active_session.proposal_name,
lims_name,
)
parcels = self.get_parcels_by_investigation_id()
parcels = self.get_parcels()

sample_sheets = self.get_samples_sheets()

queue_samples = []
for parcel in parcels:
pucks = parcel["content"]
Expand All @@ -150,7 +154,7 @@ def get_samples(self, lims_name):
)
for tracking_sample in tracking_samples:
queue_samples.append(
self.__to_sample(tracking_sample, puck)
self.__to_sample(tracking_sample, puck, sample_sheets)
)

except Exception as e:
Expand All @@ -169,8 +173,32 @@ def find(self, arr, atribute_name):
return x["value"]
return ""

def __to_sample(self, tracking_sample, puck):
def get_sample_sheet_by_id(
self, samples: List[SampleSheet], sample_id: int
) -> Optional[SampleSheet]:
"""
Retrieves a sample sheet by its unique ID.

Args:
samples (List[SampleSheet]): A list of Sample objects.
sample_id (int): The unique identifier of the sample sheet to retrieve.

Returns:
Optional[Sample]: The Sample object if found, otherwise None.
"""
return next((sample for sample in samples if sample.id == sample_id), None)

def __to_sample(self, tracking_sample, puck, sample_sheets: List[SampleSheet]):
"""Converts the sample tracking into the expected sample data structure"""

sample_name = str(tracking_sample["name"])
protein_acronym = sample_name
sample_sheet = self.get_sample_sheet_by_id(
sample_sheets, tracking_sample["sampleId"]
)
if sample_sheet is not None:
protein_acronym = sample_sheet.name

experiment_plan = tracking_sample["experimentPlan"]
return {
"cellA": self.find(experiment_plan, "unit_cell_a"),
Expand Down Expand Up @@ -201,10 +229,10 @@ def __to_sample(self, tracking_sample, puck):
"requiredResolution": self.find(experiment_plan, "requiredResolution"),
},
"experimentType": self.find(experiment_plan, "workflowType"),
"proteinAcronym": tracking_sample["name"],
"proteinAcronym": protein_acronym,
"sampleId": tracking_sample["sampleId"],
"sampleLocation": tracking_sample["sampleContainerPosition"],
"sampleName": str(tracking_sample["name"]),
"sampleName": sample_name,
"smiles": None,
}

Expand Down Expand Up @@ -481,7 +509,7 @@ def get_user_name(self):
def to_sessions(self, investigations):
return [self.__to_session(investigation) for investigation in investigations]

def get_parcels_by_investigation_id(self):
def get_parcels(self):
"""Returns the parcels associated to an investigation"""
try:
logging.getLogger("HWR").debug(
Expand All @@ -491,6 +519,7 @@ def get_parcels_by_investigation_id(self):
parcels = self.icatClient.get_parcels_by(
self.session_manager.active_session.session_id
)

logging.getLogger("HWR").debug(
"[ICAT] Successfully retrieved %s parcels" % (len(parcels))
)
Expand All @@ -501,6 +530,27 @@ def get_parcels_by_investigation_id(self):
)
return []

def get_samples_sheets(self) -> List[SampleSheet]:
"""Returns the samples sheets associated to an investigation"""
try:
logging.getLogger("HWR").debug(
"[ICAT] Retrieving samples by investigation_id %s "
% (self.session_manager.active_session.session_id)
)
samples = self.icatClient.get_samples_by(
self.session_manager.active_session.session_id
)
logging.getLogger("HWR").debug(
"[ICAT] Successfully retrieved %s samples" % (len(samples))
)
# Convert to object
return [SampleSheet.parse_obj(sample) for sample in samples]
except Exception as e:
logging.getLogger("HWR").error(
"[ICAT] get_samples_by_investigation_id %s " % (str(e))
)
return []

def echo(self):
"""Mockup for the echo method."""
return True
Expand Down
48 changes: 48 additions & 0 deletions mxcubecore/model/lims_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
timedelta,
)
from typing import (
Any,
Dict,
List,
Optional,
Expand Down Expand Up @@ -70,6 +71,43 @@ class Session(BaseModel):
logbook_URL: Optional[str] = None


class Instrument(BaseModel):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would in general be very nice with some comments on each field and class, unless its evident what it is. It would make it easier to understand what purpose they have and for others to adopt.

name: str
id: int
instrumentScientists: List[Any]


class Investigation(BaseModel):
name: str
startDate: datetime
endDate: datetime
id: int
title: str
visitId: str
summary: str
parameters: Dict[str, Any]
instrument: Instrument
investigationUsers: List[Any]


class Parameter(BaseModel):
name: str
value: str
id: int
units: str


class MetaPage(BaseModel):
totalWithoutFilters: int
total: int
totalPages: int
currentPage: int


class Meta(BaseModel):
page: MetaPage


class LimsUser(BaseModel):
user_name: str = ""
sessions: Optional[List[Session]] = []
Expand All @@ -79,3 +117,13 @@ class LimsSessionManager(BaseModel):
active_session: Optional[Session] = None
sessions: Optional[List[Session]] = []
users: Optional[Dict[str, LimsUser]] = {}


class SampleSheet(BaseModel):
id: int
name: str
investigation: Investigation
modTime: datetime
parameters: List[Parameter]
datasets: List[Any]
meta: Meta
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ PyTango = "^9.3.6"
python-ldap = "^3.4.0"
requests = "^2.31.0"
colorama = "^0.4.6"
pyicat-plus = {version = "^0.5.1", optional = true}
pyicat-plus = {version = "^0.6.0", optional = true}

[tool.poetry.group.dev.dependencies]
pre-commit = "^4.1.0"
Expand Down
Loading