我有多個輸入文件(outputXXX.pdb),我希望爲其編寫輸出文件(outputXXX.gjf)。Perl - 在具有特定擴展名的所有文件中打印標題
輸入文件具有以下格式:
ATOM 1 CAY GLY X 1 -0.124 0.401 -0.153 1.00 2.67 PEP
ATOM 2 HY1 GLY X 1 -0.648 0.043 -1.064 1.00 0.00 PEP
ATOM 3 HY2 GLY X 1 -0.208 1.509 -0.145 1.00 0.00 PEP
對於每個輸入文件,我想創建其包含在一個單獨的文本文件「gaussian.txt」發現的報頭,以及抓鬥的輸出文件每個相應輸入文件的第2,6,7,8列中的內容產生如下所示:
Title
Header Line 1
Header Line 2
CAY -0.124 0.401 -0.153
HY1 -0.648 0.043 -1.064
HY2 -0.208 1.509 -0.145
下面是我當前的腳本。
#!/usr/bin/perl
use strict;
use warnings;
use File::Basename;
my $input_path = $ARGV[0];
my $output_path = $ARGV[1];
foreach my $filename (<$input_path/*.pdb>) {
my $output_file = basename($filename, '.pdb');
open(my $input_fh, "<", $filename) or die $!;
open(my $output_fh, ">", "$output_path/$output_file.gjf") or die $!;
open(my $header, "<", "gaussian.txt") or die $!;
while (<$input_fh>) {
if (/CAY/ .. /HT2/) {
print {$output_fh} $header;
print {$output_fh} join(" ", +(split)[ 2, 6, 7, 8 ]), "\n";
}
}
close($output_fh);
close($input_fh);
}
然而它寫入outputXXX.gjf文件不打印頭和不保留格式,更何況它打印GLOB?
GLOB(0x1622950)CAY -0.124 0.401 -0.153
GLOB(0x1622950)HY1 -0.648 0.043 -1.064
GLOB(0x1622950)HY2 -0.208 1.509 -0.145
有關如何改進我的腳本的任何建議,非常感謝。
UPDATE更新後的代碼如下。這很混亂(我不是很有經驗),但可以輸出以下內容。
輸出:
Title
Header Line 1
Header Line 2
CAY-0.124 0.401 -0.153
HY1-0.648 0.043 -1.064
HY20.208 1.509 -0.145
代碼:
#!/usr/bin/perl
use strict;
use warnings;
use File::Basename;
my $input_path = $ARGV[0];
my $output_path = $ARGV[1];
my $header = "gaussian.txt";
foreach my $filename (<$input_path/*.pdb>) {
my $output_file = basename ($filename, '.pdb');
open (my $header_fh, "<", $header) or die $!;
my @lines;
while (<$header_fh>) {
push (@lines, $_);
open (my $output_fh, ">", "$output_path/$output_file.gjf") or die $!;
print {$output_fh} @lines;
open (my $input_fh, "<", $filename) or die $!;
while (<$input_fh>) {
if (/CAY/../HT2/) {
print {$output_fh} join (" ", (split)[2,6,7,8]), "\n";
}
}
close($output_fh);
close($input_fh);
}
}
行'if(/CAY/../HT2/)'的用途是什麼?你的PDB文件是否總是以相同的順序包含第三列中的三個值?文件中是否有其他行要忽略?另外,你是否希望你的輸出文件名看起來像'myfile.pdb.gjf'?這就是你所編碼的內容,但用* .gjf替換* .pdb會更平常。 – Borodin