解析html后Perl未初始化的值错误

eug*_*ugh 2 html perl bioinformatics bioperl

我正在Bioperl寻找GOterms基因.我检索一个html文件,将其转换为文本,删除所有额外的空格和换行符,并尝试通过生成的数组.

但是,我一直在获取访问数组中未初始化值的错误.我进行了很多检查,以确保数组不是空的,我不会超出界限.我怎样才能摆脱这个错误?

我以更易读的格式重新发布了代码.谢谢您的帮助.

它似乎成功地从html中解析出正确的数据,所以我不知道什么是错的.

#!/usr/bin/perl -w
use strict;
use LWP::Simple;
use HTML::TreeBuilder;
use HTML::FormatText;

my $URL         = get("http://amigo.geneontology.org/amigo/term/GO:0000001");
my $Format      = HTML::FormatText->new;
my $TreeBuilder = HTML::TreeBuilder->new;
$TreeBuilder->parse($URL);
my $Parsed = $Format->format($TreeBuilder);
print "$Parsed";
my @parsed = split( /[ ]{2,}|(\n+)|(\r+)/, $Parsed );
if ( @parsed == 1 ) { return; }

my %termhash;
my $count = 0;

while ( $count < @parsed ) {
    if ( defined $parsed[$count] && $parsed[$count] eq 'Name' ) {
        my $count2 = $count;
        while ( ( $parsed[$count2] ne 'Feedback' ) && ( $count2 < @parsed ) ) {
            $count2++;
        }
        $count2--;
        @parsed = @parsed[ $count .. $count2 ];    # Gets the slice of the array needed
        last;
    }
    $count++;
}

if ( @parsed <= 1 ) { return; }
print "\n";

print @parsed;

$count = 0;
while ( $count < @parsed ) {
    if ( $parsed[$count] eq 'Name' ) {
        while ( $parsed[$count] ne 'Ontology' && ( $count < @parsed )) {
            $termhash{'Name'} .= $parsed[$count];
            $count++;
        }
    }
    if ( $parsed[$count] eq 'Ontology' ) {
        while ( $parsed[$count] ne 'Synonyms' && ( $count < @parsed )) {
            $termhash{'Category'} .= $parsed[$count];
            $count++;
        }
    }
    if ( $parsed[$count] eq 'Synonyms' ) {
        while ( $parsed[$count] ne 'Definition' && ( $count < @parsed )) {
            $termhash{'Aliases'} .= $parsed[$count];
            $count++;
        }
    }
    if ( $parsed[$count] eq 'Definition' ) {
        while ( $parsed[$count] ne 'Comment' && ( $count < @parsed )) {
            $termhash{'Definition'} .= $parsed[$count];
            $count++;
        }
    }
    if ( $parsed[$count] eq 'Comment' ) {
        while ( $parsed[$count] ne 'History' && ( $count < @parsed )) {
            $termhash{'Comment'} .= $parsed[$count];
            $count++;
        }
    }
    if ( $parsed[$count] eq 'History' ) {
        while ( $parsed[$count] ne 'Subset' && ( $count < @parsed )) {
            $termhash{'Version'} .= $parsed[$count];
            $count++;
        }
    }
    if ( $parsed[$count] eq 'Subset' ) {
        while ( ( $parsed[$count] ne 'Community' ) && ( $count < @parsed ) ) {
            $count++;
        }
    }
    if ( $parsed[$count] eq 'Community' ) {
        while ( ( $parsed[$count] ne 'Related' ) && ( $count < @parsed ) ) {
            $count++;
        }
    }
    if ( $parsed[$count] eq 'Related' ) {
        for ( $count < @parsed ) {
            $termhash{'Definition references'} .= $parsed[$count];
            $count++;
        }
    }
}
if ( $termhash{'Definition'} =~ m/OBSOLETE/ ) { $termhash{'Is obsolete'} = 1 }
else { $termhash{'Is obsolete'} = 0 }
#print %termhash;
Run Code Online (Sandbox Code Playgroud)

主要错误消息是:

  • 在/home/adur/workspace/BI7643/ParseGOhtml.pl第23行的字符串ne中使用未初始化的值$ parsed [127].

    在/home/adur/workspace/BI7643/ParseGOhtml.pl第35行打印时使用未初始化的值$ parsed [1].

    在/home/adur/workspace/BI7643/ParseGOhtml.pl第42行的字符串ne中使用未初始化的值$ parsed [1].

    在连接(.)中使用未初始化的值$ parsed [1]或在/home/adur/workspace/BI7643/ParseGOhtml.pl第41行使用字符串.

    在/home/adur/workspace/BI7643/ParseGOhtml.pl第48行的字符串ne中使用未初始化的值$ parsed [17].

    在连接(.)中使用未初始化的值$ 17解析[17]或在/home/adur/workspace/BI7643/ParseGOhtml.pl第47行使用字符串.

    在/home/adur/workspace/BI7643/ParseGOhtml.pl第54行的字符串ne中使用未初始化的值$ parsed [29].

    在连接(.)中使用未初始化的值$解析[29]或在/home/adur/workspace/BI7643/ParseGOhtml.pl第53行使用字符串.

    在/home/adur/workspace/BI7643/ParseGOhtml.pl第60行的字符串ne中使用未初始化的值$ parsed [41].

    在连接(.)中使用未初始化的值$解析[41]或在/home/adur/workspace/BI7643/ParseGOhtml.pl第59行使用字符串.

    在/home/adur/workspace/BI7643/ParseGOhtml.pl第66行的字符串ne中使用未初始化的值$ parsed [79].

    在连接(.)中使用未初始化的值$解析[79]或在/home/adur/workspace/BI7643/ParseGOhtml.pl第65行使用字符串.

    在/home/adur/workspace/BI7643/ParseGOhtml.pl第72行的字符串ne中使用未初始化的值$ parsed [83].

    在连接(.)中使用未初始化的值$解析[83]或在/home/adur/workspace/BI7643/ParseGOhtml.pl第71行使用字符串.

    在/home/adur/workspace/BI7643/ParseGOhtml.pl第77行的字符串ne中使用未初始化的值$ parsed [95].

    在/home/adur/workspace/BI7643/ParseGOhtml.pl第82行的字符串ne中使用未初始化的值$ parsed [107].

har*_*vey 5

你的意思是不引用第二次和第三次调用来解析?

htmlparse('GO:0000001');
htmlparse(GO:0000002);
htmlparse(GO:0000003); 
Run Code Online (Sandbox Code Playgroud)

应该;

htmlparse('GO:0000001');
htmlparse('GO:0000002');
htmlparse('GO:0000003'); 
Run Code Online (Sandbox Code Playgroud)

此外,请确保将以下内容添加到文件顶部

use warnings;
use diagnostics;
Run Code Online (Sandbox Code Playgroud)

如果仍有问题,请回发生成的错误消息.

此外,根据http://geneontology.org/page/lead-database-guide,您可以直接查询在线数据库而无需解析html,他们使用他们的工具提供以下示例;

GOshell.pl -d go_latest -dbuser go_select -dbauth amigo -port 4085 -h mysql.ebi.ac.uk
Run Code Online (Sandbox Code Playgroud)

相关cpan信息; http://cpansearch.perl.org/src/SJCARBON/go-db-perl-0.04/doc/go-db-perl-doc.html