Commit 7a95c142 authored by eric pellegrini's avatar eric pellegrini

Fully docstringed the MDANSE.Core.MMTKDefinitions.py module

Added related MMTK manipulation at MDANSE __init__.py
parent 6cd66db1
import os
import platform
# Hack for the (in)famous "(python:865): LIBDBUSMENU-GLIB-WARNING **: Trying to remove a child that doesn't believe we're it's parent."
if platform.dist()[0].lower() == "ubuntu":
os.environ["UBUNTU_MENUPROXY"] = "0"
"""
This class implements the MDANSE error handler.
#MDANSE : Molecular Dynamics Analysis for Neutron Scattering Experiments
#------------------------------------------------------------------------------------------
#Copyright (C)
#2015- Eric C. Pellegrini Institut Laue-Langevin
#BP 156
#6, rue Jules Horowitz
#38042 Grenoble Cedex 9
#France
#pellegrini[at]ill.fr
#goret[at]ill.fr
#aoun[at]ill.fr
#
#This library is free software; you can redistribute it and/or
#modify it under the terms of the GNU Lesser General Public
#License as published by the Free Software Foundation; either
#version 2.1 of the License, or (at your option) any later version.
#
#This library is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
#Lesser General Public License for more details.
#
#You should have received a copy of the GNU Lesser General Public
#License along with this library; if not, write to the Free Software
#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
It inherits from the Exception class. It can be used to filter the exceptions
raised within the MDANSE framework
"""
'''
Created on Mar 30, 2015
@author: pellegrini
'''
class Error(Exception):
'''
Base class for handling exception occuring in MDANSE.
Any exception defined in MDANSE should derive from it in order to be properly handled
in the GUI application.
'''
def __init__(self, msg=None):
......
#MDANSE : Molecular Dynamics Analysis for Neutron Scattering Experiments
#------------------------------------------------------------------------------------------
#Copyright (C)
#2015- Eric C. Pellegrini Institut Laue-Langevin
#BP 156
#6, rue Jules Horowitz
#38042 Grenoble Cedex 9
#France
#pellegrini[at]ill.fr
#goret[at]ill.fr
#aoun[at]ill.fr
#
#This library is free software; you can redistribute it and/or
#modify it under the terms of the GNU Lesser General Public
#License as published by the Free Software Foundation; either
#version 2.1 of the License, or (at your option) any later version.
#
#This library is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
#Lesser General Public License for more details.
#
#You should have received a copy of the GNU Lesser General Public
#License along with this library; if not, write to the Free Software
#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
'''
Created on Mar 30, 2015
@author: pellegrini
'''
import cPickle
import os
import shutil
from MMTK.Biopolymers import defineAminoAcidResidue, defineNucleicAcidResidue
from MMTK.PDB import defineMolecule
......@@ -9,34 +42,71 @@ from MDANSE.Core.Error import Error
from MDANSE.Core.Singleton import Singleton
class MMTKDefinitionsError(Error):
'''
Handles the exceptions related to MMTKDefinitions.
'''
pass
class MMTKDefinitions(dict):
'''
This singleton class stores the patches for MMTK chemical database in a dictionary which is reloaded at MDANSE
start through the use of cPickle mechanism.
When converting a trajectory, that uses PDB file format to define the system, to a MMTK trajectory, it may happen
that some molecules (molecule, amino acids, nucleic acids) are not defined in the default database shipped with MMTK.
In such case, it is compulsory to register them in order that they can be recognized as a valid molecule by MMTK.
'''
__metaclass__ = Singleton
_path = os.path.join(PLATFORM.application_directory(),"mmtk_definitions")
# These are the patchable types
_types = ['amino_acid','molecule','nucleic_acid']
def add(self, name, typ, databaseName):
def add(self, code, typ, filename):
'''
Add a new MMTK definition to the MMTK definitions store.
:param code: the MMTK code for the molecule to register (i.e. HOH for water)
:type code: str
:param typ: the molecular type for the molecule to register (one of 'amino_acid','molecule','nucleic_acid')
:type typ: str
:param filename: the filename that stores the MMTK definition of the molecule to register
:type filename: str
'''
if type not in self._types:
raise MMTKDefinitionsError('The type %r is not a valid MMTK definition type. It must be one of %s' % (str(name),MMTKDefinitions._types))
if typ not in self._types:
raise MMTKDefinitionsError('The type %r is not a valid MMTK definition type. It must be one of %s' % (str(code),MMTKDefinitions._types))
# Extract the basename from the full name of the MMTK definition file
basename = os.path.basename(filename)
basename = os.path.splitext(basename)[0]
# Define them in MMTK
try:
if typ == 'molecule':
defineMolecule(name,databaseName)
defineMolecule(code,basename)
defPath = os.path.join(PLATFORM.local_mmtk_database_directory(),"Molecules")
elif typ == 'amino_acid':
defineAminoAcidResidue(databaseName,name)
defineAminoAcidResidue(basename,code)
defPath = os.path.join(PLATFORM.local_mmtk_database_directory(),"Groups")
elif typ == 'nucleic_acid':
defineNucleicAcidResidue(databaseName,name)
defineNucleicAcidResidue(basename,code)
defPath = os.path.join(PLATFORM.local_mmtk_database_directory(),"Groups")
except ValueError as e:
raise MMTKDefinitions(str(e))
raise MMTKDefinitionsError(str(e))
else:
self[name] = (typ,databaseName)
defPath = os.path.join(defPath,basename)
# Register the MMTK definition
self[code] = (typ,basename)
# Copy the MMTK definition file to the right place
shutil.copy(filename,defPath)
def load(self):
'''
Load the MMTK definitions file.
'''
if not os.path.exists(MMTKDefinitions._path):
return
......@@ -53,6 +123,12 @@ class MMTKDefinitions(dict):
raise MMTKDefinitionsError("The mmtk definitions file could not be loaded")
def save(self, path=None):
'''
Save the MMTK definitions file
:param path: the filename in which the MMTK definitions will be saved
:type path: str
'''
if path is None:
path = MMTKDefinitions._path
......
......@@ -41,5 +41,54 @@ from MDANSE.Data.ElementsDatabase import ELEMENTS
from MDANSE.Logging.Logger import LOGGER
import MDANSE.Framework
del MDANSE.Framework
import Framework
del Framework
import os
# MMTK imports.
from MMTK import Database
from MMTK.Database import path
from MMTK.Utility import checkURL, isURL, joinURL
def databasePath(filename, directory, try_direct = False):
if isURL(filename):
return filename
filename = os.path.expanduser(filename)
if try_direct and os.path.exists(filename):
return os.path.normcase(filename)
entries = None
dirname,basename = os.path.split(filename)
if dirname == '':
for p in path:
if isURL(p):
url = joinURL(p, directory+'/'+basename)
if checkURL(url):
entries = url
break
else:
full_name = os.path.join(os.path.join(p, directory), basename)
if os.path.exists(full_name):
entries = os.path.normcase(full_name)
break
if entries is None:
if directory == "Atoms":
LOGGER("Atom %r not found in the MMTK database. nMolDyn will create a default one." % basename,"warning")
ELEMENTS.add_element(basename,save=True)
return os.path.join(PLATFORM.local_mmtk_database_directory(),"Atoms", basename)
else:
raise IOError("Database entry %s/%s not found" % (directory, filename))
else:
return entries
# Add the path to the nmoldyn database to complete the MMTK database.
Database.databasePath = databasePath
Database.path.insert(0,os.path.join(PLATFORM.package_directory(), 'Data'))
Database.path.insert(0,PLATFORM.local_mmtk_database_directory())
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment