cbouy / mols2grid

Interactive molecule viewer for 2D structures

Home Page:https://mols2grid.readthedocs.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

command-line tool

UnixJunkie opened this issue · comments

Hello,

Is there a way to have a command-line tool for this use case:

  • I am in a terminal
  • I want to create a 2D picture (as a grid) of all molecules in the input file
  • as a .png or .pdf, or ("a la rigueur") as a .html file.

I am not under jupyter or what not.

My input files are either .sdf, .mol2 or .smi (and in a SMILES file, the SMILES string are the first field/column).

Thanks a lot,
Francois.

Hi Francois,

  • There's no CLI in mols2grid but you could just make a simple script to emulate that.
  • You can generate the complete grid of molecules (as opposed to the paginated document) by specifying template="table" (see below). Please refer to the documentation for more info on the other arguments that you can supply.
  • You can save an HTML doc by using mols2grid.save(input, output="document.html", template="table", <your other args>) instead of mols2grid.display. The HTML document will have a special attribute inside (page-break-inside: avoid) which prevents page breaks if you decide to then convert that HTML to a PDF with multiple pages. There are some CLI utilities to do that conversion to pdf and images (wkhtmltopdf among others) or most browsers and OS have a print-to-pdf functionality.

Feel free to close the issue if it answers your question.

Hope this helps,
Cédric

Thanks, I'll give it a try.
I'm trying to get rid of the mview proprietary tool.

Sometimes, I would like mols2grid to just be able to visualize some molecules in a browser.
Either the molecules are SMILES in a file; or in a mol2 or sdf file.
I still think a basic CLI to visualize a bunch of molecules, not requiring any Python programming from the end-user would be nice.

If you know mview from chemaxon, than that's what I'd like but in open-source.

I had written one in the past, as you suggested, but it is now broken:
https://github.com/UnixJunkie/molenc/blob/master/bin/molenc_mview.py

mols2grid.save(mols, output='caff.html', template="table", prerender=True)

Gives me:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
Cell In [4], line 1
----> 1 mols2grid.save(mols, output='caff.html', template="table", prerender=True)

File ~/usr/miniconda/lib/python3.11/functools.py:909, in singledispatch.<locals>.wrapper(*args, **kw)
    905 if not args:
    906     raise TypeError(f'{funcname} requires at least '
    907                     '1 positional argument')
--> 909 return dispatch(args[0].__class__)(*args, **kw)

File ~/usr/miniconda/lib/python3.11/site-packages/mols2grid/dispatch.py:298, in _(mols, **kwargs)
    294 @save.register(Series)
    295 @save.register(list)
    296 @save.register(tuple)
    297 def _(mols, **kwargs):
--> 298     template, kwargs, render_kwargs = _prepare_kwargs(kwargs, "save")
    299     output = kwargs.pop("output")
    300     return MolGrid.from_mols(mols, **kwargs).save(
    301         output, template=template, **render_kwargs
    302     )

File ~/usr/miniconda/lib/python3.11/site-packages/mols2grid/dispatch.py:24, in _prepare_kwargs(kwargs, kind)
     20 """Separate kwargs for the init and render methods of MolGrid"""
     21 template = kwargs.pop("template", _SIGNATURE["render"]["template"].default)
     22 render_kwargs = {
     23     param: kwargs.pop(param, sig.default)
---> 24     for param, sig in _SIGNATURE[f"to_{template}"].items()
     25 }
     26 if kind == "display":
     27     render_kwargs.update(
     28         {
     29             param: kwargs.pop(param, sig.default)
     30             for param, sig in _SIGNATURE["display"].items()
     31         }
     32     )

KeyError: 'to_table'

Software that was working in the past, but doesn't anymore makes me feel very sad.

For the KeyError, that's because there were some breaking changes (here the renaming of the possible values for template from pages/table to interactive/static) between v1 and v2. You can either:

  • pin your dependencies to mols2grid<2 and keep your code as is,
  • or mols2grid>=2,<3 but using template=static,
  • or don't pin anything and use either template=table or template=static depending on the version of mols2grid installed with mols2grid.__version__.

For the CLI, yeah I can see some use case for it, but I have some other priorities for this package and rarely enough time to spare unfortunately. I remember you opening PR #54, apologies for not giving feedback on that. If you're still up for it I can make some suggestions and guide you to integrate it as an actual CLI tool.

I could repair molenc_mview.py, thanks!