【问题标题】:Parsing file into Parent/ Child format for a JSON file将文件解析为 JSON 文件的父/子格式
【发布时间】:2015-10-07 10:25:39
【问题描述】:

我想要一些关于如何为Gene ontology (.obo)解析此文件的帮助/建议

我正在 D3 中创建一个可视化,需要创建一个 JSON 格式的“树”文件 -

{
 "name": "flare",
 "description": "flare",
 "children": [
  {
   "name": "analytic",
   "description": "analytics",
   "children": [
    {
     "name": "cluster",
     "description": "cluster",
     "children": [
      {"name": "Agglomer", "description": "AgglomerativeCluster", "size": 3938},
      {"name": "Communit", "description": "CommunityStructure", "size": 3812},
      {"name": "Hierarch", "description": "HierarchicalCluster", "size": 6714},
      {"name": "MergeEdg", "description": "MergeEdge", "size": 743}
     ]
    }, etc..

这种格式似乎很容易在 python 的字典中复制,每个条目有 3 个字段:名称、描述和子项[]。

我的问题实际上是如何提取数据。上面链接的文件的“对象”结构如下:

[Term]
id: GO:0000001
name: mitochondrion inheritance
namespace: biological_process
def: "The distribution of mitochondria, including the mitochondrial genome, into daughter cells after mitosis or meiosis, mediated by interactions between mitochondria and the cytoskeleton." [GOC:mcc, PMID:10873824, PMID:11389764]
synonym: "mitochondrial inheritance" EXACT []
is_a: GO:0048308 ! organelle inheritance
is_a: GO:0048311 ! mitochondrion distribution

我需要 id、is_a 和 name 字段。我曾尝试使用 python 来解析这个,但我似乎无法找到一种方法来定位每个对象。

有什么想法吗?

【问题讨论】:

    标签: python json genetics


    【解决方案1】:

    这是解析“.obo”文件中对象的一种相当简单的方法。它将对象数据保存到dict 中,id 作为键,nameis_a 数据保存在列表中。然后它使用标准的json 模块的.dumps 函数漂亮地打印它。

    出于测试目的,我在您的链接中使用了该文件的截断版本,最多只包含id: GO:0000006

    此代码忽略任何包含is_obsolete 字段的对象。它还会从is_a 字段中删除描述信息;我想您可能想要这样做,但禁用该功能很容易。

    #!/usr/bin/env python
    
    ''' Parse object data from a .obo file
    
        From http://stackoverflow.com/q/32989776/4014959
    
        Written by PM 2Ring 2015.10.07
    '''
    
    from __future__ import print_function, division
    
    import json
    from collections import defaultdict
    
    fname = "go-basic.obo"
    term_head = "[Term]"
    
    #Keep the desired object data here
    all_objects = {}
    
    def add_object(d):
        #print(json.dumps(d, indent = 4) + '\n')
        #Ignore obsolete objects
        if "is_obsolete" in d:
            return
    
        #Gather desired data into a single list,
        # and store it in the main all_objects dict
        key = d["id"][0]
        is_a = d["is_a"]
        #Remove the next line if you want to keep the is_a description info
        is_a = [s.partition(' ! ')[0] for s in is_a]
        all_objects[key] = d["name"] + is_a
    
    
    #A temporary dict to hold object data
    current = defaultdict(list)
    
    with open(fname) as f:
        #Skip header data
        for line in f:
            if line.rstrip() == term_head:
                break
    
        for line in f:
            line = line.rstrip()
            if not line:
                #ignore blank lines
                continue
            if line == term_head:
                #end of term
                add_object(current)
                current = defaultdict(list)
            else:
                #accumulate object data into current
                key, _, val = line.partition(": ")
                current[key].append(val)
    
    if current:
        add_object(current)    
    
    print("\nall_objects =")
    print(json.dumps(all_objects, indent = 4, sort_keys=True))
    

    输出

    all_objects =
    {
        "GO:0000001": [
            "mitochondrion inheritance", 
            "GO:0048308", 
            "GO:0048311"
        ], 
        "GO:0000002": [
            "mitochondrial genome maintenance", 
            "GO:0007005"
        ], 
        "GO:0000003": [
            "reproduction", 
            "GO:0008150"
        ], 
        "GO:0000006": [
            "high-affinity zinc uptake transmembrane transporter activity", 
            "GO:0005385"
        ]
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-23
      • 2018-11-07
      • 2020-02-12
      • 1970-01-01
      • 2015-04-26
      • 2023-03-31
      • 1970-01-01
      相关资源
      最近更新 更多