def test_to_signed_graph():
    ia = IndraNetAssembler([ab1, ab2, ab3, ab4, bc1, bc2, bc3, bc4])
    df = ia.make_df()
    net = IndraNet.from_df(df)
    signed_graph = net.to_signed_graph(sign_dict=default_sign_dict,
                                       weight_mapping=_weight_mapping)
    assert len(signed_graph.nodes) == 3
    assert len(signed_graph.edges) == 4
    assert set([
        stmt['stmt_type'] for stmt in signed_graph['a']['b'][0]['statements']
    ]) == {'Activation', 'IncreaseAmount'}
    assert set([
        stmt['stmt_type'] for stmt in signed_graph['a']['b'][1]['statements']
    ]) == {'Inhibition'}
    assert set([
        stmt['stmt_type'] for stmt in signed_graph['b']['c'][0]['statements']
    ]) == {'Activation', 'IncreaseAmount'}
    assert set([
        stmt['stmt_type'] for stmt in signed_graph['b']['c'][1]['statements']
    ]) == {'Inhibition', 'DecreaseAmount'}
    assert all(signed_graph.edges[e].get('belief', False)
               for e in signed_graph.edges)
    assert all(
        isinstance(signed_graph.edges[e]['belief'], (float, np.longfloat))
        for e in signed_graph.edges)
    assert all(signed_graph.edges[e].get('weight', False)
               for e in signed_graph.edges)
    assert all(
        isinstance(signed_graph.edges[e]['weight'], (float, np.longfloat))
        for e in signed_graph.edges)
Exemple #2
0
 def assemble_unsigned_graph(self):
     """Assemble the model into unsigned graph and return the assembled graph."""
     if not self.assembled_stmts:
         self.run_assembly()
     ia = IndraNetAssembler(self.assembled_stmts)
     unsigned_graph = ia.make_model(graph_type='digraph')
     return unsigned_graph
Exemple #3
0
def test_make_df():
    ia = IndraNetAssembler([st1, st2, st3, st4, st5, st6])
    df = ia.make_df()
    assert isinstance(df, pd.DataFrame)
    assert len(df) == 9
    assert set(df.columns) == {
        'agA_name', 'agB_name', 'agA_ns', 'agA_id', 'agB_ns', 'agB_id',
        'stmt_type', 'evidence_count', 'stmt_hash', 'belief', 'source_counts'
    }
Exemple #4
0
def export_joint_tsv(all_stmts, fname):
    all_stmts_by_hash = {}
    for kinase, stmts in all_stmts.items():
        for stmt in stmts:
            all_stmts_by_hash[stmt.get_hash()] = stmt
    all_stmts_flat = list(all_stmts_by_hash.values())
    ia = IndraNetAssembler(all_stmts_flat)
    df = ia.make_df()
    df.to_csv(fname, index=False, sep='\t')
def test_conversion():
    ia = IndraNetAssembler([st8])
    ug = ia.make_model(graph_type='multi_graph')
    assert len(ug.nodes) == 3
    assert len(ug.edges) == 2, ug.edges
    sg = ia.make_model(graph_type='signed')
    assert len(sg.nodes) == 3
    assert len(sg.edges) == 2, sg.edges
    assert ('PI3K', 'PIP3', 0) in sg.edges, sg.edges
    assert ('PI3K', 'PIP2', 1) in sg.edges, sg.edges
Exemple #6
0
 def assemble_unsigned_graph(self, **kwargs):
     """Assemble the model into unsigned graph and return the assembled
     graph."""
     if not self.assembled_stmts:
         self.run_assembly()
     ia = IndraNetAssembler(self.assembled_stmts)
     unsigned_graph = ia.make_model(graph_type='digraph',
                                    extra_columns=[('internal', is_internal)
                                                   ])
     return unsigned_graph
def test_complex_members():
    ia = IndraNetAssembler([st1, st6])
    # First assemble with dataframe method
    g = ia.make_model(method='df', complex_members=4)
    assert len(g.nodes) == 5
    assert len(g.edges) == 13, len(g.edges)
    assert ('h', 'i', 0) in g.edges
    assert ('i', 'h', 0) in g.edges
    # Get same result assembling with preassembly
    g = ia.make_model(method='preassembly', complex_members=4)
    assert len(g.nodes) == 5
    assert len(g.edges) == 13, len(g.edges)
    assert ('h', 'i', 0) in g.edges
    assert ('i', 'h', 0) in g.edges
def test_exclude_stmts():
    ia = IndraNetAssembler([st1, st2, st3])
    # First assemble with dataframe method
    g = ia.make_model(method='df', exclude_stmts=['Inhibition'])
    assert len(g.nodes) == 3
    assert len(g.edges) == 2
    assert 'c' not in g.nodes
    assert ('a', 'c', 0) not in g.edges
    # Get same result assembling with preassembly
    g = ia.make_model(method='preassembly', exclude_stmts=['Inhibition'])
    assert len(g.nodes) == 3
    assert len(g.edges) == 2
    assert 'c' not in g.nodes
    assert ('a', 'c', 0) not in g.edges
Exemple #9
0
def test_getting_started9_10():
    # Chunk 9
    # pa.export_model('sbml', file_name='model.sbml')

    # Chunk 10
    from indra.assemblers.indranet import IndraNetAssembler
    indranet_assembler = IndraNetAssembler(statements=gn_stmts)
    indranet = indranet_assembler.make_model()
    assert len(indranet.nodes) > 0, 'indranet contains no nodes'
    assert len(indranet.edges) > 0, 'indranet contains no edges'

    # Chunk 11
    signed_graph = indranet.to_signed_graph()
    assert len(signed_graph.nodes) > 0, 'signed graph contains no nodes'
    assert len(signed_graph.edges) > 0, 'signed graph conatins no edges'
Exemple #10
0
 def assemble_signed_graph(self, mode='local', bucket=EMMAA_BUCKET_NAME):
     """Assemble the model into signed graph and return the assembled graph.
     """
     if not self.assembled_stmts:
         self.run_assembly()
     ia = IndraNetAssembler(self.assembled_stmts)
     signed_graph = ia.make_model(graph_type='signed')
     if mode == 's3' and 'indranet' in self.export_formats:
         fname = f'indranet_{self.date_str}.tsv'
         df = ia.make_df()
         df.to_csv(fname, sep='\t', index=False)
         logger.info(f'Uploading {fname}')
         client = get_s3_client(unsigned=False)
         client.upload_file(fname, bucket, f'exports/{self.name}/{fname}')
     return signed_graph
def test_initial_signs():
    a = Event(Concept('a'), QualitativeDelta(polarity=1))
    b = Event(Concept('b'), QualitativeDelta(polarity=1))
    c = Event(Concept('c'), QualitativeDelta(polarity=-1))
    d = Event(Concept('d'), QualitativeDelta(polarity=-1))
    st1 = Influence(a, b)
    st2 = Influence(b, c)
    st3 = Influence(c, d)
    st4 = Influence(b, d)
    ia = IndraNetAssembler([st1, st2, st3, st4])
    sg = ia.make_model(graph_type='signed')
    assert len(sg.nodes) == 4
    assert len(sg.edges) == 4
    assert ('a', 'b', 0) in sg.edges
    assert ('b', 'c', 0) not in sg.edges
    assert ('b', 'c', 1) in sg.edges
    assert ('c', 'd', 0) in sg.edges
    assert ('c', 'd', 1) not in sg.edges
    assert ('b', 'd', 0) not in sg.edges
    assert ('b', 'd', 1) in sg.edges
Exemple #12
0
def test_from_df():
    ia = IndraNetAssembler([st1, st2, st3, st4, st5, st6, st7])
    df = ia.make_df()
    net = IndraNet.from_df(df)
    assert len(net.nodes) == 6
    assert len(net.edges) == 9
    # Stmt with 1 agent should not be added
    assert 'e' not in net.nodes
    # Complex with more than 3 agents should not be added
    assert ('f', 'g', 0) in net.edges
    assert ('h', 'i', 0) not in net.edges
    # Test node attributes
    assert net.nodes['a']['ns'] == 'HGNC', net.nodes['a']['ns']
    assert net.nodes['a']['id'] == '1'
    # Test edge attributes
    e = net['a']['c'][0]
    assert e['stmt_type'] == 'Inhibition'
    assert e['belief'] == 0.76
    assert e['evidence_count'] == 3
    assert net['b']['d'][0]['evidence_count'] == 0
def test_to_digraph():
    ia = IndraNetAssembler([ab1, ab2, ab3, ab4, bc1, bc2, bc3, bc4])
    df = ia.make_df()
    net = IndraNet.from_df(df)
    assert len(net.nodes) == 3
    assert len(net.edges) == 8
    digraph = net.to_digraph(weight_mapping=_weight_mapping)
    assert len(digraph.nodes) == 3
    assert len(digraph.edges) == 2
    assert set([
        stmt['stmt_type'] for stmt in digraph['a']['b']['statements']
    ]) == {'Activation', 'Phosphorylation', 'Inhibition', 'IncreaseAmount'}
    assert all(digraph.edges[e].get('belief', False) for e in digraph.edges)
    assert all(
        isinstance(digraph.edges[e]['belief'], (float, np.longfloat))
        for e in digraph.edges)
    assert all(digraph.edges[e].get('weight', False) for e in digraph.edges)
    assert all(
        isinstance(digraph.edges[e]['weight'], (float, np.longfloat))
        for e in digraph.edges)
    digraph_from_df = IndraNet.digraph_from_df(df)
    assert nx.is_isomorphic(digraph, digraph_from_df)
def test_conversion():
    ia = IndraNetAssembler([st8])
    # First assemble with dataframe method
    ug = ia.make_model(method='df', graph_type='multi_graph')
    assert len(ug.nodes) == 3
    assert len(ug.edges) == 2, ug.edges
    sg = ia.make_model(method='df', graph_type='signed')
    assert len(sg.nodes) == 3
    assert len(sg.edges) == 2, sg.edges
    assert ('PI3K', 'PIP3', 0) in sg.edges, sg.edges
    assert ('PI3K', 'PIP2', 1) in sg.edges, sg.edges
    # Get same result assembling with preassembly
    ug = ia.make_model(method='preassembly', graph_type='multi_graph')
    assert len(ug.nodes) == 3
    assert len(ug.edges) == 2, ug.edges
    sg = ia.make_model(method='preassembly', graph_type='signed')
    assert len(sg.nodes) == 3, sg.nodes
    assert len(sg.edges) == 2, sg.edges
    assert ('PI3K', 'PIP3', 0) in sg.edges, sg.edges
    assert ('PI3K', 'PIP2', 1) in sg.edges, sg.edges
Exemple #15
0
def test_gene_network():
    # Chunk 1: this is tested in _get_gene_network_stmts
    # from indra.tools.gene_network import GeneNetwork
    # gn = GeneNetwork(['H2AX'])
    # biopax_stmts = gn.get_biopax_stmts()
    # bel_stmts = gn.get_bel_stmts()

    # Chunk 2
    from indra import literature
    pmids = literature.pubmed_client.get_ids_for_gene('H2AX')

    # Chunk 3
    from indra import literature
    paper_contents = {}
    for pmid in pmids:
        content, content_type = literature.get_full_text(pmid, 'pmid')
        if content_type == 'abstract':
            paper_contents[pmid] = content
        if len(paper_contents) == 5:  # Is 10 in actual code
            break

    # Chunk 4
    from indra.sources import reach

    literature_stmts = []
    for pmid, content in paper_contents.items():
        rp = reach.process_text(content, url=reach.local_text_url)
        literature_stmts += rp.statements
    print('Got %d statements' % len(literature_stmts))
    assert literature_stmts  # replaces a print statements

    # Chunk 6
    from indra.tools import assemble_corpus as ac
    # stmts = biopax_stmts + bel_stmts + literature_stmts  # tested elsewhere
    stmts = gn_stmts + literature_stmts  # Added instead of above line
    stmts = ac.map_grounding(stmts)
    stmts = ac.map_sequence(stmts)
    stmts = ac.run_preassembly(stmts)
    assert stmts

    # Chunk 7
    from indra.assemblers.cx import CxAssembler
    from indra.databases import ndex_client
    cxa = CxAssembler(stmts)
    cx_str = cxa.make_model()
    assert cx_str

    # Chunk 8
    # ndex_cred = {'user': '******', 'password': '******'}
    # network_id = ndex_client.create_network(cx_str, ndex_cred)
    # print(network_id)

    # Chunk 9
    from indra.assemblers.indranet import IndraNetAssembler
    indranet_assembler = IndraNetAssembler(statements=stmts)
    indranet = indranet_assembler.make_model()
    assert len(indranet.nodes) > 0, 'indranet conatins no nodes'
    assert len(indranet.edges) > 0, 'indranet conatins no edges'

    # Chunk 10
    import networkx as nx
    paths = nx.single_source_shortest_path(G=indranet, source='H2AX', cutoff=1)
    assert paths

    # Chunk 11
    from indra.assemblers.pysb import PysbAssembler
    pysb = PysbAssembler(statements=stmts)
    pysb_model = pysb.make_model()
    assert pysb_model
Exemple #16
0
def export_tsv(statements, fname):
    """Export statements into TSV."""
    ia = IndraNetAssembler(statements)
    df = ia.make_df()
    df.to_csv(fname, index=False, sep='\t')