Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Readxyz #266

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 58 additions & 31 deletions molSimplify/Classes/mol3D.py
Original file line number Diff line number Diff line change
Expand Up @@ -2826,7 +2826,7 @@
ss += "%s \t%f\t%f\t%f\n" % (atom.sym, xyz[0], xyz[1], xyz[2])
return (ss)

def readfromxyz(self, filename: str, ligand_unique_id=False, read_final_optim_step=False):
def readfromxyz(self, filename: str, ligand_unique_id=False, read_final_optim_step=False, readstring=False):
"""
Read XYZ into a mol3D class instance.

Expand All @@ -2841,41 +2841,68 @@
read_final_optim_step : boolean
if there are multiple geometries in the xyz file
(after an optimization run) use only the last one
readstring : boolean
Flag for deciding whether a string of xyz file is being passed as the filename
"""

globs = globalvars()
amassdict = globs.amass()
self.graph = []
self.xyzfile = filename
with open(filename, 'r') as f:
s = f.read().splitlines()
try:
atom_count = int(s[0])
except ValueError:
atom_count = 0
start = 2
if read_final_optim_step:
start = len(s) - int(s[0])
for line in s[start:start+atom_count]:
line_split = line.split()
# If the split line has more than 4 elements, only elements 0 through 3 will be used.
# this means that it should work with any XYZ file that also stores something like mulliken charge
# Next, this looks for unique atom IDs in files
if len(line_split) > 0:
lm = re.search(r'\d+$', line_split[0])
# if the string ends in digits m will be a Match object, or None otherwise.
if line_split[0] in list(amassdict.keys()) or ligand_unique_id:
atom = atom3D(line_split[0], [float(line_split[1]), float(
line_split[2]), float(line_split[3])])
elif lm is not None:
print(line_split)
symb = re.sub(r'\d+', '', line_split[0])
atom = atom3D(symb, [float(line_split[1]), float(line_split[2]), float(line_split[3])],
name=line_split[0])
else:
print('cannot find atom type')
sys.exit()
self.addAtom(atom)
if not readstring:
with open(filename, 'r') as f:
s = f.read().splitlines()
try:
atom_count = int(s[0])
except ValueError:
atom_count = 0

Check warning on line 2858 in molSimplify/Classes/mol3D.py

View check run for this annotation

Codecov / codecov/patch

molSimplify/Classes/mol3D.py#L2857-L2858

Added lines #L2857 - L2858 were not covered by tests
start = 2
if read_final_optim_step:
start = len(s) - int(s[0])
for line in s[start:start+atom_count]:
line_split = line.split()
# If the split line has more than 4 elements, only elements 0 through 3 will be used.
# this means that it should work with any XYZ file that also stores something like mulliken charge
# Next, this looks for unique atom IDs in files
if len(line_split) > 0:
lm = re.search(r'\d+$', line_split[0])
# if the string ends in digits m will be a Match object, or None otherwise.
if line_split[0] in list(amassdict.keys()) or ligand_unique_id:
atom = atom3D(line_split[0], [float(line_split[1]), float(
line_split[2]), float(line_split[3])])
elif lm is not None:
print(line_split)
symb = re.sub(r'\d+', '', line_split[0])
atom = atom3D(symb, [float(line_split[1]), float(line_split[2]), float(line_split[3])],

Check warning on line 2876 in molSimplify/Classes/mol3D.py

View check run for this annotation

Codecov / codecov/patch

molSimplify/Classes/mol3D.py#L2873-L2876

Added lines #L2873 - L2876 were not covered by tests
name=line_split[0])
else:
print('cannot find atom type')
sys.exit()

Check warning on line 2880 in molSimplify/Classes/mol3D.py

View check run for this annotation

Codecov / codecov/patch

molSimplify/Classes/mol3D.py#L2879-L2880

Added lines #L2879 - L2880 were not covered by tests
self.addAtom(atom)
else:
s = filename.split('\n')
try:
s.remove('')
except ValueError:

Check notice

Code scanning / CodeQL

Empty except Note

'except' clause does nothing but pass and there is no explanatory comment.
pass
s = [str(val) + '\n' for val in s]
for line in s[0:]:
line_split = line.split()
if len(line_split) == 4 and line_split[0]:

Check warning on line 2891 in molSimplify/Classes/mol3D.py

View check run for this annotation

Codecov / codecov/patch

molSimplify/Classes/mol3D.py#L2883-L2891

Added lines #L2883 - L2891 were not covered by tests
# this looks for unique atom IDs in files
lm = re.search(r'\d+$', line_split[0])

Check warning on line 2893 in molSimplify/Classes/mol3D.py

View check run for this annotation

Codecov / codecov/patch

molSimplify/Classes/mol3D.py#L2893

Added line #L2893 was not covered by tests
# if the string ends in digits m will be a Match object, or None otherwise.
if lm is not None:
symb = re.sub(r'\d+', '', line_split[0])
atom = atom3D(symb, [float(line_split[1]), float(line_split[2]), float(line_split[3])],

Check warning on line 2897 in molSimplify/Classes/mol3D.py

View check run for this annotation

Codecov / codecov/patch

molSimplify/Classes/mol3D.py#L2895-L2897

Added lines #L2895 - L2897 were not covered by tests
name=line_split[0])
elif line_split[0] in list(amassdict.keys()):
atom = atom3D(line_split[0], [float(line_split[1]), float(

Check warning on line 2900 in molSimplify/Classes/mol3D.py

View check run for this annotation

Codecov / codecov/patch

molSimplify/Classes/mol3D.py#L2899-L2900

Added lines #L2899 - L2900 were not covered by tests
line_split[2]), float(line_split[3])])
else:
print('cannot find atom type')
sys.exit()
self.addAtom(atom)

Check warning on line 2905 in molSimplify/Classes/mol3D.py

View check run for this annotation

Codecov / codecov/patch

molSimplify/Classes/mol3D.py#L2903-L2905

Added lines #L2903 - L2905 were not covered by tests

def readfrommol(self, filename):
"""
Expand Down Expand Up @@ -3051,7 +3078,7 @@
String of XYZ file.
"""

# print('!!!!', filename)
# print('Deprecated: use readfromxyz(readstring=True)
globs = globalvars()
amassdict = globs.amass()
self.graph = []
Expand Down
Loading