模拟 RNA 合成的 Perl 程序

标签 perl hash bioinformatics

寻找有关如何处理我的 Perl 编程作业以编写 RNA 合成程序的建议。我总结并概述了下面的程序。具体来说,我正在寻找对以下 block 的反馈(我将编号以方便引用)。我已经阅读了 Andrew Johnson 的 Elements of Programming with Perl (好书)的第 6 章。我还阅读了 perlfunc 和 perlop pod-pages,没有任何关于从哪里开始的内容。

程序说明:程序应从命令行读取输入文件,将其翻译成 RNA,然后将 RNA 转录成大写的单字母氨基酸名称序列。

  1. 接受以命令行​​命名的文件

    here I will use the <> operator

  2. 检查以确保文件仅包含 acgt 或 die

    if ( <> ne [acgt] ) { die "usage: file must only contain nucleotides \n"; }  
    
  3. 将DNA转录成RNA(每个A被U替换,T被A替换,C被G替换,G被C替换)

    not sure how to do this

  4. 将此转录本并从“AUG”的第一次出现开始将其分解为 3 个字符“密码子”

    not sure but I'm thinking this is where I will start a %hash variables?

  5. 取 3 个字符“密码子”并给它们一个单字母符号(大写的单字母氨基酸名称)

    Assign a key a value using (there are 70 possibilities here so I'm not sure where to store or how to access)

  6. 如果遇到间隙,则开始新行并重复处理

    not sure but we can assume that gaps are multiples of threes.

  7. 我的方法是否正确?是否有一个我忽略的 Perl 函数可以简化主程序?

注意

必须是独立程序(密码子名称和符号的存储值)。

当程序读取一个没有符号的密码子时,这是 RNA 中的一个缺口,它应该开始一个新的输出行,并从下一次出现的“AUG”开始。为简单起见,我们可以假设间隙总是三的倍数。

在我花任何额外的时间进行研究之前,我希望得到确认,我正在采取正确的方法。感谢您花时间阅读并分享您的专业知识!

最佳答案

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 (音译)运算符在这里很有帮助。


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)。在循环的下一次迭代中(使用 .= )将行添加到这两个基础,然后 then 再次测试“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?

你知道,看看上面的内容,它似乎太复杂了。我 build 了一些积木;子程序 read_codontranslate : 我认为它们极大地帮助了程序的逻辑。

我知道这是一项家庭作业,但我认为它可能会帮助您了解其他可能的方法:

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;
    }
}

关于模拟 RNA 合成的 Perl 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4112003/

相关文章:

apache - Apache mod_perl 中的 Mojolicious

regex - Perl 将正则表达式匹配组分配给变量

java - 使用 SHA1 进行哈希处理

c# - 合并多个文件的 MD5 哈希值

c - Objective-C : SHA1

python - 如果 Pandas 中两个单元格为空,则填充 .fillna 列

perl - 模板工具包中的单引号转义

macos - 编译时出现 Perl 语法错误 - 表达式中不允许出现 "use"

python - 如何使用 sys.argv 列表在 python 脚本中输入多个文件

java - biojava包导入错误