English 中文(简体)
perl脚本,用于在multivasta文件中搜索主题,并打印完整的序列和标题行
原标题:perl Script to search for a motif in a multifasta file and print the complete sequence along with the header line

我能够在多fasta文件中搜索主题,并打印包含主题的行。。。。但我需要打印所有序列以及包含fasta序列的基序的标题行。请帮帮我,我只是perl的初学者

#!usr/bin/perl -w
use strict;

print STDOUT "Enter the motif: ";
my $motif = <STDIN>;
chomp $motif;


my $line;
open (FILE, "data.fa");
while ($line = <FILE>) {
  if ($line =~ /$motif/)  {
     print $line;
   }
}
问题回答

试试这个:

生物::DB::Fasta

页面上的说明。有关更多示例或说明,请在谷歌上搜索:“使用Bio::DB::Fasta”

要安装它,只需遵循以下任何说明,我建议使用CPAN.pm方法作为超级用户:

安装Perl模块

上面所写的脚本不记得当前的序列标识符,因此您不知道哪个标识符与每个序列相关联。

我修改了下面的脚本,将所有FASTA序列读取到一个映射的哈希中(标识符=>;序列),然后迭代该哈希,在适当的时候打印出匹配项。对于非常大的序列文件来说,这是一种不合适的方法,但在编写分析数据的新脚本时,学习如何编写这样的小辅助函数可能会大大加快速度。了解如何在Perl中使用和操作散列和其他数据结构也很重要,因为您遇到的大多数代码都不是由初学者编写的。

#!/usr/bin/perl

use strict;
use warnings;

print STDOUT "Enter the motif: ";
my $motif = <STDIN>;
chomp $motif;

my %seqs = %{ read_fasta_as_hash(  data.fa  ) };
foreach my $id ( keys %seqs ) {
    if ( $seqs{$id} =~ /$motif/ ) {
        print $id, "
";
        print $seqs{$id}, "
";
    }
}

sub read_fasta_as_hash {
    my $fn = shift;

    my $current_id =   ;
    my %seqs;
    open FILE, "<$fn" or die $!;
    while ( my $line = <FILE> ) {
        chomp $line;
        if ( $line =~ /^(>.*)$/ ) {
            $current_id  = $1;
        } elsif ( $line !~ /^s*$/ ) { # skip blank lines
            $seqs{$current_id} .= $line
        }
    }
    close FILE or die $!;

    return \%seqs;
}

@詹姆斯的回答很好。如果你正在寻找更通用的东西,我会用它。如果你正在寻找一个更简单的版本(也许是为了教学。

#!usr/bin/perl -w
use strict;

print STDOUT "Enter the motif: ";
my $motif = <STDIN>;
chomp $motif;


my $line;
my $defline;
open (FILE, "data.fa");
while ($line = <FILE>) {
  if ($line =~ /^>/) {
     $defline = $line;
   } elsif ($line =~ /$motif/)  {
     print($defline,$line);
   }
}
close (FILE);

您会注意到,我还在文件句柄上添加了一个显式关闭。





相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签