將Material Studio的xtd軌跡文件導出為xyz軌跡文件的方法
將Material Studio的xtd軌跡文件導出為xyz軌跡文件的方法
文/Sobereva @北京科音 First release: 2012-May-23
Material Studio(MS)的xtd文件包含了原子或者粗粒化模擬中的bead的軌跡信息。這是MS的私有格式,為了能將軌跡放到其它程序,比如VMD中做更靈活細致的分析,需要轉換為通用軌跡格式。xtd文件內含的實際軌跡信息實際上儲存在同目錄下的同名的.trj隱藏文件里(二進制文件),一種轉換成通用格式的方法是利用MS自帶的trj2ascii.exe程序將.trj文件內容轉換成普通文本文件,然后再寫個程序將其轉換成其它軌跡格式。但這樣步驟稍微麻煩些。
另一種做法是直接利用MS內部支持的Perl腳本,循環每一幀每一個原子或Bead,將其坐標屬性寫入到外部文本文件。比如按照常見的xyz坐標格式來寫,就可以生成xyz軌跡了,可以被VMD等程序直接載入。
在http://chembytes.wikidot.com/materials-studio上有人提供了現成的這樣的Perl腳本將原子軌跡轉換成xyz軌跡。我進一步對其進行了修改使之用起來更方便些,另外加入了對周期性體系的支持。腳本如下所示
#!perl
#**********************************************************
#* *
#* XTD2XYZ - Convert XTD files into XYZ ormat *
#* *
#**********************************************************
# Version: 0.1
# Author: Andrea Minoia
# Date: 08/09/2010
#
# Convert MS trajectory xtd file into xYZ trajectory file.
# Backup of files that are about to be overwritten is managed
# by MS. The most recent file is that with higher index number (N)
# The script has to be in the same directory of the
# structure to modify and the user has to update the
# variable $doc (line 31) according to the name of the
# file containing the trajectory.
# The xmol trajectory is stored in trj.txt file and it is not
# possible to rename the file within MS, nor it is possible to
# automatically export it as xyz or car file. You should manage
# the new trajectory manually for further use (e.g. VMD)
#
# Modificator: Sobereva (sobereva@sina.com)
# Date: 2012-May-23
# The range of the frames to be outputted can be altered by line 49 and 51
use strict;
use MaterialsScript qw(:all);
#open the multiframe trajectory structure file or die
my $doc = $Documents{"./benzene.xtd"};
if (!$doc) {die "no document";}
my $trajectory = $doc->Trajectory;
if ($trajectory->NumFrames>1) {
print "Found ".$trajectory->NumFrames." frames in the trajectory\n";
# Open new xmol trajectory file
my $xmolFile=Documents->New("trj.txt");
#get atoms in the structure
# my $atoms = $doc->Atoms;
my $atoms = $doc->DisplayRange->Atoms;
my $Natoms=@$atoms;
# loops over the frames
my $framebegin=1;
my $frameend=$trajectory->NumFrames;
# my $frameend=10;
for (my $frame=$framebegin; $frame<=$frameend; ++$frame){
$trajectory->CurrentFrame = $frame;
#write header xyz
$xmolFile->Append(sprintf "%i \n", $Natoms);
$xmolFile->Append(sprintf "%s %i \n", "Frame",$frame);
foreach my $atom (@$atoms) {
# write atom symbol and x-y-z- coordinates
$xmolFile->Append(sprintf "%s %f %f %f \n",$atom->ElementSymbol, $atom->X, $atom->Y,
$atom->Z);
}
}
#close trajectory file
$xmolFile->Close;
}
else {
print "The " . $doc->Name . " is not a multiframe trajectory file \n";
}
使用時先將這些內容復制到一個文本文件里,后綴名改為.pl。然后在MS里將這個.pl加入到項目中。要轉換哪個目錄下的xtd文件就把這pl文件挪到哪個目錄中,并且把my $doc = $Documents{"./benzene.xtd"}; 當中的文件名改成要轉換的文件名。之后,保持此腳本文件窗口處于激活狀態,選tools-scripting-debug(或者直接按F5,或者按工具欄的藍色三角按鈕)就開始對xtd文件進行轉換,轉換結束后在當前目錄下會輸出trj.txt文件。將其后綴改為.xyz之后就能被VMD等程序直接讀取了。
debug模式對于大體系、幀數較多的軌跡轉換起來頗慢,可以用tools-scripting-Run on server模式來運行,這樣轉換速度明顯快得多,trj.txt將會生成到新的目錄,當前目錄下的其它文件也會被強行復制過去一份。
此腳本默認轉換所有幀。如果想轉換指定幀數范圍,就把my $framebegin=1;和my $frameend=10;改成自定的起止幀號就行了,需要先將my $frameend=10;前面的注釋去掉。
如果是周期性體系,那么在MS當中看起來軌跡是什么樣轉換過去就是什么樣。比如,如果在display style-lattice中在某個方向上多顯示一個周期,那么轉換出的軌跡在相應方向上也會多出一倍原子。Default、In-Cell、Original的顯示模式下轉換出的原子坐標也會相應地可能有所不同。
上面的這個名為xtd2xyz腳本只能轉換全原子模擬的軌跡,如Forcite的xtd軌跡,卻不能轉換粗粒化模擬的軌跡,如Mesocite的以bead描述粒子的xtd軌跡。我將之修改成下面的xtdbead2xyz腳本,專門用來轉換粗粒化模擬的軌跡(但不能轉換全原子的),用法同前。
#!perl
# XTDbead2XYZ - Convert the XTD files containing beads into XYZ format
# Creator: Sobereva (sobereva@sina.com)
# Date: 2012-May-23
use strict;
use MaterialsScript qw(:all);
#open the multiframe trajectory structure file or die
my $doc = $Documents{"./bilayer.xtd"};
if (!$doc) {die "no document";}
my $trajectory = $doc->Trajectory;
if ($trajectory->NumFrames>1) {
print "Found ".$trajectory->NumFrames." frames in the trajectory\n";
# Open new xmol trajectory file
my $xmolFile=Documents->New("trj.txt");
#get atoms in the structure
my $Beads = $doc->DisplayRange->Beads;
my $NBeads=@$Beads;
# loops over the frames
my $framebegin=1;
my $frameend=$trajectory->NumFrames;
# my $frameend=10;
for (my $frame=$framebegin; $frame<=$frameend; ++$frame){
$trajectory->CurrentFrame = $frame;
#write header xyz
$xmolFile->Append(sprintf "%i \n", $NBeads);
$xmolFile->Append(sprintf "%s %i \n", "Frame",$frame);
foreach my $Bead (@$Beads) {
# write atom symbol and x-y-z- coordinates
$xmolFile->Append(sprintf "%s %f %f %f \n",$Bead->Name, $Bead->X, $Bead->Y, $Bead-
>Z);
}
}
#close trajectory file
$xmolFile->Close;
}
else {
print "The " . $doc->Name . " is not a multiframe trajectory file \n";
}