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

Permission denied error when using ConformerEnsemble.prune_rmsd() #66

Open
shariR1001 opened this issue Jan 26, 2024 · 2 comments · May be fixed by #67
Open

Permission denied error when using ConformerEnsemble.prune_rmsd() #66

shariR1001 opened this issue Jan 26, 2024 · 2 comments · May be fixed by #67

Comments

@shariR1001
Copy link

Hey:)

I am trying to use the following function for ConformerEnsemble_from_CREST:
ce.prune_rmsd(method = "obrms-batch")
It gives me the following error:
PermissionError: [Errno 13] Permission denied: '~\AppData\Local\Temp\tmpnp4iv4_n.xyz'

I modified the conformer.py write_xyz function by adding 2 lines (import os and os.unlink(file)):
`` def write_xyz(
self,
file: str | PathLike,
ids: Iterable[int] | None = None,
unit: str = "kcal/mol",
relative: bool = True,
separate: bool = False,
n_decimals: int = 3,
) -> None:
"""Write conformers to xyz file.

    Args:
        file: Filename or path object. Needs filename if `separate=True`
        ids: Conformer indices (1-indexed)
        unit: Output unit for energies in xyz comment field: 'hartree', 'kcal/mol',
            'kJ/mol'
        relative: Whether to give energies relative to lowest energy conformer
        separate: Whether to write conformers to separate xyz files
        n_decimals: Number of decimals for energies

    Raises:
        TypeError: When separate=True and file is not str
    """
    if ids is None:
        ids_ = np.arange(len(self.conformers))
    else:
        ids_ = np.array(ids) - 1
    ids = ids_

    # Retrieve symbols, coordinates and energies
    symbols = convert_elements(self.elements, output="symbols")
    conformer_coordinates = self.get_coordinates()[ids]
    energies = self.get_relative_energies(unit=unit, relative=relative)[ids].round(
        n_decimals
    )

    # Write conformers
    if separate:
        if not isinstance(file, str):
            raise TypeError("file must be str when separate=True")
        for i, coordinates, energy in zip(ids, conformer_coordinates, energies):
            conf_filename = file.split(".")[0] + f"_{i + 1}.xyz"
            write_xyz(conf_filename, symbols, coordinates, comments=[energy])
    else:
        import os
        os.unlink(file)
        write_xyz(file, symbols, conformer_coordinates, comments=energies)

Now the function runs without an error message.

@kjelljorner
Copy link
Collaborator

Hello and thanks for reporting the issue, it is a problem with NamedTemporaryFile on Windows. There seems to be a way to fix this generally in Python 3.12, but I don't want to break backwards incompatibility that far.

Your fix works coincidentally for the very specific case, but a more general fix built on that could be to change:

p_ref = Path(ref_file.name)

to

p_ref = Path(ref_file.name)
p_ref.unlink()

I don't have access to a Windows machine at the moment, but can you try and see if it works?

@shariR1001
Copy link
Author

Hey, thank you for the help and considering my issue. I tried what you have suggested and it fixes the issue!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants