Skip to content

Commit

Permalink
Fix dihedral verify func + Enable evaluate model without args + Paths…
Browse files Browse the repository at this point in the history
… handling OK
  • Loading branch information
CharlyEmpereurmot committed Oct 1, 2020
1 parent a04fcb8 commit afaa7b7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 23 deletions.
6 changes: 3 additions & 3 deletions swarmcg/evaluate_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,9 @@ def main():
action='store_true', default=False)

# display help if script was called without arguments
if len(sys.argv) == 1:
args_parser.print_help()
sys.exit()
# if len(sys.argv) == 1:
# args_parser.print_help()
# sys.exit()

# arguments handling, display command line if help or no arguments provided
ns = args_parser.parse_args()
Expand Down
7 changes: 5 additions & 2 deletions swarmcg/optimize_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,10 @@ def run(ns):
for top_line in top_lines:
if top_line.startswith('#include'):
top_include = top_line.split()[1].replace('"', '').replace("'", '') # remove potential single and double quotes around filenames
top_includes_filenames.append(os.path.dirname(arg_entries[user_provided_filenames[5]]) + '/' + top_include)
arg_dirname = os.path.dirname(arg_entries[user_provided_filenames[5]])
if arg_dirname == '':
arg_dirname = '.'
top_includes_filenames.append(arg_dirname + '/' + top_include)

# check gmx arguments conflicts
if ns.gmx_args_str != '' and (ns.nb_threads != 0 or ns.gpu_id != ''):
Expand Down Expand Up @@ -248,7 +251,7 @@ def run(ns):
with open(ns.exec_folder+'/'+config.opti_pairwise_distances_file, 'w'):
pass

# set these to None to then check the variables have been filled (!= None), so we will do these calculations
# set these to None to then check the variables have been filled (is not None), so we will do these calculations
# one single time in function compare_models that is called at each iteration during optimization
ns.gyr_aa_mapped, ns.gyr_aa_mapped_std = None, None
ns.sasa_aa_mapped, ns.sasa_aa_mapped_std = None, None
Expand Down
37 changes: 19 additions & 18 deletions swarmcg/swarmCG.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,12 @@ def section_switch(section_read, section_active):

for section_current in section_read:
section_read[section_current] = False
if section_active != None:
if section_active is not None:
section_read[section_active] = True


# vs_type in [2, 3, 4, n], then they each have specific functions to define their positions
def vs_error_control(ns, bead_id, vs_type, func, vs_def_beads_ids, line_nb):
def vs_error_control(ns, bead_id, vs_type, func, line_nb, vs_def_beads_ids=None):

if bead_id >= len(ns.cg_itp['atoms']):
msg = (
Expand All @@ -149,13 +149,14 @@ def vs_error_control(ns, bead_id, vs_type, func, vs_def_beads_ids, line_nb):
)
raise exceptions.MissformattedFile(msg)

for bid in vs_def_beads_ids:
if bid >= len(ns.cg_itp['atoms']):
msg = (
f"The definition of virtual site ID {bead_id + 1} makes use of ID {bid + 1}, while this ID exceeds"
f" the number of atoms defined in the CG ITP file."
)
raise exceptions.MissformattedFile(msg)
if vs_def_beads_ids is not None:
for bid in vs_def_beads_ids:
if bid >= len(ns.cg_itp['atoms']):
msg = (
f"The definition of virtual site ID {bead_id + 1} makes use of ID {bid + 1}, while this ID exceeds"
f" the number of atoms defined in the CG ITP file."
)
raise exceptions.MissformattedFile(msg)

if not ns.cg_itp['atoms'][bead_id]['bead_type'].startswith('v'):
msg = (
Expand Down Expand Up @@ -400,7 +401,7 @@ def read_cg_itp_file(ns):
bead_id = int(sp_itp_line[0])-1
vs_def_beads_ids = [int(bid)-1 for bid in sp_itp_line[1:3]]
func = sp_itp_line[3] # will be casted to int in the verification below (for factorizing checks)
func = vs_error_control(ns, bead_id, vs_type, func, vs_def_beads_ids, i) # i is the line number
func = vs_error_control(ns, bead_id, vs_type, func, i, vs_def_beads_ids) # i is the line number
vs_params = float(sp_itp_line[4])
ns.cg_itp['atoms'][bead_id]['vs_type'] = vs_type
ns.cg_itp['virtual_sites2'][bead_id] = {'bead_id': bead_id, 'func': func, 'vs_def_beads_ids': vs_def_beads_ids, 'vs_params': vs_params}
Expand All @@ -411,7 +412,7 @@ def read_cg_itp_file(ns):
bead_id = int(sp_itp_line[0])-1
vs_def_beads_ids = [int(bid)-1 for bid in sp_itp_line[1:4]]
func = sp_itp_line[4] # will be casted to int in the verification below (for factorizing checks)
func = vs_error_control(ns, bead_id, vs_type, func, vs_def_beads_ids, i) # i is the line number
func = vs_error_control(ns, bead_id, vs_type, func, i, vs_def_beads_ids) # i is the line number
if func in [1, 2, 3]:
vs_params = [float(param) for param in sp_itp_line[5:7]]
elif func == 4:
Expand All @@ -425,7 +426,7 @@ def read_cg_itp_file(ns):
bead_id = int(sp_itp_line[0]) - 1
vs_def_beads_ids = [int(bid) - 1 for bid in sp_itp_line[1:5]]
func = sp_itp_line[5] # will be casted to int in the verification below (for factorizing checks)
func = vs_error_control(ns, bead_id, vs_type, func, vs_def_beads_ids, i) # i is the line number
func = vs_error_control(ns, bead_id, vs_type, func, i, vs_def_beads_ids) # i is the line number
vs_params = [float(param) for param in sp_itp_line[6:9]]
ns.cg_itp['atoms'][bead_id]['vs_type'] = vs_type
ns.cg_itp['virtual_sites4'][bead_id] = {'bead_id': bead_id, 'func': func, 'vs_def_beads_ids': vs_def_beads_ids, 'vs_params': vs_params}
Expand All @@ -436,14 +437,14 @@ def read_cg_itp_file(ns):
bead_id = int(sp_itp_line[0])-1
func = sp_itp_line[1] # will be casted to int in verification below (for factorizing checks)
# here we do the check in 2 steps, because the reading of beads_ids depends on the function
func = vs_error_control(ns, bead_id, vs_type, func, vs_def_beads_ids, i) # i is the line number
func = vs_error_control(ns, bead_id, vs_type, func, i, vs_def_beads_ids=None) # i is the line number
if func == 3:
vs_def_beads_ids = [int(sp_itp_line[2:][i])-1 for i in range(0, len(sp_itp_line[2:]), 2)]
vs_params = [float(sp_itp_line[2:][i]) for i in range(1, len(sp_itp_line[2:]), 2)]
else:
vs_def_beads_ids = [int(bid) - 1 for bid in sp_itp_line[2:]]
vs_params = None
func = vs_error_control(ns, bead_id, vs_type, func, vs_def_beads_ids, i) # i is the line number
func = vs_error_control(ns, bead_id, vs_type, func, i, vs_def_beads_ids) # i is the line number
ns.cg_itp['atoms'][bead_id]['vs_type'] = vs_type
ns.cg_itp['virtual_sitesn'][bead_id] = {'bead_id': bead_id, 'func': func, 'vs_def_beads_ids': vs_def_beads_ids, 'vs_params': vs_params}

Expand Down Expand Up @@ -817,7 +818,7 @@ def write_cg_itp_file(itp_obj, out_path_itp, print_sections=['constraint', 'bond

for i in range(len(itp_obj['atoms'])):
# if the ITP did NOT contain masses, they are set at 0 in this field during ITP reading
if itp_obj['atoms'][i]['mass'] != None:
if itp_obj['atoms'][i]['mass'] is not None:
fp.write('{0:<4} {1:>4} {6:>2} {2:>6} {3:>6} {4:<4} {5:9.5f} {7:<5.2f}\n'.format(
itp_obj['atoms'][i]['bead_id']+1, itp_obj['atoms'][i]['bead_type'],
itp_obj['atoms'][i]['residue'], itp_obj['atoms'][i]['atom'], i+1, itp_obj['atoms'][i]['charge'],
Expand Down Expand Up @@ -1374,7 +1375,7 @@ def get_AA_bonds_distrib(ns, beads_ids, grp_type, grp_nb):
print(' Ref. AA-mapped distrib. rescaled to avg', bond_avg_final, 'nm for', grp_type, grp_nb+1, '(initially', bond_avg_init, 'nm)')

# or if specific lengths were provided for constraints and/or bonds
elif ns.bonds_scaling_specific != None:
elif ns.bonds_scaling_specific is not None:

if grp_type.startswith('constraint'):
geom_id_full = 'C'+str(grp_nb+1)
Expand Down Expand Up @@ -2393,7 +2394,7 @@ def modify_mdp(mdp_filename, sim_time=None, nb_frames=1500, log_write_freq=5000,
nstxout_compressed_line = i

# adjust simulation time according to timestep
if sim_time != None:
if sim_time is not None:
if dt_line != -1 and nsteps_line != -1:
nsteps = int(sim_time*1000 / dt)
mdp_lines_in[nsteps_line] = sp_nsteps_line[0]+'= '+str(nsteps)+' ; automatically modified by Swarm-CG'
Expand Down Expand Up @@ -2664,7 +2665,7 @@ def eval_function(parameters_set, ns):
ns.total_model_eval_time += datetime.now().timestamp() - start_model_eval_ts

# if gmx sasa failed to compute, it's most likely because there were inconsistent shifts across PBC in the trajectory = failed run
if ns.sasa_cg != None:
if ns.sasa_cg is not None:

# store the distributions for each evaluation step
shutil.move('distributions.png', '../'+config.distrib_plots_all_evals_dirname+'/distributions_eval_step_'+str(ns.nb_eval)+'.png')
Expand Down

0 comments on commit afaa7b7

Please sign in to comment.