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

plugins/plug-in architecture #1353

Closed
ctb opened this issue Feb 28, 2021 · 15 comments
Closed

plugins/plug-in architecture #1353

ctb opened this issue Feb 28, 2021 · 15 comments
Labels
plugin_todo Write a plugin for this! plugin a plugin for sourmash!

Comments

@ctb
Copy link
Contributor

ctb commented Feb 28, 2021

how might we be thinking about better supporting plugins as a way for people to extend sourmash?

ref https://twitter.com/betatim/status/1355902709237473281

UPDATE July 2023:

save/load plugins were added in #2428
command-line plugins were added in #2438, under sourmash scripts ...

sourmash info -v displays available plugins.

https://github.com/ctb/sourmash-mixers is my attempt to gather "sourmash team" plugins under a common metapackage for single-command install.

the future is bright. 😎

moving remaining ideas to:

@ctb
Copy link
Contributor Author

ctb commented Mar 5, 2021

The packaging docs for Python on plugin support are an interesting read.

So far I like the "naming convention" approach the best.

@luizirber
Copy link
Member

@olgabot
Copy link
Collaborator

olgabot commented Mar 17, 2021

Yes to this! I've been thinking about scanpy (scverse/scanpy#271) + sourmash extensions for a bit but haven't gotten around to it yet.

Here's an example of one: https://github.com/icbi-lab/scirpy

@olgabot
Copy link
Collaborator

olgabot commented Mar 17, 2021

Also found this for the scanpy ecosystem: https://scanpy.readthedocs.io/en/stable/ecosystem.html

@ctb
Copy link
Contributor Author

ctb commented Jun 26, 2021

some thoughts on possible plug-in functionality -

  • add new sourmash CLI commands, perhaps under plugin or extension subcommand?
  • new Index loading functions?
  • new taxonomy reporting functionality?
  • new picklist/manifest-related functionality?

@ctb
Copy link
Contributor Author

ctb commented Dec 31, 2022

Apparently entrypoints is now deprecated in favor of importlib.metadata. Per link,

This package is in maintenance-only mode. New code should use the importlib.metadata module in the Python standard library to find and load entry points.

So:

https://docs.python.org/3/library/importlib.metadata.html

...which, ultimately, after following many small twisty paths, leads to:

https://setuptools.pypa.io/en/latest/userguide/entry_point.html

@ctb
Copy link
Contributor Author

ctb commented Jan 3, 2023

I think it would be easy and maybe useful to add new taxonomy loading via plugins 🤔 . New taxonomies themselves would be trickier (e.g. LINS) and it's not 100% clear we need more than NCBI, GTDB, and LINS.

@ctb
Copy link
Contributor Author

ctb commented Jan 5, 2023

Other plugin ideas:

ctb added a commit that referenced this issue Jan 7, 2023
…ts new signature saving & loading mechanisms (#2428)

Implement support for `load_from` and `save_to` plugins via
`importlib.metadata` entry points.

This supports a few of the plugins suggested in
#1353

I am nominating this as an experimental feature that is not under
semantic versioning/not public yet.

Documentation page [here, in
dev_plugins.html](https://sourmash--2428.org.readthedocs.build/en/2428/dev_plugins.html).

A template repo for new plugins is at
https://github.com/sourmash-bio/sourmash_plugin_template.

## Implementation/this PR

This PR refactors the `_load_database` loading and
`SaveSignaturesToLocation` saving code to build a prioritized list of
functions to try in order, and then adds hooks in via the new
`sourmash.plugins` module that insert additional loading/saving
functions into that list.

This PR also moves the current saving/loading functions out of
`sourmash.sourmash_args` into the `sourmash.save_load` submodule, and
simplifies the code a bit.

## Example plugins:

- read JSON sigs and manifests from URLs:
https://github.com/sourmash-bio/sourmash_plugin_load_urls
- read and write signatures in Apache Avro:
https://github.com/sourmash-bio/sourmash_plugin_avro - use extension
`.avrosig` to write.

Specific TODOs:
- [x] provide a minimal "getting started" template repo
- [x] add tests for multiple plugins & priorities
- [ ] maybe try writing CSV export/import as a plugin?
#1098

For later:
- think about other kinds of plugins - new CLI entry points, picklist
classes, tax loading, tax structure, ??.
- work on getting avro support into rust over in
luizirber/2021-02-11-sourmash-binary-format#1
@ctb
Copy link
Contributor Author

ctb commented Jan 8, 2023

I've been wondering about Python plugins that build on the Rust library. Will we have multiple versions of sourmash-rs installed and present? Is that a problem? (probably not a problem, but maybe inefficient)

In particular, it might be interesting to try out some of the stuff in https://github.com/ctb/2022-pymagsearch as a CLI plugin.

@ctb
Copy link
Contributor Author

ctb commented Jan 9, 2023

@betatim mentioned pluggy as an alternative approach to importlib.metadata. Might take a look!

@betatim
Copy link
Contributor

betatim commented Jan 9, 2023

It isn't strictly an alternative (pluggy also uses entrypoints to discover plugins), I look at it more as a "framework to use when making your application pluggable" or some such. It lets you define hooks, register implementations, etc.

@ctb
Copy link
Contributor Author

ctb commented Mar 1, 2023

more things to do, coming from #2438:

  • decide on default CLI plugin entry point name - scripts or ext?
  • test it!
  • document and test the priority stuff for load & save

ctb added a commit that referenced this issue Mar 1, 2023
Add command-line plug-in interface, per
#1353.

This PR builds on #2428 to
provide support for adding new sourmash subcommands, under `sourmash
scripts <cmd>`.

The CLI plugin interface looks like this on the client side -
```
class cmd_somewhat:
    command = 'somewhat'
    description = "does a thing"

    def __init__(self, subparser):
        super().__init__(p)
        # add arguments etc here 
        debug_literal('RUNNING cmd_somewhat.__init__')
        subparser.add_argument('foo', type=int)

    def main(self, args):
        super().main(args)
        # what we actually run. 
        print('RUNNING cmd', self, args)
        print('XYZ somewhat', args.foo)
        print(f'2*{args.foo} is {2*args.foo}')
```

Adding this plugin enables:
```
% sourmash scripts  -h          

== This is sourmash version 4.6.1. ==
== Please cite Brown and Irber (2016), doi:10.21105/joss.00027. ==

usage:  scripts [-h]

optional arguments:
  -h, --help  show this help message and exit

extension commands:
  sourmash scripts somewhat --help - do a thing
```
and
```
% sourmash scripts somewhat 5 

== This is sourmash version 4.6.1. ==
== Please cite Brown and Irber (2016), doi:10.21105/joss.00027. ==

...
2*5 is 10
```
which is prety cool 🎉 

## Alternatives to `script`

could be `sourmash ext` or `sourmash plugin`?
@ctb ctb added plugin_todo Write a plugin for this! plugin a plugin for sourmash! labels Mar 12, 2023
@ctb
Copy link
Contributor Author

ctb commented Mar 25, 2023

taxonomy plugin for greengenes using code from over in #2538

@ctb
Copy link
Contributor Author

ctb commented Jul 31, 2023

save/load plugins were added in #2428
command-line plugins were added in #2438, under sourmash scripts ...

sourmash info -v displays available plugins.

https://github.com/ctb/sourmash-mixers is my attempt to gather "sourmash team" plugins under a common metapackage for single-command install.

the future is bright. 😎

moving remaining ideas to:

@ctb ctb closed this as completed Jul 31, 2023
@ctb
Copy link
Contributor Author

ctb commented Jul 31, 2023

I've been wondering about Python plugins that build on the Rust library. Will we have multiple versions of sourmash-rs installed and present? Is that a problem? (probably not a problem, but maybe inefficient)

@luizirber says that the answer is yes and I believe him :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
plugin_todo Write a plugin for this! plugin a plugin for sourmash!
Projects
None yet
Development

No branches or pull requests

4 participants