Misc.¶
calculate_average_com(gro_file_path, molecule_names)
¶
Calculates the average center of mass (COM) of specified molecules in a GRO file.
This function loads a molecular structure from a GRO file and calculates the average center of mass for the specified molecules. If no molecule names are provided, it calculates the COM for all atoms in the file. This can be useful for determining the central point of a group of molecules or the entire system for further analysis or manipulation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
gro_file_path |
str
|
The path to the input GRO file containing the molecular structure. |
required |
molecule_names |
list[str]
|
A list of molecule names (residue names) for which to calculate the COM. If empty, the COM of all atoms in the file is calculated. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: A 1D numpy array containing the x, y, z coordinates of the average center of mass. |
Raises:
Type | Description |
---|---|
ValueError
|
If no atoms are found for the specified molecule names in the GRO file. |
Source code in martignac/utils/misc.py
convert_pdb_to_gro(pdb_file, output_gro, box_vector)
¶
Converts a PDB file to a GROMACS GRO file with specified box dimensions.
This function utilizes MDAnalysis to load a PDB file, set the unit cell dimensions to the provided box vector, and then save the structure in GROMACS GRO format. This is useful for preparing molecular dynamics simulations where a specific box size is required.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pdb_file |
str
|
The path to the input PDB file. |
required |
output_gro |
str
|
The path where the output GRO file will be saved. |
required |
box_vector |
ndarray
|
A 1D array containing the box dimensions (x, y, z, alpha, beta, gamma) in nanometers. |
required |
Returns:
Name | Type | Description |
---|---|---|
None |
None
|
This function does not return a value but writes the converted file to |
Source code in martignac/utils/misc.py
copy_files_to(files, destination_dir)
¶
Copies a list of files to a specified destination directory.
This function iterates over a list of file paths, copying each file to the destination directory specified. If a file already exists at the destination, a message is logged, and the file is not copied again to avoid overwriting. This is useful for organizing files or preparing directories with necessary files without manual copying.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
files |
list[str]
|
A list of file paths to be copied. |
required |
destination_dir |
str
|
The directory path where the files should be copied to. |
required |
Returns:
Name | Type | Description |
---|---|---|
None |
None
|
This function does not return a value but copies files and logs actions. |
Source code in martignac/utils/misc.py
generate_top_file_for_generic_molecule(molecule_name, force_field_filenames, top_filename, num_molecules=1)
¶
Generates a GROMACS topology file for a generic molecule.
This function creates a topology file (.top) for a specified molecule using a list of force field filenames. It writes the necessary include statements for the force field files, defines the system, and specifies the number of molecules present. This is particularly useful for setting up molecular dynamics simulations where custom molecules or force fields are involved.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
molecule_name |
str
|
The name of the molecule for which the topology is being generated. |
required |
force_field_filenames |
list[str]
|
A list of filenames for the force field files to be included in the topology. |
required |
top_filename |
str
|
The path and filename where the topology file will be saved. |
required |
num_molecules |
int
|
The number of molecules of this type present in the system. Defaults to 1. |
1
|
Returns:
Name | Type | Description |
---|---|---|
None |
None
|
This function does not return a value but writes a .top file at the specified location. |
Source code in martignac/utils/misc.py
sub_template_mdp(mdp_source, template, new_entry, mdp_dest)
¶
Substitutes a template placeholder in a GROMACS MDP file with a new entry.
This function reads a GROMACS MDP (molecular dynamics parameters) file, searches for a specified template placeholder, and replaces it with a new entry. The modified MDP content is then written to a new destination file. This is useful for dynamically adjusting simulation parameters without manually editing MDP files.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mdp_source |
str
|
The path to the source MDP file containing the template placeholder. |
required |
template |
str
|
The template placeholder to be replaced. This should be a regex pattern. |
required |
new_entry |
str
|
The new entry to replace the template placeholder with. |
required |
mdp_dest |
str
|
The path where the modified MDP file will be saved. |
required |
Returns:
Name | Type | Description |
---|---|---|
None |
None
|
This function does not return a value but writes the modified MDP content to a new file. |
Source code in martignac/utils/misc.py
translate_gro_by_vector(gro_file_path, output_gro_file_path, com_diff_vector)
¶
Translates the positions of all atoms in a GRO file by a specified vector and saves the result to a new file.
This function loads a molecular structure from a GRO file, applies a translation to all atom positions by adding the specified center of mass difference vector, and then writes the modified structure to a new GRO file. This can be useful for adjusting the position of molecules within a simulation box, for example, to center a molecule or to move it to a specific location within the box.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
gro_file_path |
str
|
The path to the input GRO file containing the molecular structure to be translated. |
required |
output_gro_file_path |
str
|
The path where the translated GRO file will be saved. |
required |
com_diff_vector |
ndarray
|
A 1D numpy array containing the x, y, z components of the translation vector. |
required |
Returns:
Name | Type | Description |
---|---|---|
None |
None
|
This function does not return a value but writes the translated structure to |
Source code in martignac/utils/misc.py
update_nested_dict(d, u)
¶
Recursively updates a nested dictionary with another dictionary.
This function takes two dictionaries, d
and u
. It iterates through u
, updating d
with u
's key-value pairs.
If a value in u
is a dictionary and the corresponding key exists in d
with a dictionary as its value, this
function recursively updates the nested dictionary. Otherwise, it directly updates the value. This is useful for
merging configurations or settings where nested structures are involved without losing the nested specificity.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
d |
dict
|
The original dictionary to be updated. |
required |
u |
dict
|
The dictionary with updates to apply to |
required |
Returns:
Name | Type | Description |
---|---|---|
dict |
The updated dictionary |
Source code in martignac/utils/misc.py
zip_directories(directory_names, output_file_name)
¶
Zips multiple directories into a single archive and moves it to a specified output path.
This function takes a list of directory paths and an output file name, creates a ZIP archive of these directories, and then moves the archive to the current working directory or a specified directory. It excludes symlinks from the archive and ensures that the archive's internal structure reflects the relative paths of files to the directories being zipped. This is useful for bundling multiple directories into a single, compressed file for easy distribution or storage.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
directory_names |
list
|
A list of strings representing the paths to the directories to be zipped. |
required |
output_file_name |
str
|
The name for the output ZIP file. The '.zip' extension is appended automatically. |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The path to the final output ZIP file. |
Raises:
Type | Description |
---|---|
ValueError
|
If the |