2
2
import socket
3
3
import logging
4
4
import time
5
+ import xmltodict
5
6
from runfolder import __version__ as version
6
7
7
8
from arteria .web .state import State
@@ -12,7 +13,7 @@ class RunfolderInfo:
12
13
Information about a runfolder. Status must be defined in RunfolderState:
13
14
"""
14
15
15
- def __init__ (self , host , path , state ):
16
+ def __init__ (self , host , path , state , metadata ):
16
17
"""
17
18
Initializes the object
18
19
@@ -25,6 +26,7 @@ def __init__(self, host, path, state):
25
26
self .path = path
26
27
self .state = state
27
28
self .service_version = version
29
+ self .metadata = metadata
28
30
29
31
def __repr__ (self ):
30
32
return "{0}: {1}@{2}" .format (self .state , self .path , self .host )
@@ -100,6 +102,27 @@ def create_runfolder(self, path):
100
102
os .makedirs (path )
101
103
self ._logger .info (
102
104
"Created a runfolder at {0} - intended for tests only" .format (path ))
105
+ runparameters_path = os .path .join (path , "runParameters.xml" )
106
+ if os .path .isfile (runparameters_path ):
107
+ raise CannotOverrideFile ("runParameters.xml already exists at {0}" .format (runparameters_path ))
108
+
109
+ runparameters_dict = {
110
+ 'RunParameters' : {
111
+ 'ReagentKitBarcode' : 'AB1234567-123V1' ,
112
+ 'RfidsInfo' : {
113
+ 'LibraryTubeSerialBarcode' : 'NV0012345-LIB'
114
+ }
115
+ }
116
+ }
117
+
118
+ output_xml = xmltodict .unparse (runparameters_dict , pretty = True )
119
+
120
+ with open (runparameters_path , 'a' ) as f :
121
+ f .write (output_xml )
122
+
123
+ self ._logger .info (
124
+ "Added 'runParameters.xml' to '{0}' - intended for tests only" .format (runparameters_path ))
125
+
103
126
104
127
def add_sequencing_finished_marker (self , path ):
105
128
"""
@@ -137,7 +160,8 @@ def get_runfolder_by_path(self, path):
137
160
138
161
if not self ._dir_exists (path ):
139
162
raise DirectoryDoesNotExist ("Directory does not exist: '{0}'" .format (path ))
140
- info = RunfolderInfo (self ._host (), path , self .get_runfolder_state (path ))
163
+ info = RunfolderInfo (self ._host (), path , self .get_runfolder_state (path ),
164
+ self .get_metadata (path ))
141
165
return info
142
166
143
167
def _get_runfolder_state_from_state_file (self , runfolder ):
@@ -249,14 +273,63 @@ def _enumerate_runfolders(self):
249
273
directory = os .path .join (monitored_root , subdir )
250
274
self ._logger .debug ("Found potential runfolder {0}" .format (directory ))
251
275
state = self .get_runfolder_state (directory )
252
- info = RunfolderInfo (self ._host (), directory , state )
276
+ info = RunfolderInfo (self ._host (), directory , state ,
277
+ self .get_metadata (directory ))
253
278
yield info
254
279
255
280
def _requires_enabled (self , config_key ):
256
281
"""Raises an ActionNotEnabled exception if the specified config value is false"""
257
282
if not self ._configuration_svc [config_key ]:
258
283
raise ActionNotEnabled ("The action {0} is not enabled" .format (config_key ))
259
284
285
+ def get_metadata (self , path ):
286
+ run_parameters = self .read_run_parameters (path )
287
+ reagent_kit_barcode = self .get_reagent_kit_barcode (path , run_parameters )
288
+ library_tube_barcode = self .get_library_tube_barcode (path , run_parameters )
289
+ metadata = {}
290
+ if reagent_kit_barcode :
291
+ metadata ['reagent_kit_barcode' ] = reagent_kit_barcode
292
+ if library_tube_barcode :
293
+ metadata ['library_tube_barcode' ] = library_tube_barcode
294
+ return metadata
295
+
296
+ def get_reagent_kit_barcode (self , path , run_parameters ):
297
+ try :
298
+ barcode = run_parameters ['RunParameters' ]['ReagentKitBarcode' ]
299
+ except KeyError :
300
+ # Reagent kit barcode is not available for all run types,
301
+ # it is therefore expected to not be found in all cases
302
+ self ._logger .debug ("Reagent kit barcode not found" )
303
+ return None
304
+ except TypeError :
305
+ self ._logger .debug ("[Rr]unParameters.xml not found" )
306
+ return None
307
+ return barcode
308
+
309
+ def get_library_tube_barcode (self , path , run_parameters ):
310
+ try :
311
+ barcode = run_parameters ['RunParameters' ]['RfidsInfo' ]['LibraryTubeSerialBarcode' ]
312
+ except KeyError :
313
+ # Library tube barcode is not available for all run types,
314
+ # it is therefore expected to not be found in all cases
315
+ self ._logger .debug ("Library tube barcode not found" )
316
+ return None
317
+ except TypeError :
318
+ self ._logger .debug ("[Rr]unParameters.xml not found" )
319
+ return None
320
+ return barcode
321
+
322
+ def read_run_parameters (self , path ):
323
+ alt_1 = os .path .join (path , "runParameters.xml" )
324
+ alt_2 = os .path .join (path , "RunParameters.xml" )
325
+ if os .path .exists (alt_1 ):
326
+ with open (alt_1 ) as f :
327
+ return xmltodict .parse (f .read ())
328
+ elif os .path .exists (alt_2 ):
329
+ with open (alt_2 ) as f :
330
+ return xmltodict .parse (f .read ())
331
+ else :
332
+ return None
260
333
261
334
class CannotOverrideFile (Exception ):
262
335
pass
@@ -284,4 +357,3 @@ class InvalidRunfolderState(Exception):
284
357
285
358
class ConfigurationError (Exception ):
286
359
pass
287
-
0 commit comments