Skip to content

Commit

Permalink
Merge pull request #54 from Daniel-Ibarrola/main
Browse files Browse the repository at this point in the history
Merged without further comments.
  • Loading branch information
dprada authored Jun 28, 2022
2 parents db73c7a + e6e9a04 commit 390976a
Show file tree
Hide file tree
Showing 120 changed files with 2,810 additions and 4,823 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,7 @@ devtools/playground/*
!devtools/playground/README.md
!molsysmt/build/*
!molsysmt/docs/contents/build/*
todo.txt

# pycharm
.idea/
15 changes: 13 additions & 2 deletions Untitled.ipynb
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
{
"cells": [],
"cells": [
{
"cell_type": "raw",
"source": [],
"metadata": {
"collapsed": false,
"pycharm": {
"name": ""
}
}
}
],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 5
}
}
15 changes: 6 additions & 9 deletions molsysmt/_private/atom_indices.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
import numpy as np


def complementary_atom_indices(molecular_system, atom_indices):

from molsysmt.basic import get

n_atoms = get(molecular_system, element='system', n_atoms=True)

mask = np.ones(n_atoms,dtype=bool)
mask[atom_indices]=False
mask = np.ones(n_atoms, dtype=bool)
mask[atom_indices] = False
return list(np.where(mask)[0])


def atom_indices_to_AmberMask(molecular_system, atom_indices):

from molsysmt.basic import get

n_atoms = get(molecular_system, element='system', n_atoms=True)
mask = np.zeros(n_atoms,dtype=int)
mask[atom_indices]=1
mask = np.zeros(n_atoms, dtype=int)
mask[atom_indices] = 1
return list(mask)

def atom_indices_to_csv(atom_indices):

return ",".join([str(ii) for ii in atom_indices])

10 changes: 4 additions & 6 deletions molsysmt/_private/digestion/__init__.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
from .atom_indices import digest_atom_indices
from .argument import digest_argument
from .box import digest_box
from .box import digest_box_lengths, digest_box_lengths_value
from .box import digest_box_angles, digest_box_angles_value
from .box import digest_box_vectors
from .coordinates import digest_coordinates
from .comparison import digest_comparison
from .element import digest_element
from .engine import digest_engine
from .form import digest_form, digest_to_form
from .group_indices import digest_group_indices
from .indices import digest_indices
from .indices import digest_indices, digest_structure_indices, digest_multiple_structure_indices
from .indices import digest_atom_indices, digest_group_indices
from .item import digest_item
from .molecular_system import digest_single_molecular_system, digest_multiple_molecular_systems
from .output import digest_output
from .selection import digest_selection, digest_multiple_selections
from .step import digest_step
from .structure_indices import digest_structure_indices, digest_multiple_structure_indices
from .syntaxis import digest_syntaxis, digest_to_syntaxis
from .element import digest_element
from .time import digest_time
Expand Down
16 changes: 16 additions & 0 deletions molsysmt/_private/digestion/argument.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from molsysmt.attribute.attributes import attribute_synonyms, attributes
from ..exceptions import WrongGetArgumentError


def digest_argument(argument, element):
""" Helper function to check keyword arguments passed to get function.
"""
output_argument = argument.lower()
if output_argument in ['index', 'indices', 'name', 'names', 'id', 'ids', 'type', 'types', 'order']:
output_argument = '_'.join([element, output_argument])
if output_argument in attribute_synonyms:
output_argument = attribute_synonyms[output_argument]
if output_argument in attributes:
return output_argument
else:
raise WrongGetArgumentError(argument)
16 changes: 0 additions & 16 deletions molsysmt/_private/digestion/atom_indices.py

This file was deleted.

120 changes: 58 additions & 62 deletions molsysmt/_private/digestion/box.py
Original file line number Diff line number Diff line change
@@ -1,76 +1,72 @@
import numpy as np
from molsysmt import puw
from ..exceptions import *
from ..exceptions import IncorrectShapeError

def digest_box(box):

def digest_box(box):
# TODO: Function doesn't do anything.
return box

def digest_box_lengths_value(box_lengths):

output = None

if type(box_lengths) is not np.ndarray:
box_lengths = np.array(box_lengths)

shape = box_lengths.shape

if len(shape)==1:
if shape[0]==3:
output = np.expand_dims(box_lengths, axis=0)
else:
raise ValueError('box_lengths array with has not the correct shape.')
elif len(shape)==2:
if shape[1]==3:
output = box_lengths
else:
raise ValueError('box_lengths array with has not the correct shape.')
else:
raise ValueError('box_lengths array with has not the correct shape.')

return output

def digest_box_lengths(box_lengths):

output = None
unit = puw.get_unit(box_lengths)
box_lengths_value = puw.get_value(box_lengths)
box_lengths_value = digest_box_lengths_value(box_lengths_value)
output = box_lengths_value*unit

return output
def digest_box_vectors_value(box_vectors):
""" Checks if box_vectors has the correct shape.
def digest_box_angles_value(box_angles):
The array should have shape (n, 3) where n is any integer.
However, if a list, tuple is passed it will be converted
to an array with the desired shape. Also, if an array of
shape (3, ) is passed its dimensions will be expanded to
(1, 3). If an array with rank > 2 is passed and exception
is raised.
output = None
Parameters
----------
box_vectors : np.ndarray, list or tuple
A quantity with the box lengths.
if type(box_angles) is not np.ndarray:
box_angles = np.array(box_angles)
Raises
------
IncorrectShapeError
If box_vectors doesn't have the correct shape.
"""
if not(isinstance(box_vectors, np.ndarray)):
box_vectors = np.array(box_vectors)

shape = box_angles.shape
shape = box_vectors.shape

if len(shape)==1:
if shape[0]==3:
output = np.expand_dims(box_angles, axis=0)
if len(shape) == 1:
if shape[0] == 3:
box_vectors = np.expand_dims(box_vectors, axis=0)
else:
raise ValueError('box_angles array with has not the correct shape.')
elif len(shape)==2:
if shape[1]==3:
output = box_angles
else:
raise ValueError('box_angles array with has not the correct shape.')
raise IncorrectShapeError(expected_shape="(1, 3)", actual_shape=str(shape))
elif len(shape) == 2:
if shape[1] != 3:
raise IncorrectShapeError(expected_shape="(n, 3)", actual_shape=str(shape))
else:
raise ValueError('box_angles array with has not the correct shape.')

return output

def digest_box_angles(box_angles):

output = None
unit = puw.get_unit(box_angles)
box_angles_value = puw.get_value(box_angles)
box_angles_value = digest_box_angles_value(box_angles_value)
output = box_angles_value*unit

return output

raise IncorrectShapeError(expected_shape="(n, 3)", actual_shape=str(shape))

return box_vectors


def digest_box_vectors(box_vectors):
""" Checks if box vectors have the correct shape. Can be used
to check box_lengths and box_angles.
Parameters
----------
box_vectors : puw.Quantity
A quantity with the box vectors.
Returns
-------
puw.Quantity
The box vectors with the shape corrected.
Raises
------
IncorrectShapeError
If box_vectors doesn't have the correct shape.
"""
unit = puw.get_unit(box_vectors)
box_vectors = puw.get_value(box_vectors)
box_vectors = digest_box_vectors_value(box_vectors)
return box_vectors * unit
3 changes: 2 additions & 1 deletion molsysmt/_private/digestion/comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
from molsysmt import puw
from ..exceptions import *

def digest_comparison(comparison):

def digest_comparison(comparison):
# TODO: function doesn't do anything
return comparison

3 changes: 2 additions & 1 deletion molsysmt/_private/digestion/coordinates.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from ..exceptions import *

def digest_coordinates(coordinates):

def digest_coordinates(coordinates):
# TODO: function doesn't do anything
return coordinates

54 changes: 33 additions & 21 deletions molsysmt/_private/digestion/element.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from ..exceptions import *
from ..exceptions import WrongElementError

elements = [
'atom',
Expand All @@ -11,28 +11,40 @@
'bond',
]

elements_from_plural={
'atoms' : 'atom',
'groups' : 'group',
'residue' : 'group',
'residues' : 'group',
'components' : 'component',
'chains' : 'chain',
'molecules' : 'molecule',
'entities' : 'entity',
'systems' : 'system',
'bonds' : 'bond',
elements_from_plural = {
'atoms': 'atom',
'groups': 'group',
'residue': 'group',
'residues': 'group',
'components': 'component',
'chains': 'chain',
'molecules': 'molecule',
'entities': 'entity',
'systems': 'system',
'bonds': 'bond',
}


def digest_element(element):
""" Helper function to check an element type. Raises a BadCallError
if the element type is not supported by MolSysMT.
Parameters
----------
element : str
The name of the element.
try:
tmp_element = element.lower()
if tmp_element in elements_from_plural:
tmp_element = elements_from_plural[tmp_element]
elif tmp_element not in elements:
raise BadCallError()
return tmp_element
except:
raise BadCallError()
Raises
------
BadCallError
If the element is not a string or its name is not valid.
"""
if isinstance(element, str):
element_name_lower = element.lower()
if element_name_lower in elements_from_plural:
return elements_from_plural[element_name_lower]
if element_name_lower not in elements:
raise WrongElementError("wrong element name")
return element_name_lower

raise WrongElementError("element is not a string")
26 changes: 20 additions & 6 deletions molsysmt/_private/digestion/engine.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from ..exceptions import *
from ..exceptions import WrongEngineError

engines = [
'Amber',
Expand All @@ -13,12 +13,26 @@
'NGLView',
]

engines_from_lowercase={ ii.lower() : ii for ii in engines }
engines_from_lowercase = {ii.lower(): ii for ii in engines}


def digest_engine(engine):
""" Check the name of the engine.
try:
return engines_from_lowercase[engine.lower()]
except:
raise BadCallError()
Parameters
---------
engine : str
The name of the engine
Raises
------
BadCallError
If the engine name is not valid.
"""
if isinstance(engine, str):
try:
return engines_from_lowercase[engine.lower()]
except KeyError:
# TODO: create a wrong engine error
raise WrongEngineError
raise WrongEngineError
Loading

0 comments on commit 390976a

Please sign in to comment.