Mercurial > repos > rv43 > test_tomo_reconstruct
comparison workflow/link_to_galaxy.py @ 0:98e23dff1de2 draft default tip
planemo upload for repository https://github.com/rolfverberg/galaxytools commit f8c4bdb31c20c468045ad5e6eb255a293244bc6c-dirty
author | rv43 |
---|---|
date | Tue, 21 Mar 2023 16:22:42 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:98e23dff1de2 |
---|---|
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 # RV yaml input not incorporated yet, since Galaxy can't use pyspec right now | |
41 # if extension == '.yml' or extension == '.yaml': | |
42 # with open(filename, 'r') as f: | |
43 # data = safe_load(f) | |
44 # elif extension == '.nxs': | |
45 if extension == '.nxs': | |
46 with NXFile(filename, mode='r') as nxfile: | |
47 data = nxfile.readfile() | |
48 else: | |
49 raise ValueError(f'Invalid filename extension ({extension})') | |
50 if isinstance(data, dict): | |
51 # Create Nexus format object from input dictionary | |
52 wf = TomoWorkflow(**data) | |
53 if len(wf.sample_maps) > 1: | |
54 raise ValueError(f'Multiple sample maps not yet implemented') | |
55 nxroot = NXroot() | |
56 for sample_map in wf.sample_maps: | |
57 import_scanparser(sample_map.station) | |
58 # RV raw data must be included, since Galaxy can't use pyspec right now | |
59 # sample_map.construct_nxentry(nxroot, include_raw_data=False) | |
60 sample_map.construct_nxentry(nxroot, include_raw_data=True) | |
61 nxentry = nxroot[nxroot.attrs['default']] | |
62 elif isinstance(data, NXroot): | |
63 nxentry = data[data.attrs['default']] | |
64 else: | |
65 raise ValueError(f'Invalid input file data ({data})') | |
66 | |
67 # Get a Galaxy instance | |
68 if user is not None and password is not None : | |
69 gi = GalaxyInstance(url=galaxy, email=user, password=password) | |
70 elif api_key is not None: | |
71 gi = GalaxyInstance(url=galaxy, key=api_key) | |
72 else: | |
73 exit('Please specify either a valid Galaxy username/password or an API key.') | |
74 | |
75 cycle = nxentry.instrument.source.attrs['cycle'] | |
76 btr = nxentry.instrument.source.attrs['btr'] | |
77 sample = nxentry.sample.name | |
78 | |
79 # Create a Galaxy work library/folder | |
80 # Combine the cycle, BTR and sample name as the base library name | |
81 lib_path = [p.strip() for p in f'{cycle}/{btr}/{sample}'.split('/')] | |
82 (library_id, folder_id, folder_names) = get_folder_id(gi, lib_path) | |
83 if not library_id: | |
84 library = gi.libraries.create_library(lib_path[0], description=None, synopsis=None) | |
85 library_id = library['id'] | |
86 # if user: | |
87 # gi.libraries.set_library_permissions(library_id, access_ids=user, | |
88 # manage_ids=user, modify_ids=user) | |
89 logger.info(f'Created Library:\n{library}') | |
90 if len(folder_names): | |
91 folder = gi.libraries.create_folder(library_id, folder_names[0], description=None, | |
92 base_folder_id=folder_id)[0] | |
93 folder_id = folder['id'] | |
94 logger.info(f'Created Folder:\n{folder}') | |
95 folder_names.pop(0) | |
96 while len(folder_names): | |
97 folder = gi.folders.create_folder(folder['id'], folder_names[0], | |
98 description=None) | |
99 folder_id = folder['id'] | |
100 logger.info(f'Created Folder:\n{folder}') | |
101 folder_names.pop(0) | |
102 | |
103 # Create a sym link for the Nexus file | |
104 dataset = gi.libraries.upload_from_galaxy_filesystem(library_id, path.abspath(filename), | |
105 folder_id=folder_id, file_type='auto', dbkey='?', link_data_only='link_to_files', | |
106 roles='', preserve_dirs=False, tag_using_filenames=False, tags=None)[0] | |
107 | |
108 # Make a history for the data | |
109 history_name = f'tomo {btr} {sample}' | |
110 history = gi.histories.create_history(name=history_name) | |
111 logger.info(f'Created history:\n{history}') | |
112 history_id = history['id'] | |
113 gi.histories.copy_dataset(history_id, dataset['id'], source='library') | |
114 | |
115 # TODO add option to either | |
116 # get a URL to share the history | |
117 # or to share with specific users | |
118 # This might require using: | |
119 # https://bioblend.readthedocs.io/en/latest/api_docs/galaxy/docs.html#using-bioblend-for-raw-api-calls | |
120 |