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

[WIP] Makes Pylint happier #90

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
533 changes: 533 additions & 0 deletions .pylintrc

Large diffs are not rendered by default.

6 changes: 0 additions & 6 deletions vermouth/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

"""
Created on Tue Aug 22 11:41:47 2017

@author: Peter Kroon
"""

# Find the data directory once.
try:
import pkg_resources
Expand Down
6 changes: 0 additions & 6 deletions vermouth/deferred_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

"""
Created on Mon Oct 9 10:04:01 2017

@author: Peter Kroon
"""

import os
import sys
import tempfile
Expand Down
26 changes: 17 additions & 9 deletions vermouth/ffinput.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

import collections
import copy
import math
import numbers
import json
from .molecule import (
Expand All @@ -33,8 +32,6 @@
Choice, NotDefinedOrNot,
ParamDistance, ParamAngle, ParamDihedral, ParamDihedralPhase,
)
import networkx as nx
import copy

VALUE_PREDICATES = {
'not': NotDefinedOrNot,
Expand All @@ -55,7 +52,7 @@ def _tokenize(line):
An interaction line is any uncommented and non empty line that follows a
section header about an interaction type. Such a line is composed of the
following parts:

* a list of atoms involved in the interaction,
* an optional delimiter that indicates the end of the atom list,
* a list of parameters for the interaction.
Expand Down Expand Up @@ -218,7 +215,7 @@ def _substitute_macros(line, macros):
end = start + len(macro_value)
return line


def _some_atoms_left(tokens, atoms, natoms):
if tokens and tokens[0] == '--':
tokens.popleft()
Expand Down Expand Up @@ -285,7 +282,7 @@ def _treat_block_interaction_atoms(atoms, context, section):
msg = ('Atom names in blocks cannot be prefixed with + or -. '
'The name "{}", used in section "{}" of the block "{}" '
'is not valid in a block.')
raise IOError(msg.format(reference, section, block.name))
raise IOError(msg.format(reference, section, context.name))


def _split_node_key(key):
Expand All @@ -296,6 +293,7 @@ def _split_node_key(key):
raise IOError('A node key cannot be empty.')

# If the atom name is prefixed, we can get the order.
prefix_end = 0 # Make sure prefix_end is defined even if key is empty
for prefix_end, char in enumerate(key):
if char not in '+-><*':
break
Expand All @@ -320,7 +318,7 @@ def _split_node_key(key):
def _get_order_and_prefix_from_attributes(attributes):
prefix_from_attributes = ''
order_from_attributes = None
Sequence = collections.Sequence
Sequence = collections.Sequence # pylint: disable=invalid-name
if attributes.get('order') is not None:
order = attributes['order']
order_from_attributes = order
Expand Down Expand Up @@ -707,7 +705,7 @@ def _parse_patterns(tokens, context, context_type):
context.patterns.append(atoms)


def _parse_variables(tokens, force_field):
def _parse_variables(tokens, force_field, section):
if len(tokens) > 2:
raise IOError('Unexpected column in section "{}".'.format(section))
elif len(tokens) < 2:
Expand All @@ -724,6 +722,16 @@ def _parse_features(tokens, context, context_type):


def read_ff(lines, force_field):
"""
Read a .ff file and update the force field with its content.

Parameters
----------
lines
Iterable on the file lines to read.
force_field: vermouth.ForceField
The force field to update.
"""
interactions_natoms = {
'bonds': 2,
'angles': 3,
Expand Down Expand Up @@ -808,7 +816,7 @@ def read_ff(lines, force_field):
if context is not None:
raise IOError('The [variables] section must be defined '
'before the blocks and the links.')
_parse_variables(tokens, force_field)
_parse_variables(tokens, force_field, section)
elif tokens[0] == '#meta':
_parse_meta(tokens, context, context_type, section)
else:
Expand Down
52 changes: 47 additions & 5 deletions vermouth/forcefield.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import itertools
from glob import glob
from pathlib import Path
import os
from .gmx.rtp import read_rtp
from .ffinput import read_ff
Expand All @@ -24,6 +23,19 @@


class ForceField(object):
"""
Description of a force field.

Attributes
----------
blocks: dict
links: list
modifications: list
renamed_residues: dict
name: str
variables: dict
reference_graphs: dict
"""
def __init__(self, directory):
self.blocks = {}
self.links = []
Expand All @@ -34,6 +46,12 @@ def __init__(self, directory):
self.read_from(directory)

def read_from(self, directory):
"""
Populate or update the force field from a directory.

The provided directory must contain a subdirectory with the same name
as the force field.
"""
source_files = iter_force_field_files(directory)
for source in source_files:
extension = os.path.splitext(source)[-1]
Expand Down Expand Up @@ -69,14 +87,35 @@ def has_feature(self, feature):
bool
"""
return feature in self.features


def find_force_fields(directory, force_fields=None):
"""
Find all the force fields in the given directory.
Read all the force fields in the given directory.

A force field is defined as a directory that contains at least one RTP
file. The name of the force field is the base name of the directory.

If the force field argument is not ``None``, then it must be a dictionary
with force field names as keys and instances of :class:`ForceField` as
values. The force fields in the dictionary will be updated if force fields
with the same names are found in the directory.

Parameters
----------
directory: pathlib.Path or str
The path to the directory containing the force fields.
force_fields: dict (optional)
A dictionary of force fields to update.

Returns
-------
dict
A dictionary of force fields read or updated. Keys are force field
names as strings, and values are instances of :class:`ForceField`. If a
dictionary was provided as the "force_fields" argument, then the
returned dictionary is the same instance as the one provided but with
updated content.
"""
if force_fields is None:
force_fields = {}
Expand All @@ -99,10 +138,13 @@ def find_force_fields(directory, force_fields=None):
return force_fields


def iter_force_field_files(directory, parsers=FORCE_FIELD_PARSERS):
def iter_force_field_files(directory, extensions=FORCE_FIELD_PARSERS.keys()):
"""
Returns a generator over the path of all the force field files in the directory.
"""
return itertools.chain(*(
glob(os.path.join(directory, '*' + extension))
for extension in parsers
for extension in extensions
))


Expand Down
34 changes: 17 additions & 17 deletions vermouth/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def distance_matrix(coordinates_a, coordinates_b):
)


def angle(vectorBA, vectorBC):
def angle(vector_ba, vector_bc):
"""
Calculate the angle in radians between two vectors.

Expand All @@ -61,8 +61,8 @@ def angle(vectorBA, vectorBC):

It returns the angle between BA and BC.
"""
nominator = np.dot(vectorBA, vectorBC)
denominator = np.linalg.norm(vectorBA) * np.linalg.norm(vectorBC)
nominator = np.dot(vector_ba, vector_bc)
denominator = np.linalg.norm(vector_ba) * np.linalg.norm(vector_bc)
cosine = nominator / denominator
# Floating errors at the limits may cause issues.
cosine = np.clip(cosine, -1, 1)
Expand All @@ -84,13 +84,13 @@ def dihedral(coordinates):
float
The calculated angle between -pi and +pi.
"""
vectorAB = coordinates[1, :] - coordinates[0, :]
vectorBC = coordinates[2, :] - coordinates[1, :]
vectorCD = coordinates[3, :] - coordinates[2, :]
normalABC = np.cross(vectorAB, vectorBC)
normalBCD = np.cross(vectorBC, vectorCD)
psin = np.dot(normalABC, vectorCD) * np.linalg.norm(vectorBC)
pcos = np.dot(normalABC, normalBCD)
vector_ab = coordinates[1, :] - coordinates[0, :]
vector_bc = coordinates[2, :] - coordinates[1, :]
vector_cd = coordinates[3, :] - coordinates[2, :]
normal_abc = np.cross(vector_ab, vector_bc)
normal_bcd = np.cross(vector_bc, vector_cd)
psin = np.dot(normal_abc, vector_cd) * np.linalg.norm(vector_bc)
pcos = np.dot(normal_abc, normal_bcd)
return np.arctan2(psin, pcos)


Expand All @@ -114,10 +114,10 @@ def dihedral_phase(coordinates):
dihedral
Calculate a dihedral angle.
"""
angle = dihedral(coordinates)
angle -= np.pi
if angle > np.pi:
angle -= 2 * np.pi
if angle < -np.pi:
angle += 2 * np.pi
return angle
dihedral_angle = dihedral(coordinates)
dihedral_angle -= np.pi
if dihedral_angle > np.pi:
dihedral_angle -= 2 * np.pi
if dihedral_angle < -np.pi:
dihedral_angle += 2 * np.pi
return dihedral_angle
6 changes: 0 additions & 6 deletions vermouth/gmx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

"""
Created on Tue Aug 22 11:42:17 2017

@author: Peter Kroon
"""

from .gro import read_gro, write_gro
from .itp import write_molecule_itp
from .rtp import read_rtp
5 changes: 0 additions & 5 deletions vermouth/gmx/gro.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

"""
Created on Tue Aug 22 11:34:12 2017

@author: Peter Kroon
"""
from ..molecule import Molecule
from ..utils import first_alpha
from ..truncating_formatter import TruncFormatter
Expand Down
5 changes: 0 additions & 5 deletions vermouth/graph_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

"""
Created on Tue Oct 10 10:51:03 2017

@author: peterkroon
"""
from collections import defaultdict
import itertools
import networkx as nx
Expand Down
6 changes: 5 additions & 1 deletion vermouth/map_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,13 @@ def read_mapping_directory(directory):
except IOError:
raise IOError('An error occured while reading "{}".'.format(path))
for from_ff in all_from_ff:
to_ff = None
for to_ff in all_to_ff:
mappings[from_ff][to_ff][name] = (mapping, weights, extra)
mappings[from_ff][to_ff] = dict(mappings[from_ff][to_ff])
if to_ff is not None:
# If all_to_ff is empty, then to_ff will not be redefined by
# the above for loop.
mappings[from_ff][to_ff] = dict(mappings[from_ff][to_ff])
return dict(mappings)


Expand Down
6 changes: 0 additions & 6 deletions vermouth/molecule.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

"""
Created on Thu Sep 14 10:58:04 2017

@author: Peter Kroon
"""

from collections import defaultdict, OrderedDict, namedtuple
import copy
from functools import partial
Expand Down
6 changes: 0 additions & 6 deletions vermouth/pdb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,4 @@
# See the License for the specific language governing permissions and
# limitations under the License.

"""
Created on Tue Aug 22 11:42:42 2017

@author: Peter Kroon
"""

from .pdb import read_pdb, write_pdb
5 changes: 0 additions & 5 deletions vermouth/pdb/pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

"""
Created on Tue Aug 22 11:33:07 2017

@author: Peter Kroon
"""
from ..molecule import Molecule
from ..utils import first_alpha, distance
from ..truncating_formatter import TruncFormatter
Expand Down
6 changes: 0 additions & 6 deletions vermouth/processors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

"""
Created on Wed Oct 4 10:45:54 2017

@author: peterkroon
"""

from .gro_reader import GROInput
from .make_bonds import MakeBonds
from .pdb_reader import PDBInput
Expand Down
5 changes: 0 additions & 5 deletions vermouth/processors/apply_blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

"""
Created on Fri Oct 27 14:39:20 2017

@author: peterkroon
"""
from ..gmx import read_rtp

from .processor import Processor
Expand Down
Loading