Skip to content

Commit

Permalink
Merge pull request #99 from MolarVerse/dev
Browse files Browse the repository at this point in the history
Release 1.1.0
  • Loading branch information
97gamjak authored Jun 5, 2024
2 parents 8404a91 + 672c94a commit a927871
Show file tree
Hide file tree
Showing 35 changed files with 1,720 additions and 126 deletions.
21 changes: 21 additions & 0 deletions .githooks/commit-msg
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,24 @@ if [[ "$lowercase_message" =~ ^ci(\(.+\))?: ]]; then
group="CI"
fi

#pylint
if [[ "$lowercase_message" =~ ^pylint(\(.+\))?: ]]; then
matched=true
group="Pylint"
fi

if [[ "$lowercase_message" =~ ^admin(\(.+\))?: ]]; then
matched=true
group="Administrative"
fi

#random stuff that doesn't fit into any of the above categories
#starting with a $
if [[ "$lowercase_message" =~ ^\$ ]]; then
matched=true
group="Random"
fi

# ... (similar checks for other patterns in the list)

# No match found
Expand All @@ -120,6 +138,9 @@ if ! $matched; then
echo " - Example: example:, examples:"
echo " - Dependency: deps:"
echo " - CI: ci:"
echo " - Pylint: pylint:"
echo " - Administrative: admin:"
echo " - Random: $"
# ... (similar output for other patterns in the list)
echo " - --------------------------------------------\n"

Expand Down
Binary file modified .github/.pylint_cache/PQAnalysis_1.stats
Binary file not shown.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ build/
_version.py
**/*.ipynb

.github/.pylint_cache
.github/.pylint_cache/__*

.coverage
coverage.xml
Expand Down
17 changes: 10 additions & 7 deletions PQAnalysis/io/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ def __init__(
self.filename = filename

self.mode = FileWritingMode(mode)
self.original_mode = FileWritingMode(mode)

def open(self) -> None:
"""
Expand Down Expand Up @@ -144,13 +145,15 @@ class :py:class:`~PQAnalysis.io.formats.FileWritingMode`.
"""
mode = FileWritingMode(mode)

if (mode == FileWritingMode.WRITE and self.filename is not None
and os.path.isfile(self.filename)):
if (
mode == FileWritingMode.WRITE and self.filename is not None and
os.path.isfile(self.filename)
):
self.logger.error(
(
f"File {self.filename} already exists. "
"Use mode \'a\' to append to the file or mode "
"\'o\' to overwrite the file."
f"File {self.filename} already exists. "
"Use mode \'a\' to append to the file or mode "
"\'o\' to overwrite the file."
),
exception=FileWritingModeError
)
Expand Down Expand Up @@ -211,8 +214,8 @@ def __init__(self, filename: str | List[str]) -> None:
if not os.path.isfile(_filename):
self.logger.error(
(
"At least one of the given files does not exist. "
f"File {_filename} not found."
"At least one of the given files does not exist. "
f"File {_filename} not found."
),
exception=PQFileNotFoundError
)
Expand Down
2 changes: 2 additions & 0 deletions PQAnalysis/io/topology_file/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@
A subpackage to handle trajectory files.
"""

from .exceptions import TopologyFileError

from .topology_file_reader import TopologyFileReader
from .topology_file_writer import TopologyFileWriter
77 changes: 63 additions & 14 deletions PQAnalysis/io/topology_file/topology_file_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@

import logging

from beartype.typing import List
from beartype.typing import List, Tuple

from PQAnalysis.io.base import BaseReader
from PQAnalysis.topology import Bond, BondedTopology, Angle, Dihedral
from PQAnalysis.utils.custom_logging import setup_logger
from PQAnalysis import __package_name__
from PQAnalysis.utils.string import is_comment_line
from PQAnalysis.type_checking import runtime_type_checking
from PQAnalysis import __package_name__

from .exceptions import TopologyFileError

Expand Down Expand Up @@ -83,14 +84,14 @@ def _get_definitions(self) -> dict[str, List[str]]:
with open(self.filename, "r", encoding="utf-8") as file:
lines = file.readlines()

# remove all #.... parts from all lines
lines = [line.split("#")[0] for line in lines]

# remove all empty lines
lines = [line for line in lines if line.strip()]
# remove all comment lines and empty lines
no_comment_lines = [
line for line in lines if
not is_comment_line(line, comment_token="#", empty_line=True)
]

# check if last line is END else raise error
if lines[-1].strip().lower() != "end":
if no_comment_lines[-1].strip().lower() != "end":
self.logger.error(
"Something went wrong. Each block should end with 'END'",
exception=TopologyFileError,
Expand All @@ -101,9 +102,12 @@ def _get_definitions(self) -> dict[str, List[str]]:
block = []
for line in lines:
if line.strip().lower() == "end":
block.append(line)
blocks.append(block)
block = []
else:
elif not is_comment_line(
line, comment_token="#", empty_line=True
):
block.append(line)

# make a dictionary for each block with the key
Expand Down Expand Up @@ -201,7 +205,10 @@ def _parse_bonds(self, block: List[str]) -> List[Bond]:
If the number of columns in the block is not 3 or 4.
"""
bonds = []
for line in block:
for line in block[:-1]: # [-1] to avoid the "END" line of the block

line, comment = self._get_data_line_comment(line)

if len(line.split()) == 4:
index, target_index, bond_type, _ = line.split()
is_linker = True
Expand All @@ -220,6 +227,7 @@ def _parse_bonds(self, block: List[str]) -> List[Bond]:
index2=int(target_index),
bond_type=int(bond_type),
is_linker=is_linker,
comment=comment
)
)

Expand Down Expand Up @@ -251,7 +259,10 @@ def _parse_angles(self, block: List[str]) -> List[Angle]:
If the number of columns in the block is not 4 or 5.
"""
angles = []
for line in block:
for line in block[:-1]: # [-1] to avoid the "END" line of the block

line, comment = self._get_data_line_comment(line)

if len(line.split()) == 5:
index1, index2, index3, angle_type, _ = line.split()
is_linker = True
Expand All @@ -271,6 +282,7 @@ def _parse_angles(self, block: List[str]) -> List[Angle]:
index3=int(index3),
angle_type=int(angle_type),
is_linker=is_linker,
comment=comment
)
)

Expand Down Expand Up @@ -302,7 +314,10 @@ def _parse_dihedrals(self, block: List[str]) -> List[Dihedral]:
If the number of columns in the block is not 5 or 6.
"""
dihedrals = []
for line in block:
for line in block[:-1]: # [-1] to avoid the "END" line of the block

line, comment = self._get_data_line_comment(line)

if len(line.split()) == 6:
index1, index2, index3, index4, dihedral_type, _ = line.split()
is_linker = True
Expand All @@ -323,6 +338,7 @@ def _parse_dihedrals(self, block: List[str]) -> List[Dihedral]:
index4=int(index4),
dihedral_type=int(dihedral_type),
is_linker=is_linker,
comment=comment
)
)

Expand Down Expand Up @@ -354,7 +370,10 @@ def _parse_impropers(self, block: List[str]) -> List[Dihedral]:
If the number of columns in the block is not 5 or 6.
"""
dihedrals = []
for line in block:
for line in block[:-1]: # [-1] to avoid the "END" line of the block

line, comment = self._get_data_line_comment(line)

if len(line.split()) == 6:
index1, index2, index3, index4, dihedral_type, _ = line.split()
is_linker = True
Expand All @@ -376,6 +395,7 @@ def _parse_impropers(self, block: List[str]) -> List[Dihedral]:
dihedral_type=int(dihedral_type),
is_linker=is_linker,
is_improper=True,
comment=comment
)
)

Expand Down Expand Up @@ -407,7 +427,10 @@ def _parse_shake(self, block: List[str]) -> List[Bond]:
If the number of columns in the block is not 3 or 4.
"""
shake_bonds = []
for line in block:
for line in block[:-1]: # [-1] to avoid the "END" line of the block

line, comment = self._get_data_line_comment(line)

if len(line.split()) == 4:
index, target_index, distance, _ = line.split()
is_linker = True
Expand All @@ -422,7 +445,33 @@ def _parse_shake(self, block: List[str]) -> List[Bond]:
equilibrium_distance=float(distance),
is_linker=is_linker,
is_shake=True,
comment=comment
)
)

return shake_bonds

@staticmethod
def _get_data_line_comment(line: str) -> Tuple[str, str | None]:
"""
Get the data and the comment from a line.
Parameters
----------
line : str
A line from the topology file.
Returns
-------
tuple[str, str]
A tuple with the data and the comment.
"""
splitted_line = line.split("#")
data = splitted_line[0].strip()

if len(splitted_line) > 1:
comment = "#".join(splitted_line[1:]).strip()
else:
comment = None

return data, comment
Loading

0 comments on commit a927871

Please sign in to comment.