尝试在Perl中使用HTML :: TableExtract从HTML文件中提取表,但是失败了

时间:2013-08-12 16:21:06

标签: perl html-tableextract

我试图从以下网站的表中提取每种G蛋白偶联受体的信息:

http://www.iuphar-db.org/DATABASE/ObjectDisplayForward?objectId=1&familyId=1

更具体地说,我想从列(Ligand,Sp。,Action,Affinity,Units)中提取信息。目前,我一直在从提取中输出空文件,因此模块似乎无法识别我指定的表。这是我到目前为止编写的代码,旨在通过每个与每个G蛋白偶联受体信息相对应的HTML文件。

use warnings;
use strict;
use HTML::TableExtract;

my @names = `ls /home/wallakin/LINDA/ligands/iuphar/data/html`;

foreach (@names)
{
#Delete empty lines in HTML
open (IN, "</home/wallakin/LINDA/ligands/iuphar/data/html/$_") or die "Can't open html";
my @htmllines = <IN>;
close IN;
for (@htmllines)
{
    s/^\s*$// or s/^\s*//;
}
open (OUT, ">/home/wallakin/LINDA/ligands/iuphar/data/html2/$_");
print OUT @htmllines;
close OUT;

#Extract data from HTML tables based on column headers
my $te = HTML::TableExtract->new ( 
                    headers => [ qw(Ligand Sp. Action Affinity Units) ],
                    depth => 1,
                    count => 1


                    );


$te->parse_file("/home/wallakin/LINDA/ligands/iuphar/data/html2/$_");

my $output = $_;
$output =~ s/\.html/\.txt/g;
open (RESET, ">/home/wallakin/LINDA/ligands/iuphar/data/ligands/$output");
close RESET;
open (DATA, ">>/home/wallakin/LINDA/ligands/iuphar/data/ligands/$output");
binmode (DATA, ":utf8");
binmode (STDOUT, ":utf8");  


foreach my $ts ($te->tables)
{
    print "Table (", join(',', $ts->coords), "):\n";


    foreach my $row ($te->rows)
    {

        foreach ( grep {defined} @$row)
        {
            $_ =~ s/\n/\ /g;
            $_ =~ s/\r//g;  
            #$_ =~ s/\s+/ /g;
        }

        #Each column's data separated by tabs
        print DATA join ("\t", grep {defined} @$row),"\n";
    }
}
close DATA;
}

我写了一个以前的程序(幸运的是,它有效),它可以获取每个G蛋白偶联受体的所有HTML文件并将其传递给该程序。我不确定我是否使用了正确的标题,深度或计数。

如果这篇文章听起来很愚蠢,我很抱歉,但总的来说,我是生物信息学和编程的新手。谢谢你的帮助!

1 个答案:

答案 0 :(得分:1)

这似乎适用于您提供的网址:

use 5.014;
use strict;
use warnings;
use open qw(:std :utf8);

use HTML::TableExtract;

my $te = HTML::TableExtract->new(
    headers => [qw(Ligand Sp. Action Affinity Units Reference)],
);

$te->parse_file('sample.html');

my @tables = $te->tables;
for my $t (@tables) {
    my @rows = $t->rows;
    for my $r (@rows) {
        for my $c (@$r) {
            $c =~ s/\A\s+//;
            $c =~ s/\s+\z//;
        }
        say "@$r";
    }
}