<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 CLIXO Ontology Tree Generator
This is a notebook to generate tree data file from original table and annotations.
 This is the final version of the script creating an Cytoscape.js file with gene count.
Requirment
DAG file for CLIXO
Term to gene assignment file
GO alignment file
CLIXO TERM COUNT = 4805
Step1:  Build Base CyJS Network
Step2:  Layout with networkx
<ASSISTANT_TASK:>
Python Code:
# Load data sets
import pandas as pd
treeSourceUrl = './data/preds_yeastnet_no_gi_0.04_0.5.txt.propagate.small_parent_tree'
geneCountFile = './data/preds_yeastnet_no_gi_0.04_0.5.txt.propagate.term_sizes'
alignmentFile = './data/alignments_FDR_0.1_t_0.1'
geneAssignment = './data/preds_yeastnet_no_gi_0.04_0.5.txt.propagate.mapping'
# Load the tree data
treeColNames = ['parent', 'child', 'type', 'in_tree']
tree = pd.read_csv(treeSourceUrl, delimiter='\t', names=treeColNames)
tree.tail()
assignment = pd.read_csv(geneAssignment, sep='\t', names=['gene', 'clixo'])
print(assignment['clixo'].unique().shape)
assignment.head()
al = pd.read_csv(alignmentFile, sep='\t', names=['clixo', 'go', 'similarity', 'fdr', 'genes'])
al.head()
mapping = {}
for row in al.itertuples():
    entry = {
        'go': row[2],
        'score': row[3],
        'dfr': row[4]
    }
    mapping[str(row[1])] = entry
geneCounts = pd.read_csv(geneCountFile, names=['clixo', 'count'], sep='\t')
term2count = {}
for row in geneCounts.itertuples():
    term2count[str(row[1])] = row[2].item()
# Get unique terms
clixo_terms = set()
for row in tree.itertuples():
    etype = row[3]
    if not etype.startswith('gene'):
        clixo_terms.add(str(row[1]))
        clixo_terms.add(str(row[2]))
print(len(clixo_terms))
Explanation: CLIXO Ontology Tree Generator
This is a notebook to generate tree data file from original table and annotations.
 This is the final version of the script creating an Cytoscape.js file with gene count.
Requirment
DAG file for CLIXO
Term to gene assignment file
GO alignment file
CLIXO TERM COUNT = 4805
End of explanation
import json
clixoTree = {
    'data': {
        'name': 'CLIXO Tree'
    },
    'elements': {
        'nodes': [],
        'edges': []
    }
}
print(json.dumps(clixoTree, indent=4))
def get_node(id, count):
    node = {
        'data': {
            'id': id,
            'geneCount': count
        }
    }
        
    return node
def get_edge(source, target):
    edge = {
        'data': {
            'source': target,
            'target': source
        }
    }
    
    return edge
edges = []
PREFIX = 'CLIXO:'
for row in tree.itertuples():
    etype = row[3]
    in_tree = row[4]
    if etype.startswith('gene') or in_tree == 'NOT_TREE':
        continue
    
    source = PREFIX + str(row[1])
    child = PREFIX + str(row[2])
    edges.append(get_edge(source, child))
    
print(len(edges))
nodes = []
for id in clixo_terms:
    node = get_node(PREFIX + id, term2count[id])
    nodes.append(node)
print(len(nodes))
clixoTree['elements']['nodes'] = nodes
clixoTree['elements']['edges'] = edges
with open('./data/clixo-tree.cyjs', 'w') as outfile:
    json.dump(clixoTree, outfile)
Explanation: Build Base CyJS Network
End of explanation
import networkx as nx
DG=nx.DiGraph()
for node in nodes:
    DG.add_node(node['data']['id'])
for edge in edges:
    DG.add_edge(edge['data']['source'], edge['data']['target'])
import matplotlib.pyplot as plt
nx.draw_circular(DG)
# pos = nx.nx_pydot.pydot_layout(DG)
Explanation: Layout with networkx
End of explanation
<END_TASK>