Skip to content

Commit

Permalink
Merge branch 'JcGKitten-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
whitews committed Jan 27, 2022
2 parents e5860db + f92c0a0 commit f48e0ea
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
25 changes: 18 additions & 7 deletions flowio/flowdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ class FlowData(object):
:param filename_or_handle: a path string or a file handle for an FCS file
:param ignore_offset_error: option to ignore data offset error (see above note), default is False
:param only_text: option to only read the "text" segment of the FCS file without loading event data,
default is False
"""
def __init__(self, filename_or_handle, ignore_offset_error=False):
def __init__(self, filename_or_handle, ignore_offset_error=False, only_text=False):
if isinstance(filename_or_handle, basestring):
self._fh = open(str(filename_or_handle), 'rb')
else:
Expand Down Expand Up @@ -86,12 +88,15 @@ def __init__(self, filename_or_handle, ignore_offset_error=False):
if d_stop > self.file_size:
raise EOFError("FCS header indicates data section greater than file size")

self.events = self.__parse_data(
self.cur_offset,
d_start,
d_stop,
self.text
)
if only_text:
self.events = None
else:
self.events = self.__parse_data(
self.cur_offset,
d_start,
d_stop,
self.text
)

self.channels = self._parse_channels()

Expand Down Expand Up @@ -377,6 +382,12 @@ def write_fcs(self, filename, extra=None, extra_non_standard=None):
:param extra_non_standard: an optional dictionary for adding extra non-standard keywords/values
:return: None
"""
if self.events is None:
raise AttributeError(
"FlowData instance does not contain event data. This might"
"occur if the FCS file was read with the only_text=True option."
)

pnn_labels = [''] * len(self.channels)
pns_labels = [''] * len(self.channels)

Expand Down
6 changes: 6 additions & 0 deletions flowio/tests/flowdata_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ def test_event_count(self):
def test_get_text(self):
self.assertEqual(self.flow_data.text['cyt'], 'FACScan')

def test_load_only_text(self):
flow_data = FlowData('examples/fcs_files/3FITC_4PE_004.fcs', only_text=True)

self.assertIsNone(flow_data.events)
self.assertRaises(AttributeError, flow_data.write_fcs, 'delete_this_file.fcs')

@staticmethod
def test_load_fcs_from_memory():
with open('examples/fcs_files/3FITC_4PE_004.fcs', 'rb') as f:
Expand Down

0 comments on commit f48e0ea

Please sign in to comment.