Beispiel #1
0
 def get_agent(cls, cl_agent):
     """Get an agent from the kqml cl-json representation (KQMLList)."""
     agent_json = cls.converter.cl_to_json(cl_agent)
     if isinstance(agent_json, list):
         return [ensure_agent_type(Agent._from_json(agj))
                 for agj in agent_json]
     else:
         return ensure_agent_type(Agent._from_json(agent_json))
    def map_agent(self, agent, do_rename):
        """Return the given Agent with its grounding mapped.

        This function grounds a single agent. It returns the new Agent object
        (which might be a different object if we load a new agent state
        from json) or the same object otherwise.

        Parameters
        ----------
        agent : :py:class:`indra.statements.Agent`
            The Agent to map.
        do_rename: bool
            If True, the Agent name is updated based on the mapped grounding.
            If do_rename is True the priority for setting the name is
            FamPlex ID, HGNC symbol, then the gene name
            from Uniprot.

        Returns
        -------
        grounded_agent : :py:class:`indra.statements.Agent`
            The grounded Agent.
        maps_to_none : bool
            True if the Agent is in the grounding map and maps to None.
        """
        # We always standardize DB refs as a functionality in the
        # GroundingMapper. If a new module is implemented which is
        # responsible for standardizing grounding, this can be removed.
        agent.db_refs = self.standardize_db_refs(agent.db_refs)
        # If there is no TEXT available, we can return immediately since we
        # can't do mapping
        agent_text = agent.db_refs.get('TEXT')
        if not agent_text:
            # We still do the name standardization here
            if do_rename:
                self.standardize_agent_name(agent, standardize_refs=False)
            return agent, False
        mapped_to_agent_json = self.agent_map.get(agent_text)
        if mapped_to_agent_json:
            mapped_to_agent = \
                Agent._from_json(mapped_to_agent_json['agent'])
            return mapped_to_agent, False
        # Look this string up in the grounding map
        # If not in the map, leave agent alone and continue
        if agent_text in self.gm:
            map_db_refs = self.gm[agent_text]
            # If it's in the map but it maps to None, then filter out
            # this statement by skipping it
            if map_db_refs is None:
                logger.debug("Skipping %s" % agent_text)
                return None, True
            # If it has a value that's not None, map it and add it
            else:
                self.update_agent_db_refs(agent, agent_text, do_rename)
        # This happens whene there is an Agent text but it is not in the
        # grounding map. We still do the name standardization here.
        if do_rename:
            self.standardize_agent_name(agent, standardize_refs=False)
        # Otherwise just return
        return agent, False
Beispiel #3
0
    def map_agent(self, agent, do_rename):
        """Return the given Agent with its grounding mapped.

        This function grounds a single agent. It returns the new Agent object
        (which might be a different object if we load a new agent state
        from json) or the same object otherwise.

        Parameters
        ----------
        agent : :py:class:`indra.statements.Agent`
            The Agent to map.
        do_rename: bool
            If True, the Agent name is updated based on the mapped grounding.
            If do_rename is True the priority for setting the name is
            FamPlex ID, HGNC symbol, then the gene name
            from Uniprot.

        Returns
        -------
        grounded_agent : :py:class:`indra.statements.Agent`
            The grounded Agent.
        """
        # We always standardize DB refs as a functionality in the
        # GroundingMapper. If a new module is implemented which is
        # responsible for standardizing grounding, this can be removed.
        agent.db_refs = self.standardize_db_refs(agent.db_refs)
        # If there is no TEXT available, we can return immediately since we
        # can't do mapping
        agent_text = agent.db_refs.get('TEXT')
        if not agent_text:
            # We still do the name standardization here
            if do_rename:
                self.standardize_agent_name(agent, standardize_refs=False)
            return agent

        # 1. Check if there is a full agent mapping and apply if there is
        if agent_text in self.agent_map:
            mapped_to_agent = \
                Agent._from_json(self.agent_map[agent_text]['agent'])
            return mapped_to_agent

        # 2. Look agent text up in the grounding map
        if agent_text in self.grounding_map:
            self.update_agent_db_refs(agent, self.grounding_map[agent_text],
                                      do_rename)

        # 3. Look agent text up in the misgrounding map
        if agent_text in self.misgrounding_map:
            self.remove_agent_db_refs(agent, self.misgrounding_map[agent_text])
        # This happens when there is an Agent text but it is not in the
        # grounding map. We still do the name standardization here.
        if do_rename:
            self.standardize_agent_name(agent, standardize_refs=False)
        # Otherwise just return
        return agent
Beispiel #4
0
    def map_agent(self, agent, do_rename):
        """Return the given Agent with its grounding mapped.

        This function grounds a single agent. It returns the new Agent object
        (which might be a different object if we load a new agent state
        from json) or the same object otherwise.

        Parameters
        ----------
        agent : :py:class:`indra.statements.Agent`
            The Agent to map.
        do_rename: bool
            If True, the Agent name is updated based on the mapped grounding.
            If do_rename is True the priority for setting the name is
            FamPlex ID, HGNC symbol, then the gene name
            from Uniprot.

        Returns
        -------
        grounded_agent : :py:class:`indra.statements.Agent`
            The grounded Agent.
        maps_to_none : bool
            True if the Agent is in the grounding map and maps to None.
        """

        agent_text = agent.db_refs.get('TEXT')
        mapped_to_agent_json = self.agent_map.get(agent_text)
        if mapped_to_agent_json:
            mapped_to_agent = \
                Agent._from_json(mapped_to_agent_json['agent'])
            return mapped_to_agent, False
        # Look this string up in the grounding map
        # If not in the map, leave agent alone and continue
        if agent_text in self.gm.keys():
            map_db_refs = self.gm[agent_text]
        else:
            return agent, False

        # If it's in the map but it maps to None, then filter out
        # this statement by skipping it
        if map_db_refs is None:
            # Increase counter if this statement has not already
            # been skipped via another agent
            logger.debug("Skipping %s" % agent_text)
            return None, True
        # If it has a value that's not None, map it and add it
        else:
            # Otherwise, update the agent's db_refs field
            self.update_agent_db_refs(agent, agent_text, do_rename)
        return agent, False
Beispiel #5
0
    def map_agent(self, agent, do_rename):
        """Return the given Agent with its grounding mapped.

        This function grounds a single agent. It returns the new Agent object
        (which might be a different object if we load a new agent state
        from json) or the same object otherwise.

        Parameters
        ----------
        agent : :py:class:`indra.statements.Agent`
            The Agent to map.
        do_rename: bool
            If True, the Agent name is updated based on the mapped grounding.
            If do_rename is True the priority for setting the name is
            FamPlex ID, HGNC symbol, then the gene name
            from Uniprot.

        Returns
        -------
        grounded_agent : :py:class:`indra.statements.Agent`
            The grounded Agent.
        maps_to_none : bool
            True if the Agent is in the grounding map and maps to None.
        """

        agent_text = agent.db_refs.get('TEXT')
        mapped_to_agent_json = self.agent_map.get(agent_text)
        if mapped_to_agent_json:
            mapped_to_agent = \
                Agent._from_json(mapped_to_agent_json['agent'])
            return mapped_to_agent, False
        # Look this string up in the grounding map
        # If not in the map, leave agent alone and continue
        if agent_text in self.gm.keys():
            map_db_refs = self.gm[agent_text]
        else:
            return agent, False

        # If it's in the map but it maps to None, then filter out
        # this statement by skipping it
        if map_db_refs is None:
            # Increase counter if this statement has not already
            # been skipped via another agent
            logger.debug("Skipping %s" % agent_text)
            return None, True
        # If it has a value that's not None, map it and add it
        else:
            # Otherwise, update the agent's db_refs field
            self.update_agent_db_refs(agent, agent_text, do_rename)
        return agent, False
Beispiel #6
0
    def map_agent(self, agent, do_rename):
        """Grounds an agent; returns the new agent object (which might be
        a different object if we load a new agent state from javascript).

        Parameters
        ----------
        agent: indra.statements.Agent
            The agent to map
        do_rename: bool
            Whether to rename the agent text

        Returns
        -------
        grounded_agent: indra.statements.Agent
            The grounded agent
        maps_to_none: bool
            Whether the agent is in the grounding map and maps to None
        """

        agent_text = agent.db_refs.get('TEXT')
        mapped_to_agent_json = self.agent_map.get(agent_text)
        if mapped_to_agent_json:
            mapped_to_agent = \
                Agent._from_json(mapped_to_agent_json['agent'])
            return mapped_to_agent, False
        # Look this string up in the grounding map
        # If not in the map, leave agent alone and continue
        if agent_text in self.gm.keys():
            map_db_refs = self.gm[agent_text]
        else:
            return agent, False
        # If it's in the map but it maps to None, then filter out
        # this statement by skipping it
        if map_db_refs is None:
            # Increase counter if this statement has not already
            # been skipped via another agent
            logger.debug("Skipping %s" % agent_text)
            return None, True
        # If it has a value that's not None, map it and add it
        else:
            # Otherwise, update the agent's db_refs field
            self.update_agent_db_refs(agent, agent_text, do_rename)
        return agent, False
Beispiel #7
0
 def map_agents(self, stmts, do_rename=True):
     # Make a copy of the stmts
     mapped_stmts = []
     num_skipped = 0
     # Iterate over the statements
     for stmt in stmts:
         mapped_stmt = deepcopy(stmt)
         # Iterate over the agents
         skip_stmt = False
         mapped_agent_list = mapped_stmt.agent_list()
         for idx, agent in enumerate(mapped_agent_list):
             if agent is None or agent.db_refs.get('TEXT') is None:
                 continue
             agent_text = agent.db_refs.get('TEXT')
             mapped_to_agent_json = self.agent_map.get(agent_text)
             if mapped_to_agent_json:
                 mapped_to_agent = \
                     Agent._from_json(mapped_to_agent_json['agent'])
                 mapped_agent_list[idx] = mapped_to_agent
                 mapped_stmt.set_agent_list(mapped_agent_list)
             # Look this string up in the grounding map
             # If not in the map, leave agent alone and continue
             try:
                 map_db_refs = self.gm[agent_text]
             except KeyError:
                 continue
             # If it's in the map but it maps to None, then filter out
             # this statement by skipping it
             if map_db_refs is None:
                 # Increase counter if this statement has not already
                 # been skipped via another agent
                 if not skip_stmt:
                     num_skipped += 1
                 logger.debug("Skipping %s" % agent_text)
                 skip_stmt = True
             # If it has a value that's not None, map it and add it
             else:
                 # Otherwise, update the agent's db_refs field
                 gene_name = None
                 map_db_refs = deepcopy(self.gm.get(agent_text))
                 up_id = map_db_refs.get('UP')
                 hgnc_sym = map_db_refs.get('HGNC')
                 if up_id and not hgnc_sym:
                     gene_name = uniprot_client.get_gene_name(up_id, False)
                     if gene_name:
                         hgnc_id = hgnc_client.get_hgnc_id(gene_name)
                         if hgnc_id:
                             map_db_refs['HGNC'] = hgnc_id
                 elif hgnc_sym and not up_id:
                     # Override the HGNC symbol entry from the grounding
                     # map with an HGNC ID
                     hgnc_id = hgnc_client.get_hgnc_id(hgnc_sym)
                     if hgnc_id:
                         map_db_refs['HGNC'] = hgnc_id
                         # Now get the Uniprot ID for the gene
                         up_id = hgnc_client.get_uniprot_id(hgnc_id)
                         if up_id:
                             map_db_refs['UP'] = up_id
                     # If there's no HGNC ID for this symbol, raise an
                     # Exception
                     else:
                         raise ValueError('No HGNC ID corresponding to gene '
                                          'symbol %s in grounding map.' %
                                          hgnc_sym)
                 # If we have both, check the gene symbol ID against the
                 # mapping from Uniprot
                 elif up_id and hgnc_sym:
                     # Get HGNC Symbol from Uniprot
                     gene_name = uniprot_client.get_gene_name(up_id)
                     if not gene_name:
                         raise ValueError('No gene name found for Uniprot '
                                          'ID %s (expected %s)' %
                                          (up_id, hgnc_sym))
                     # We got gene name, compare it to the HGNC name
                     else:
                         if gene_name != hgnc_sym:
                             raise ValueError('Gene name %s for Uniprot ID '
                                              '%s does not match HGNC '
                                              'symbol %s given in grounding '
                                              'map.' %
                                              (gene_name, up_id, hgnc_sym))
                         else:
                             hgnc_id = hgnc_client.get_hgnc_id(hgnc_sym)
                             if not hgnc_id:
                                 raise ValueError('No HGNC ID '
                                                  'corresponding to gene '
                                                  'symbol %s in grounding '
                                                  'map.' % hgnc_sym)
                 # Assign the DB refs from the grounding map to the agent
                 agent.db_refs = map_db_refs
                 # Are we renaming right now?
                 if do_rename:
                     # If there's a Bioentities ID, prefer that for the name
                     if agent.db_refs.get('BE'):
                         agent.name = agent.db_refs.get('BE')
                     # Get the HGNC symbol or gene name (retrieved above)
                     elif hgnc_sym is not None:
                         agent.name = hgnc_sym
                     elif gene_name is not None:
                         agent.name = gene_name
         # Check if we should skip the statement
         if not skip_stmt:
             mapped_stmts.append(mapped_stmt)
     logger.info('%s statements filtered out' % num_skipped)
     return mapped_stmts
Beispiel #8
0
def test_all_target_list():
    d = DTDA()
    assert d.all_targets
    assert all('HGNC' in Agent._from_json(e).db_refs.keys()
               for e in d.all_targets)
Beispiel #9
0
def test_all_drug_list():
    d = DTDA()
    assert d.all_drugs
    assert all('HMS-LINCS' in Agent._from_json(e).db_refs.keys()
               for e in d.all_drugs)