@@ -134,6 +134,7 @@ def self.run_differential_expression_job(cluster_group, study, user, annotation_
134134 metadata_url = study_file . is_viz_anndata? ?
135135 RequestUtils . data_fragment_url ( study_file , 'metadata' ) :
136136 study . metadata_file . gs_url
137+ cluster_group_id = cluster_group . id
137138 # begin assembling parameters
138139 de_params = {
139140 annotation_name :,
@@ -143,10 +144,12 @@ def self.run_differential_expression_job(cluster_group, study, user, annotation_
143144 group2 :,
144145 annotation_file : annotation_scope == 'cluster' ? cluster_url : metadata_url ,
145146 cluster_file : cluster_url ,
146- cluster_name : cluster_group . name
147+ cluster_name : set_cluster_name ( study , cluster_group , annotation_name , annotation_scope ) ,
148+ cluster_group_id :
147149 }
148150 raw_matrix = ClusterVizService . raw_matrix_for_cluster_cells ( study , cluster_group )
149151 de_params [ :matrix_file_path ] = raw_matrix . gs_url
152+ de_params [ :matrix_file_id ] = raw_matrix . id
150153 if raw_matrix . file_type == 'MM Coordinate Matrix'
151154 de_params [ :matrix_file_type ] = 'mtx'
152155 # we know bundle exists and is completed as :raw_matrix_for_cluster_cells will throw an exception if it isn't
@@ -287,6 +290,21 @@ def self.annotation_eligible?(name)
287290 ALLOWED_ANNOTS =~ name && EXCLUDED_ANNOTS !~ name
288291 end
289292
293+ # determine if the requested cluster/annotation has an existing result object
294+ # used for setting cluster name or in pairwise DE when merging in new results
295+ #
296+ # * *params*
297+ # - +study+ (Study) => Study in which results exist
298+ # - +cluster_group+ (ClusterGroup) => Clustering object to source name/file from
299+ # - +annotation_name+ (String) => Name of requested annotation
300+ # - +annotation_scope+ (String) => Scope of requested annotation ('study' or 'cluster')
301+ #
302+ # * *returns*
303+ # - (DifferentialExpressionResult, nil)
304+ def self . find_existing_result ( study , cluster_group , annotation_name , annotation_scope )
305+ DifferentialExpressionResult . find_by ( study :, cluster_group :, annotation_name :, annotation_scope :)
306+ end
307+
290308 # determine if a study already has DE results for an annotation, taking scope into account
291309 # cluster-based annotations must match to the specified cluster in the annotation object
292310 # for study-wide annotations, return true if any results exist, regardless of cluster as this indicates that DE
@@ -309,6 +327,21 @@ def self.results_exist?(study, annotation)
309327 ) . exists?
310328 end
311329
330+ # helper to set cluster_name when existing results are present
331+ # this prevents result file URLs breaking because the cluster has been renamed at some point
332+ #
333+ # * *params*
334+ # - +study+ (Study) => Study in which results exist
335+ # - +cluster_group+ (ClusterGroup) => Clustering object to source name/file from
336+ # - +annotation_name+ (String) => Name of requested annotation
337+ # - +annotation_scope+ (String) => Scope of requested annotation ('study' or 'cluster')
338+ #
339+ # * *returns*
340+ # - (String)
341+ def self . set_cluster_name ( study , cluster_group , annotation_name , annotation_scope )
342+ find_existing_result ( study , cluster_group , annotation_name , annotation_scope ) &.cluster_name || cluster_group . name
343+ end
344+
312345 # determine if a study meets the requirements for differential expression:
313346 # 1. public
314347 # 2. has clustering/metadata
@@ -383,12 +416,15 @@ def self.validate_annotation(cluster_group, study, annotation_name, annotation_s
383416 elsif pairwise
384417 missing = [ group1 , group2 ] - annotation [ :values ]
385418 raise ArgumentError , "#{ annotation_name } does not contain '#{ missing . join ( ', ' ) } '" if missing . any?
419+
386420 cell_count = {
387- " #{ group1 } " => cells_by_label [ group1 ] . count ,
388- " #{ group2 } " => cells_by_label [ group2 ] . count
421+ group1 . to_s => cells_by_label [ group1 ] & .count . to_i ,
422+ group2 . to_s => cells_by_label [ group2 ] & .count . to_i
389423 } . keep_if { |_ , c | c < 2 }
390- raise ArgumentError ,
391- "#{ cell_count . keys . join ( ', ' ) } does not have enough cells represented in #{ identifier } " if cell_count . any?
424+ if cell_count . any?
425+ raise ArgumentError , "#{ cell_count . keys . join ( ', ' ) } does not have enough cells represented in #{ identifier } " \
426+ "for #{ cluster_group . name } "
427+ end
392428 end
393429 end
394430
0 commit comments