Mercurial > repos > rv43 > tomo
comparison workflow/link_to_galaxy.py @ 69:fba792d5f83b draft
planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
author | rv43 |
---|---|
date | Fri, 10 Mar 2023 16:02:04 +0000 |
parents | |
children | 1cf15b61cd83 |
comparison
equal
deleted
inserted
replaced
68:ba5866d0251d | 69:fba792d5f83b |
---|---|
1 #!/usr/bin/env python3 | |
2 | |
3 import logging | |
4 logger = logging.getLogger(__name__) | |
5 | |
6 from bioblend.galaxy import GalaxyInstance | |
7 from nexusformat.nexus import * | |
8 from os import path | |
9 from yaml import safe_load | |
10 | |
11 from .models import import_scanparser, TomoWorkflow | |
12 | |
13 def get_folder_id(gi, path): | |
14 library_id = None | |
15 folder_id = None | |
16 folder_names = path[1:] if len(path) > 1 else [] | |
17 new_folders = folder_names | |
18 libs = gi.libraries.get_libraries(name=path[0]) | |
19 if libs: | |
20 for lib in libs: | |
21 library_id = lib['id'] | |
22 folders = gi.libraries.get_folders(library_id, folder_id=None, name=None) | |
23 for i, folder in enumerate(folders): | |
24 fid = folder['id'] | |
25 details = gi.libraries.show_folder(library_id, fid) | |
26 library_path = details['library_path'] | |
27 if library_path == folder_names: | |
28 return (library_id, fid, []) | |
29 elif len(library_path) < len(folder_names): | |
30 if library_path == folder_names[:len(library_path)]: | |
31 nf = folder_names[len(library_path):] | |
32 if len(nf) < len(new_folders): | |
33 folder_id = fid | |
34 new_folders = nf | |
35 return (library_id, folder_id, new_folders) | |
36 | |
37 def link_to_galaxy(filename:str, galaxy=None, user=None, password=None, api_key=None) -> None: | |
38 # Read input file | |
39 extension = path.splitext(filename)[1] | |
40 if extension == '.yml' or extension == '.yaml': | |
41 with open(filename, 'r') as f: | |
42 data = safe_load(f) | |
43 elif extension == '.nxs': | |
44 with NXFile(filename, mode='r') as nxfile: | |
45 data = nxfile.readfile() | |
46 else: | |
47 raise ValueError(f'Invalid filename extension ({extension})') | |
48 if isinstance(data, dict): | |
49 # Create Nexus format object from input dictionary | |
50 wf = TomoWorkflow(**data) | |
51 if len(wf.sample_maps) > 1: | |
52 raise ValueError(f'Multiple sample maps not yet implemented') | |
53 nxroot = NXroot() | |
54 for sample_map in wf.sample_maps: | |
55 import_scanparser(sample_map.station) | |
56 sample_map.construct_nxentry(nxroot, include_raw_data=False) | |
57 nxentry = nxroot[nxroot.attrs['default']] | |
58 elif isinstance(data, NXroot): | |
59 nxentry = data[data.attrs['default']] | |
60 else: | |
61 raise ValueError(f'Invalid input file data ({data})') | |
62 | |
63 # Get a Galaxy instance | |
64 if user is not None and password is not None : | |
65 gi = GalaxyInstance(url=galaxy, email=user, password=password) | |
66 elif api_key is not None: | |
67 gi = GalaxyInstance(url=galaxy, key=api_key) | |
68 else: | |
69 exit('Please specify either a valid Galaxy username/password or an API key.') | |
70 | |
71 cycle = nxentry.instrument.source.attrs['cycle'] | |
72 btr = nxentry.instrument.source.attrs['btr'] | |
73 sample = nxentry.sample.name | |
74 | |
75 # Create a Galaxy work library/folder | |
76 # Combine the cycle, BTR and sample name as the base library name | |
77 lib_path = [p.strip() for p in f'{cycle}/{btr}/{sample}'.split('/')] | |
78 (library_id, folder_id, folder_names) = get_folder_id(gi, lib_path) | |
79 if not library_id: | |
80 library = gi.libraries.create_library(lib_path[0], description=None, synopsis=None) | |
81 library_id = library['id'] | |
82 # if user: | |
83 # gi.libraries.set_library_permissions(library_id, access_ids=user, | |
84 # manage_ids=user, modify_ids=user) | |
85 logger.info(f'Created Library:\n{library}') | |
86 if len(folder_names): | |
87 folder = gi.libraries.create_folder(library_id, folder_names[0], description=None, | |
88 base_folder_id=folder_id)[0] | |
89 folder_id = folder['id'] | |
90 logger.info(f'Created Folder:\n{folder}') | |
91 folder_names.pop(0) | |
92 while len(folder_names): | |
93 folder = gi.folders.create_folder(folder['id'], folder_names[0], | |
94 description=None) | |
95 folder_id = folder['id'] | |
96 logger.info(f'Created Folder:\n{folder}') | |
97 folder_names.pop(0) | |
98 | |
99 # Create a sym link for the Nexus file | |
100 dataset = gi.libraries.upload_from_galaxy_filesystem(library_id, path.abspath(filename), | |
101 folder_id=folder_id, file_type='auto', dbkey='?', link_data_only='link_to_files', | |
102 roles='', preserve_dirs=False, tag_using_filenames=False, tags=None)[0] | |
103 | |
104 # Make a history for the data | |
105 history_name = f'tomo {btr} {sample}' | |
106 history = gi.histories.create_history(name=history_name) | |
107 logger.info(f'Created history:\n{history}') | |
108 history_id = history['id'] | |
109 gi.histories.copy_dataset(history_id, dataset['id'], source='library') | |
110 | |
111 # TODO add option to either | |
112 # get a URL to share the history | |
113 # or to share with specific users | |
114 # This might require using: | |
115 # https://bioblend.readthedocs.io/en/latest/api_docs/galaxy/docs.html#using-bioblend-for-raw-api-calls | |
116 |