@@ -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
@@ -158,6 +161,7 @@ def self.run_differential_expression_job(cluster_group, study, user, annotation_
158161 elsif raw_matrix . file_type == 'AnnData'
159162 de_params [ :matrix_file_type ] = 'h5ad'
160163 de_params [ :file_size ] = raw_matrix . upload_file_size
164+ de_params [ :raw_location ] = raw_matrix . ann_data_file_info . raw_location
161165 else
162166 de_params [ :matrix_file_type ] = 'dense'
163167 end
@@ -287,6 +291,21 @@ def self.annotation_eligible?(name)
287291 ALLOWED_ANNOTS =~ name && EXCLUDED_ANNOTS !~ name
288292 end
289293
294+ # determine if the requested cluster/annotation has an existing result object
295+ # used for setting cluster name or in pairwise DE when merging in new results
296+ #
297+ # * *params*
298+ # - +study+ (Study) => Study in which results exist
299+ # - +cluster_group+ (ClusterGroup) => Clustering object to source name/file from
300+ # - +annotation_name+ (String) => Name of requested annotation
301+ # - +annotation_scope+ (String) => Scope of requested annotation ('study' or 'cluster')
302+ #
303+ # * *returns*
304+ # - (DifferentialExpressionResult, nil)
305+ def self . find_existing_result ( study , cluster_group , annotation_name , annotation_scope )
306+ DifferentialExpressionResult . find_by ( study :, cluster_group :, annotation_name :, annotation_scope :)
307+ end
308+
290309 # determine if a study already has DE results for an annotation, taking scope into account
291310 # cluster-based annotations must match to the specified cluster in the annotation object
292311 # for study-wide annotations, return true if any results exist, regardless of cluster as this indicates that DE
@@ -309,6 +328,21 @@ def self.results_exist?(study, annotation)
309328 ) . exists?
310329 end
311330
331+ # helper to set cluster_name when existing results are present
332+ # this prevents result file URLs breaking because the cluster has been renamed at some point
333+ #
334+ # * *params*
335+ # - +study+ (Study) => Study in which results exist
336+ # - +cluster_group+ (ClusterGroup) => Clustering object to source name/file from
337+ # - +annotation_name+ (String) => Name of requested annotation
338+ # - +annotation_scope+ (String) => Scope of requested annotation ('study' or 'cluster')
339+ #
340+ # * *returns*
341+ # - (String)
342+ def self . set_cluster_name ( study , cluster_group , annotation_name , annotation_scope )
343+ find_existing_result ( study , cluster_group , annotation_name , annotation_scope ) &.cluster_name || cluster_group . name
344+ end
345+
312346 # determine if a study meets the requirements for differential expression:
313347 # 1. public
314348 # 2. has clustering/metadata
@@ -383,12 +417,15 @@ def self.validate_annotation(cluster_group, study, annotation_name, annotation_s
383417 elsif pairwise
384418 missing = [ group1 , group2 ] - annotation [ :values ]
385419 raise ArgumentError , "#{ annotation_name } does not contain '#{ missing . join ( ', ' ) } '" if missing . any?
420+
386421 cell_count = {
387- " #{ group1 } " => cells_by_label [ group1 ] . count ,
388- " #{ group2 } " => cells_by_label [ group2 ] . count
422+ group1 . to_s => cells_by_label [ group1 ] & .count . to_i ,
423+ group2 . to_s => cells_by_label [ group2 ] & .count . to_i
389424 } . 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?
425+ if cell_count . any?
426+ raise ArgumentError , "#{ cell_count . keys . join ( ', ' ) } does not have enough cells represented in #{ identifier } " \
427+ "for #{ cluster_group . name } "
428+ end
392429 end
393430 end
394431
0 commit comments