2015-05-22 40 views
2

我正在使用Bioperl找到GOterms的基因。 我檢索一個html文件,將其轉換爲文本,擺脫所有多餘的空格和換行符,並嘗試通過結果數組。解析後的Perl未初始化值錯誤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; 

主要錯誤信息是:

  • 使用未初始化值$的解析[127]在 /home/adur/workspace/BI7643/ParseGOhtml.pl線23串NE 。

    使用未初始化值$的解析[1]在 /home/adur/workspace/BI7643/ParseGOhtml.pl線打印35.

    使用未初始化值$的解析在串NE [1]在 /home/adur/workspace/BI7643/ParseGOhtml.pl線42

    使用[1]在級聯解析未初始化值$的(。)或串 在/home/adur/workspace/BI7643/ParseGOhtml.pl線41

    使用未初始化值$的解析[17]在 /home/adur/workspace/BI7643/ParseGOhtml.pl線48串NE 。

    使用未初始化值$的解析[17]在級聯(。)或串 在/home/adur/workspace/BI7643/ParseGOhtml.pl線47

    使用未初始化值$的解析[29]在 /home/adur/workspace/BI7643/ParseGOhtml.pl線54

    使用未初始化值$的分析的字符串NE [29]在級聯(。 ),或者在 /home/adur/workspace/BI7643/ParseGOhtml.pl在/home/adur/workspace/BI7643/ParseGOhtml.pl行字符串 53.

    未初始化值$使用解析[41]在串NE第60行。

    在串聯(。)或字符串中使用未初始化的值$ parsed [41]在/home/adur/workspace/BI7643/ParseGOhtml.pl線59

    使用未初始化值$的解析[79]在 /home/adur/workspace/BI7643/ParseGOhtml.pl線66串NE。

    使用未初始化值$的解析[79]在級聯(。)或串 在/home/adur/workspace/BI7643/ParseGOhtml.pl線65.

    解析未初始化值$的用途[83] in string ne /home/adur/workspace/BI7643/ParseGOhtml。PL線72

    使用未初始化值$的解析[83]在級聯(。)或串 在/home/adur/workspace/BI7643/ParseGOhtml.pl線71

    未初始化值$使用解析[95]在串NE在 /home/adur/workspace/BI7643/ParseGOhtml.pl管線77

    未初始化值$使用解析[107]在 /家/ adur /工作區/ BI7643串NE /ParseGOhtml.pl line 82.

+0

它可能有助於以包括來自錯誤消息的更多細節。 –

+0

你正在用'count2 - '做什麼對我來說沒有什麼意義。 – stevesliva

+1

你不應該壓縮你的代碼太多,以致它不再可讀。利用空白。並使用警告。並且不要使用'使用Switch',模塊已被棄用 - 使用if-else來代替。 – TLP

回答

5

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

htmlparse('GO:0000001'); 
htmlparse(GO:0000002); 
htmlparse(GO:0000003); 

應該是;

htmlparse('GO:0000001'); 
htmlparse('GO:0000002'); 
htmlparse('GO:0000003'); 

此外,請務必添加到您的文件的頂部以下

use warnings; 
use diagnostics; 

回來後所產生的錯誤信息,如果還存在一定的問題。

另外,根據http://geneontology.org/page/lead-database-guide您可以查詢在線數據庫直接而不必解析HTML,他們提供使用他們的工具,下面的例子;

​​

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

+0

實際上需要下載數據庫。我無法使用它,但謝謝。現在它主要是一個解析問題。 – eugheugh