Python builtins.dict() Examples

The following are 30 code examples of builtins.dict(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module builtins , or try the search function .
Example #1
Source File: action_parser.py    From indra with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def make_annotation(self):
        """Returns a dictionary with all properties of the action
        and each of its action mentions."""
        annotation = dict()

        # Put all properties of the action object into the annotation
        for item in dir(self):
            if len(item) > 0 and item[0] != '_' and \
                    not inspect.ismethod(getattr(self, item)):
                annotation[item] = getattr(self, item)

        # Add properties of each action mention
        annotation['action_mentions'] = list()
        for action_mention in self.action_mentions:
            annotation_mention = action_mention.make_annotation()
            annotation['action_mentions'].append(annotation_mention)

        return annotation 
Example #2
Source File: context_client.py    From indra with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def get_mutations(gene_names, cell_types):
    """Return protein amino acid changes in given genes and cell types.

    Parameters
    ----------
    gene_names : list
        HGNC gene symbols for which mutations are queried.
    cell_types : list
        List of cell type names in which mutations are queried.
        The cell type names follow the CCLE database conventions.

        Example: LOXIMVI_SKIN, BT20_BREAST

    Returns
    -------
    res : dict[dict[list]]
        A dictionary keyed by cell line, which contains another dictionary
        that is keyed by gene name, with a list of amino acid substitutions
        as values.
    """
    mutations = cbio_client.get_ccle_mutations(gene_names, cell_types)
    return mutations 
Example #3
Source File: chembl_client.py    From indra with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def get_evidence(assay):
    """Given an activity, return an INDRA Evidence object.

    Parameters
    ----------
    assay : dict
        an activity from the activities list returned by a query to the API

    Returns
    -------
    ev : :py:class:`Evidence`
        an :py:class:`Evidence` object containing the kinetics of the
    """
    kin = get_kinetics(assay)
    source_id = assay.get('assay_chembl_id')
    if not kin:
        return None
    annotations = {'kinetics': kin}
    chembl_doc_id = str(assay.get('document_chembl_id'))
    pmid = get_pmid(chembl_doc_id)
    ev = Evidence(source_api='chembl', pmid=pmid, source_id=source_id,
                  annotations=annotations)
    return ev 
Example #4
Source File: chembl_client.py    From indra with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def send_query(query_dict):
    """Query ChEMBL API

    Parameters
    ----------
    query_dict : dict
        'query' : string of the endpoint to query
        'params' : dict of params for the query

    Returns
    -------
    js : dict
        dict parsed from json that is unique to the submitted query
    """
    query = query_dict['query']
    params = query_dict['params']
    url = 'https://www.ebi.ac.uk/chembl/api/data/' + query + '.json'
    r = requests.get(url, params=params)
    r.raise_for_status()
    js = r.json()
    return js 
Example #5
Source File: lincs_client.py    From indra with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def get_protein_refs(self, hms_lincs_id):
        """Get the refs for a protein from the LINCs protein metadata.

        Parameters
        ----------
        hms_lincs_id : str
            The HMS LINCS ID for the protein

        Returns
        -------
        dict
            A dictionary of protein references.
        """
        # TODO: We could get phosphorylation states from the protein data.
        refs = {'HMS-LINCS': hms_lincs_id}

        entry = self._get_entry_by_id(self._prot_data, hms_lincs_id)
        # If there is no entry for this ID
        if not entry:
            return refs
        mappings = dict(egid='Gene ID', up='UniProt ID')
        for k, v in mappings.items():
            if entry.get(v):
                refs[k.upper()] = entry.get(v)
        return refs 
Example #6
Source File: chembl_client.py    From indra with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def activities_by_target(activities):
    """Get back lists of activities in a dict keyed by ChEMBL target id

    Parameters
    ----------
    activities : list
        response from a query returning activities for a drug

    Returns
    -------
    targ_act_dict : dict
        dictionary keyed to ChEMBL target ids with lists of activity ids
    """
    targ_act_dict = defaultdict(lambda: [])
    for activity in activities:
        target_chembl_id = activity['target_chembl_id']
        activity_id = activity['activity_id']
        targ_act_dict[target_chembl_id].append(activity_id)
    for target_chembl_id in targ_act_dict:
        targ_act_dict[target_chembl_id] = \
            list(set(targ_act_dict[target_chembl_id]))
    return targ_act_dict 
Example #7
Source File: chembl_client.py    From indra with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def get_protein_targets_only(target_chembl_ids):
    """Given list of ChEMBL target ids, return dict of SINGLE PROTEIN targets

    Parameters
    ----------
    target_chembl_ids : list
        list of chembl_ids as strings

    Returns
    -------
    protein_targets : dict
        dictionary keyed to ChEMBL target ids with lists of activity ids
    """
    protein_targets = {}
    for target_chembl_id in target_chembl_ids:
        target = query_target(target_chembl_id)
        if 'SINGLE PROTEIN' in target['target_type']:
            protein_targets[target_chembl_id] = target
    return protein_targets 
Example #8
Source File: cbio_client.py    From indra with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def get_case_lists(study_id):
    """Return a list of the case set ids for a particular study.

    TAKE NOTE the "case_list_id" are the same thing as "case_set_id"
    Within the data, this string is referred to as a "case_list_id".
    Within API calls it is referred to as a 'case_set_id'.
    The documentation does not make this explicitly clear.

    Parameters
    ----------
    study_id : str
        The ID of the cBio study.
        Example: 'cellline_ccle_broad' or 'paad_icgc'

    Returns
    -------
    case_set_ids : dict[dict[int]]
        A dict keyed to cases containing a dict keyed to genes
        containing int
    """
    data = {'cmd': 'getCaseLists',
            'cancer_study_id': study_id}
    df = send_request(**data)
    case_set_ids = df['case_list_id'].tolist()
    return case_set_ids 
Example #9
Source File: api.py    From indra with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def _load_data():
    """Load the data from the csv in data.

    The "gene_id" is the Entrez gene id, and the "approved_symbol" is the
    standard gene symbol. The "hms_id" is the LINCS ID for the drug.

    Returns
    -------
    data : list[dict]
        A list of dicts of row values keyed by the column headers extracted from
        the csv file, described above.
    """
    # Get the cwv reader object.
    csv_path = path.join(HERE, path.pardir, path.pardir, 'resources',
                         DATAFILE_NAME)
    data_iter = list(read_unicode_csv(csv_path))

    # Get the headers.
    headers = data_iter[0]

    # For some reason this heading is oddly formatted and inconsistent with the
    # rest, or with the usual key-style for dicts.
    headers[headers.index('Approved.Symbol')] = 'approved_symbol'
    return [{header: val for header, val in zip(headers, line)}
            for line in data_iter[1:]] 
Example #10
Source File: action_parser.py    From indra with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def _init_action_list(self, action_filename):
        """Parses the file and populates the data."""

        self.actions = list()
        self.hiid_to_action_index = dict()

        f = codecs.open(action_filename, 'r', encoding='latin-1')
        first_line = True
        for line in f:
            line = line.rstrip()
            if first_line:
                # Ignore the first line
                first_line = False
            else:
                self.actions.append(GenewaysAction(line))

                latestInd = len(self.actions)-1
                hiid = self.actions[latestInd].hiid
                if hiid in self.hiid_to_action_index:
                    raise Exception('action hiid not unique: %d' % hiid)
                self.hiid_to_action_index[hiid] = latestInd 
Example #11
Source File: test_hume.py    From indra with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def test_migration_events():
    bp = process_jsonld_file(migration_events)
    assert bp, "Processor is none."
    assert len(bp.statements) == 1
    stmt = bp.statements[0]
    assert isinstance(stmt, Migration)
    assert len(stmt.context.locations) == 2
    location_dict = dict()
    assert isinstance(stmt.context.locations[0]['location'], RefContext)
    for location_ref in stmt.context.locations:
        location_dict[location_ref['role']] = location_ref['location'].name
    assert location_dict['origin'] == "South Sudan"
    assert location_dict['destination'] == (
        "Federal Democratic Republic of Ethiopia")
    assert isinstance(stmt.context.time, TimeContext)
    assert "2018" in stmt.context.time.text
    assert isinstance(stmt.delta, QuantitativeState)
    assert stmt.delta.value == 10000
    assert stmt.delta.unit == "Monthly"
    assert stmt.delta.modifier == "Min" 
Example #12
Source File: DataFrameModelManager.py    From qtpandas with MIT License 6 votes vote down vote up
def update_file(self, filepath, df, notes=None):
        """
        Sets a new DataFrame for the DataFrameModel registered to filepath.
        :param filepath (str)
            The filepath to the DataFrameModel to be updated
        :param df (pandas.DataFrame)
            The new DataFrame to register to the model.

        :param notes (str, default None)
            Optional notes to register along with the update.

        """
        assert isinstance(df, pd.DataFrame), "Cannot update file with type '{}'".format(type(df))

        self._models[filepath].setDataFrame(df, copyDataFrame=False)

        if notes:
            update = dict(date=pd.Timestamp(datetime.datetime.now()),
                                                     notes=notes)

            self._updates[filepath].append(update)
        self._paths_updated.append(filepath) 
Example #13
Source File: parse_tees.py    From indra with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def __init__(self, sentence_segmentations):
        self.index_to_text = dict()
        # It would be less memory intensive to use a tree, but this is simpler
        # to code

        root = etree.fromstring(sentence_segmentations.encode('utf-8'))
        for element in root.iter('sentence'):
            offset_str = element.get('charOffset')
            offset_list = offset_str.split('-')
            #
            first_offset = int(offset_list[0])
            second_offset = int(offset_list[1])
            text = element.get('text')

            for i in range(first_offset, second_offset+1):
                self.index_to_text[i] = text 
Example #14
Source File: processor.py    From indra with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def _get_dict_from_list(dict_key, list_of_dicts):
    """Retrieve a specific dict from a list of dicts.

    Parameters
    ----------
    dict_key : str
        The (single) key of the dict to be retrieved from the list.
    list_of_dicts : list
        The list of dicts to search for the specific dict.

    Returns
    -------
    dict value
        The value associated with the dict_key (e.g., a list of nodes or
        edges).
    """
    the_dict = [cur_dict for cur_dict in list_of_dicts
                if cur_dict.get(dict_key)]
    if not the_dict:
        raise ValueError('Could not find a dict with key %s' % dict_key)
    return the_dict[0][dict_key] 
Example #15
Source File: test_signor.py    From indra with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def test_parse_csv_from_file():
    # Should work with both data file and complexes
    sp = process_from_file(test_data_file, test_complexes_file)
    assert isinstance(sp._data, list)
    assert isinstance(sp._data[0], _SignorRow_)
    assert isinstance(sp.statements, list)
    assert isinstance(sp.statements[0], Statement)
    # Test the complex map
    assert isinstance(sp.complex_map, dict)
    assert len(sp.complex_map) == 9
    assert 'SIGNOR-C1' in sp.complex_map
    assert isinstance(sp.complex_map['SIGNOR-C1'], list)
    assert sp.complex_map['SIGNOR-C1'] == ['P23511', 'P25208', 'Q13952']
    # Make sure we don't error if Complexes data is not provided
    sp = process_from_file(test_data_file)
    assert isinstance(sp.statements[0], Statement)
    assert sp.complex_map == {} 
Example #16
Source File: utilities.py    From skidl with MIT License 6 votes vote down vote up
def merge_dicts(dct, merge_dct):
    """ 
    Dict merge that recurses through both dicts and updates keys.

    Args:
        dct: The dict that will be updated.
        merge_dct: The dict whose values will be inserted into dct.

    Returns:
        Nothing.
    """

    for k, v in list(merge_dct.items()):
        if (
            k in dct
            and isinstance(dct[k], dict)
            and isinstance(merge_dct[k], collections.Mapping)
        ):
            merge_dicts(dct[k], merge_dct[k])
        else:
            dct[k] = merge_dct[k] 
Example #17
Source File: processor.py    From indra with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def _initialize_edge_attributes(self):
        edge_attr = _get_dict_from_list('edgeAttributes', self.cx)
        for ea in edge_attr:
            edge_id = ea.get('po')
            ea_type = ea.get('n')
            ea_value = ea.get('v')
            ea_info = self._edge_attributes.get(edge_id)
            # If we don't have any info about this edge, initialize an empty
            # dict
            if ea_info is None:
                ea_info = {'pmids': []}
                self._edge_attributes[edge_id] = ea_info
            # Collect PMIDs from the various edge types
            if ea_type == 'ndex:citation' or ea_type == 'citation_ids':
                pmids = []
                assert isinstance(ea_value, list)
                # ndex:citations are in the form 'pmid:xxxxx'
                for cit in ea_value:
                    if cit.upper().startswith('PMID:'):
                        pmid = cit[5:]
                        if pmid: # Check for empty PMID strings!
                            pmids.append(pmid)
                    else:
                        logger.info("Unexpected PMID format: %s" % cit)
                ea_info['pmids'] += pmids 
Example #18
Source File: api.py    From indra with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def process_cx(cx_json, summary=None, require_grounding=True):
    """Process a CX JSON object into Statements.

    Parameters
    ----------
    cx_json : list
        CX JSON object.
    summary : Optional[dict]
        The network summary object which can be obtained via
        get_network_summary through the web service. THis contains metadata
        such as the owner and the creation time of the network.
    require_grounding: bool
        Whether network nodes lacking grounding information should be included
        among the extracted Statements (default is True).

    Returns
    -------
    NdexCxProcessor
        Processor containing Statements.
    """
    ncp = NdexCxProcessor(cx_json, summary=summary,
                          require_grounding=require_grounding)
    ncp.get_statements()
    return ncp 
Example #19
Source File: spice.py    From skidl with MIT License 5 votes vote down vote up
def _get_kwargs(part, kw):
    """Return a dict of keyword arguments to PySpice element constructor."""
    kwargs = {}

    for key, param_name in kw.items():
        try:
            # The key indicates some attribute of the part.
            part_attr = getattr(part, key)
        except AttributeError:
            pass
        else:
            # If the keyword argument is a Part, then substitute the part
            # reference because it's probably a control current for something
            # like a current-controlled source or switch.
            if isinstance(part_attr, Part):
                kwargs.update({param_name: part_attr.ref})
            # If the keyword argument is a Net, substitute the net name.
            elif isinstance(part_attr, Net):
                kwargs.update({param_name: node(part_attr)})
            # If the keyword argument is a Pin, skip it. It gets handled below.
            elif isinstance(part_attr, Pin):
                continue
            else:
                kwargs.update({param_name: part_attr})

    for pin in part.pins:
        if pin.is_connected():
            try:
                param_name = kw[pin.name]
                kwargs.update({param_name: node(pin)})
            except KeyError:
                logger.error(
                    "Part {}-{} has no {} pin: {}".format(
                        part.ref, part.name, pin.name, part
                    )
                )

    return kwargs 
Example #20
Source File: mongo_import.py    From pygermanet with MIT License 5 votes vote down vote up
def insert_lemmatisation_data(germanet_db):
    '''
    Creates the lemmatiser collection in the given MongoDB instance
    using the data derived from the Projekt deutscher Wortschatz.

    Arguments:
    - `germanet_db`: a pymongo.database.Database object
    '''
    # drop the database collection if it already exists
    germanet_db.lemmatiser.drop()
    num_lemmas = 0
    input_file = gzip.open(os.path.join(os.path.dirname(__file__),
                                        LEMMATISATION_FILE))
    for line in input_file:
        line = line.decode('iso-8859-1').strip().split('\t')
        assert len(line) == 2
        germanet_db.lemmatiser.insert(dict(list(zip(('word', 'lemma'), line))))
        num_lemmas += 1
    input_file.close()
    # index the collection on 'word'
    germanet_db.lemmatiser.create_index('word')

    print('Inserted {0} lemmatiser entries.'.format(num_lemmas))


# ------------------------------------------------------------
#  Information content for GermaNet similarity
# ------------------------------------------------------------ 
Example #21
Source File: assemble_corpus.py    From indra with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def _filter(kwargs, arg_list):
    return dict(filter(lambda x: x[0] in arg_list, kwargs.items())) 
Example #22
Source File: germanet.py    From pygermanet with MIT License 5 votes vote down vote up
def hypernym_distances(self):
        '''
        Returns a list of synsets on the path from this synset to the root
        node, counting the distance of each node on the way.
        '''
        retval = dict()
        for (synset, dist) in reduce(
                set.union,
                [[(synset, idx) for (idx, synset) in enumerate(reversed(path))]
                 for path in self.hypernym_paths],
                set()):
            if synset not in retval or dist < retval[synset]:
                retval[synset] = dist
        return set(retval.items()) 
Example #23
Source File: mongo_import.py    From pygermanet with MIT License 5 votes vote down vote up
def insert_lexical_information(germanet_db, lex_files):
    '''
    Reads in the given lexical information files and inserts their
    contents into the given MongoDB database.

    Arguments:
    - `germanet_db`: a pymongo.database.Database object
    - `lex_files`: a list of paths to XML files containing lexial
      information
    '''
    # drop the database collections if they already exist
    germanet_db.lexunits.drop()
    germanet_db.synsets.drop()
    # inject data from XML files into the database
    for lex_file in lex_files:
        synsets = read_lexical_file(lex_file)
        for synset in synsets:
            synset = dict((SYNSET_KEY_REWRITES.get(key, key), value)
                          for (key, value) in synset.items())
            lexunits = synset['lexunits']
            synset['lexunits'] = germanet_db.lexunits.insert(lexunits)
            synset_id = germanet_db.synsets.insert(synset)
            for lexunit in lexunits:
                lexunit['synset']   = synset_id
                lexunit['category'] = synset['category']
                germanet_db.lexunits.save(lexunit)
    # index the two collections by id
    germanet_db.synsets.create_index('id')
    germanet_db.lexunits.create_index('id')
    # also index lexunits by lemma, lemma-pos, and lemma-pos-sensenum
    germanet_db.lexunits.create_index([('orthForm', DESCENDING)])
    germanet_db.lexunits.create_index([('orthForm', DESCENDING),
                                       ('category', DESCENDING)])
    germanet_db.lexunits.create_index([('orthForm', DESCENDING),
                                       ('category', DESCENDING),
                                       ('sense', DESCENDING)])
    print('Inserted {0} synsets, {1} lexical units.'.format(
        germanet_db.synsets.count(),
        germanet_db.lexunits.count())) 
Example #24
Source File: gmail_client.py    From indra with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def get_headers(msg):
    """Takes email.message.Message object initialized from unicode string,
    returns dict with header fields."""
    headers = {}
    for k in msg.keys():
        # decode_header decodes header but does not convert charset, so these
        # may still be bytes, even in Python 3. However, if it's ASCII
        # only (hence unambiguous encoding), the header fields come back
        # as str (unicode) in Python 3.
        (header_txt, charset) = email.header.decode_header(msg[k])[0]
        if charset is not None:
            header_txt = header_txt.decode(charset)
        headers[k] = header_txt
    return headers 
Example #25
Source File: test_kami_assembler.py    From indra with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def test_demod_one_step():
    stmt = Deubiquitination(mek, erk, 'K', '123')
    ka = KamiAssembler()
    ka.add_statements([stmt])
    model = ka.make_model()
    print(json.dumps(model, indent=1))
    assert isinstance(model, dict)
    assert isinstance(model['graphs'], list)
    assert isinstance(model['typing'], list)
    graph_list = model['graphs']
    assert len(graph_list) == 2
    assert len(graph_list[1]['graph']['edges']) == 3
    assert len(graph_list[1]['graph']['nodes']) == 4 
Example #26
Source File: test_kami_assembler.py    From indra with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def test_mod_one_step():
    stmt = Phosphorylation(mek, erk)
    ka = KamiAssembler()
    ka.add_statements([stmt])
    model = ka.make_model()
    print(json.dumps(model, indent=1))
    assert isinstance(model, dict)
    assert isinstance(model['graphs'], list)
    assert isinstance(model['typing'], list)
    graph_list = model['graphs']
    assert len(graph_list) == 2
    assert len(graph_list[1]['graph']['edges']) == 3
    assert len(graph_list[1]['graph']['nodes']) == 4 
Example #27
Source File: test_kami_assembler.py    From indra with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def test_complex_not_bound_condition():
    stmt = Complex([mek_no_braf, erk])
    ka = KamiAssembler()
    ka.add_statements([stmt])
    model = ka.make_model()
    assert isinstance(model, dict)
    assert isinstance(model['graphs'], list)
    assert isinstance(model['typing'], list)
    graph_list = model['graphs']
    assert len(graph_list) == 3
    assert len(graph_list[1]['graph']['edges']) == 6
    assert len(graph_list[1]['graph']['nodes']) == 7
    print(json.dumps(model, indent=1)) 
Example #28
Source File: test_kami_assembler.py    From indra with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def test_complex_bound_condition():
    stmt = Complex([mek_braf, erk])
    ka = KamiAssembler()
    ka.add_statements([stmt])
    model = ka.make_model()
    assert isinstance(model, dict)
    assert isinstance(model['graphs'], list)
    assert isinstance(model['typing'], list)
    graph_list = model['graphs']
    assert len(graph_list) == 3
    assert len(graph_list[1]['graph']['edges']) == 6
    assert len(graph_list[1]['graph']['nodes']) == 7
    print(json.dumps(model, indent=1)) 
Example #29
Source File: test_kami_assembler.py    From indra with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def test_complex_no_conditions():
    stmt = Complex([mek, erk])
    ka = KamiAssembler()
    ka.add_statements([stmt])
    model = ka.make_model()
    assert isinstance(model, dict)
    assert isinstance(model['graphs'], list)
    assert isinstance(model['typing'], list)
    graph_list = model['graphs']
    assert len(graph_list) == 3
    assert len(graph_list[1]['graph']['edges']) == 4
    assert len(graph_list[1]['graph']['nodes']) == 5
    print(json.dumps(model, indent=1)) 
Example #30
Source File: test_ndex_cx_processor.py    From indra with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def test_initialize_node_agents():
    assert isinstance(ncp_file._node_agents, dict)
    for ndex_id, agent in ncp_file._node_agents.items():
        assert isinstance(ndex_id, int)
        assert isinstance(agent, Agent)
        assert agent.db_refs.get('HGNC')
        assert agent.db_refs.get('UP')