morfeus icon indicating copy to clipboard operation
morfeus copied to clipboard

Permission denied error when using ConformerEnsemble.prune_rmsd()

Open shariR1001 opened this issue 2 years ago • 2 comments

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.

shariR1001 avatar Jan 26 '24 14:01 shariR1001

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: https://github.com/digital-chemistry-laboratory/morfeus/blob/974e5ddce24e0e4403206a364fa28c9d5d89651c/morfeus/conformer.py#L1297

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?

kjelljorner avatar Jan 27 '24 11:01 kjelljorner

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

shariR1001 avatar Jan 29 '24 13:01 shariR1001