Skip to content

Commit

Permalink
Increase unit test coverage for strucreader. Close #5
Browse files Browse the repository at this point in the history
  • Loading branch information
f3rmion committed Feb 18, 2021
1 parent 105ac9a commit ff5c2a3
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 36 deletions.
33 changes: 16 additions & 17 deletions src/kallisto/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def cli(config, verbose: bool, shift: int):
def cns(config, inp: str, out: click.File, cntype: str):
"""Atomic coordination numbers."""

molecule = ksr.constructMolecule(geometry=inp)
molecule = ksr.constructMolecule(geometry=inp, out=out)
cns = molecule.get_cns(cntype)
verbosePrinter(config.verbose, cns, out)

Expand Down Expand Up @@ -85,7 +85,7 @@ def cns(config, inp: str, out: click.File, cntype: str):
def cnsp(config, inp: str, out: click.File, cntype: str):
"""Atomic coordination number spheres."""

molecule = ksr.constructMolecule(geometry=inp)
molecule = ksr.constructMolecule(geometry=inp, out=out)
nat = molecule.get_number_of_atoms()
cnsp = molecule.get_cnspheres(cntype)
for i in range(nat):
Expand Down Expand Up @@ -119,7 +119,7 @@ def bonds(config, inp: str, partner: int, constrain: bool, out: click.File):

import os

molecule = ksr.constructMolecule(geometry=inp)
molecule = ksr.constructMolecule(geometry=inp, out=out)

if partner == "X":
# Get index table of covalent bonding partners
Expand Down Expand Up @@ -174,14 +174,14 @@ def sort(config, inp: str, start: str, out: click.File):
start defines on which atom we start the sorting process.
"""

molecule = ksr.constructMolecule(geometry=inp)
molecule = ksr.constructMolecule(geometry=inp, out=out)
bonds = molecule.get_bonds()
nat = molecule.get_number_of_atoms()

# construct graph
from kallisto.sort import Graph

g = Graph(inp)
g = Graph(inp, out)

for i in range(nat):
partners = bonds[i]
Expand Down Expand Up @@ -218,7 +218,7 @@ def sort(config, inp: str, start: str, out: click.File):
def eeq(config, inp: str, out: click.File, chrg: int):
"""Electronegativity equilibration atomic partial charges."""

molecule = ksr.constructMolecule(geometry=inp)
molecule = ksr.constructMolecule(geometry=inp, out=out)
nat = molecule.get_number_of_atoms()
eeq = molecule.get_eeq(chrg)
for i in range(nat):
Expand Down Expand Up @@ -248,7 +248,7 @@ def eeq(config, inp: str, out: click.File, chrg: int):
def alp(config, inp: str, out: click.File, chrg: int, molecular: bool):
"""Static atomic polarizabilities in Bohr^3."""

molecule = ksr.constructMolecule(geometry=inp)
molecule = ksr.constructMolecule(geometry=inp, out=out)
nat = molecule.get_number_of_atoms()
alp = molecule.get_alp(charge=chrg)
if molecular:
Expand Down Expand Up @@ -291,7 +291,7 @@ def alp(config, inp: str, out: click.File, chrg: int, molecular: bool):
def vdw(config, inp: str, out: click.File, chrg: int, vdwtype: str, angstrom: bool):
"""Charge-dependent atomic van der Waals radii in Bohr."""

molecule = ksr.constructMolecule(geometry=inp)
molecule = ksr.constructMolecule(geometry=inp, out=out)
nat = molecule.get_number_of_atoms()
vdw = np.zeros(shape=(nat,), dtype=np.float64)

Expand Down Expand Up @@ -327,20 +327,19 @@ def rms(config, compare: Tuple[str, str], out: click.File):

from kallisto.rmsd import rmsd

mol1 = ksr.constructMolecule(geometry=compare[0])
mol1 = ksr.constructMolecule(geometry=compare[0], out=out)
nat1 = mol1.get_number_of_atoms()
mol2 = ksr.constructMolecule(geometry=compare[1])
mol2 = ksr.constructMolecule(geometry=compare[1], out=out)
nat2 = mol2.get_number_of_atoms()

# for RMSD comparison both coordinates need the same atom count
if nat1 != nat2:
click.echo(
errorbye(
"Error: number of atoms do not match in {} and in {}".format(
compare[0], compare[1]
),
file=out, # type: ignore
out,
)
errorbye(out)

coord1 = mol1.get_positions()
coord2 = mol2.get_positions()
Expand Down Expand Up @@ -377,7 +376,7 @@ def lig(config, inp: str, center: int, out: click.File):
"""Get all substructures (or ligands) that are bound to the center atom."""

# setup reference molecular structure
ref = ksr.constructMolecule(geometry=inp)
ref = ksr.constructMolecule(geometry=inp, out=out)
nat = ref.get_number_of_atoms()

# get all covalent bonding partner in reference complex
Expand Down Expand Up @@ -455,8 +454,8 @@ def exs(
Calculate the RMSD between two structures using quaternions."""

# setup reference molecular structure
ref = ksr.constructMolecule(geometry=inp[0])
substrate = ksr.constructMolecule(geometry=inp[1])
ref = ksr.constructMolecule(geometry=inp[0], out=out)
substrate = ksr.constructMolecule(geometry=inp[1], out=out)
nat = ref.get_number_of_atoms()

# get all covalent bonding partner in reference complex
Expand Down Expand Up @@ -511,7 +510,7 @@ def stm(config, inp: str, origin: int, partner: int, out: click.File):
"""Calculate sterimol descriptors using kallisto van der Waals radii."""

# setup molecular structure
mol = ksr.constructMolecule(geometry=inp)
mol = ksr.constructMolecule(geometry=inp, out=out)

# calculate Sterimol descriptors: L, bmin, bmax
from kallisto.sterics import getClassicalSterimol
Expand Down
23 changes: 8 additions & 15 deletions src/kallisto/reader/strucreader.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,27 @@
# src/kallisto/reader/strucreader.py

from kallisto.atom import Atom
import click

from kallisto.molecule import Molecule
import kallisto.reader.turbomole as tm
import kallisto.reader.xyz as xyz
from kallisto.utils import errorbye


def constructMolecule(geometry: str) -> Molecule:
def constructMolecule(geometry: str, out: click.File) -> Molecule:
"""Helper function to construct a Molecule."""
try:
with open(geometry, "r+") as fileObject:
# read atoms from input file
atoms = read(fileObject)

# create molecule from atoms
molecule = Molecule(symbols=atoms)
except FileNotFoundError:
print(" Error: Input file '" + geometry + "' not found.")
exit()
errorbye('Error: Inpt file "{0}" not found.'.format(geometry), out)

return molecule


def construct_atom():
return Atom()


def read(fileObject):
"""Method to first check the file type and then read
Expand All @@ -48,10 +44,7 @@ def read(fileObject):

fileObject.close()

atoms = None

if atoms is None:
atoms = []
atoms = []

newfile = open(fname, "r+")

Expand All @@ -60,8 +53,8 @@ def read(fileObject):
elif filetp == "xyz":
atoms = xyz.read(newfile)
else:
print("Error: file type of input '" + fname + "' not recognized")
quit()
print('Error: file type of input "{0}" not recognized'.format(fname))
exit(1)

newfile.close()

Expand Down
5 changes: 3 additions & 2 deletions src/kallisto/sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from collections import defaultdict

import click

from kallisto.data import chemical_symbols
import kallisto.reader.strucreader as ksr
Expand All @@ -12,11 +13,11 @@
class Graph:
"""Define a molecular graph."""

def __init__(self, inp: str):
def __init__(self, inp: str, out: click.File):

self.inp = inp
self.graph = defaultdict(list) # type: ignore
self.molecule = ksr.constructMolecule(geometry=self.inp)
self.molecule = ksr.constructMolecule(geometry=self.inp, out=out)
self.nat = self.molecule.get_number_of_atoms()
self.at = self.molecule.get_atomic_numbers()
self.coordinates = self.molecule.get_positions()
Expand Down
3 changes: 2 additions & 1 deletion src/kallisto/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ def verbosePrinter(config: bool, message: str, out: click.File):
click.echo(message, file=out) # type: ignore


def errorbye(out: click.File):
def errorbye(message: str, out: click.File):
"""Exit application due to error."""
click.echo("", file=out) # type: ignore
click.echo(message) # type: ignore
click.echo(
"""-> kallisto was terminated due to an error.
Please check the output.""",
Expand Down
17 changes: 16 additions & 1 deletion tests/test_strucreader.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import os
import tempfile

import pytest

from kallisto.atom import Atom
import kallisto.reader.strucreader as ksr

Expand All @@ -11,7 +13,7 @@


# turbomole coord files can be read
def test_a_user_can_read_a_TMfile():
def test_a_user_can_read_coord():
with tempfile.NamedTemporaryFile(mode="w+", encoding="utf-8") as f:
f.write("$coord" + s)
f.write(" 1.87167924 -0.101043656 0.1596818582 c" + s)
Expand All @@ -26,3 +28,16 @@ def test_a_user_can_read_a_TMfile():
got = type(atoms[0])
want = Atom
assert got is want


def test_a_user_cannot_read_invalid_coord():
with tempfile.NamedTemporaryFile(mode="w+", encoding="utf-8") as f:
f.write("$xoord" + s)
f.write(" 1.87167924 -0.101043656 0.1596818582 c" + s)
f.write("$end")
f.flush()
fname = open(f.name, "r+")
with pytest.raises(SystemExit) as error:
ksr.read(fname)
assert error.value.args[0] == 1
fname.close()

0 comments on commit ff5c2a3

Please sign in to comment.