示例#1
0
def test_cited():
    cxa = CxAssembler()
    cxa.add_statements([st_cited])
    cxa.make_model()
    assert(len(cxa.cx['citations']) == 1)
    assert(len(cxa.cx['edgeCitations']) == 1)
    citation = cxa.cx['citations'][0]
    assert(citation.get('dc:identifier') == 'pmid:12345')
    cid = citation.get('@id')
    assert(cxa.cx['edgeCitations'][0]['citations'][0] == cid)
示例#2
0
文件: api.py 项目: lijielife/indra
def assemble_cx():
    """Assemble INDRA Statements and return CX network json."""
    response = request.body.read().decode('utf-8')
    body = json.loads(response)
    stmts_json = body.get('statements')
    stmts = stmts_from_json(stmts_json)
    ca = CxAssembler(stmts)
    model_str = ca.make_model()
    res = {'model': model_str}
    return res
示例#3
0
def test_set_context():
    cxa = CxAssembler()
    cxa.add_statements([st_phos, st_dephos])
    cxa.make_model()
    cxa.set_context('BT20_BREAST')
    print(cxa.cx['nodeAttributes'])
    assert (len(cxa.cx['nodeAttributes']) == 10)
示例#4
0
def assemble_cx(stmts, name):
    ca = CxAssembler()
    ca.network_name = name
    ca.add_statements(stmts)
    ca.make_model()
    cx_str = ca.print_cx()
    return cx_str
示例#5
0
文件: api.py 项目: budakn/INDRA
def share_model_ndex():
    """Upload the model to NDEX"""
    if request.method == 'OPTIONS':
        return {}
    response = request.body.read().decode('utf-8')
    body = json.loads(response)
    stmts_str = body.get('stmts')
    stmts_json = json.loads(stmts_str)
    stmts = stmts_from_json(stmts_json["statements"])
    ca = CxAssembler(stmts)
    for n, v in body.items():
        ca.cx['networkAttributes'].append({'n': n, 'v': v, 'd': 'string'})
    ca.make_model()
    network_id = ca.upload_model(private=False)
    return {'network_id': network_id}
示例#6
0
def test_set_context():
    cxa = CxAssembler()
    cxa.add_statements([st_phos, st_dephos])
    cxa.make_model()
    cxa.set_context('BT20_BREAST')
    print(cxa.cx['nodeAttributes'])
    assert(len(cxa.cx['nodeAttributes']) == 10)
示例#7
0
def assemble_cx(stmts, out_file):
    """Return a CX assembler."""
    stmts = ac.filter_belief(stmts, 0.95)
    stmts = ac.filter_top_level(stmts)
    stmts = ac.strip_agent_context(stmts)
    ca = CxAssembler()
    ca.add_statements(stmts)
    model = ca.make_model()
    ca.save_model(out_file)
    return ca
示例#8
0
def test_cited():
    cxa = CxAssembler()
    cxa.add_statements([st_cited])
    cxa.make_model()
    assert (len(cxa.cx['citations']) == 1)
    assert (len(cxa.cx['edgeCitations']) == 1)
    citation = cxa.cx['citations'][0]
    assert (citation.get('dc:identifier') == 'pmid:12345')
    cid = citation.get('@id')
    assert (cxa.cx['edgeCitations'][0]['citations'][0] == cid)
    print(cxa.print_cx())
示例#9
0
def upload_to_ndex(stmts, ndex_cred):
    nd = ndex.client.Ndex('http://public.ndexbio.org',
                          username=ndex_cred.get('user'),
                          password=ndex_cred.get('password'))
    network_id = ndex_cred.get('network')

    ca = CxAssembler()
    ca.network_name = 'rasmachine'
    ca.add_statements(stmts)
    ca.make_model()
    cx_str = ca.print_cx()

    try:
        logger.info('Getting network summary...')
        summary = nd.get_network_summary(network_id)
    except Exception as e:
        logger.error('Could not get NDEx network summary.')
        logger.error(e)
        return

    try:
        logger.info('Updating network...')
        cx_stream = io.BytesIO(cx_str.encode('utf-8'))
        with open('cx_upload.cx', 'wb') as fh:
            fh.write(cx_str.encode('utf-8'))
        nd.update_cx_network(cx_stream, network_id)
    except Exception as e:
        logger.error('Could not update NDEx network.')
        logger.error(e)
        return
    ver_str = summary.get('version')
    new_ver = _increment_ndex_ver(ver_str)
    profile = {
        'name': summary.get('name'),
        'description': summary.get('description'),
        'version': new_ver,
    }
    logger.info('Updating NDEx network (%s) profile to %s' %
                (network_id, profile))
    profile_retries = 5
    for i in range(profile_retries):
        try:
            time.sleep(5)
            nd.update_network_profile(network_id, profile)
            break
        except Exception as e:
            logger.error('Could not update NDEx network profile.')
            logger.error(e)
示例#10
0
def assemble_cx(stmts, out_file):
    """Return a CX assembler."""
    stmts = ac.filter_belief(stmts, 0.95)
    stmts = ac.filter_top_level(stmts)
    stmts = ac.strip_agent_context(stmts)
    ca = CxAssembler()
    ca.add_statements(stmts)
    model = ca.make_model()
    ca.save_model(out_file)
    return ca
示例#11
0
def assemble_cx(stmts, out_file_prefix, network_type):
    """Return a CX assembler."""
    stmts = ac.filter_belief(stmts, 0.95)
    stmts = ac.filter_top_level(stmts)
    if network_type == 'direct':
        stmts = ac.filter_direct(stmts)

    out_file = '%s_%s.cx' % (out_file_prefix, network_type)

    ca = CxAssembler()
    ca.add_statements(stmts)
    model = ca.make_model()
    ca.save_model(out_file)
    return ca
示例#12
0
def test_invalid_cited():
    cxa = CxAssembler()
    cxa.add_statements([st_invalid_cited])
    cxa.make_model()
    assert (not cxa.cx['citations'])
    assert (not cxa.cx['edgeCitations'])
示例#13
0
def test_edge_attributes():
    cxa = CxAssembler()
    cxa.add_statements([st_phos, st_dephos])
    cxa.make_model()
    assert (len(cxa.cx['edgeAttributes']) == 12)
示例#14
0
def test_rasgap():
    cxa = CxAssembler()
    cxa.add_statements([st_rasgap])
    cxa.make_model()
    assert (len(cxa.cx['nodes']) == 2)
    assert (len(cxa.cx['edges']) == 1)
示例#15
0
def test_act():
    cxa = CxAssembler()
    cxa.add_statements([st_act, st_act2])
    cxa.make_model()
    assert (len(cxa.cx['nodes']) == 3)
    assert (len(cxa.cx['edges']) == 2)
示例#16
0
def test_rasgap():
    cxa = CxAssembler()
    cxa.add_statements([st_rasgap])
    cxa.make_model()
    assert(len(cxa.cx['nodes']) == 2)
    assert(len(cxa.cx['edges']) == 1)
示例#17
0
def test_make_print_model():
    cxa = CxAssembler()
    cxa.add_statements([st_phos])
    cx_str = cxa.make_model()
    assert(cx_str)
示例#18
0
def test_make_print_model():
    cxa = CxAssembler()
    cxa.add_statements([st_phos])
    cx_str = cxa.make_model()
    assert (cx_str)
示例#19
0
def test_no_pmid():
    cxa = CxAssembler([st_not_cited])
    cxa.make_model()
    assert(not cxa.cx['edgeCitations'])
示例#20
0
def test_dephos():
    cxa = CxAssembler()
    cxa.add_statements([st_phos, st_dephos])
    cxa.make_model()
    assert(len(cxa.cx['nodes']) == 3)
    assert(len(cxa.cx['edges']) == 2)
示例#21
0
def test_supports():
    cxa = CxAssembler()
    cxa.add_statements([st_cited])
    cxa.make_model()
    assert(len(cxa.cx['supports']) == 1)
    assert(len(cxa.cx['edgeSupports']) == 1)
示例#22
0
def test_invalid_cited():
    cxa = CxAssembler()
    cxa.add_statements([st_invalid_cited])
    cxa.make_model()
    assert(not cxa.cx['citations'])
    assert(not cxa.cx['edgeCitations'])
示例#23
0
def test_complex():
    cxa = CxAssembler()
    cxa.add_statements([st_complex])
    cxa.make_model()
    assert(len(cxa.cx['nodes']) == 3)
    assert(len(cxa.cx['edges']) == 3)
示例#24
0
def test_edge_attributes():
    cxa = CxAssembler()
    cxa.add_statements([st_phos, st_dephos])
    cxa.make_model()
    assert(len(cxa.cx['edgeAttributes']) == 8)
示例#25
0
def assemble_sif(stmts, data, out_file):
    """Return an assembled SIF."""
    # Filter for high-belief statements
    stmts = ac.filter_belief(stmts, 0.99)
    stmts = ac.filter_top_level(stmts)
    # Filter for Activation / Inhibition
    stmts_act = ac.filter_by_type(stmts, Activation)
    stmts_inact = ac.filter_by_type(stmts, Inhibition)
    stmts = stmts_act + stmts_inact
    # Get Ras227 and filter statments
    ras_genes = process_data.get_ras227_genes()
    #ras_genes = [x for x in ras_genes if x not in ['YAP1']]
    stmts = ac.filter_gene_list(stmts, ras_genes, 'all')

    # Get the drugs inhibiting their targets as INDRA
    # statements
    def get_drug_statements():
        drug_targets = process_data.get_drug_targets()
        drug_stmts = []
        for dn, tns in drug_targets.items():
            da = Agent(dn + ':Drugs')
            for tn in tns:
                ta = Agent(tn)
                drug_stmt = Inhibition(da, ta)
                drug_stmts.append(drug_stmt)
        return drug_stmts

    drug_stmts = get_drug_statements()
    stmts = stmts + drug_stmts
    # Rewrite statements to replace genes with their corresponding
    # antibodies when possible
    stmts = rewrite_ab_stmts(stmts, data)

    def filter_ab_edges(st, policy='all'):
        st_out = []
        for s in st:
            if policy == 'all':
                all_ab = True
                for a in s.agent_list():
                    if a is not None:
                        if a.name.find('_p') == -1 and \
                           a.name.find('Drugs') == -1:
                            all_ab = False
                            break
                if all_ab:
                    st_out.append(s)
            elif policy == 'one':
                any_ab = False
                for a in s.agent_list():
                    if a is not None and a.name.find('_p') != -1:
                        any_ab = True
                        break
                if any_ab:
                    st_out.append(s)
        return st_out

    stmts = filter_ab_edges(stmts, 'all')

    # Get a list of the AB names that end up being covered in the prior network
    # This is important because other ABs will need to be taken out of the
    # MIDAS file to work.
    def get_ab_names(st):
        prior_abs = set()
        for s in st:
            for a in s.agent_list():
                if a is not None:
                    if a.name.find('_p') != -1:
                        prior_abs.add(a.name)
        return sorted(list(prior_abs))

    pkn_abs = get_ab_names(stmts)

    def get_drug_names(st):
        prior_drugs = set()
        for s in st:
            for a in s.agent_list():
                if a is not None:
                    if a.name.find('Drugs') != -1:
                        prior_drugs.add(a.name.split(':')[0])
        return sorted(list(prior_drugs))

    pkn_drugs = get_drug_names(stmts)
    print('Boolean PKN contains these antibodies: %s' % ', '.join(pkn_abs))

    # Because of a bug in CNO,
    # node names containing AND need to be replaced
    # node names containing - need to be replaced
    # node names starting in a digit need to be replaced
    # must happen before SIF assembly, but not sooner as that will drop
    # names from the MIDAS file
    def rename_nodes(st):
        for s in st:
            for a in s.agent_list():
                if a is not None:
                    if a.name.find('AND') != -1:
                        a.name = a.name.replace('AND', 'A_ND')
                    if a.name.find('-') != -1:
                        a.name = a.name.replace('-', '_')
                    if a.name[0].isdigit():
                        a.name = 'abc_' + a.name

    rename_nodes(stmts)
    # Make the SIF model
    sa = SifAssembler(stmts)
    sa.make_model(use_name_as_key=True)
    sif_str = sa.print_model()
    # assemble and dump a cx of the sif
    ca = CxAssembler()
    ca.add_statements(stmts)
    model = ca.make_model()
    ca.save_model('sif.cx')
    with open(out_file, 'wb') as fh:
        fh.write(sif_str.encode('utf-8'))
    # Make the MIDAS data file used for training the model
    midas_data = process_data.get_midas_data(data, pkn_abs, pkn_drugs)
    return sif_str
示例#26
0
def assemble_cx(stmts, save_file):
    cxa = CxAssembler(stmts)
    cxa.make_model(add_indra_json=False)
    cxa.save_model(save_file)
    return cxa
示例#27
0
def test_no_pmid():
    cxa = CxAssembler([st_not_cited])
    cxa.make_model()
    assert (not cxa.cx['edgeCitations'])
示例#28
0
def test_dephos():
    cxa = CxAssembler()
    cxa.add_statements([st_phos, st_dephos])
    cxa.make_model()
    assert (len(cxa.cx['nodes']) == 3)
    assert (len(cxa.cx['edges']) == 2)
示例#29
0
def test_complex():
    cxa = CxAssembler()
    cxa.add_statements([st_complex])
    cxa.make_model()
    assert (len(cxa.cx['nodes']) == 3)
    assert (len(cxa.cx['edges']) == 3)
示例#30
0
def test_supports():
    cxa = CxAssembler()
    cxa.add_statements([st_cited])
    cxa.make_model()
    assert (len(cxa.cx['supports']) == 1)
    assert (len(cxa.cx['edgeSupports']) == 1)
示例#31
0
def test_act():
    cxa = CxAssembler()
    cxa.add_statements([st_act, st_act2])
    cxa.make_model()
    assert(len(cxa.cx['nodes']) == 3)
    assert(len(cxa.cx['edges']) == 2)