From 4257c2041aa66582235d0cb0c01a69190e6e432b Mon Sep 17 00:00:00 2001 From: hotwa Date: Tue, 19 Dec 2023 13:48:50 +0800 Subject: [PATCH] add test func on python --- process_trajectory.py | 83 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 process_trajectory.py diff --git a/process_trajectory.py b/process_trajectory.py new file mode 100644 index 0000000..b721575 --- /dev/null +++ b/process_trajectory.py @@ -0,0 +1,83 @@ +#!/usr/bin/env python +# -*- encoding: utf-8 -*- +''' +@file :process_trajectory.py +@Description: : +@Date :2023/12/11 11:10:24 +@Author :lyzeng +@Email :pylyzeng@gmail.com +@version :1.0 +''' + + +from runner import SimulationRunner +import subprocess + +# 新增处理轨迹的方法 +def process_trajectory(temp_folder, output_folder, extract_interval): + # 根据提供的脚本逻辑读取和保存索引文件 + ndx_dict = SimulationRunner.read_ndx_file(f'{runner_folder}/index.ndx') + + # 根据索引文件内容决定如何处理轨迹 + if any(key.startswith("LG") for key in ndx_dict): + # 处理含有LG组的情况 + new_ndx_dict = {key: value for key, value in ndx_dict.items() if key.startswith("LG") or key in ["Protein", "Protein_LIG"]} + SimulationRunner.save_ndx_file(f"{temp_folder}/tarj_show.ndx", new_ndx_dict) + # 构建处理轨迹的命令 + command_1 = f'echo "Protein_LIG" | gmx trjconv -dt {extract_interval} -s {tpr_file} -f {xtc_file} -n {temp_folder}/tarj_show.ndx -pbc mol -o {temp_folder}/temp.xtc' + command_2 = f'echo "Protein\nProtein\nProtein_LIG" | gmx trjconv -s {tpr_file} -f {temp_folder}/temp.xtc -n {temp_folder}/tarj_show.ndx -center -fit rot+trans -o {output_folder}/traj_show.xtc' + command_3 = f'echo "Protein\nProtein\nProtein_LIG" | gmx trjconv -s {tpr_file} -f {temp_folder}/temp.xtc -n {temp_folder}/tarj_show.ndx -center -fit rot+trans -b 0 -e 0 -o {output_folder}/tarj_show.pdb' + else: + # 处理只含有蛋白质组的情况 + new_ndx_dict = {key: value for key, value in ndx_dict.items() if key in ["Protein"]} + SimulationRunner.save_ndx_file(f"{temp_folder}/tarj_show.ndx", new_ndx_dict) + # 构建处理轨迹的命令 + command_1 = f'echo "Protein" | gmx trjconv -dt {extract_interval} -s {tpr_file} -f {xtc_file} -n {temp_folder}/tarj_show.ndx -pbc mol -o {temp_folder}/temp.xtc' + command_2 = f'echo "Protein\nProtein\nProtein" | gmx trjconv -s {tpr_file} -f {temp_folder}/temp.xtc -n {temp_folder}/tarj_show.ndx -center -fit rot+trans -o {output_folder}/traj_show.xtc' + command_3 = f'echo "Protein\nProtein\nProtein" | gmx trjconv -s {tpr_file} -f {temp_folder}/temp.xtc -n {temp_folder}/tarj_show.ndx -center -fit rot+trans -b 0 -e 0 -o {output_folder}/tarj_show.pdb' + subprocess.run(command_1, shell=True, check=True) + subprocess.run(command_2, shell=True, check=True) + subprocess.run(command_3, shell=True, check=True) + +''' +def read_ndx_file(filename): + ndx_dict = {} + current_section = None + + with open(filename, 'r') as file: + for line in file: + line = line.strip() + + if line.startswith('[') and line.endswith(']'): + current_section = line[1:-1].strip() + ndx_dict[current_section] = [] + else: + if current_section is not None: + ndx_dict[current_section].extend(map(int, line.split())) + + return ndx_dict +def save_ndx_file(filename, ndx_dict): + with open(filename, 'w') as file: + for section, content in ndx_dict.items(): + file.write(f"[ {section} ]\n") + for i, num in enumerate(content, 1): + file.write(str(num)) + if i % 15 == 0: + file.write("\n") + else: + file.write("\t") + file.write("\n") +ndx_dict = read_ndx_file('index.ndx') +new_ndx_dict = {key: value for key, value in ndx_dict.items() if key in ["Protein"]} +temp_folder = 'traj_tmp' +extract_interval = 100 +tpr_file = 'md.tpr' +xtc_file = 'md.xtc' +output_folder = 'extra_traj' +save_ndx_file(f"{temp_folder}/tarj_show.ndx", new_ndx_dict) +!echo "Protein" | gmx_mpi trjconv -dt {extract_interval} -s {tpr_file} -f {xtc_file} -n {temp_folder}/tarj_show.ndx -pbc mol -o {temp_folder}/temp.xtc +!echo "Protein\nProtein\nProtein" | gmx_mpi trjconv -s {tpr_file} -f {temp_folder}/temp.xtc -n {temp_folder}/tarj_show.ndx -center -fit rot+trans -o {output_folder}/traj_show.xtc +# - echo "Protein\nProtein\nProtein" | gmx_mpi trjconv -s {tpr_file} -f md.xtc -n {temp_folder}/tarj_show.ndx -center -fit rot+trans -o {output_folder}/traj_show.xtc +!echo "Protein\nProtein\nProtein" | gmx_mpi trjconv -s {tpr_file} -f {temp_folder}/temp.xtc -n {temp_folder}/tarj_show.ndx -center -fit rot+trans -b 0 -e 0 -o {output_folder}/tarj_show.pdb +# - !echo "Protein\nProtein\nProtein" | gmx_mpi trjconv -s {tpr_file} -f md.xtc -n {temp_folder}/tarj_show.ndx -center -fit rot+trans -b 0 -e 0 -o {output_folder}/tarj_show.pdb +''' \ No newline at end of file