mosdef-hub / foyer

A package for atom-typing as well as applying and disseminating forcefields
https://foyer.mosdef.org
MIT License
117 stars 75 forks source link

Error when foyer is imported #543

Closed qqlovekerry closed 9 months ago

qqlovekerry commented 11 months ago

Hello, I am trying to use one of the examples in mosdef_slitpore, I followed the instructions to create the environment by the following steps,

  1. I installed conda first
  2. run conda env create -f environment.yml at the root folder.
  3. run conda activate slitpore37 in the terminal
  4. run pip3 install -e . to install the packages

But after all the steps, I still failed to run the first Python file to build the vapor and liquid box by using python GEMC_NVT_Psat_water_298K.py . The problem is about importing foyer packages.

I tried to import the packages in the terminal one by one and I found I was not able to import the foyerpackage. The following are the details.

Python 3.9.16 | packaged by conda-forge | (main, Feb  1 2023, 21:39:03) 
[GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> import mbuild
/storage/programs/miniconda3/envs/slitpore37/lib/python3.9/site-packages/mdtraj/geometry/order.py:25: DeprecationWarning: pkg_resources is deprecated as an API. See https://setuptools.pypa.io/en/latest/pkg_resources.html
  from pkg_resources import parse_version
/storage/programs/miniconda3/envs/slitpore37/lib/python3.9/site-packages/pkg_resources/__init__.py:2871: DeprecationWarning: Deprecated call to `pkg_resources.declare_namespace('mpl_toolkits')`.
Implementing implicit namespace packages (as specified in PEP 420) is preferred to `pkg_resources.declare_namespace`. See https://setuptools.pypa.io/en/latest/references/keywords.html#keyword-namespace-packages
  declare_namespace(pkg)
/storage/programs/miniconda3/envs/slitpore37/lib/python3.9/site-packages/pkg_resources/__init__.py:2871: DeprecationWarning: Deprecated call to `pkg_resources.declare_namespace('google')`.
Implementing implicit namespace packages (as specified in PEP 420) is preferred to `pkg_resources.declare_namespace`. See https://setuptools.pypa.io/en/latest/references/keywords.html#keyword-namespace-packages
  declare_namespace(pkg)
>>> from foyer import Forcefield
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/storage/programs/miniconda3/envs/slitpore37/lib/python3.9/site-packages/foyer/__init__.py", line 2, in <module>
    from foyer.forcefield import Forcefield
  File "/storage/programs/miniconda3/envs/slitpore37/lib/python3.9/site-packages/foyer/forcefield.py", line 37, in <module>
    from foyer.atomtyper import find_atomtypes
  File "/storage/programs/miniconda3/envs/slitpore37/lib/python3.9/site-packages/foyer/atomtyper.py", line 6, in <module>
    from gmso import Topology
  File "/storage/programs/miniconda3/envs/slitpore37/lib/python3.9/site-packages/gmso/__init__.py", line 2, in <module>
    from .core.angle import Angle
  File "/storage/programs/miniconda3/envs/slitpore37/lib/python3.9/site-packages/gmso/core/angle.py", line 6, in <module>
    from gmso.abc.abstract_connection import Connection
  File "/storage/programs/miniconda3/envs/slitpore37/lib/python3.9/site-packages/gmso/abc/abstract_connection.py", line 5, in <module>
    from gmso.abc.abstract_site import Site
  File "/storage/programs/miniconda3/envs/slitpore37/lib/python3.9/site-packages/gmso/abc/abstract_site.py", line 10, in <module>
    from gmso.abc.gmso_base import GMSOBase
  File "/storage/programs/miniconda3/envs/slitpore37/lib/python3.9/site-packages/gmso/abc/gmso_base.py", line 8, in <module>
    from pydantic.validators import dict_validator
ImportError: cannot import name 'dict_validator' from 'pydantic.validators' (/storage/programs/miniconda3/envs/slitpore37/lib/python3.9/site-packages/pydantic/validators.py)

Could you please tell me how to solve this problem?

I also tried to use pip3 install foyer to install the package and the installation seems to be alright

Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: foyer in /home/xxxxxx/.local/lib/python3.6/site-packages (0.7.6)

The I tried to run the default Python executable and import the package in the terminal, but I got the following errors:

>>> from foyer import Forcefield
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/wenqiang/.local/lib/python3.6/site-packages/foyer/__init__.py", line 1, in <module>
    from foyer.forcefield import Forcefield
  File "/home/wenqiang/.local/lib/python3.6/site-packages/foyer/forcefield.py", line 14, in <module>
    import simtk.openmm.app.element as elem
ModuleNotFoundError: No module named 'simtk.openmm'

Could you please help me with this problem? I am trying to run the Psat_SPCE_298K example and I really need this to work.

Any help would be greatly appreciated!

daico007 commented 11 months ago

Hi there, so, to answer your first question, I believe the conda install is pulling in wrong version of pydantic, you could try running conda install pydantic=1.9 and see if that would fix the issue (doing mamba install may be quicker if you have that set up locally). On the second question about install foyer locally using pip, I believe the python version for that environment may be a bit too old (as I saw python3.6) and that may affect the installation of foyer dependency. So I would suggest update the python version for that environment if possible, and also make sure that all foyer dependency is installed (especially make sure the openmm version is >=7.7)

qqlovekerry commented 11 months ago

Hello, Thank you very much for your quick reply! That is really helpful. However, I still have one problem and one question. Could you please help me more?

  1. For the first question, conda install pydantic=1.9 perfectly solved my problem. Thank you very much! However, when I tried to run the Python code, another error appeared.

    File "/storage/mosdef_slitpore/simulations/Psat_SPCE_298K/gomc/build_liq_vap_boxes/GEMC_NVT_Psat_water_298K.py", line 5, in <module>
    from mosdef_slitpore.utils import charmm_writer as mf_charmm
    File "/storage/mosdef_slitpore/mosdef_slitpore/utils/charmm_writer.py", line 10, in <module>
    from parmed.utils.six.moves import range
    ModuleNotFoundError: No module named 'parmed.utils.six'

    It looks like the problem is that the current parmed package does not have the module parmed.utils.six, maybe I should also change the version of parmed, I found the page of this module, but I can not figure out which version it is. Could you please tell me how I should fix this error?

  2. Thanks for the advice! I will try to upgrade python3 version. I have searched before but still failed to install openmm, since it seems that openmm can only be installed by conda or compile from the source files, could you please tell me how I could install openmm for foyer dependency?

Thanks very much in advance!

daico007 commented 11 months ago

Hi, the first issue is that parmed has dropped the utils.six module (in this PR specifically https://github.com/ParmEd/ParmEd/pull/1138), so you could try revert it back to an earlier version, but I don't know if other things may break (we might have a few breaking changes happened between then and now). For the second question, the easiest path forward is to update the python version and install openmm through conda. If you're worried about keeping everything in that original env, I would suggest cloning it to a new env, and update all the package there. The mosdef_slitpore is probably bit old so it definitely needs some update, I can try update in the coming days, but I can't promise a definitely timeline due to other commitments I currently have.

qqlovekerry commented 11 months ago

Hi, the first issue is that parmed has dropped the utils.six module (in this PR specifically ParmEd/ParmEd#1138), so you could try revert it back to an earlier version, but I don't know if other things may break (we might have a few breaking changes happened between then and now). For the second question, the easiest path forward is to update the python version and install openmm through conda. If you're worried about keeping everything in that original env, I would suggest cloning it to a new env, and update all the package there. The mosdef_slitpore is probably bit old so it definitely needs some update, I can try update in the coming days, but I can't promise a definitely timeline due to other commitments I currently have.

Hello, Thank you very much for the quick reply! Sorry to ask again but I am not experienced in conda or python environment. I think the easiest way maybe is to use parmed with lower version and to try whether it works. I found available packages on this page, and I used conda install -c omnia parmed=3.2.0 to install the lower version. And I got the following message:

Collecting package metadata (current_repodata.json): - done
Solving environment: unsuccessful initial attempt using frozen solve. Retrying with flexible solve.
Solving environment: unsuccessful attempt using repodata from current_repodata.json, retrying with next repodata source.
Collecting package metadata (repodata.json): done
Solving environment: unsuccessful initial attempt using frozen solve. Retrying with flexible solve.
Solving environment: \ 
Found conflicts! Looking for incompatible packages.
This can take several minutes.  Press CTRL-C to abort.
failed                                                     

UnsatisfiableError: The following specifications were found
to be incompatible with the existing python installation in your environment:

Specifications:

  - parmed=3.2.0 -> python[version='2.7.*|3.6.*|3.7.*']

Your python: python=3.9

If python is on the left-most side of the chain, that's the version you've asked for.
When python appears to the right, that indicates that the thing on the left is somehow
not available for the python version you are constrained to. Note that conda will not
change your python version to a different minor version unless you explicitly specify
that.

The following specifications were found to be incompatible with your system:

  - feature:/linux-64::__glibc==2.27=0
  - feature:|@/linux-64::__glibc==2.27=0

Your installed version is: 2.27

I am not sure whether it is successful. And I tried to check the version with conda list, and I found the version seems not to be changed with parmed 4.1.0 py39h227be39_0 conda-forge. Could you please tell me whether I need to use a lower version of python executable too?

To install openmm and the environment thing seems to be complicated for me since I do not have much experience. I will try to use the slitpore37 environment first and hope it could work.

Thanks very much for the possible update of the mosdef_slitpore program! I just need the example Psat_SPCE_298K, and I think the only step that I need the python environment is for the first step Psat_SPCE_298K/gomc/build_liq_vap_boxes to build the initial configurations. The following steps are all about the files to run GOMC. I would appreciate it very much if you have any suggestions.

Thanks very much in advance!

daico007 commented 11 months ago

Hi, if you just need to build a configuration, the easiest way would be to keep thing as is and go into the file and comment out the GOMC portion (and the related import line), and if importing foyer Forcefield is giving openmm related issue, you can remove that for now.

import mbuild as mb
# from foyer import Forcefield
import sys
sys.path.append('../../../../')
# from mosdef_slitpore.utils import charmm_writer as mf_charmm

Water_res_name = 'H2O'
FF_file_water = '../../../../mosdef_slitpore/ffxml/pore-spce.xml'

water = mb.load('O', smiles=True)
water.name = Water_res_name
water.energy_minimize(forcefield = FF_file_water , steps=10**9)

FF_Dict = {water.name: FF_file_water }

residues_List = [water.name]

Fix_bonds_angles_residues = [ water.name ]

print('Running: filling liquid box')
water_box_liq = mb.fill_box(compound=[water],
                            density= 950 ,
                            compound_ratio = [1] ,
                            box=[3.0, 3.0, 3.0] )
print('Completed: filling liquid box')

print('Running: filling vapor box')
water_box_vap = mb.fill_box(compound=[water,],
                            n_compounds = [50],
                            box=[60, 60, 60])
print('Completed: filling vapor box')

# print('Running: GOMC FF file, and the psf and pdb files')
# mf_charmm.charmm_psf_psb_FF(water_box_liq,
#                             'Box_0_liq_water_30A_L_cubed_box',
#                             structure_1 = water_box_vap ,
#                             filename_1 = 'Box_1_vap_water_600A_L_cubed_box',
#                             FF_filename ="GOMC_water_FF" ,
#                              forcefield_files = FF_Dict,
#                              residues= residues_List ,
#                              Bead_to_atom_name_dict = None,
#                              fix_residue = None,
#                              fix_res_bonds_angles = Fix_bonds_angles_residues,
#                              reorder_res_in_pdb_psf = False
#                             )
print('Completed: GOMC FF file, and the psf and pdb files')
daico007 commented 11 months ago

Can you try paste that in place of the GEMC_NVT_Psat_water_298K.py file, and run it with whatever parmed version you have?

daico007 commented 11 months ago

My email daico007@gmail.com, if you want to send me more detail about what you're trying to do, I may be able to do a zoom/screen share session to do some troubleshooting/get your environment up to date (depends on how urgent you need to get this work).

qqlovekerry commented 11 months ago

Hello, Thank you indeed for all the kind and detailed replies!

I tried to paste the code you provided and tried to run it. Unfortunately maybe because the last part in the mf_charmm.charmm_psf_psb_FF is commented, no output files are found.

Thank you very much for your email address! I will write email to you to ask for your help!

daico007 commented 11 months ago

oh yes, sorry about that part, you can add a line

water_box_vap.save("water_box_vap.pdb", overwrite=True)

to anywhere after the box is created. It should write out a pdb file (other formats like mol2, xyz is also supported I believe).