将HTML可扩展列表转换为表格或csv格式



我知道这个话题已经被讨论过几次了,但我找不到一个适用于我的案例。我不是一个有经验的计算机用户,请记住,虽然我可以玩bash, R和可能运行perl脚本太。我在我的机器上运行Ubuntu。

我想做的是将以下网页的可扩展列表http://www.genome.jp/kegg-bin/get_htext?br08902.keg(请使用"一键模式"完全展开)转换为表格或csv格式,其中每个级别的缩进到一个单独的列。

如果父类别对所有在它下面分组的元素重复,那就没有那么糟糕了。类似下面的标签,我手动为页面的前几行。

Pathways and Ontologies Pathways    br08901  KEGG pathway maps
Pathways and Ontologies Functional hierarchies  br08902  BRITE functional hierarchies
Genes and Proteins  Orthologs and modules   ko00001  KEGG Orthology (KO)
Genes and Proteins  Orthologs and modules   ko00002  KEGG pathway modules
Genes and Proteins  Orthologs and modules   ko00003  KEGG modules and reaction modules
Genes and Proteins  Protein families: metabolism    ko01000  Enzymes
Genes and Proteins  Protein families: metabolism    ko01001  Protein kinases
Genes and Proteins  Protein families: metabolism    ko01009  Protein phosphatases and associated proteins
Genes and Proteins  Protein families: metabolism    ko01002  Peptidases
Genes and Proteins  Protein families: metabolism    ko01003  Glycosyltransferases
Genes and Proteins  Protein families: metabolism    ko01005  Lipopolysaccharide biosynthesis proteins
Genes and Proteins  Protein families: metabolism    ko01004  Lipid biosynthesis proteins

提前感谢!

这个任务需要一些很好的分离步骤。崩溃:

获取页面内容。您可以使用例如curlwgetfetch或类似的程序。例如

curl http://...

将下载页面内容。

在你的页面中,存在一个链接"下载文本"。当你检查它指向的位置时,你会发现你需要从链接

下载
http://www.kegg.jp/kegg-bin/download_htext?htext=br08902.keg&format=htext&filedir=
                                                 ^^^^^^^^^^^ name of your needed keg

所以在

之后
curl "http://www.kegg.jp/kegg-bin/download_htext?htext=br08902.keg&format=htext&filedir=" > mykeg.txt

将得到如下格式的文件:(缩短)

+C      Br number
#<h2><a href="/kegg/kegg2.html"><img src="/Fig/bget/kegg3.gif" align="middle" border=0></a>&nbsp; BRITE Functional Hierarchies</h2>
#<!---
#ENTRY       br08902
#NAME        Brite
#DEFINITION  BRITE functional hierarchies
#--->
!
A<b>Pathways and Ontologies</b>
B  Pathways
C    br08901  KEGG pathway maps
B  Functional hierarchies
C    br08902  BRITE functional hierarchies
#
A<b>Genes and Proteins</b>
B  Orthologs and modules
C    ko00001  KEGG Orthology (KO)
C    ko00002  KEGG pathway modules

这是一个很好的文本文件,大部分没有HTML标记。可使用常用bash工具轻松解析。

先清理一下:

使用sed命令删除所有不需要的行

sed '/^[#!+]/d'

删除不需要的HTML标记(通常不可能使用正则表达式,但在这种情况下是可能的)

sed 's/<[^>]*>//g'

为起始字符

添加分隔符
sed 's/^./& /'
在上面的

之后,您将得到类似于下一个

的文本。
A Pathways and Ontologies
B   Pathways
C     br08901  KEGG pathway maps
B   Functional hierarchies
C     br08902  BRITE functional hierarchies
A Genes and Proteins
B   Orthologs and modules
C     ko00001  KEGG Orthology (KO)
C     ko00002  KEGG pathway modules
C     ko00003  KEGG modules and reaction modules

如何使用bash

创建一个良好的可解析结构?
while read -r prefix content
do
     echo "do something with a line >>$content<< with a prefix >>$prefix<<"
done
例如,您可以使用case命令测试prefix,如:
case "$prefix" in
    A) a="$content" ;;
    B) b="$content" ;;
    C) c="$content" ;;
esac

存在一个更好的替代方案,使用associative arrays,但上面是简单和工作…

你现在有了所有的信息,你需要做一个工作的解决方案(在8行)。

下一步取决于你…;)

编辑

通常我不会做全部的工作,因为stackoverflow不是一个免费的编程服务,但是好吧-这是脚本:

kegfile="KEG"
while read -r prefix content
do
    case "$prefix" in
        A) col1="$content" ;;
        B) col2="$content" ;;
        C) echo -e "$col1t$col2t$content";;
    esac
done < <(sed '/^[#!+]/d;s/<[^>]*>//g;s/^./& /' < "$kegfile")

最新更新