def main(): options = options_desc.parse_args(sys.argv)[0] if (options.common_filename): options.molecule = options.common_filename + ".pdb" options.presampling = options.common_filename + ".trr" options.internals = options.common_filename + ".int" options.grompp = options.common_filename + ".mdp" options.topology = options.common_filename + ".top" options.index = options.common_filename + ".ndx" print("Options:\n%s\n" % pformat(eval(str(options)))) assert (path.exists(options.molecule)) assert (path.exists(options.presampling)) assert (path.exists(options.internals)) assert (path.exists(options.grompp)) assert (path.exists(options.topology)) #TODO: what if there is no index-file? (make_ndx) assert (path.exists(options.index)) assert ('moi' in gromacs.read_index_file( options.index)), "group 'MOI' should be defined in index file" # checks e.g. if the mdp-file looks good mdp_options = gromacs.read_mdp_file(options.grompp) temperatures = [ ref_t for ref_t in re.findall("[0-9]+", mdp_options["ref_t"]) ] assert (len(set(temperatures)) == 1 ), "temperature definition in mdp file is ambiguous" temperature = temperatures[0] # get sampling temperature from mdp file if (int(temperature) > 310): if not (userinput( "Your sampling temperature is set to %s K. Continue?" % temperature, "bool")): sys.exit("Quit by user.") # options we can fix mdp_options_dirty = False #if set, a new mdp-file will be written # the value of the following options need to be fixed critical_mdp_options = { "dihre": "yes", "dihre_fc": "1", "disre": "simple", "disre_fc": "1", "gen_temp": temperature } for (k, v) in critical_mdp_options.items(): if (mdp_options.has_key(k) and mdp_options[k].strip() != v): print "Error. I do not want to use '%s' for option '%s' ('%s' required). Please fix your mdp file." % ( mdp_options[k].strip(), k, v) sys.exit("Quitting.") else: mdp_options[k] = v mdp_options_dirty = True # the value of the following options does not matter, but they should be there noncritical_mdp_options = { "tcoupl": "no", "pcoupl": "no", "gen_vel": "no", "gen_seed": "-1" } for (k, v) in noncritical_mdp_options.items(): if not (mdp_options.has_key(k)): mdp_options[k] = v mdp_options_dirty = True a = mdp_options.has_key("energygrps") and "moi" not in [ str(egrp) for egrp in re.findall('[\S]+', mdp_options["energygrps"]) ] b = not (mdp_options.has_key("energygrps")) if (a or b): if not (userinput( "'MOI' is not defined as an energy group in your mdp file. Maybe you have forgotten to define proper 'energygrps'. Continue?", "bool")): sys.exit("Quit by user.") a, b = mdp_options.has_key("nstxout"), mdp_options.has_key("nstenergy") if (a and not b): mdp_options["nstenergy"] = mdp_options["nstxout"] mdp_options_dirty = True elif (b and not a): mdp_options["nstxout"] = mdp_options["nstenergy"] mdp_options_dirty = True elif (b and a): assert (mdp_options["nstxout"] == mdp_options["nstenergy"] ), "nstxout should equal nstenergy" if (int(mdp_options["nsteps"]) > 1e6): msg = "Number of MD-steps?" mdp_options["nsteps"] = str( userinput(msg, "int", default=int(mdp_options["nsteps"]))) # create a fixed mdp-file if (mdp_options_dirty): print("Creating copy of mdp-file and adding missing options.") out_fn = options.grompp.rsplit(".", 1)[0] + "_fixed.mdp" f = open(out_fn, "w") # append f.write("; Generated by zgf_create_pool\n") for i in sorted(mdp_options.items()): f.write("%s = %s\n" % i) f.write("; EOF\n") f.close() options.grompp = out_fn # check if subsampling is reasonable if (os.path.getsize(options.presampling) > 100e6): # 100MB print("Presampling trajectory is large") trr = TrrFile(options.presampling) dt = trr.first_frame.next().t - trr.first_frame.t trr.close() print("Presampling timestep is %.2f ps" % dt) if (dt < 10): # picoseconds #TODO: maybe calculate subsampling factor individually, or ask? msg = "Subsample presampling trajectory by a tenth?" if (userinput(msg, "bool")): out_fn = options.presampling.rsplit(".", 1)[0] + "_tenth.trr" cmd = [ "trjconv", "-f", options.presampling, "-o", out_fn, "-skip", "10" ] check_call(cmd) options.presampling = out_fn # balance linears if (options.balance_linears): print("Balance Linears") old_converter = Converter(options.internals) print("Loading presampling....") frames = old_converter.read_trajectory(options.presampling) new_coord_list = [] for c in old_converter: if (not isinstance(c, LinearCoordinate)): new_coord_list.append(c) continue # we do not work on other Coordinate-Types #TODO: is this a good way to determine new_weight and new_offset??? new_weight = c.weight / sqrt(2 * frames.var().getcoord(c)) new_offset = c.offset + frames.mean().getcoord(c) new_coord = LinearCoordinate(*c.atoms, label=c.label, weight=new_weight, offset=new_offset) new_coord_list.append(new_coord) new_converter = Converter(coord_list=new_coord_list) assert (old_converter.filename.endswith(".int")) options.internals = old_converter.filename[:-4] + "_balanced.int" print("Writing balanced Converter to: " + options.internals) f = open(options.internals, "w") f.write(new_converter.serialize()) f.close() assert (len(Converter(options.internals)) == len(new_coord_list) ) #try parsing # Finally: Create root-node and pool pool = Pool() if (len(pool) != 0): print("ERROR: A pool already exists here.") sys.exit(1) pool.int_fn = options.internals pool.mdp_fn = options.grompp pool.top_fn = options.topology pool.ndx_fn = options.index pool.temperature = int(temperature) pool.gr_threshold = options.gr_threshold pool.gr_chains = options.gr_chains pool.alpha = None pool.save() # save pool for the first time... # ... then we can save the first node... node0 = Node() node0.state = "refined" node0.save() # also creates the node directory ... needed for symlink os.symlink(os.path.relpath(options.presampling, node0.dir), node0.trr_fn) os.symlink(os.path.relpath(options.molecule, node0.dir), node0.pdb_fn) pool.root_name = node0.name pool.save() #... now we have to save the pool again. if (not path.exists("analysis")): os.mkdir("analysis")
def converter(self): #TODO maybe cache instance if (hasattr(self, "int_fn")): return (Converter(self.int_fn)) return (None)
def main(): usage_txt = "\nTakes trr-trajectory and internals-definition and outputs timeseries as npz- or mat-file.\nOutput-filetype is decided based on given filename-extension.\n\n %prog [options] <trr-input-file> <int-file> <output-file>" parser = OptionParser(usage=usage_txt, version="%prog 0.1") parser.add_option("-d", "--degrees", action="store_true", default=False, help="save dihedrals in [deg]") (options, args) = parser.parse_args() if (len(args) != 3): parser.error("incorrect number of arguments") if (options.degrees): print("\nDihedral angles will be saved in [deg].\n") else: print("\nDihedral angles will be saved in [rad].\n") trr_fn = args[0] internals_fn = args[1] out_fn = args[2] converter = Converter(internals_fn) all_frames_int = converter.read_trajectory(trr_fn) print("loaded trr-file with %d frames" % len(all_frames_int)) if (options.degrees): all_frames_int.dihedral_array = np.degrees( all_frames_int.dihedral_array) dih_labels = [c.label for c in converter.dihedrals] lin_labels = [c.label for c in converter.linears] if (out_fn.endswith(".npz")): print("Writing NumPy file: " + out_fn) if (all_frames_int.has_dihedrals and all_frames_int.has_linears): np.savez(out_fn, linears=all_frames_int.linear_array, linear_labels=lin_labels, dihedrals=all_frames_int.dihedral_array, dihedral_labels=dih_labels) elif (all_frames_int.has_dihedrals): np.savez(out_fn, dihedrals=all_frames_int.dihedral_array, dihedral_labels=dih_labels) else: np.savez(out_fn, linears=all_frames_int.linear_array, linear_labels=lin_labels) elif (out_fn.endswith(".mat")): print("Writing Matlab file: " + out_fn) if (all_frames_int.has_dihedrals and all_frames_int.has_linears): savemat( out_fn, { 'linears': all_frames_int.linear_array, 'dihedrals': all_frames_int.dihedral_array }) elif (all_frames_int.has_dihedrals): savemat(out_fn, {'dihedrals': all_frames_int.dihedral_array}) else: savemat(out_fn, {'linears': all_frames_int.linear_array}) else: raise (Exception("Unkown output filetype: " + out_fn))