Skip to content

Commit

Permalink
Move read_top function
Browse files Browse the repository at this point in the history
  • Loading branch information
ajfriedman22 committed Oct 7, 2024
1 parent c39916c commit 3addd41
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 67 deletions.
6 changes: 3 additions & 3 deletions ensemble_md/replica_exchange_EE.py
Original file line number Diff line number Diff line change
Expand Up @@ -1591,7 +1591,7 @@ def process_top(self):
df_top = pd.DataFrame()
for f, file_name in enumerate(self.top):
# Read file
input_file = coordinate_swap.read_top(file_name, self.resname_list[f])
input_file = gmx_parser.read_top(file_name, self.resname_list[f])

# Determine the atom names corresponding to the atom numbers
start_line, atom_name, state = coordinate_swap.get_names(input_file)
Expand Down Expand Up @@ -1623,9 +1623,9 @@ def process_top(self):
X, Y = [int(swap[0][0]), int(swap[1][0])]
lam = {X: int(swap[0][1]), Y: int(swap[1][1])}
for A, B in zip([X, Y], [Y, X]):
input_A = coordinate_swap.read_top(self.top[A], self.resname_list[A])
input_A = gmx_parser.read_top(self.top[A], self.resname_list[A])
start_line, A_name, state = coordinate_swap.get_names(input_A)
input_B = coordinate_swap.read_top(self.top[B], self.resname_list[B])
input_B = gmx_parser.read_top(self.top[B], self.resname_list[B])
start_line, B_name, state = coordinate_swap.get_names(input_B)

A_only = [x for x in A_name if x not in B_name]
Expand Down
9 changes: 0 additions & 9 deletions ensemble_md/tests/test_coordinate_swap.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,12 +380,3 @@ def test_determine_connection():
select_cmpr_df = cmpr_df[(cmpr_df['Swap A'] == 'E2F') & (cmpr_df['Swap B'] == 'D2E')]
for col in ['Anchor Atom Name A', 'Anchor Atom Name B', 'Alignment Atom A', 'Alignment Atom B', 'Angle Atom A', 'Angle Atom B', 'Missing Atom Name']: # noqa: E501
assert test_df[col].to_list()[0] == select_cmpr_df[col].to_list()[0]


def test_read_top():
top_files = ['A-B.top', 'B-C.top', 'C-D.top', 'D-E.top', 'E-F.top']
resname_list = ['A2B', 'B2C', 'C2D', 'D2E', 'E2F']

for resname, top_file in zip(resname_list, top_files):
top = coordinate_swap.read_top(f'{input_path}/coord_swap/{top_file}', resname)
assert len(top) > 0
9 changes: 9 additions & 0 deletions ensemble_md/tests/test_gmx_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,12 @@ def test_compare_MDPs():

assert result_1 == dict_1
assert result_2 == dict_2


def test_read_top():
top_files = ['A-B.top', 'B-C.top', 'C-D.top', 'D-E.top', 'E-F.top']
resname_list = ['A2B', 'B2C', 'C2D', 'D2E', 'E2F']

for resname, top_file in zip(resname_list, top_files):
top = gmx_parser.read_top(f'{input_path}/coord_swap/{top_file}', resname)
assert len(top) > 0
55 changes: 0 additions & 55 deletions ensemble_md/utils/coordinate_swap.py
Original file line number Diff line number Diff line change
Expand Up @@ -1444,58 +1444,3 @@ def determine_connection(main_only, other_only, main_name, other_name, df_top, m
df['Missing Atom Name'] = miss_sep_reformat

return df


def read_top(file_name, resname):
"""
Reads the topology to find the file containing the molecule of interest
Parameters
----------
file_name : str
Name for the topology file.
resname : str
Name of the residue of interest we are searching for.
Returns
-------
input_file : list
A list of strings containing the content of the topology file which contains the
residue of interest.
"""
input_file = open(file_name).readlines()
itp_files = []
atom_sect = True
for line in input_file:
if line == '[ atoms ]\n':
atom_sect = True
if atom_sect:
if line == '\n':
atom_sect = False
line_sep = line.split(' ')
while '' in line_sep:
line_sep.remove('')
if len(line_sep) > 4 and line_sep[3] == resname:
return input_file
if '#include' in line:
line_sep = line.split(' ')
while '' in line_sep:
line_sep.remove('')
itp_files.append(line_sep[-1].strip('\n""'))
file_dir, file_name = os.path.split(file_name)
for file in itp_files:
if os.path.exists(f'{file_dir}/{file}'):
input_file = open(f'{file_dir}/{file}').readlines()
atom_sect = False
for line in input_file:
if line == '[ atoms ]\n':
atom_sect = True
if atom_sect:
if line == '\n':
break
line_sep = line.split(' ')
while '' in line_sep:
line_sep.remove('')
if len(line_sep) > 4 and line_sep[3] == resname:
return input_file
raise Exception(f'Residue {resname} can not be found in {file_name}')
55 changes: 55 additions & 0 deletions ensemble_md/utils/gmx_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,3 +353,58 @@ def compare_MDPs(mdp_list, print_diff=False):
print()

return diff_params


def read_top(file_name, resname):
"""
Reads the topology to find the file containing the molecule of interest
Parameters
----------
file_name : str
Name for the topology file.
resname : str
Name of the residue of interest we are searching for.
Returns
-------
input_file : list
A list of strings containing the content of the topology file which contains the
residue of interest.
"""
input_file = open(file_name).readlines()
itp_files = []
atom_sect = True
for line in input_file:
if line == '[ atoms ]\n':
atom_sect = True
if atom_sect:
if line == '\n':
atom_sect = False
line_sep = line.split(' ')
while '' in line_sep:
line_sep.remove('')
if len(line_sep) > 4 and line_sep[3] == resname:
return input_file
if '#include' in line:
line_sep = line.split(' ')
while '' in line_sep:
line_sep.remove('')
itp_files.append(line_sep[-1].strip('\n""'))
file_dir, file_name = os.path.split(file_name)
for file in itp_files:
if os.path.exists(f'{file_dir}/{file}'):
input_file = open(f'{file_dir}/{file}').readlines()
atom_sect = False
for line in input_file:
if line == '[ atoms ]\n':
atom_sect = True
if atom_sect:
if line == '\n':
break
line_sep = line.split(' ')
while '' in line_sep:
line_sep.remove('')
if len(line_sep) > 4 and line_sep[3] == resname:
return input_file
raise Exception(f'Residue {resname} can not be found in {file_name}')

0 comments on commit 3addd41

Please sign in to comment.