私はPerlのイントロクラスの学生で、原子に関するデータを分析する小さな(しかしトリッキーな)プログラムを作成するための私のアプローチに関する提案とフィードバックを探しています。私の教授はフォーラムを奨励しています。私はPerlのサブまたはモジュール(Bioperlを含む)に精通していないので、あなたの提案やコードから理解して学ぶことができるように、適切な「初心者レベル」に応答を制限してください(「マジック」も制限してください)。
プログラムの要件は次のとおりです。
コマンドラインからファイル(Atomに関するデータを含む)を読み取り、アトムレコードの配列を作成します(改行ごとに1つのレコード/アトム)。各レコードについて、プログラムは以下を保存する必要があります。
•原子のシリアル番号(列7〜11 )•原子
が属するアミノ酸の3文字の名前(列18〜20)
•原子の3つの座標(x、y、z)(列31〜54)
•原子の1文字または2文字の要素名(例:C、O、N、Na)(列77-78)次の3つのコマンドのいずれかを要求します:freq、length、density d(dはいくつかの数値です):
•freq-ファイル内の各タイプの原子の数(例:窒素、ナトリウムなどは次のように表示されます:N:918 S:23
•長さ-座標間の距離
•密度d(dは数値) -プログラムは、計算を保存するファイルの名前を要求し、その原子と他のすべての原子との間の距離を含みます。その距離が数値d以下の場合、原子の数のカウントをインクリメントします。そのカウントがファイルにゼロでない限り、その距離内にあります。出力は次のようになります:
1:5
2:3
3:6
...(非常に大きなファイル)そして終了すると閉じます。
以下のコードで私が書いた(そして書く必要がある)ものについてのフィードバックを探しています。私は特に私の潜水艦を書くことにアプローチする方法についてのフィードバックに感謝します。下部にサンプル入力データを含めました。
私が見ているプログラムの構造と関数の説明:
$^W = 1; # turn on warnings
use strict; # behave!
my @fields;
my @recs;
while ( <DATA> ) {
chomp;
@fields = split(/\s+/);
push @recs, makeRecord(@fields);
}
for (my $i = 0; $i < @recs; $i++) {
printRec( $recs[$i] );
}
my %command_table = (
freq => \&freq,
length => \&length,
density => \&density,
help => \&help,
quit => \&quit
);
print "Enter a command: ";
while ( <STDIN> ) {
chomp;
my @line = split( /\s+/);
my $command = shift @line;
if ($command !~ /^freq$|^density$|length|^help$|^quit$/ ) {
print "Command must be: freq, length, density or quit\n";
}
else {
$command_table{$command}->();
}
print "Enter a command: ";
}
sub makeRecord
# Read the entire line and make records from the lines that contain the
# word ATOM or HETATM in the first column. Not sure how to do this:
{
my %record =
(
serialnumber => shift,
aminoacid => shift,
coordinates => shift,
element => [ @_ ]
);
return\%record;
}
sub freq
# take an array of atom records, return a hash whose keys are
# distinct atom names and whose values are the frequences of
# these atoms in the array.
sub length
# take an array of atom records and return the max distance
# between all pairs of atoms in that array. My instructor
# advised this would be constructed as a for loop inside a for loop.
sub density
# take an array of atom records and a number d and will return a
# hash whose keys are atom serial numbers and whose values are
# the number of atoms within that distance from the atom with that
# serial number.
sub help
{
print "To use this program, type either\n",
"freq\n",
"length\n",
"density followed by a number, d,\n",
"help\n",
"quit\n";
}
sub quit
{
exit 0;
}
# truncating for testing purposes. Actual data is aprox. 100 columns
# and starts with ATOM or HETATM.
__DATA__
ATOM 4743 CG GLN A 704 19.896 32.017 54.717 1.00 66.44 C
ATOM 4744 CD GLN A 704 19.589 30.757 55.525 1.00 73.28 C
ATOM 4745 OE1 GLN A 704 18.801 29.892 55.098 1.00 75.91 O