Skip to content

Commit 89d7b75

Browse files
committed
Add run tests and rewrite tests to use pytest
1 parent 1d3d352 commit 89d7b75

7 files changed

Lines changed: 2815 additions & 87 deletions

File tree

Manifest.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include test/101M_prof.cif

prepmd/model.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import pathlib
1111
import shutil
1212
from prepmd import get_residues
13+
import sys
1314

1415
placeholder = "sequence:::::::::"
1516

@@ -158,8 +159,9 @@ def fix_missing_residues(code, fastafile, alignmentout, inmodel, outmodel,
158159

159160
pdb_dirs = ['.', '../atom_files']
160161

162+
modeller.log.none()
163+
161164
if fastafile:
162-
modeller.log.none()
163165
env = modeller.Environ()
164166
model = modeller.Model(env, file=inmodel)
165167
aln = modeller.Alignment(env)
@@ -175,6 +177,7 @@ def fix_missing_residues(code, fastafile, alignmentout, inmodel, outmodel,
175177
print("Aligning sequences...")
176178
alignments = aligner.align(
177179
pdb_sequence[0][1], original_sequence[0][1])
180+
print("Succesfully aligned.")
178181
Align.write(alignments[-1], alignmentout, "fasta")
179182
# the file doesn't have names/labels for whatever reason
180183
retitle_alignment(alignmentout, code, code +
@@ -184,7 +187,7 @@ def fix_missing_residues(code, fastafile, alignmentout, inmodel, outmodel,
184187
"sequences in the PDB and the original protein are too "
185188
"different)")
186189
fastafile = None
187-
print("Trying to use the sequence data in the input file...")
190+
print("Will try to use the sequence data in the input file...")
188191

189192
if not fastafile:
190193
print("Getting missing residues from input file...")
@@ -199,11 +202,16 @@ def fix_missing_residues(code, fastafile, alignmentout, inmodel, outmodel,
199202
aln = modeller.Alignment(env)
200203
aln.append(file=code+".seq", align_codes=code)
201204
aln.append(file=code+"_fill.seq", align_codes=code+"_fill")
202-
aln.align2d()
205+
aln.salign()
206+
print("Succesfully aligned.", end="")
203207
aln.write(file=alignmentout)
208+
print(" Wrote alignment to "+alignmentout)
204209

205210
env.io.atom_files_directory = pdb_dirs
206211
print("Modelling missing loops...")
212+
old_stdout = sys.stdout
213+
f = open(os.devnull, 'w')
214+
sys.stdout = f
207215
a = automodel(env, alnfile=alignmentout,
208216
knowns=code, sequence=code+"_fill")
209217
a.auto_align()
@@ -212,6 +220,9 @@ def fix_missing_residues(code, fastafile, alignmentout, inmodel, outmodel,
212220
a.set_output_model_format("MMCIF")
213221

214222
a.make()
223+
224+
sys.stdout = old_stdout
225+
print("Finished modelling missing loops.")
215226

216227
best_pdb = get_best_pdb(wdir)
217228
shutil.copy2(best_pdb, outmodel)

prepmd/prep.py

Lines changed: 81 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import glob
1111
import random
1212
import string
13-
import sys
13+
#import sys
1414
import shutil
1515

1616
from prepmd import download
@@ -20,8 +20,8 @@
2020

2121
parser = argparse.ArgumentParser(prog="prepmd",
2222
description="Get structures from the PDB ready for "
23-
"molecular dynamics runs",
24-
epilog="Also, run 'prepmd test' for the test suite")
23+
"molecular dynamics runs")
24+
# epilog="Also, run 'prepmd test' for the test suite")
2525
parser.add_argument("code", help="4 or 12-character PDB code")
2626
parser.add_argument("out", help="Output filename")
2727
parser.add_argument("-w", "--wdir", help="Working directory")
@@ -94,14 +94,16 @@ def prep(code, outmodel, workingdir, folder=None, fastafile=None, inmodel=None,
9494
Returns:
9595
nothing, but writes out a file to outmodel.
9696
"""
97-
97+
9898
# infer download format from output format
9999
if not download_format:
100100
if (".pdb") in outmodel:
101101
download_format = "pdb"
102+
print("No download format specified, downloading PDB.")
102103
elif (".cif") in outmodel or ".mmcif" in outmodel or (
103104
".pdbx") in outmodel:
104105
download_format = "mmCif"
106+
print("No download format specified, downloading mmCif.")
105107

106108
# check that input/output are specified in the same format
107109
# i'm not against converting the files but it shouldn't happen implicitly
@@ -148,6 +150,7 @@ def prep(code, outmodel, workingdir, folder=None, fastafile=None, inmodel=None,
148150
print("Downloading structure file")
149151
inmodel = download.get_structure(
150152
code, workingdir, file_format=download_format)
153+
print("Downloaded to "+inmodel)
151154

152155
# fix
153156
if not fix_after:
@@ -201,9 +204,9 @@ def prep(code, outmodel, workingdir, folder=None, fastafile=None, inmodel=None,
201204

202205
def entry_point():
203206
"CLI entry point function. Uses sys.argv and argparse args object."
204-
if len(sys.argv) == 2:
205-
if sys.argv[1] == "test":
206-
tests()
207+
# if len(sys.argv) == 2:
208+
# if sys.argv[1] == "test":
209+
# tests()
207210
args = parser.parse_args()
208211
fix_after = not args.fixstart
209212
if args.wdir is None:
@@ -228,80 +231,82 @@ def test_minimise():
228231
os.getcwd()+os.path.sep+"6TY4"+"_"+genid+".cif",
229232
os.getcwd()+os.path.sep+"testout"+os.path.sep+"6TY4"+"_"+genid)
230233

231-
def tests():
232-
os.system("")
233-
class style():
234-
RED = '\033[31m'
235-
GREEN = '\033[32m'
236-
YELLOW = '\033[33m'
237-
BLUE = '\033[34m'
238-
MAGENTA = '\033[35m'
239-
CYAN = '\033[36m'
240-
WHITE = '\033[37m'
241-
UNDERLINE = '\033[4m'
242-
RESET = '\033[0m'
243234

244-
#testinputs = ["6xov", "9CS5", "8RM8", ]#"8VV2", "9B8B", "8CAE", "8QZA", "8RTL", "8RTO", "9A9W"] # regular pdbs
245-
#testinputs = ["6TY4", "6XOV", "9CS5", "8CAE", "8QZA", "8RTO"]
235+
# note: deprecated now that ctest support has been added
236+
# will be removed!!!
237+
238+
# def tests():
239+
# os.system("")
240+
# class style():
241+
# RED = '\033[31m'
242+
# GREEN = '\033[32m'
243+
# YELLOW = '\033[33m'
244+
# BLUE = '\033[34m'
245+
# MAGENTA = '\033[35m'
246+
# CYAN = '\033[36m'
247+
# WHITE = '\033[37m'
248+
# UNDERLINE = '\033[4m'
249+
# RESET = '\033[0m'
246250

247-
tests = [
248-
{"id": "6TY4", "format":"pdb"},
249-
{"id": "6XOV", "format":"pdb"},
250-
{"id": "9CS5", "format":"pdb"},
251-
{"id": "8CAE", "format":"pdb"},
252-
{"id": "8QZA", "format":"pdb"},
253-
{"id": "8RTO", "format":"mmCif"},
254-
{"id": "7IB8", "format":"mmCif"},
255-
{"id": "9A9G", "format":"mmCif"},
256-
{"id": "9I3U", "format":"pdb"},
257-
#test_minimise,
258-
#test_mmcif_support
259-
]
260-
#testinputs = ["8rto"]
251+
# tests = [
252+
# # {"id": "6TY4", "format":"pdb"},
253+
# # {"id": "6XOV", "format":"pdb"},
254+
# {"id": "9CS5", "format":"pdb"},
255+
# {"id": "8CAE", "format":"pdb"},
256+
# {"id": "8QZA", "format":"pdb"},
257+
# {"id": "8RTO", "format":"mmCif"},
258+
# {"id": "7IB8", "format":"mmCif"},
259+
# {"id": "9A9G", "format":"mmCif"},
260+
# {"id": "9I3U", "format":"pdb"},
261+
# #test_minimise,
262+
# #test_mmcif_support
263+
# ]
261264

262-
results = {}
263-
state = 0
264-
cwd = os.getcwd()
265+
# types = {"mmCif":"cif", "cif":"cif", "pdb":"pdb"}
266+
267+
# results = {}
268+
# state = 0
269+
# cwd = os.getcwd()
265270

266-
for test in range(len(tests)):
267-
try:
268-
os.chdir(cwd)
269-
code = tests[test]["id"]
270-
curr_format = tests[test]["format"]
271-
print(f"Testing {code} ({test}/{len(tests)})")
272-
genid = id_generator(6)
273-
pathlib.Path(os.getcwd()+os.path.sep+"testout").mkdir(
274-
parents=True, exist_ok=True)
275-
if type(tests[test]) == dict:
276-
prep(code,
277-
os.getcwd()+os.path.sep+code+"_"+genid+"."+"cif",
278-
os.getcwd()+os.path.sep+"testout"+os.path.sep+code+"_"+genid,
279-
download_format=curr_format)
280-
elif callable(tests[test]):
281-
test()
282-
print(f"{style.GREEN}PASSED: {test} {style.RESET}")
283-
results[code] = "PASS"
284-
except Exception as e:
285-
print(f"{style.RED}FAILED: {test}{style.RESET}")
286-
results[code] = e
287-
state = 1
288-
print("")
289-
print("RESULTS:")
290-
for name, result in results.items():
291-
if result == "PASS":
292-
print(f"{name}: {style.GREEN}{result}{style.RESET}")
293-
else:
294-
errtype = type(result).__name__, # TypeError
295-
errfile = __file__, # /tmp/example.py
296-
errline = result.__traceback__.tb_lineno # 2
297-
error = str(errtype)+" on line " + \
298-
str(errline)+" in "+str(errfile)
299-
print(f"{name}: {style.RED}{error}{style.RESET}")
300-
for name, result in results.items():
301-
if result != "PASS":
302-
print(f"{style.YELLOW}{name} exception: {result}{style.RESET}")
271+
# for test in range(len(tests)):
272+
# try:
273+
# os.chdir(cwd)
274+
# code = tests[test]["id"]
275+
# curr_format = tests[test]["format"]
276+
# print(f"Testing {code} ({test}/{len(tests)})")
277+
# genid = id_generator(6)
278+
# pathlib.Path(os.getcwd()+os.path.sep+"testout").mkdir(
279+
# parents=True, exist_ok=True)
280+
# if type(tests[test]) == dict:
281+
# prep(code,
282+
# os.getcwd()+os.path.sep+code+"_"+genid+"."+types[curr_format],
283+
# os.getcwd()+os.path.sep+"testout"+os.path.sep+code+"_"+genid,
284+
# download_format=curr_format)
285+
# elif callable(tests[test]):
286+
# test()
287+
# print(f"{style.GREEN}PASSED: {test} {style.RESET}")
288+
# results[code] = "PASS"
289+
# except Exception as e:
290+
# print(f"{style.RED}FAILED: {test}{style.RESET}")
291+
# results[code] = e
292+
# state = 1
293+
# print("")
294+
# print("RESULTS:")
295+
# for name, result in results.items():
296+
# if result == "PASS":
297+
# print(f"{name}: {style.GREEN}{result}{style.RESET}")
298+
# else:
299+
# errtype = type(result).__name__, # TypeError
300+
# errfile = __file__, # /tmp/example.py
301+
# errline = result.__traceback__.tb_lineno # 2
302+
# error = str(errtype)+" on line " + \
303+
# str(errline)+" in "+str(errfile)
304+
# print(f"{name}: {style.RED}{error}{style.RESET}")
305+
# for name, result in results.items():
306+
# if result != "PASS":
307+
# print(f"{style.YELLOW}{name} exception: {result}{style.RESET}")
303308

304-
sys.exit(state)
309+
# sys.exit(state)
305310

306311
if __name__ == "__main__":
307312
entry_point()

0 commit comments

Comments
 (0)