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

# (1) program reads file
# (2) uses the split fxn to split the columns
# (3) loads the IDs that begin with FBgn from the 1st column as keys to a hash
# (4) loads the gene symbol from the second column as the value in each hash entry

my $infile = "fb_synonym_fb_2013_05.tsv";

open( FILE, "<", $infile )
    or die "Cannot open file $!";

my @data = <FILE>;

foreach my $line (@data) {
    my @column = split( /\s+/, $line );
    #print $column[0],"\n";
    my $columnID = $column[0];
        foreach ( my $columnID ) {
            while ( $columnID =~ /(^FBgn\d+)/ ) {
                my $ID = $1;
                print $ID, "\n";
            }
        }
 }

我尝试运行代码,但它说 $columnID 是一个未初始化的值。我被困在这一点上。我需要选择以“FBgn”开头的列

输入:数据文件非常大,所以这里是一个选择

##primary_FBid current_symbol current_fullname fullname_synonym(s) symbol_synonym(s)
FBtr0000004 Ecol\lacZ[svp-3]RA      lacZ[svp-3]RA
FBtr0000005 Ecol\lacZ[hkb-5953]RA       lacZ[5953]RA,lacZ[hkb-5953]RA,hkb-  lacZ,5953
FBtr0000007 Mab4A11[+]R2.1          
FBtr0000009 Ecol\lacZ[betaTub56D.AS1]RA lacZ[betaTub56D.AS1]RA
FBtr0000010 Ecol\lacZ[betaTub56D.AS2]RA lacZ[betaTub56D.AS2]RA
FBtr0000011 Ecol\lacZ[betaTub56D.AS3]RA lacZ[betaTub56D.AS3]RA
FBtr0000012 Ecol\lacZ[betaTub56D.NN]RA  lacZ[betaTub56D.NN]RA
FBtr0000013 Ecol\lacZ[betaTub56D.NC]RA  lacZ[betaTub56D.NC]RA
FBtr0000014 Ecol\lacZ[betaTub56D.CP]RA  lacZ[betaTub56D.CP]RA
FBtr0000015 Ecol\lacZ[betaTub56D.ACO]RA lacZ[betaTub56D.ACO]RA
FBtr0000016 Ecol\lacZ[betaTub56D.AC3]RA lacZ[betaTub56D.AC3]RA
4

3 回答 3

4

我怀疑本节存在逻辑问题:

my $columnID = $column[0];
foreach ( my $columnID ) {
    while ( $columnID =~ /(^FBgn\d+)/ ) {
        my $ID = $1;
        print $ID, "\n";
    }
}

您首先遍历每一行,然后将该行拆分为列。到目前为止,一切都很好。

然后你取第一列并将其放入 scalar$columnID中。也很好。值得检查是否已定义(如果您有空行)。

问题在于下一行:

foreach ( my $columnID )

你到底在迭代什么?你声明了一个新变量(一个新的实例$columnID- 你肯定不想这样做,因为它会隐藏你刚刚在上面设置的 $columnID ) - 但是......这一行对你没有帮助。

接下来你有:

while ( $columnID =~ /(^FBgn\d+)/ ) {

...另一个循环。但你不需要它。您只想知道这是对还是错:最好写:

if ( $columnID =~ ... ) {
    # action if true
}

摘要:尝试将代码重写为:

my @data = <FILE>;

foreach my $line (@data) {
    my @column = split( /\s+/, $line );
    my $columnID = $column[0];
    next if ( ! defined( $columnID ) ); # bad line

    if ( $columnID =~ /(^FBgn\d+)/ ) {
        my $ID = $1;
        print $ID, "\n";
    }
}
于 2013-10-27T19:56:05.987 回答
0

派对迟到了,但无论如何,这是我的答案:

#!/usr/bin/perl
use Data::Dumper;
@data = ("FBgn34  2.2  3  xxxx\n","FBgn35 20.2 30 yyyy\n");

my %hash = ();
foreach my $line (@data) {
    my @column = split( /\s+/, $line );
    if ( $column[0] =~ /(^FBgn\d+)/ ) {
        $hash{ $column[0] } = $column[1];
    }
 }
 print Dumper(\%hash);

输出:

{
    'FBgn35' => '20.2',
    'FBgn34' => '2.2'
};
于 2013-10-27T20:09:41.630 回答
0

您必须预先定义变量$columnID$ID在循环之外分配它们。您还定义my $columnID了两次。

尝试:

my @data = <FILE>;
my @column;
my $columnID;
my $ID;

foreach my $line (@data) {
    @column = split( /\s+/, $line );
    #print $column[0],"\n";
    $columnID = $column[0];
    if ( $columnID =~ /(^FBgn\d+)/ ) {
        $ID = $1;
        print $ID, "\n";
    }
 }
于 2013-10-27T19:54:10.893 回答