Skip to content

Commit

Permalink
pass string as arguments instead of file paths #7
Browse files Browse the repository at this point in the history
  • Loading branch information
felipeZ committed Aug 10, 2018
1 parent 6c7de63 commit d5b7d4b
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 69 deletions.
20 changes: 5 additions & 15 deletions lie_structures/cheminfo_wamp/cheminfo_molhandle_wamp.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,9 @@ def convert_structures(self, request, claims):
lie_structures/schemas/endpoints/convert_response_v1.json
"""
molobject = self.read_mol(request)
file_path = self.create_output_file(request)

outfmt = request.get('output_format', request['input_format'])
output = mol_write(molobject, mol_format=outfmt, file_path=file_path)
output = mol_write(molobject, mol_format=outfmt, file_path=None)

# Update session
status = 'completed'
Expand All @@ -66,9 +65,8 @@ def addh_structures(self, request, claims):
correctForPH=request['correctForPH'],
pH=request['pH'])

file_path = self.create_output_file(request)
outfmt = request.get('output_format', request['input_format'])
output = mol_write(molobject, mol_format=outfmt, file_path=file_path)
output = mol_write(molobject, mol_format=outfmt, file_path=None)

# Update session
status = 'completed'
Expand All @@ -84,10 +82,9 @@ def removeh_structures(self, request, claims):
lie_structures/schemas/endpoints/removeh_response_v1.json
"""
molobject = mol_removeh(self.read_mol(request))
file_path = self.create_output_file(request)

outfmt = request.get('output_format', request['input_format'])
output = mol_write(molobject, mol_format=outfmt, file_path=file_path)
output = mol_write(molobject, mol_format=outfmt, file_path=None)

# Update session
status = 'completed'
Expand All @@ -109,9 +106,8 @@ def make3d_structures(self, request, claims):
localopt=request['localopt'],
steps=request['steps'])

file_path = self.create_output_file(request)
outfmt = request.get('output_format', request['input_format'])
output = mol_write(molobject, mol_format=outfmt, file_path=file_path)
output = mol_write(molobject, mol_format=outfmt, file_path=None)

# Update session
status = 'completed'
Expand Down Expand Up @@ -151,13 +147,7 @@ def rotate_structures(self, request, claims):

rotations = request['rotations']
if rotations:
result = mol_combine_rotations(molobject, rotations=rotations)

if 'workdir' in request:
file_path = os.path.join(request['workdir'], 'rotations.mol2')
with open(file_path, 'w') as otp:
otp.write(result)
output = file_path
output = mol_combine_rotations(molobject, rotations=rotations)
status = 'completed'
else:
status = 'failed'
Expand Down
2 changes: 1 addition & 1 deletion lie_structures/schemas/endpoints/addh_request.v1.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
},
"mol": {
"type": "string",
"description": "Moleculare file"
"description": "Molecular representation"
},
"input_format": {
"type": "string",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@
"type": "object",
"properties": {
"mol": {
"type": [
"string",
"object"
],
"description": "Moleculare file"
"type": "string",
"description": "Molecular representation"
},
"from_file": {
"type": "boolean",
Expand Down
2 changes: 1 addition & 1 deletion lie_structures/schemas/resources/read_write_mol.v1.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
},
"mol": {
"type": "string",
"description": "Moleculare file"
"description": "Molecular representation"
},
"input_format": {
"type": "string",
Expand Down
75 changes: 28 additions & 47 deletions tests/wamp_test/test_lie_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,95 +7,83 @@
import numpy as np
import os
import pybel
import shutil

file_path = os.path.realpath(__file__)
root = os.path.split(file_path)[0]

def create_workdir(name, path="/tmp/mdstudio/lie_structures"):
"""Create temporal workdir dir"""
workdir = join(path, name)
if not os.path.isdir(workdir):
os.makedirs(workdir)
return workdir


def read_mol(mol, fmt="mol2"):
"""Read a molecular object either from a file or string"""
if not os.path.isfile(mol):
name = '/tmp/structure.{}'.format(fmt)
with open(name, 'w') as f:
f.write(mol)
mol = name
if os.path.isfile(mol):
mol = pybel.readfile(fmt, mol).next()
else:
mol = pybel.readstring(fmt, mol)

return mol

return pybel.readfile(fmt, mol).next()

def read_file(file_path):
""" Returns file content"""
with open(file_path, 'r') as f:
content = f.read()

def copy_to_workdir(file_path, workdir):
shutil.copy(file_path, workdir)
base = os.path.basename(file_path)
return join(workdir, base)
return content


def compare_molecules(mol1, mol2, rtol=1e-5, atol=1e-8):
"""Compare the coordinates of two molecules"""
def compare_molecules(mol1, mol2):
"""Compare if the coordinates array are the same shape"""
m1 = read_mol(mol1)
m2 = read_mol(mol2)

arr = np.array([x.coords for x in m1])
brr = np.array([x.coords for x in m2])

return np.allclose(arr, brr, rtol, atol)
return arr.shape == brr.shape


dict_convert = {
"output_format": "mol2",
"workdir": create_workdir("convert"),
"workdir": "/tmp",
"input_format": "smi",
"mol": "O1[C@@H](CCC1=O)CCC",
"from_file": False,
"to_file": True
"mol": "O1[C@@H](CCC1=O)CCC"
}

dict_make3d = {
"from_file": True,
"workdir": create_workdir("make3d"),
"workdir": "/tmp",
"input_format": "mol2",
"output_format": "mol2",
"mol": join(root, 'files/structure.mol2')
"mol": read_file(join(root, 'files/structure.mol2'))
}

dict_addh = {
"from_file": True,
"workdir": create_workdir("addh"),
"workdir": "/tmp",
"input_format": "mol2",
"output_format": "mol2",
"mol": join(root, "files/structure3D.mol2"),
"mol": read_file(join(root, "files/structure3D.mol2")),
"pH": 7.4,
"correctForPH": False
}

dict_info = {
"mol": join(root, "files/structure3D.mol2"),
"workdir": create_workdir("info"),
"mol": read_file(join(root, "files/structure3D.mol2")),
"workdir": "/tmp",
"input_format": "mol2",
}

dict_rotate = {
"from_file": True,
"workdir": create_workdir("rotate"),
"workdir": "/tmp",
"input_format": "mol2",
"output_format": "mol2",
"rotations": [
[1, 0, 0, 90], [1, 0, 0, -90], [0, 1, 0, 90],
[0, 1, 0, -90], [0, 0, 1, 90], [0, 0, 1, -90]],
"mol": join(root, "files/structureHs.mol2"),
"mol": read_file(join(root, "files/structureHs.mol2")),
}

dict_similarity = {
"mol_format": "smi",
"ci_cutoff": 0.3617021276595745,
"workdir": create_workdir('similarity'),
"workdir": "/tmp",
"test_set": ["O1[C@@H](CCC1=O)CCC"],
"reference_set": [
"c1(c(cccc1Nc1c(cccc1)C(=O)O)C)C",
Expand Down Expand Up @@ -152,27 +140,21 @@ def on_run(self):

convert = yield self.call(
"mdgroup.lie_structures.endpoint.convert", dict_convert)
print(convert)
assert compare_molecules(convert['mol'], join(root, 'files/structure.mol2'))
print("converting {} from smile to mol2 succeeded!".format(
dict_convert['mol']))

dict_make3d['mol'] = copy_to_workdir(dict_make3d['mol'], dict_make3d['workdir'])
make3d = yield self.call(
"mdgroup.lie_structures.endpoint.make3d", dict_make3d)
# assert compare_molecules(make3d['mol'], join(root, 'files/structure3D.mol2'), atol=1e-2)
print(make3d)
assert compare_molecules(make3d['mol'], join(root, 'files/structure3D.mol2'))
print("successful creation of a 3D structure for {}".format(
dict_convert['mol']))

dict_addh['mol'] = copy_to_workdir(dict_addh['mol'], dict_addh['workdir'])
addh = yield self.call(
"mdgroup.lie_structures.endpoint.addh", dict_addh)
print(addh)
assert compare_molecules(addh['mol'], join(root, 'files/structureHs.mol2'))
print("added hydrogens sucessfully!")

dict_info['mol'] = copy_to_workdir(dict_info['mol'], dict_info['workdir'])
info = yield self.call(
"mdgroup.lie_structures.endpoint.info", dict_info)
print("info", info)
Expand All @@ -181,17 +163,16 @@ def on_run(self):
atts['formula'] == 'C7H12O2', atts['exactmass'] - 128.083729624 < 1e-5))
print('attributes information successfully retrieved!')

dict_rotate['mol'] = copy_to_workdir(dict_rotate['mol'], dict_info['workdir'])
rotate = yield self.call(
"mdgroup.lie_structures.endpoint.rotate", dict_rotate)
print("rotate: ", rotate)
assert compare_molecules(rotate['mol'], join(root, 'files/rotations.mol2'))
print("rotatation method succeeded!")

similarity = yield self.call(
"mdgroup.lie_structures.endpoint.chemical_similarity",
dict_similarity)
print("similarity: ", similarity)
assert similarity['results']['idx_max_sim']['0'] == 20
print("Similarity method succeeded!")


if __name__ == "__main__":
Expand Down

0 comments on commit d5b7d4b

Please sign in to comment.