From fbb127ce0ae270a478b0a1da15bd24049d067313 Mon Sep 17 00:00:00 2001 From: CalCraven Date: Mon, 6 Jan 2025 14:26:40 -0600 Subject: [PATCH 1/3] initialize mol2 function definition --- gmso/formats/mol2.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/gmso/formats/mol2.py b/gmso/formats/mol2.py index 91e3b5ffd..89caceb70 100644 --- a/gmso/formats/mol2.py +++ b/gmso/formats/mol2.py @@ -87,6 +87,8 @@ def read_mol2(filename, site_type="atom", verbose=False): # TODO: read in parameters to correct attribute as well. This can be saved in various rti sections. return topology +@saves_as(".mol2") +def write_mol2() def _parse_lj(top, section, verbose): """Parse atom of lj style from mol2 file.""" From cf1786f6c1c351060c6206d37936482317b71bbb Mon Sep 17 00:00:00 2001 From: CalCraven Date: Thu, 9 Jan 2025 16:25:57 -0600 Subject: [PATCH 2/3] Add more structure for mol2 writer --- gmso/formats/mol2.py | 95 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 93 insertions(+), 2 deletions(-) diff --git a/gmso/formats/mol2.py b/gmso/formats/mol2.py index 89caceb70..aa050b429 100644 --- a/gmso/formats/mol2.py +++ b/gmso/formats/mol2.py @@ -3,13 +3,14 @@ import itertools import warnings from pathlib import Path +import os import unyt as u from gmso import Atom, Bond, Box, Topology from gmso.abc.abstract_site import Molecule, Residue from gmso.core.element import element_by_name, element_by_symbol -from gmso.formats.formats_registry import loads_as +from gmso.formats.formats_registry import loads_as, saves_as @loads_as(".mol2") @@ -88,7 +89,69 @@ def read_mol2(filename, site_type="atom", verbose=False): return topology @saves_as(".mol2") -def write_mol2() +def write_mol2(top, filename): + """Write a gmso.Topology to a TRIPOS mol2 file. + + Creates a mol2 file from a Topology structure. This will generate the following + sections in the mol2 file. + - @ATOM + - @BOND + ??- @CRYSIN?? + - @FF_PBC + - @MOLECULE + + Parameters + ---------- + filename : string + path to the file where the mol2 file will be saved + overwrite : bool, optional, default=False + If True, overwrite the file if it already exists. Raise error if False and + overwriting is attempted. + site_type : string ('atom' or 'lj'), default='atom' + tells the reader to consider the elements saved in the mol2 file, and + if the type is 'lj', to not try to identify the element of the site, + instead saving the site name. + verbose : bool, optional, default=False + If True, raise warnings for any assumptions made during the parsing. + + Returns + ------- + None + """ + # Massaging/type checking + + # Loop through topology and write each section + with open(filename, "w") as f: + + + #ATOM top.sites + #@ATOM + # 1 C -0.7600 1.1691 -0.0005 C.ar 1 BENZENE 0.000 + f.writelines("@ATOM\n") + for site in top.sites: + _write_site_info(site, f) + + + #BOND top.bonds + # bond_id origin_atom_id target_atom_id bond_type + for bond in top.bonds: + _write_bond_info(bond, f) + + #FF_PBC + #format_version_number pbc_type pbc_x_coord_min + # pbc_y_coord_min pbc_z_coord_min pbc_x_coord_max + # pbc_y_coord_max pbc_z_coord_max solvent_type + # num_solvent_shells reorient_molecule_flag + # status_flag apply_ + if top.box: + _write_box_info(top.box, f) + + #MOLECULE + # mol_name + # num_atoms [num_bonds [num_subst [num_feat[num_sets]]]] + # mol_type + # charge_type + _write_molecule_info(top, f) def _parse_lj(top, section, verbose): """Parse atom of lj style from mol2 file.""" @@ -197,3 +260,31 @@ def _parse_box(top, section, verbose): def _parse_molecule(top, section, verbose): """Parse molecule information from the mol2 file.""" top.label = str(section[0].strip()) + +def _write_site_info(site, f, index=1): + """Write site information to ATOM section.""" + # TODO: Create rules to make sure nothing is too long, so that it cuts off. + lineList = [ + str(index), + site.element.symbol, + *map(str,site.position.value), + site.atom_type.name if site.atom_type else site.name, + str(site.molecule.number), + site.molecule.name, + str(site.charge) if site.charge else "0.00" + ] + formattedStr = "\t".join(lineList)+"\n" + f.writelines(formattedStr) + index+=1 + #ATOM top.sites + #@ATOM + # 1 C -0.7600 1.1691 -0.0005 C.ar 1 BENZENE 0.000 + +def _write_bond_info(bond, f): + pass + +def _write_molecule_info(top, f): + pass + +def _write_box_info(box, f): + pass From 518c08ac367fb3aa8742b65dd739fc11f4b9cd48 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 9 Jan 2025 22:26:15 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- gmso/formats/mol2.py | 58 +++++++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 28 deletions(-) diff --git a/gmso/formats/mol2.py b/gmso/formats/mol2.py index aa050b429..b00a159d5 100644 --- a/gmso/formats/mol2.py +++ b/gmso/formats/mol2.py @@ -3,7 +3,6 @@ import itertools import warnings from pathlib import Path -import os import unyt as u @@ -88,9 +87,10 @@ def read_mol2(filename, site_type="atom", verbose=False): # TODO: read in parameters to correct attribute as well. This can be saved in various rti sections. return topology + @saves_as(".mol2") def write_mol2(top, filename): - """Write a gmso.Topology to a TRIPOS mol2 file. + """Write a gmso.Topology to a TRIPOS mol2 file. Creates a mol2 file from a Topology structure. This will generate the following sections in the mol2 file. @@ -105,7 +105,7 @@ def write_mol2(top, filename): filename : string path to the file where the mol2 file will be saved overwrite : bool, optional, default=False - If True, overwrite the file if it already exists. Raise error if False and + If True, overwrite the file if it already exists. Raise error if False and overwriting is attempted. site_type : string ('atom' or 'lj'), default='atom' tells the reader to consider the elements saved in the mol2 file, and @@ -122,37 +122,35 @@ def write_mol2(top, filename): # Loop through topology and write each section with open(filename, "w") as f: - - - #ATOM top.sites - #@ATOM + # ATOM top.sites + # @ATOM # 1 C -0.7600 1.1691 -0.0005 C.ar 1 BENZENE 0.000 f.writelines("@ATOM\n") for site in top.sites: _write_site_info(site, f) - - #BOND top.bonds - # bond_id origin_atom_id target_atom_id bond_type + # BOND top.bonds + # bond_id origin_atom_id target_atom_id bond_type for bond in top.bonds: _write_bond_info(bond, f) - #FF_PBC - #format_version_number pbc_type pbc_x_coord_min - # pbc_y_coord_min pbc_z_coord_min pbc_x_coord_max - # pbc_y_coord_max pbc_z_coord_max solvent_type - # num_solvent_shells reorient_molecule_flag - # status_flag apply_ + # FF_PBC + # format_version_number pbc_type pbc_x_coord_min + # pbc_y_coord_min pbc_z_coord_min pbc_x_coord_max + # pbc_y_coord_max pbc_z_coord_max solvent_type + # num_solvent_shells reorient_molecule_flag + # status_flag apply_ if top.box: _write_box_info(top.box, f) - #MOLECULE + # MOLECULE # mol_name # num_atoms [num_bonds [num_subst [num_feat[num_sets]]]] # mol_type # charge_type _write_molecule_info(top, f) + def _parse_lj(top, section, verbose): """Parse atom of lj style from mol2 file.""" for line in section: @@ -261,30 +259,34 @@ def _parse_molecule(top, section, verbose): """Parse molecule information from the mol2 file.""" top.label = str(section[0].strip()) + def _write_site_info(site, f, index=1): """Write site information to ATOM section.""" # TODO: Create rules to make sure nothing is too long, so that it cuts off. lineList = [ - str(index), - site.element.symbol, - *map(str,site.position.value), - site.atom_type.name if site.atom_type else site.name, - str(site.molecule.number), - site.molecule.name, - str(site.charge) if site.charge else "0.00" + str(index), + site.element.symbol, + *map(str, site.position.value), + site.atom_type.name if site.atom_type else site.name, + str(site.molecule.number), + site.molecule.name, + str(site.charge) if site.charge else "0.00", ] - formattedStr = "\t".join(lineList)+"\n" + formattedStr = "\t".join(lineList) + "\n" f.writelines(formattedStr) - index+=1 - #ATOM top.sites - #@ATOM + index += 1 + # ATOM top.sites + # @ATOM # 1 C -0.7600 1.1691 -0.0005 C.ar 1 BENZENE 0.000 + def _write_bond_info(bond, f): pass + def _write_molecule_info(top, f): pass + def _write_box_info(box, f): pass