Bug fixes.
This commit is contained in:
@@ -269,7 +269,7 @@ from .controls import *
|
||||
from .organizations import *
|
||||
from .kits import *
|
||||
from .submissions import *
|
||||
from .audit import AuditLog
|
||||
from .audit import *
|
||||
|
||||
# NOTE: Add a creator to the submission for reagent association. Assigned here due to circular import constraints.
|
||||
# https://docs.sqlalchemy.org/en/20/orm/extensions/associationproxy.html#sqlalchemy.ext.associationproxy.association_proxy.params.creator
|
||||
|
||||
@@ -332,7 +332,6 @@ class KitType(BaseClass):
|
||||
return new_kit
|
||||
|
||||
|
||||
|
||||
class ReagentRole(BaseClass):
|
||||
"""
|
||||
Base of reagent type abstract
|
||||
|
||||
@@ -1265,7 +1265,9 @@ class BasicSubmission(BaseClass, LogMixin):
|
||||
logger.error(f"Couldn't save association with {equip} due to {e}")
|
||||
if equip.tips:
|
||||
for tips in equip.tips:
|
||||
logger.debug(f"Attempting to add tips assoc: {tips} (pydantic)")
|
||||
tassoc = tips.to_sql(submission=self)
|
||||
logger.debug(f"Attempting to add tips assoc: {tips.__dict__} (sql)")
|
||||
if tassoc not in self.submission_tips_associations:
|
||||
tassoc.save()
|
||||
else:
|
||||
|
||||
@@ -2,6 +2,6 @@
|
||||
Contains pandas and openpyxl convenience functions for interacting with excel workbooks
|
||||
'''
|
||||
|
||||
from .reports import *
|
||||
from .parser import *
|
||||
from .reports import *
|
||||
from .writer import *
|
||||
|
||||
@@ -4,20 +4,24 @@ from datetime import datetime
|
||||
from tools import Settings
|
||||
from backend import BasicSample
|
||||
from backend.db import IridaControl, ControlType
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
logger = logging.getLogger(f"submissions.{__name__}")
|
||||
|
||||
|
||||
def script(ctx:Settings):
|
||||
def script(ctx: Settings):
|
||||
"""
|
||||
Grabs Irida controls from secondary database.
|
||||
|
||||
Args:
|
||||
ctx (Settings): Settings inherited from app.
|
||||
|
||||
"""
|
||||
ct = ControlType.query(name="Irida Control")
|
||||
existing_controls = [item.name for item in IridaControl.query()]
|
||||
# NOTE: Because the main session will be busy in another thread, this requires a new session.
|
||||
new_session = Session(ctx.database_session.get_bind())
|
||||
# ct = ControlType.query(name="Irida Control")
|
||||
ct = new_session.query(ControlType).filter(ControlType.name == "Irida Control").first()
|
||||
# existing_controls = [item.name for item in IridaControl.query()]
|
||||
existing_controls = [item.name for item in new_session.query(IridaControl)]
|
||||
prm_list = ", ".join([f"'{thing}'" for thing in existing_controls])
|
||||
ctrl_db_path = ctx.directory_path.joinpath("submissions_parser_output", "submissions.db")
|
||||
try:
|
||||
@@ -25,29 +29,39 @@ def script(ctx:Settings):
|
||||
except AttributeError as e:
|
||||
logger.error(f"Error, could not import from irida due to {e}")
|
||||
return
|
||||
sql = f"SELECT name, submitted_date, submission_id, contains, matches, kraken, subtype, refseq_version, " \
|
||||
f"kraken2_version, kraken2_db_version, sample_id FROM _iridacontrol INNER JOIN _control on _control.id " \
|
||||
sql = "SELECT name, submitted_date, submission_id, contains, matches, kraken, subtype, refseq_version, " \
|
||||
"kraken2_version, kraken2_db_version, sample_id FROM _iridacontrol INNER JOIN _control on _control.id " \
|
||||
f"= _iridacontrol.id WHERE _control.name NOT IN ({prm_list})"
|
||||
cursor = conn.execute(sql)
|
||||
records = [dict(name=row[0], submitted_date=row[1], submission_id=row[2], contains=row[3], matches=row[4], kraken=row[5],
|
||||
subtype=row[6], refseq_version=row[7], kraken2_version=row[8], kraken2_db_version=row[9],
|
||||
sample_id=row[10]) for row in cursor]
|
||||
records = [
|
||||
dict(name=row[0], submitted_date=row[1], submission_id=row[2], contains=row[3], matches=row[4], kraken=row[5],
|
||||
subtype=row[6], refseq_version=row[7], kraken2_version=row[8], kraken2_db_version=row[9],
|
||||
sample_id=row[10]) for row in cursor]
|
||||
for record in records:
|
||||
instance = IridaControl.query(name=record['name'])
|
||||
# instance = IridaControl.query(name=record['name'])
|
||||
instance = new_session.query(IridaControl).filter(IridaControl.name == record['name']).first()
|
||||
if instance:
|
||||
logger.warning(f"Irida Control {instance.name} already exists, skipping.")
|
||||
continue
|
||||
record['contains'] = json.loads(record['contains'])
|
||||
assert isinstance(record['contains'], dict)
|
||||
record['matches'] = json.loads(record['matches'])
|
||||
assert isinstance(record['matches'], dict)
|
||||
record['kraken'] = json.loads(record['kraken'])
|
||||
assert isinstance(record['kraken'], dict)
|
||||
for thing in ['contains', 'matches', 'kraken']:
|
||||
if record[thing]:
|
||||
record[thing] = json.loads(record[thing])
|
||||
assert isinstance(record[thing], dict)
|
||||
else:
|
||||
record[thing] = {}
|
||||
# record['matches'] = json.loads(record['matches'])
|
||||
# assert isinstance(record['matches'], dict)
|
||||
# record['kraken'] = json.loads(record['kraken'])
|
||||
# assert isinstance(record['kraken'], dict)
|
||||
record['submitted_date'] = datetime.strptime(record['submitted_date'], "%Y-%m-%d %H:%M:%S.%f")
|
||||
assert isinstance(record['submitted_date'], datetime)
|
||||
instance = IridaControl(controltype=ct, **record)
|
||||
sample = BasicSample.query(submitter_id=instance.name)
|
||||
# sample = BasicSample.query(submitter_id=instance.name)
|
||||
sample = new_session.query(BasicSample).filter(BasicSample.submitter_id == instance.name).first()
|
||||
if sample:
|
||||
instance.sample = sample
|
||||
instance.submission = sample.submissions[0]
|
||||
instance.save()
|
||||
# instance.save()
|
||||
new_session.add(instance)
|
||||
new_session.commit()
|
||||
new_session.close()
|
||||
|
||||
@@ -295,7 +295,8 @@ class PydTips(BaseModel):
|
||||
Returns:
|
||||
SubmissionTipsAssociation: Association between queried tips and submission
|
||||
"""
|
||||
tips = Tips.query(name=self.name, lot=self.lot, limit=1)
|
||||
tips = Tips.query(name=self.name, limit=1)
|
||||
logger.debug(f"Tips query has yielded: {tips}")
|
||||
assoc = SubmissionTipsAssociation.query(tip_id=tips.id, submission_id=submission.id, role=self.role, limit=1)
|
||||
if assoc is None:
|
||||
assoc = SubmissionTipsAssociation(submission=submission, tips=tips, role_name=self.role)
|
||||
@@ -900,17 +901,20 @@ class PydSubmission(BaseModel, extra='allow'):
|
||||
return render
|
||||
|
||||
# @report_result
|
||||
def check_kit_integrity(self, extraction_kit: str | dict | None = None) -> Tuple[List[PydReagent], Report]:
|
||||
def check_kit_integrity(self, extraction_kit: str | dict | None = None, exempt:List[PydReagent]=[]) -> Tuple[
|
||||
List[PydReagent], Report]:
|
||||
"""
|
||||
Ensures all reagents expected in kit are listed in Submission
|
||||
|
||||
Args:
|
||||
reagenttypes (list | None, optional): List to check against complete list. Defaults to None.
|
||||
extraction_kit (str | dict | None, optional): kit to be checked. Defaults to None.
|
||||
exempt (List[PydReagent], optional): List of reagents that don't need to be checked. Defaults to []
|
||||
|
||||
Returns:
|
||||
Report: Result object containing a message and any missing components.
|
||||
Tuple[List[PydReagent], Report]: List of reagents and Result object containing a message and any missing components.
|
||||
"""
|
||||
report = Report()
|
||||
# logger.debug(f"The following reagents are exempt from the kit integrity check:\n{exempt}")
|
||||
if isinstance(extraction_kit, str):
|
||||
extraction_kit = dict(value=extraction_kit)
|
||||
if extraction_kit is not None and extraction_kit != self.extraction_kit['value']:
|
||||
@@ -922,7 +926,8 @@ class PydSubmission(BaseModel, extra='allow'):
|
||||
expected_check = [item.role for item in ext_kit_rtypes]
|
||||
output_reagents = [rt for rt in self.reagents if rt.role in expected_check]
|
||||
missing_check = [item.role for item in output_reagents]
|
||||
missing_reagents = [rt for rt in ext_kit_rtypes if rt.role not in missing_check]
|
||||
missing_reagents = [rt for rt in ext_kit_rtypes if rt.role not in missing_check and rt.role not in exempt]
|
||||
# logger.debug(f"Missing reagents: {missing_reagents}")
|
||||
missing_reagents += [rt for rt in output_reagents if rt.missing]
|
||||
output_reagents += [rt for rt in missing_reagents if rt not in output_reagents]
|
||||
# NOTE: if lists are equal return no problem
|
||||
@@ -930,8 +935,8 @@ class PydSubmission(BaseModel, extra='allow'):
|
||||
result = None
|
||||
else:
|
||||
result = Result(
|
||||
msg=f"The excel sheet you are importing is missing some reagents expected by the kit.\n\nIt looks like you are missing: {[item.role.upper() for item in missing_reagents]}\n\nAlternatively, you may have set the wrong extraction kit.\n\nThe program will populate lists using existing reagents.\n\nPlease make sure you check the lots carefully!",
|
||||
status="Warning")
|
||||
msg=f"The excel sheet you are importing is missing some reagents expected by the kit.\n\nIt looks like you are missing: {[item.role.upper() for item in missing_reagents]}\n\nAlternatively, you may have set the wrong extraction kit.\n\nThe program will populate lists using existing reagents.\n\nPlease make sure you check the lots carefully!",
|
||||
status="Warning")
|
||||
report.add_result(result)
|
||||
return output_reagents, report
|
||||
|
||||
|
||||
Reference in New Issue
Block a user