Skip to content

Commit

Permalink
Add --embedded-mode param to parse (#278)
Browse files Browse the repository at this point in the history
* bump version

* added param

* linted
  • Loading branch information
hrshdhgd authored Jul 1, 2022
1 parent 5fc5d25 commit bbbd22e
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 9 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
##########################
[metadata]
name = sssom
version = 0.3.12.dev
version = 0.3.12
description = Operations on SSSOM mapping tables
long_description = file: README.md
long_description_content_type = text/markdown
Expand Down
12 changes: 11 additions & 1 deletion sssom/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,15 @@ def convert(input: str, output: TextIO, output_format: str):
required=True,
help="If True (default), records with unknown prefixes are removed from the SSSOM file.",
)
@click.option(
"-E",
"--embedded-mode",
default=True,
is_flag=True,
help="If False, the resultant SSSOM file will be saved\
in the 'filename'.tsv provided by -o/--output option\
AND the metadata gets saved in the 'filename'.yml.",
)
@predicate_filter_option
@output_option
def parse(
Expand All @@ -166,17 +175,18 @@ def parse(
prefix_map_mode: str,
clean_prefixes: bool,
output: TextIO,
embedded_mode: bool,
mapping_predicate_filter: Optional[tuple],
):
"""Parse a file in one of the supported formats (such as obographs) into an SSSOM TSV file."""
# TODO: add "--embedded-mode" - boolean
parse_file(
input_path=input,
output=output,
input_format=input_format,
metadata_path=metadata,
prefix_map_mode=prefix_map_mode,
clean_prefixes=clean_prefixes,
embedded_mode=embedded_mode,
mapping_predicate_filter=mapping_predicate_filter,
)

Expand Down
5 changes: 3 additions & 2 deletions sssom/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def parse_file(
metadata_path: Optional[str] = None,
prefix_map_mode: Optional[str] = None,
clean_prefixes: bool = True,
embedded_mode: bool = True,
mapping_predicate_filter: tuple = None,
) -> None:
"""Parse an SSSOM metadata file and write to a table.
Expand All @@ -71,6 +72,7 @@ def parse_file(
:param prefix_map_mode: Defines whether the prefix map in the metadata should be extended or replaced with
the SSSOM default prefix map. Must be one of metadata_only, sssom_default_only, merged
:param clean_prefixes: If True (default), records with unknown prefixes are removed from the SSSOM file.
:param embedded_mode:If True (default), the dataframe and metadata are exported in one file (tsv), else two separate files (tsv and yaml).
:param mapping_predicate_filter: Optional list of mapping predicates or filepath containing the same.
"""
raise_for_bad_path(input_path)
Expand Down Expand Up @@ -101,8 +103,7 @@ def parse_file(
if clean_prefixes:
# We do this because we got a lot of prefixes from the default SSSOM prefixes!
doc.clean_prefix_map()
write_table(doc, output)
# TODO: add "--embedded-mode" - boolean to write_table which is optional.
write_table(doc, output, embedded_mode)


def validate_file(
Expand Down
23 changes: 18 additions & 5 deletions sssom/writers.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,13 @@
MSDFWriter = Callable[[MappingSetDataFrame, TextIO], None]


def write_table(msdf: MappingSetDataFrame, file: TextIO, serialisation="tsv") -> None:
def write_table(
msdf: MappingSetDataFrame,
file: TextIO,
embedded_mode: bool = True,
serialisation="tsv",
) -> None:
"""Write a mapping set dataframe to the file as a table."""
# TODO: based on "--embedded-mode" parameter save file a certain way.
if msdf.df is None:
raise TypeError

Expand All @@ -65,9 +69,18 @@ def write_table(msdf: MappingSetDataFrame, file: TextIO, serialisation="tsv") ->
lines = yaml.safe_dump(meta).split("\n")
lines = [f"# {line}" for line in lines if line != ""]
s = msdf.df.to_csv(sep=sep, index=False)
lines = lines + [s]
for line in lines:
print(line, file=file)

if embedded_mode:
lines = lines + [s]
for line in lines:
print(line, file=file)
else:
# Export MSDF as tsv
msdf.df.to_csv(file, sep=sep, index=False)
# Export Metadata as yaml
yml_filepath = file.name.replace("tsv", "yaml")
with open(yml_filepath, "w") as y:
yaml.safe_dump(meta, y)


def write_rdf(
Expand Down

0 comments on commit bbbd22e

Please sign in to comment.