|
2 | 2 | By: Isak Sylvin, @sylvinite""" |
3 | 3 |
|
4 | 4 | #!/usr/bin/env python |
5 | | - |
6 | 5 | import glob |
7 | 6 | import gzip |
8 | 7 | import json |
|
29 | 28 | SlurmHeader, |
30 | 29 | Threshold, |
31 | 30 | ) |
| 31 | +from microSALT.exc.exceptions import JobCreationError |
32 | 32 | from microSALT.store.db_manipulator import DB_Manipulator |
| 33 | +from microSALT.store.orm_models import Projects |
33 | 34 | from microSALT.utils.referencer import Referencer |
34 | 35 |
|
35 | 36 |
|
@@ -525,57 +526,68 @@ def create_snpsection(self): |
525 | 526 |
|
526 | 527 | def create_collection(self): |
527 | 528 | """Creates collection entry in database""" |
528 | | - if self.db_pusher.exists("Collections", {"ID_collection": self.name}): |
529 | | - self.db_pusher.purge_rec(name=self.name, type="Collections") |
| 529 | + if self.db_pusher.read_exists("Collections", {"ID_collection": self.name}): |
| 530 | + self.db_pusher.delete_collection(self.name) |
530 | 531 | for sample in self.pool: |
531 | 532 | self.db_pusher.add_rec( |
532 | 533 | {"ID_collection": self.name, "CG_ID_sample": sample}, "Collections" |
533 | 534 | ) |
534 | 535 |
|
535 | | - addedprojs = list() |
| 536 | + addedprojs = [] |
536 | 537 | for sample in self.pool: |
537 | | - proj = re.search(r"(\w+)A(?:\w+)", sample).group(1) |
538 | | - if proj not in addedprojs: |
539 | | - self.create_project(proj) |
540 | | - addedprojs.append(proj) |
| 538 | + lims_project = re.search(r"(\w+)A(?:\w+)", sample).group(1) |
| 539 | + projects: Projects | None = self.db_pusher.read_report(lims_project) |
| 540 | + if not projects and lims_project not in addedprojs: |
| 541 | + self.create_project(lims_project) |
| 542 | + addedprojs.append(lims_project) |
541 | 543 |
|
542 | | - def create_project(self, name): |
| 544 | + def create_project(self, name: str): |
543 | 545 | """Creates project in database""" |
544 | | - proj_col = dict() |
545 | | - proj_col["CG_ID_project"] = name |
546 | | - proj_col["Customer_ID_project"] = self.sample.get("Customer_ID_project") |
547 | | - proj_col["Customer_ID"] = self.sample.get("Customer_ID") |
548 | | - self.db_pusher.add_rec(proj_col, "Projects") |
549 | | - self.db_pusher.upd_rec({"CG_ID_project": name}, "Projects", proj_col) |
550 | | - |
551 | | - def create_sample(self, name): |
| 546 | + if not self.sample: |
| 547 | + raise JobCreationError( |
| 548 | + "No sample information provided. Cannot create project in database." |
| 549 | + ) |
| 550 | + project_data: dict[str, str] = { |
| 551 | + "CG_ID_project": name, |
| 552 | + "Customer_ID_project": self.sample["Customer_ID_project"], |
| 553 | + "Customer_ID": self.sample["Customer_ID"], |
| 554 | + } |
| 555 | + self.db_pusher.add_to_session(self.db_pusher.add_project(**project_data)) |
| 556 | + self.db_pusher.commit_session() |
| 557 | + |
| 558 | + def create_sample(self): |
552 | 559 | """Creates sample in database""" |
| 560 | + |
553 | 561 | try: |
554 | | - sample_col = self.db_pusher.get_columns("Samples") |
555 | | - sample_col["CG_ID_sample"] = self.sample.get("CG_ID_sample") |
556 | | - sample_col["CG_ID_project"] = self.sample.get("CG_ID_project") |
557 | | - sample_col["Customer_ID_sample"] = self.sample.get("Customer_ID_sample") |
558 | | - sample_col["reference_genome"] = self.sample.get("reference") |
559 | | - sample_col["reference_length"] = self.sample.get("reference_length") |
560 | | - sample_col["date_analysis"] = self.dt |
561 | | - sample_col["organism"] = self.sample.get("organism") |
562 | | - sample_col["application_tag"] = self.sample.get("application_tag") |
563 | | - sample_col["priority"] = self.sample.get("priority") |
564 | | - sample_col["date_arrival"] = datetime.strptime( |
565 | | - self.sample.get("date_arrival"), "%Y-%m-%d %H:%M:%S" |
566 | | - ) |
567 | | - sample_col["date_sequencing"] = datetime.strptime( |
568 | | - self.sample.get("date_sequencing"), "%Y-%m-%d %H:%M:%S" |
569 | | - ) |
570 | | - sample_col["date_libprep"] = datetime.strptime( |
571 | | - self.sample.get("date_libprep"), "%Y-%m-%d %H:%M:%S" |
572 | | - ) |
573 | | - sample_col["method_libprep"] = self.sample.get("method_libprep") |
574 | | - sample_col["method_sequencing"] = self.sample.get("method_sequencing") |
575 | | - # self.db_pusher.purge_rec(sample_col['CG_ID_sample'], 'sample') |
576 | | - self.db_pusher.add_rec(sample_col, "Samples") |
577 | | - except Exception: |
578 | | - self.logger.error(f"Unable to add sample {self.name} to database") |
| 562 | + if not self.sample: |
| 563 | + raise JobCreationError( |
| 564 | + "No sample information provided. Cannot create sample in database." |
| 565 | + ) |
| 566 | + sample_data: dict[str, str | datetime] = { |
| 567 | + "CG_ID_sample": self.sample["CG_ID_sample"], |
| 568 | + "CG_ID_project": self.sample["CG_ID_project"], |
| 569 | + "Customer_ID_sample": self.sample["Customer_ID_sample"], |
| 570 | + "reference_genome": self.sample["reference"], |
| 571 | + "reference_length": self.sample["reference_length"], |
| 572 | + "date_analysis": self.dt, |
| 573 | + "organism": self.sample["organism"], |
| 574 | + "application_tag": self.sample["application_tag"], |
| 575 | + "priority": self.sample["priority"], |
| 576 | + "date_arrival": datetime.strptime(self.sample["date_arrival"], "%Y-%m-%d %H:%M:%S"), |
| 577 | + "date_sequencing": datetime.strptime( |
| 578 | + self.sample["date_sequencing"], "%Y-%m-%d %H:%M:%S" |
| 579 | + ), |
| 580 | + "date_libprep": datetime.strptime(self.sample["date_libprep"], "%Y-%m-%d %H:%M:%S"), |
| 581 | + "method_libprep": self.sample["method_libprep"], |
| 582 | + "method_sequencing": self.sample["method_sequencing"], |
| 583 | + } |
| 584 | + self.db_pusher.add_to_session(self.db_pusher.add_sample(**sample_data)) |
| 585 | + self.db_pusher.commit_session() |
| 586 | + except JobCreationError as e: |
| 587 | + self.logger.error(f"Unable to add sample {self.name} to database: {e}") |
| 588 | + except KeyError as e: |
| 589 | + self.logger.error(f"Missing key {e} in sample information for sample {self.name}") |
| 590 | + raise JobCreationError(f"Missing key {e} in sample information for sample {self.name}") |
579 | 591 |
|
580 | 592 | def project_job(self, single_sample=False): |
581 | 593 | if self.dry: |
@@ -800,7 +812,7 @@ def sample_job(self): |
800 | 812 | except Exception: |
801 | 813 | raise |
802 | 814 | try: |
803 | | - self.create_sample(self.name) |
| 815 | + self.create_sample() |
804 | 816 | except Exception: |
805 | 817 | self.logger.error(f"Unable to access LIMS info for sample {self.name}") |
806 | 818 | except Exception as e: |
|
0 commit comments