diff --git a/ensemble_md/replica_exchange_EE.py b/ensemble_md/replica_exchange_EE.py index 4f02d57..5b4ad60 100644 --- a/ensemble_md/replica_exchange_EE.py +++ b/ensemble_md/replica_exchange_EE.py @@ -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) @@ -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] diff --git a/ensemble_md/tests/test_coordinate_swap.py b/ensemble_md/tests/test_coordinate_swap.py index 19e3243..56f8476 100644 --- a/ensemble_md/tests/test_coordinate_swap.py +++ b/ensemble_md/tests/test_coordinate_swap.py @@ -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 diff --git a/ensemble_md/tests/test_gmx_parser.py b/ensemble_md/tests/test_gmx_parser.py index 1437e4e..4a36cdb 100644 --- a/ensemble_md/tests/test_gmx_parser.py +++ b/ensemble_md/tests/test_gmx_parser.py @@ -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 diff --git a/ensemble_md/utils/coordinate_swap.py b/ensemble_md/utils/coordinate_swap.py index 595bf8f..32c878e 100644 --- a/ensemble_md/utils/coordinate_swap.py +++ b/ensemble_md/utils/coordinate_swap.py @@ -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}') diff --git a/ensemble_md/utils/gmx_parser.py b/ensemble_md/utils/gmx_parser.py index 95e671b..9c41669 100644 --- a/ensemble_md/utils/gmx_parser.py +++ b/ensemble_md/utils/gmx_parser.py @@ -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}')