biocore / biom-format

The Biological Observation Matrix (BIOM) Format Project
http://biom-format.org
Other
89 stars 95 forks source link

Add pathlib support for saving BIOM files #894

Closed gibsramen closed 1 year ago

gibsramen commented 1 year ago

BIOM version 2.1.13

Currently, it seems like providing a pathlib.Path into biom.util.biom_open does not work.

Minimal Example
```python import pathlib import biom tbl = biom.example_table outfile_str = "./example_table.str.biom" outfile_plib = pathlib.Path("./example_table.plib.biom") # Works with biom.util.biom_open(outfile_str, "w") as f: tbl.to_hdf5(f, "str") # Does not work with biom.util.biom_open(outfile_plib, "w") as f: tbl.to_hdf5(f, "plib") ```

Error:

Traceback (most recent call last):
  File "code.py", line 14, in <module>
    with biom.util.biom_open(outfile_plib, "w") as f:
  File "/home/grahman/miniconda3/envs/st3/lib/python3.8/contextlib.py", line 113, in __enter__
    return next(self.gen)
  File "/home/grahman/miniconda3/envs/st3/lib/python3.8/site-packages/biom/util.py", line 455, in biom_open
    elif mode in ['w', 'wb'] and fp.endswith('.gz'):
AttributeError: 'PosixPath' object has no attribute 'endswith'

Seems like it currently uses str.endswith to look for compression which is causing an error as this method does not exist in the pathlib.Path class. Current workaround is coercing to str when passing the file handle but I'm pretty partial to using pathlib nowadays so if it's possible I think it'd be useful. Potentially easy solution is just doing that coercion in biom.util.open prior to calling endswith but I'm not sure if that will have any unintended consequences.

wasade commented 1 year ago

Oh interesting, good catch!! I agree this would be a valuable addition