Files
analysis_pdb/process_trajectory.py
2024-01-03 11:17:37 +08:00

83 lines
4.5 KiB
Python
Executable File

#!/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
'''