Skip to content

Commit 56febc1

Browse files
committed
update load_samples utils function to also accept filename_as_id option (see #236)
1 parent b1ffe0e commit 56febc1

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

src/flowkit/_utils/sample_utils.py

+13-7
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,23 @@
77
from .._models.sample import Sample
88

99

10-
def _get_samples_from_paths(sample_paths):
10+
def _get_samples_from_paths(sample_paths, filename_as_id=False):
1111
"""
1212
Load multiple Sample instances from a list of file paths
1313
1414
:param sample_paths: list of file paths containing FCS files
15+
:param filename_as_id: Boolean option for using the file name (as it exists on the
16+
filesystem) for the Sample's ID, default is False.
1517
:return: list of Sample instances
1618
"""
1719
samples = []
1820
for path in sample_paths:
19-
samples.append(Sample(path))
21+
samples.append(Sample(path, filename_as_id=filename_as_id))
2022

2123
return samples
2224

2325

24-
def load_samples(fcs_samples):
26+
def load_samples(fcs_samples, filename_as_id=False):
2527
"""
2628
Returns a list of Sample instances from a variety of input types (fcs_samples), such as file or
2729
directory paths, a Sample instance, or lists of the previous types.
@@ -30,6 +32,9 @@ def load_samples(fcs_samples):
3032
a directory or file path, or a list of directory or file paths. If a directory, any .fcs
3133
files in the directory will be loaded. If a list, then it must be a list of file paths or a
3234
list of Sample instances. Lists of mixed types are not supported.
35+
:param filename_as_id: Boolean option for using the file name (as it exists on the
36+
filesystem) for the Sample's ID, default is False. Only applies to file paths given to the
37+
'fcs_samples' argument.
3338
:return: list of Sample instances
3439
"""
3540
sample_list = []
@@ -43,13 +48,14 @@ def load_samples(fcs_samples):
4348

4449
if len(sample_types) > 1:
4550
raise ValueError(
46-
"Each item in 'fcs_sample' list must be a FCS file path or Sample instance"
51+
"Found multiple object types for 'fcs_samples' option. "
52+
"Each item in 'fcs_samples' list must be of the same type (FCS file path or Sample instance)."
4753
)
4854

4955
if Sample in sample_types:
5056
sample_list = fcs_samples
5157
elif str in sample_types:
52-
sample_list = _get_samples_from_paths(fcs_samples)
58+
sample_list = _get_samples_from_paths(fcs_samples, filename_as_id=filename_as_id)
5359
elif isinstance(fcs_samples, Sample):
5460
# 'fcs_samples' is a single Sample instance
5561
sample_list = [fcs_samples]
@@ -59,10 +65,10 @@ def load_samples(fcs_samples):
5965
if os.path.isdir(fcs_samples):
6066
fcs_paths = glob(os.path.join(fcs_samples, '*.fcs'))
6167
if len(fcs_paths) > 0:
62-
sample_list = _get_samples_from_paths(fcs_paths)
68+
sample_list = _get_samples_from_paths(fcs_paths, filename_as_id=filename_as_id)
6369
else:
6470
# assume a path to a single FCS file
65-
sample_list = _get_samples_from_paths([fcs_samples])
71+
sample_list = _get_samples_from_paths([fcs_samples], filename_as_id=filename_as_id)
6672

6773
return sorted(sample_list)
6874

tests/sample_tests.py

+18-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import flowio
1010
import warnings
1111

12-
from flowkit import Sample, transforms, read_multi_dataset_fcs
12+
from flowkit import Sample, transforms, read_multi_dataset_fcs, load_samples
1313
from flowkit.exceptions import DataOffsetDiscrepancyError
1414

1515
data1_fcs_path = 'data/gate_ref/data1.fcs'
@@ -177,6 +177,23 @@ def test_set_id_from_file_name(self):
177177
self.assertEqual(sample_with_file_id.id, "test_comp_example.fcs")
178178
self.assertEqual(sample_with_meta_id.id, "PBMC_LRS005_IL10.fcs")
179179

180+
def test_load_samples_utils_func(self):
181+
# Tests for utils 'load_samples' function
182+
# Most of the functionality of this function is covered in the
183+
# test config file in loading data sets for other tests.
184+
# We'll just cover a few edge cases here.
185+
186+
# Note, this sample has no $FIL keyword so we'll also check the
187+
# Sample.id gets populated with the current file name.
188+
samples = load_samples([fcs_2d_file_path])
189+
190+
self.assertIsInstance(samples[0], Sample)
191+
self.assertEqual(samples[0].id, "test_data_2d_01.fcs")
192+
193+
# Test that attempting to load from a list of mixed types fails
194+
mixed_sample_list = [samples[0], fcs_2d_file_path]
195+
self.assertRaises(ValueError, load_samples, mixed_sample_list)
196+
180197
def test_data_start_offset_discrepancy(self):
181198
fcs_file = "data/noncompliant/data_start_offset_discrepancy_example.fcs"
182199
self.assertRaises(DataOffsetDiscrepancyError, Sample, fcs_file)

0 commit comments

Comments
 (0)