【问题标题】:Finding motifs and position of motif in FASTA file - Perl在 FASTA 文件中查找主题和主题的位置 - Perl
【发布时间】:2022-04-18 11:02:23
【问题描述】:

有人可以帮我处理这个 Perl 代码吗?当我运行它时,什么也没有发生。没有错误或任何对我来说很奇怪的东西。它读入并打开文件就好了。我相信问题出在 while 循环或 foreach 循环中,因为老实说我不认为我理解它们。我对这方面很陌生,有一个很糟糕的老师。

说明:声明一个名为motif的标量变量并将其设为AAA。声明一个名为位置的数组变量,这是存储主题位置的位置。将基因放在一个标量变量中。现在在 amborella 基因中寻找那个基序。代码应打印图案的位置和找到的图案。您将需要编写一个 while 循环来搜索主题并包括 push、pos 和 –length 命令,以便保存和报告位置。然后,您将需要一个 foreach 循环来打印位置和主题。 (如果它只报告基因的第一行中的位置,请记住这是因为基因位于一个只会读取第一行的标量变量中。这是可以接受的。

到目前为止我的代码:

#!/usr/bin/perl
use warnings;
use strict;

#Declare a scalar variable called motif and make it AAA.
my$motif="AAA";

#Declare an array variable called locations, which is where the
#locations of the motif will be stored.
my@locations=();
my$foundMotif="";
my$position=();

#Place the gene in a scalar variable.
my$geneFileName = 'amborella.txt';
open(GENEFILE, $geneFileName) or die "Can't read file!";
my$gene = <GENEFILE>;

#Now search for that motif in the amborella gene.
#The code should print the position of the motif and the motif
#found. You will need to write a while loop that searches for the
#motif and includes push, pos, and –length commands in order to
#save and report locations.

while($foundMotif =~ m/AAA/g) {
$position=(pos($foundMotif)-3);
push (@locations, $position);
}

#Then you will need a foreach loop to print the locations and the motif.
foreach $position (@locations){
print "\n Found motif: ", $motif, "\n at position: ", $position;
}

#close the file
close GENEFILE;

exit;

【问题讨论】:

  • 我们现在处于堆栈溢出和代码审查之间。我有一些关于你的风格的 cmets,这可能是因为教材过时了。有strictwarnings 很好。这些很有帮助。但是你也应该缩进你的代码,这样会更容易阅读。您的open 调用存在一些问题,但它们并不是您的代码没有执行您想要的操作的原因(如果您有兴趣,请查看 3 argument open)。

标签: loops perl position fasta


【解决方案1】:

你的程序很好,只是一个简单的混淆。

您正在匹配一个空字符串。

while($foundMotif =~ m/AAA/g) {
  $position = (pos($foundMotif)-3);
  push (@locations, $position);
}

您正在$foundMotif 中寻找AAA。但这是一个空字符串,因为您只是进一步声明了它。你的基因字符串(免责声明:我对生物信息学一无所知)是$gene。这就是你需要匹配的。


让我们一步一步来。我已经简化了您的代码并放入了一个示例字符串。我知道这不是基因的样子,但这没关系。这已经修复了。

use strict;
use warnings;

my $motif = "AAA";

my @locations  = ();

# ... skip reading the file
my $gene = "ABAABAAABAAAAB\n";

while ($gene =~ m/$motif/g) {                     # 1, 2
    my $position = (pos($gene) - length($motif)); # 3, 4
    push(@locations, $position);
}

foreach $position (@locations) {
    print "\n Found motif: ", $motif, "\n at position: ", $position;
}

如果你运行它,代码现在会产生有意义的输出。

 Found motif: AAA
 at position: 5
 Found motif: AAA
 at position: 9

我进行了四项更改:

  1. 您需要在$gene中搜索
  2. 如果您不使用变量$motif 进行搜索,那么您的变量将毫无意义。这样,您的程序就会变得动态。
  3. 同样,您需要在$gene 中使用pos()
  4. 要使其动态化,您不应硬编码length

您根本不需要$foundMotif 变量。 $position 实际上对于它所在的块来说是词法。这意味着,每次运行循环时它都会是一个不同的变量,这只是一个好习惯。在 Perl 中,您希望始终为变量使用尽可能小的 scope,并且只在需要时声明它们,而不是提前声明。

由于这是一个学习练习,因此单独迭代数组是有意义的。在现实生活中的程序中,您可以消除foreach 循环和数组,如果您以后不使用它们,则直接输出位置。

【讨论】:

  • 非常感谢。是的,因为covid,详细的教学水平有点过时了,我在这门课上很迷茫,这是我对Perl的第一次介绍。我仍然对为什么需要 -length($motif) 感到有些困惑。这告诉我们什么?
  • @amanda length 为您提供该字符串中的字符数。因为你的主题是一个变量,它是动态的。您也可以将它作为参数传递给您的程序,以搜索任意图案。但是如果主题不是AAA,而是说GAGAGAGA,它将超过3个字符,所以这需要是动态的,这取决于实际使用的主题。
  • 如果您想更多地了解 Perl,我建议您使用现代资源。不幸的是,与 Python 相比,没有太多花哨的在线资源,但有一些是可用的。我可能会得到 O'Reilly Learning Perl 书(其中一位作者也是这里的常客),或者如果你对编程有一点了解,Curtis Poe 的 Beginning Perl。当我在网络世界工作时,我不太了解您对生物信息学究竟需要什么,但这些都应该是坚实的基础,尤其是在您的教材有点尘土飞扬的情况下。
  • 欢迎提出更多问题。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-24
  • 1970-01-01
  • 2012-03-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多