66from collections import OrderedDict , defaultdict
77
88from flowcell_parser .classes import RunParametersParser , SampleSheetParser
9+ from ibmcloudant .cloudant_v1 import BulkDocs
910
1011from taca .element .Aviti_Runs import Aviti_Run
1112from taca .nanopore .ONT_run_classes import ONT_RUN_PATTERN , ONT_run
@@ -84,8 +85,6 @@ def update_statusdb(run_dir, inst_brand):
8485 statusdb_conf = CONFIG .get ("statusdb" )
8586 couch_connection = statusdb .StatusdbSession (statusdb_conf ).connection
8687 valueskey = datetime .datetime .now ().isoformat ()
87- db = couch_connection ["bioinfo_analysis" ]
88- view = db .view ("latest_data/sample_id" )
8988
9089 if inst_brand == "illumina" :
9190 # Fetch individual fields
@@ -128,19 +127,19 @@ def update_statusdb(run_dir, inst_brand):
128127 # If entry exists, append to existing
129128 # Special if case to handle lanes written as int, can be safely removed when old lanes
130129 # is no longer stored as int
131- try :
132- if (
133- len ( view [[ project , run_id , int ( lane ), sample ]]. rows )
134- >= 1
135- ):
136- lane = int ( lane )
137- except ValueError :
138- pass
139- if len ( view [[ project , run_id , lane , sample ]]. rows ) >= 1 :
140- remote_id = view [[ project , run_id , lane , sample ]]. rows [ 0 ]. id
141- lane = str ( lane )
142- remote_doc = db [ remote_id ] ["values" ]
143- remote_status = db [ remote_id ] ["status" ]
130+ num_rows = couch_connection . post_view (
131+ db = "bioinfo_analysis" ,
132+ ddoc = "latest_data" ,
133+ view = "sample_id" ,
134+ key = [ project , run_id , lane , sample ],
135+ ). get_result ()[ "rows" ]
136+ if len ( num_rows ) >= 1 :
137+ remote_id = num_rows [ 0 ][ "id" ]
138+ remote_doc = couch_connection . get_document (
139+ db = "bioinfo_analysis" , doc_id = remote_id
140+ ). get_result ( )
141+ remote_doc_values = remote_doc ["values" ]
142+ remote_status = remote_doc ["status" ]
144143 # Only updates the listed statuses
145144 if (
146145 remote_status
@@ -154,7 +153,7 @@ def update_statusdb(run_dir, inst_brand):
154153 and sample_status != remote_status
155154 ):
156155 # Appends old entry to new. Essentially merges the two
157- for k , v in remote_doc .items ():
156+ for k , v in remote_doc_values .items ():
158157 obj ["values" ][k ] = v
159158 logger .info (
160159 f"Updating { run_id } { project } { flowcell } { lane } { sample } as { sample_status } "
@@ -168,16 +167,22 @@ def update_statusdb(run_dir, inst_brand):
168167 )
169168 )
170169 # Update record cluster
171- obj ["_rev" ] = db [ remote_id ]. rev
170+ obj ["_rev" ] = remote_doc [ "_rev" ]
172171 obj ["_id" ] = remote_id
173- db .save (obj )
172+ couch_connection .put_document (
173+ db = "bioinfo_analysis" ,
174+ doc_id = obj ["_id" ],
175+ document = obj ,
176+ )
174177 # Creates new entry
175178 else :
176179 logger .info (
177180 f"Creating { run_id } { project } { flowcell } { lane } { sample } as { sample_status } "
178181 )
179182 # Creates record
180- db .save (obj )
183+ couch_connection .post_document (
184+ db = "bioinfo_analysis" , document = obj
185+ )
181186 # Sets FC error flag
182187 if project_info [flowcell ].value is not None :
183188 if (
@@ -258,15 +263,18 @@ def get_ss_projects_ont(ont_run, couch_connection):
258263 """Fetches project, FC, lane & sample (sample-run) status for a given folder for ONT runs"""
259264 proj_tree = Tree ()
260265 flowcell_id = ont_run .run_name
261- flowcell_info = (
262- couch_connection ["nanopore_runs" ].view ("info/lims" )[flowcell_id ].rows [0 ]
263- )
266+ flowcell_info = couch_connection .post_view (
267+ db = "nanopore_runs" ,
268+ ddoc = "info" ,
269+ view = "lims" ,
270+ key = flowcell_id ,
271+ ).get_result ()["rows" ][0 ]
264272 if (
265- flowcell_info . value
266- and flowcell_info . value .get ("loading" , [])
267- and "sample_data" in flowcell_info . value ["loading" ][- 1 ]
273+ flowcell_info [ " value" ]
274+ and flowcell_info [ " value" ] .get ("loading" , [])
275+ and "sample_data" in flowcell_info [ " value" ] ["loading" ][- 1 ]
268276 ):
269- samples = flowcell_info . value ["loading" ][- 1 ]["sample_data" ]
277+ samples = flowcell_info [ " value" ] ["loading" ][- 1 ]["sample_data" ]
270278 for sample_dict in samples :
271279 sample_id = sample_dict ["sample_name" ]
272280 project = sample_id .split ("_" )[0 ]
@@ -488,39 +496,45 @@ def fail_run(runid, project):
488496 )
489497 logger .error (e )
490498 raise e
491- bioinfo_db = status_db ["bioinfo_analysis" ]
492- if project is not None :
493- view = bioinfo_db .view ("full_doc/pj_run_to_doc" )
494- rows = view [[project , runid ]].rows
495- logger .info (
496- f"Updating status of { len (rows )} objects with flowcell_id: { runid } and project_id { project } "
497- )
499+
500+ if project :
501+ view = "pj_run_to_doc"
502+ key = [project , runid ]
498503 else :
499- view = bioinfo_db .view ("full_doc/run_id_to_doc" )
500- rows = view [[runid ]].rows
501- logger .info (f"Updating status of { len (rows )} objects with flowcell_id: { runid } " )
504+ view = "run_id_to_doc"
505+ key = [runid ]
506+
507+ rows = status_db .post_view (
508+ db = "bioinfo_analysis" ,
509+ ddoc = "full_doc" ,
510+ view = view ,
511+ key = key ,
512+ ).get_result ()["rows" ]
513+ logger .info (
514+ f"Found { len (rows )} objects with flowcell_id: { runid } "
515+ + (f" and project_id { project } " if project else "" )
516+ )
502517
503518 new_timestamp = datetime .datetime .now ().isoformat ()
504519 updated = 0
520+ to_save = []
505521 for row in rows :
506- if row . value ["status" ] != "Failed" :
507- row . value ["values" ][new_timestamp ] = {
522+ if row [ " value" ] ["status" ] != "Failed" :
523+ row [ " value" ] ["values" ][new_timestamp ] = {
508524 "sample_status" : "Failed" ,
509525 "user" : "taca" ,
510526 }
511- row .value ["status" ] = "Failed"
512- try :
513- bioinfo_db .save (row .value )
527+ row ["value" ]["status" ] = "Failed"
528+
529+ to_save .append (row ["value" ])
530+ save_result = status_db .post_bulk_docs (
531+ db = "bioinfo_analysis" , bulk_docs = BulkDocs (docs = to_save , new_edits = True )
532+ ).get_result ()
533+ for res in save_result :
534+ if "ok" in res :
514535 updated += 1
515- except Exception as e :
536+ else :
516537 logger .error (
517- "Cannot update object project-sample-run-lane: {}-{}-{}-{}" .format (
518- row .value .get ("project_id" ),
519- row .value .get ("sample" ),
520- row .value .get ("run_id" ),
521- row .value .get ("lane" ),
522- )
538+ f"Failed to save object { res .get ('id' )} : { res .get ('error' )} { res .get ('reason' )} "
523539 )
524- logger .error (e )
525- raise e
526540 logger .info (f"Successfully updated { updated } objects" )
0 commit comments