Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to omit field numbers #65

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/starfile/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def write(
na_rep: str = '<NA>',
quote_character: str = '"',
quote_all_strings: bool = False,
include_field_numbers: bool = True,
**kwargs
):
"""Write data to disk in the STAR format.
Expand All @@ -72,6 +73,11 @@ def write(
Separator between values, will be passed to pandas.
na_rep: str
Representation of null values, will be passed to pandas.
include_field_numbers: bool
Whether field numbers should be included after field names in the ouput file.
Default is True which includes field numbers (i.e. `_rlnImageName #1`) and is
compatible with RELION and Python STOPGAP. False excludes field numbers (i.e.
`_motl_idx`) and is compatible with legacy/MATLAB STOPGAP.
"""
StarWriter(
data,
Expand All @@ -81,6 +87,7 @@ def write(
separator=sep,
quote_character=quote_character,
quote_all_strings=quote_all_strings,
include_field_numbers=include_field_numbers,
).write()


Expand Down
10 changes: 7 additions & 3 deletions src/starfile/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def __init__(
na_rep: str = '<NA>',
quote_character: str = '"',
quote_all_strings: bool = False,
include_field_numbers: bool = True,
):
# coerce data
self.data_blocks = self.coerce_data_blocks(data_blocks)
Expand All @@ -40,6 +41,7 @@ def __init__(
self.na_rep = na_rep
self.quote_character = quote_character
self.quote_all_strings = quote_all_strings
self.include_field_numbers = include_field_numbers
self.buffer = TextBuffer()
self.backup_if_file_exists()

Expand Down Expand Up @@ -93,7 +95,8 @@ def data_block_generator(self) -> Generator[str, None, None]:
separator=self.sep,
na_rep=self.na_rep,
quote_character=self.quote_character,
quote_all_strings=self.quote_all_strings
quote_all_strings=self.quote_all_strings,
include_field_numbers=self.include_field_numbers,
):
yield line

Expand Down Expand Up @@ -163,15 +166,16 @@ def loop_block(
separator: str = '\t',
na_rep: str = '<NA>',
quote_character: str = '"',
quote_all_strings: bool = False
quote_all_strings: bool = False,
include_field_numbers: bool = True,
) -> Generator[str, None, None]:

# Header
yield f'data_{block_name}'
yield ''
yield 'loop_'
for idx, column_name in enumerate(df.columns, 1):
yield f'_{column_name} #{idx}'
yield f'_{column_name} #{idx}' if include_field_numbers else f'_{column_name}'

# Data
for line in df.map(lambda x:
Expand Down
18 changes: 18 additions & 0 deletions tests/test_writing.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,24 @@ def test_can_write_non_zero_indexed_one_row_dataframe():
assert (expected in output)


@pytest.mark.parametrize("include_field_numbers, expected",
[
(True, "_Brand #1\n_Price #2\n"),
(False, "_Brand\n_Price\n"),
])
def test_include_exclude_field_numbers(include_field_numbers, expected):
with TemporaryDirectory() as directory:
filename = join_path(directory, "test.star")
StarWriter(
test_df,
filename,
include_field_numbers=include_field_numbers
).write()
with open(filename) as output_file:
output = output_file.read()
assert (expected in output)


@pytest.mark.parametrize("quote_character, quote_all_strings, num_quotes",
[('"', False, 6),
('"', True, 8),
Expand Down
Loading