Skip to content

Commit 819effe

Browse files
authored
Merge pull request #2296 from broadinstitute/development
Release 1.101.2
2 parents 717934d + 075ab1f commit 819effe

File tree

6 files changed

+92
-0
lines changed

6 files changed

+92
-0
lines changed

app/models/cluster_group.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ def by_name_and_type(name, type, subsample_threshold=nil)
7474
'cell_annotations.type' => %w(group numeric)
7575
}
7676

77+
before_update :update_cluster_in_study_options
78+
7779
# method to return a single data array of values for a given data array name, annotation name, and annotation value
7880
# gathers all matching data arrays and orders by index, then concatenates into single array
7981
# can also load subsample arrays by supplying optional subsample_threshold
@@ -166,6 +168,21 @@ def can_visualize_cell_annotation?(annotation)
166168
end
167169
end
168170

171+
# whenever a cluster is updated, we need to update the study default_options ordering to reflect this
172+
def update_cluster_in_study_options
173+
return unless name_changed?
174+
175+
study = self.study
176+
list_name = spatial? ? :spatial_order : :cluster_order
177+
return unless study.present? && study.default_options[list_name].present?
178+
179+
old_name, new_name = name_change
180+
idx = study.default_options[list_name].index(old_name)
181+
idx ? study.default_options[list_name][idx] = new_name : study.default_options[list_name] << new_name
182+
study.save(validate: false) # skip validations to avoid circular dependency issues
183+
CacheRemovalJob.new(study.accession).delay.perform
184+
end
185+
169186
# method used during parsing to generate representative sub-sampled data_arrays for rendering
170187
#
171188
# annotation_name: name of annotation to subsample off of

app/models/delete_queue_job.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ def perform
3636
case file_type
3737
when 'Cluster'
3838
cluster_group = ClusterGroup.find_by(study:, study_file_id: object.id)
39+
study.update_cluster_order(cluster_group, action: :remove)
3940
delete_differential_expression_results(study:, study_file: object)
4041
delete_parsed_data(object.id, study.id, ClusterGroup, DataArray)
4142
delete_dot_plot_data(study.id, query: { cluster_group_id: cluster_group&.id })

app/models/ingest_job.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,7 @@ def set_study_default_options
543543
when 'Cluster'
544544
set_default_cluster
545545
set_default_annotation
546+
update_cluster_ordering
546547
when 'AnnData'
547548
set_default_cluster
548549
set_default_annotation
@@ -601,6 +602,12 @@ def set_default_cluster
601602
end
602603
end
603604

605+
# update the study.default_cluster_order after clustering finishes ingesting
606+
def update_cluster_ordering
607+
cluster = study.cluster_groups.by_name(cluster_name_by_file_type)
608+
study.update_cluster_order(cluster, action: :append)
609+
end
610+
604611
# set the point count on a cluster group after successful ingest
605612
#
606613
# * *yields*

app/models/study.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1324,6 +1324,24 @@ def default_spatial_order
13241324
default_options[:spatial_order] || spatial_cluster_groups.map(&:name)
13251325
end
13261326

1327+
# handle updates to the cluster order menus
1328+
def update_cluster_order(cluster, action:)
1329+
return nil unless cluster
1330+
1331+
list_name = cluster.spatial? ? :spatial_order : :cluster_order
1332+
case action.to_sym
1333+
when :append
1334+
new_list = send("default_#{list_name}").push(cluster.name).uniq
1335+
when :remove
1336+
new_list = send("default_#{list_name}").reject { |c| c == cluster.name }
1337+
else
1338+
return nil # invalid action
1339+
end
1340+
default_options[list_name] = new_list
1341+
save
1342+
CacheRemovalJob.new(accession).perform
1343+
end
1344+
13271345
###
13281346
#
13291347
# INSTANCE VALUE SETTERS & GETTERS

test/models/delete_queue_job_test.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,4 +287,24 @@ class DeleteQueueJobTest < ActiveSupport::TestCase
287287
study.reload
288288
assert_empty study.expression_matrix_cells(matrix, matrix_type: 'raw')
289289
end
290+
291+
test 'should update cluster order when deleting cluster file' do
292+
study = FactoryBot.create(:detached_study,
293+
name_prefix: 'Cluster Order Update Test',
294+
user: @user,
295+
test_array: @@studies_to_clean)
296+
cell_input = { x: [1, 2, 3], y: [4, 5, 6], cells: %w[A B C] }
297+
1.upto(3) do |i|
298+
FactoryBot.create(:cluster_file, name: "cluster_#{i}.txt", study:, cell_input:)
299+
end
300+
assert_equal 3, study.cluster_groups.count
301+
study.default_options[:cluster_order] = study.default_cluster_order
302+
study.save!
303+
304+
file = study.cluster_groups.first.study_file
305+
DeleteQueueJob.new(file).perform
306+
study.reload
307+
assert_equal %w[cluster_2.txt cluster_3.txt], study.default_cluster_order,
308+
'Did not update cluster order after deleting a cluster file'
309+
end
290310
end

test/models/study_test.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,4 +252,33 @@ class StudyTest < ActiveSupport::TestCase
252252
study.set_cell_count
253253
assert_equal 4, study.cell_count
254254
end
255+
256+
test 'should update cluster order' do
257+
study = FactoryBot.create(:detached_study,
258+
user: @user,
259+
name_prefix: 'Cluster Order Test',
260+
test_array: @@studies_to_clean)
261+
assert_empty study.default_cluster_order
262+
cell_input = {
263+
x: [1, 4, 6],
264+
y: [7, 5, 3],
265+
cells: %w(A B C)
266+
}
267+
FactoryBot.create(:cluster_file, name: 'cluster_example.txt', study:, cell_input:)
268+
cluster = study.cluster_groups.first
269+
study.update_cluster_order(cluster, action: :append)
270+
study.reload
271+
assert_equal [cluster.name], study.default_cluster_order
272+
FactoryBot.create(:cluster_file, name: 'cluster_2_example.txt', study:, cell_input:)
273+
new_cluster = study.cluster_groups.last
274+
study.update_cluster_order(new_cluster, action: :append)
275+
study.reload
276+
assert_equal [cluster.name, new_cluster.name], study.default_cluster_order
277+
278+
# test renaming cluster directly
279+
new_name = 'Renamed Cluster'
280+
cluster.update(name: new_name)
281+
study.reload
282+
assert_equal [new_name, new_cluster.name], study.default_cluster_order
283+
end
255284
end

0 commit comments

Comments
 (0)