Skip to content

Commit

Permalink
support renaming channels in Sample class (closes #198)
Browse files Browse the repository at this point in the history
  • Loading branch information
whitews committed Aug 28, 2024
1 parent b9ee2b0 commit 60c4ec2
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/flowkit/_models/sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,33 @@ def get_channel_events(self, channel_index, source='xform', subsample=False):

return events

def rename_channel(self, current_label, new_label, new_pns_label=None):
"""
Rename a channel label.
:param current_label: PnN label of a channel
:param new_label: new PnN label
:param new_pns_label: optional new PnS label
:return: None
"""
try:
chan_idx = self.pnn_labels.index(current_label)
except ValueError:
raise ValueError("Label %s was not found in self.pnn_labels")

self.pnn_labels[chan_idx] = new_label

# Use same index for PnS label
if new_pns_label is not None:
self.pns_labels[chan_idx] = new_pns_label

# Update self.channels
self.channels['pnn'] = self.pnn_labels
self.channels['pns'] = self.pns_labels

# Update self._flowjo_pnn_labels
self._flowjo_pnn_labels[chan_idx] = new_label.replace('/', '_')

def _transform(self, transform, include_scatter=False):
if isinstance(transform, _transforms.RatioTransform):
raise NotImplementedError(
Expand Down
24 changes: 24 additions & 0 deletions tests/sample_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,30 @@ def test_get_events_as_data_frame_new_column_names(self):

self.assertListEqual(list(df.columns), new_cols)

def test_rename_channel(self):
sample = copy.deepcopy(data1_sample)
chan_orig = 'FL1-H'
chan_new = 'CD4-H'
chan_pns_new = 'FITC'

sample.rename_channel(chan_orig, chan_new, new_pns_label=chan_pns_new)

chan_num = sample.get_channel_number_by_label(chan_new)
self.assertEqual(chan_num, 3)

chan_idx = sample.get_channel_index(chan_new)
self.assertEqual(chan_idx, 2)

pnn_labels = sample.pnn_labels
pns_labels = sample.pns_labels

self.assertEqual(chan_new, pnn_labels[chan_idx])
self.assertEqual(chan_pns_new, pns_labels[chan_idx])

df_channels = sample.channels
self.assertEqual(chan_new, df_channels.loc[chan_idx].pnn)
self.assertEqual(chan_pns_new, df_channels.loc[chan_idx].pns)

@staticmethod
def test_fully_custom_transform():
sample1 = Sample(fcs_path_or_data=data1_fcs_path)
Expand Down

0 comments on commit 60c4ec2

Please sign in to comment.