Bycon::pgx_create_hierarchies.py Python Code Documentation

“NCIT:C3262”: “Neoplasm” “NCIT:C7062”: “Neoplasm by Special Category” “NCIT:C4741”: “Neoplasm by Morphology” “NCIT:C3263”: “Neoplasm by Site”

onto_trees = [
    { "label": "Neoplasm", "root": "NCIT:C3262", "classes": { } },
    # { "label": "Neoplasm by Morphology", "root": "NCIT:C4741", "classes": { } },
    # { "label": "Neoplasm by Site", "root": "NCIT:C3263", "classes": { } },    
]

all_ancestors = set( ONTO_IDs )

for idx, tree in enumerate(onto_trees):

    [root] = ont.search(tree["root"])
    
    print("=> processing root "+tree["root"])
   
    for onto_id in ONTO_IDs:
    
        if onto_id == 'NCIT:C3262':
            continue
        
        [matched] = ont.search(onto_id)
        oldies = ont.ancestors(matched)
        oldies = set(filter(pre_filter.match, oldies))
        for old in oldies:
            all_ancestors.add(old)
        
        youngsters = ont.descendants(matched)
        youngsters = set(filter(pre_filter.match, youngsters))
        if len(oldies) < 1 and len(youngsters) < 1:
            print(onto_id+" not in neoplasm_core")
            onto_trees[ idx ][ "classes" ][ onto_id ] = { "parents": [ 'NCIT:C3262' ], "children": [ onto_id ], "core": False }
        else:
            print("{} <-- {} --> {}".format( len(oldies), onto_id, len(youngsters) ) )
            onto_trees[ idx ][ "classes" ][ onto_id ] = { "parents": oldies, "children": youngsters, "core": True }


    # onto_trees[ idx ][ "graph" ] = ont_g.subgraph(all_ancestors)
    # print(len(onto_trees[ idx ][ "graph" ].nodes()))

    # data = json_graph.node_link_data(onto_trees[ idx ][ "graph" ])
    # print("=> nodes for root "+tree["root"]+": "+str(len(data["nodes"])))

    # onto_dump = path.join( config[ "paths" ][ "out" ], "ontograph_"+tree[ "root" ]+".json")
    # with open(onto_dump, 'w') as fp:
    #     json.dump(data, fp)

    hier_classes = { }
    hier_match = re.compile(r"^\s*?(\w.*?) \((C\d+?)\)\s*?$")
    line_no = 0
    for line in hier_lines:
        if hier_match.match(line):
            hier_item = hier_match.match(line).group(1,2)
            hier_classes[ hier_item[1] ] = hier_item[0]
            line_no += 1
            print(("{} - {}").format(line_no, hier_classes[ hier_item[1] ]))



    print("Wrote graph JSON to "+onto_dump)

################################################################################ ################################################################################ ################################################################################

if name == ‘main’: main( )

Links

2020-06-03