1. here I will use the <> operator
好的,您的计划是逐行读取文件。走的时候不要忘记chomp
每一行,否则你的序列中会出现换行符。
2. Check to make sure the file only contains acgt or die
if ( <> ne [acgt] ) { die "usage: file must only contain nucleotides \n"; }
在 while 循环中,<>
运算符将读取的行放入特殊变量$_
中,除非您明确指定它 ( my $line = <>
)。
在上面的代码中,您正在从文件中读取一行并将其丢弃。您需要保存该行。
此外,ne
运算符比较两个字符串,而不是一个字符串和一个正则表达式。您将需要!~
此处的运算符(或=~
带有否定字符类的运算符[^acgt]
。如果您需要测试不区分大小写,请查看i
正则表达式匹配的标志。
3. Transcribe the DNA to RNA (Every A replaced by U, T replaced by A, C replaced by G, G replaced by C).
正如 GWW 所说,检查你的生物学。T->U 是转录的唯一步骤。您会发现tr
(transliterate) 运算符在这里很有帮助。
4. Take this transcription & break it into 3 character 'codons' starting at the first occurance of "AUG"
not sure but I'm thinking this is where I will start a %hash variables?
我会在这里使用缓冲区。while(<>)
在循环外定义一个标量。用于index
匹配“AUG”。如果你没有找到它,把最后两个基放在那个标量上(你可以用substr $line, -2, 2
它)。在循环的下一次迭代中,将.=
行附加到这两个基础上,然后再次测试“AUG”。如果您成功了,您就会知道在哪里,因此您可以标记该地点并开始翻译。
5. Take the 3 character "codons" and give them a single letter Symbol (an uppercase one-letter amino acid name)
Assign a key a value using (there are 70 possibilities here so I'm not sure where to store or how to access)
同样,正如 GWW 所说,构建一个哈希表:
%codons = ( AUG => 'M', ...)
.
然后您可以使用(例如)split
构建您正在检查的当前行的数组,一次构建三个元素的密码子,并从哈希表中获取正确的氨基酸代码。
6.If a gap is encountered a new line is started and process is repeated
not sure but we can assume that gaps are multiples of threes.
看上面。您可以使用 测试是否存在间隙exists $codons{$current_codon}
。
7. Am I approaching this the right way? Is there a Perl function that I'm overlooking that can simplify the main program?
你知道,看着上面,它似乎太复杂了。我建造了一些积木;子程序read_codon
和translate
:我认为它们极大地帮助了程序的逻辑。
我知道这是一项家庭作业,但我认为它可能会帮助您了解其他可能的方法:
use warnings; use strict;
use feature 'state';
# read_codon works by using the new [state][1] feature in Perl 5.10
# both @buffer and $handle represent 'state' on this function:
# Both permits abstracting reading codons from processing the file
# line-by-line.
# Once read_colon is called for the first time, both are initialized.
# Since $handle is a state variable, the current file handle position
# is never reset. Similarly, @buffer always holds whatever was left
# from the previous call.
# The base case is that @buffer contains less than 3bp, in which case
# we need to read a new line, remove the "\n" character,
# split it and push the resulting list to the end of the @buffer.
# If we encounter EOF on the $handle, then we have exhausted the file,
# and the @buffer as well, so we 'return' undef.
# otherwise we pick the first 3bp of the @buffer, join them into a string,
# transcribe it and return it.
sub read_codon {
my ($file) = @_;
state @buffer;
open state $handle, '<', $file or die $!;
if (@buffer < 3) {
my $new_line = scalar <$handle> or return;
chomp $new_line;
push @buffer, split //, $new_line;
}
return transcribe(
join '',
shift @buffer,
shift @buffer,
shift @buffer
);
}
sub transcribe {
my ($codon) = @_;
$codon =~ tr/T/U/;
return $codon;
}
# translate works by using the new [state][1] feature in Perl 5.10
# the $TRANSLATE state is initialized to 0
# as codons are passed to it,
# the sub updates the state according to start and stop codons.
# Since $TRANSLATE is a state variable, it is only initialized once,
# (the first time the sub is called)
# If the current state is 'translating',
# then the sub returns the appropriate amino-acid from the %codes table, if any.
# Thus this provides a logical way to the caller of this sub to determine whether
# it should print an amino-acid or not: if not, the sub will return undef.
# %codes could also be a state variable, but since it is not actually a 'state',
# it is initialized once, in a code block visible form the sub,
# but separate from the rest of the program, since it is 'private' to the sub
{
our %codes = (
AUG => 'M',
...
);
sub translate {
my ($codon) = @_ or return;
state $TRANSLATE = 0;
$TRANSLATE = 1 if $codon =~ m/AUG/i;
$TRANSLATE = 0 if $codon =~ m/U(AA|GA|AG)/i;
return $codes{$codon} if $TRANSLATE;
}
}