forked from Galithil/scilifelab_epps
-
Notifications
You must be signed in to change notification settings - Fork 14
Aviti manifest upload #429
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
Merged
FranBonath
merged 15 commits into
NationalGenomicsInfrastructure:master
from
FranBonath:aviti_manifest_upload
Oct 7, 2025
Merged
Changes from 8 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
499a7fb
WIP upload manifest aviti epp
FranBonath 9a5ae39
first empty draft
FranBonath 2279a58
bug fix "Failed to validate the value for workflow type"
FranBonath fa1dce2
WIP_making_zip_file
FranBonath eac4c34
make zip manifest with altered file name, log original file name in m…
FranBonath 442f483
epp to upload zipped manual aviti manifest to ngi-nas-ns
FranBonath eb5e9ac
add helpfull comments
FranBonath aa6bc28
update version log
FranBonath dc7ed8d
apply ruff linting
FranBonath 55f358f
Update scripts/upload_manifest.py
FranBonath 296e4eb
Merge branch 'master' into aviti_manifest_upload
aanil 19d8958
Ruff
aanil 83f50e2
write zip from memory
FranBonath 1ae5655
Merge branch 'aviti_manifest_upload' of github.com:FranBonath/scilife…
FranBonath 33c64e6
Ruff again
aanil File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| #!/usr/bin/env python | ||
|
|
||
| from argparse import ArgumentParser, Namespace | ||
| from datetime import datetime as dt | ||
| from zipfile import ZipFile | ||
| import os | ||
| import logging | ||
| import shutil | ||
|
|
||
|
|
||
| from genologics.config import BASEURI, PASSWORD, USERNAME | ||
| from genologics.entities import Process | ||
| from genologics.lims import Lims | ||
|
|
||
| from scilifelab_epps.wrapper import epp_decorator | ||
|
|
||
| TIMESTAMP = dt.now().strftime("%y%m%d_%H%M%S") | ||
|
|
||
|
|
||
| def get_flowcell_id(process: Process) -> str: | ||
| """Get the Element flowcell ID from the process.""" | ||
| flowcell_ids = [ | ||
| op.container.name for op in process.all_outputs() if op.type == "Analyte" | ||
| ] | ||
|
|
||
| assert len(set(flowcell_ids)) == 1, "Expected one flowcell ID." | ||
| flowcell_id = flowcell_ids[0] | ||
|
|
||
| if "-" in flowcell_id: | ||
| logging.warning( | ||
| f"Container name {flowcell_id} contains a dash, did you forget to set the name of the LIMS container to the flowcell ID?" | ||
| ) | ||
|
|
||
| return flowcell_id | ||
|
|
||
|
|
||
| @epp_decorator(script_path=__file__, timestamp=TIMESTAMP) | ||
| def main(args: Namespace): | ||
| """takes a uploaded manifest from the LIMS process, zips the file and puts the re-names zip file on ngi-nas-ns""" | ||
| lims = Lims(BASEURI, USERNAME, PASSWORD) | ||
| process = Process(lims, id=args.pid) | ||
|
|
||
| # identify the correct file slot to take the manually uploaded manifest from | ||
| matching_file_slots = [ | ||
| art | ||
| for art in process.all_outputs() | ||
| if art.name == args.file and art.type == "ResultFile" | ||
| ] | ||
| assert ( | ||
| len(matching_file_slots) == 1 | ||
| ), f"Could not find single file slot matching to '{args.file}'." | ||
|
|
||
| matching_file_slot = matching_file_slots[0] | ||
| assert matching_file_slot.files, f"'{matching_file_slot.name}' file slot is empty." | ||
|
|
||
| # identify original file name and content | ||
| manifest_file_name = matching_file_slot.files[0].original_location | ||
| file_id = matching_file_slot.files[0].id | ||
| manifest_file_contents = lims.get_file_contents(id=file_id) | ||
| manifest_file_contents_list = manifest_file_contents.split("\n") | ||
|
|
||
| # add information of old filename to manifest | ||
| for keyname_index, line in enumerate(manifest_file_contents_list): | ||
| if "KeyName" in line and "Value" in line: | ||
| break | ||
| manifest_file_contents_list.insert( | ||
| keyname_index + 1, f"copy from original file,{manifest_file_name}" | ||
| ) | ||
| manifest_file_contents_altered = "\n".join(manifest_file_contents_list) | ||
|
|
||
| flowcell_id = get_flowcell_id(process) | ||
|
|
||
| # check whether we are in an AVITI LIMS step | ||
| if "AVITI" in process.type.name.upper(): | ||
| samplesheet_type = "Aviti" | ||
| # samplesheet_type needs to be spelled exactly like the sequencer directories in ngi-nas-ns | ||
| else: | ||
| # TODO: implement for other sequencers | ||
| raise AssertionError( | ||
| f"LIMS step is not part of the Aviti protocol. This is not implemented for other protocols (yet)" | ||
aanil marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ) | ||
|
|
||
| # generate new file names | ||
| new_root_manifest_file_name = f"AVITI_run_manifest_{flowcell_id}_{process.id}_{TIMESTAMP}_{process.technician.name.replace(' ', '')}" | ||
| new_manifest_csv_file_name = f"{new_root_manifest_file_name}.csv" | ||
| new_manifest_zip_file_name = f"{new_root_manifest_file_name}.zip" | ||
|
|
||
| # Write and zip manifest(s) | ||
| with ZipFile(new_manifest_zip_file_name, "w") as zip_stream: | ||
| open(new_manifest_csv_file_name, "w").write(manifest_file_contents_altered) | ||
| zip_stream.write(new_manifest_csv_file_name) | ||
| os.remove(new_manifest_csv_file_name) | ||
aanil marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| logging.info( | ||
| f".csv file '{manifest_file_name}' has been saved as .zip file '{new_manifest_zip_file_name}'" | ||
aanil marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ) | ||
|
|
||
| # Move manifest(s) | ||
| logging.info("Moving run manifest to ngi-nas-ns...") | ||
| try: | ||
| dst = f"/srv/ngi-nas-ns/samplesheets/{samplesheet_type}/{dt.now().year}" | ||
| if not os.path.exists(dst): | ||
| logging.info(f"Happy new year! Creating {dst}") | ||
| os.mkdir(dst) | ||
| shutil.copyfile( | ||
| new_manifest_zip_file_name, | ||
| f"{dst}/{new_manifest_zip_file_name}", | ||
| ) | ||
| os.remove(new_manifest_zip_file_name) | ||
| except: | ||
| logging.error("Failed to move run manifest to ngi-nas-ns.", exc_info=True) | ||
| else: | ||
| logging.info("Run manifest moved to ngi-nas-ns.") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| # Parse args | ||
| parser = ArgumentParser() | ||
| parser.add_argument( | ||
| "--pid", | ||
| required=True, | ||
| type=str, | ||
| help="Lims ID for current Process.", | ||
| ) | ||
| parser.add_argument( | ||
| "--log", | ||
| required=True, | ||
| type=str, | ||
| help="Which file slot to use for the script log.", | ||
| ) | ||
| parser.add_argument( | ||
| "--file", | ||
| required=True, | ||
| type=str, | ||
| help="Which file slot to take the run manifest from.", | ||
| ) | ||
| args = parser.parse_args() | ||
|
|
||
| main(args) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.