<commit_before>"""Contains ligand-specific objects and functions."""

import requests
import json
import random
from .gtop import *
from .exceptions import *
from .shared import *

def get_ligand_by_id(ligand_id):
    """Returns a Ligand object of the ligand with the given ID.

    :param int ligand_id: The GtoP IDof the Ligand desired.
    :rtype: :py:class:`Ligand`
    :raises: :class:`.NoSuchLigandError`: if no such ligand exists in the database"""

    json_data = get_json_from_gtop("ligands/%i" % ligand_id)
    if json_data:
        return Ligand(json_data)
    else:
        raise NoSuchLigandError


def get_random_ligand(ligand_type=None):
    """Returns a random ligand, with the option to specify the ligand type.
    This can take a few seconds as it must first request *all* ligands.

    :param str ligand_type: If not None, the function will pick a ligand from\
     this category only.

    :rtype: :py:class:`Ligand`
    :raises: :class:`.NoSuchTypeError`: if a ligand type is supplied which doesn't exist
    """

    if ligand_type:
        json_data = get_json_from_gtop("ligands?type=%s" % ligand_type.lower())
        if not json_data:
            raise NoSuchTypeError("There are no ligands of type %s" % ligand_type)
    else:
        json_data = get_json_from_gtop("ligands")
    return Ligand(random.choice(json_data))


def get_all_ligands():
    """Returns a list of all ligands in the Guide to PHARMACOLOGY database. This
    can take a few seconds.

    :returns: list of :py:class:`Ligand` objects"""

    json_data = get_json_from_gtop("ligands")
    return [Ligand(l) for l in json_data]


def get_ligands_by(criteria):
    """Get all ligands which specify the criteria dictionary.

    :param dict criteria: A dictionary of `field=value` pairs. See the\
     `GtoP web services page <http://www.guidetopharmacology.org/\
     webServices.jsp#ligands>`_ for key/value pairs which can be supplied.
    :returns: list of :py:class:`Ligand` objects."""

    search_string = "&".join(["%s=%s" % (key, criteria[key]) for key in criteria])
    json_data = get_json_from_gtop("ligands?%s" % search_string)
    if json_data:
        return [Ligand(l) for l in json_data]
    else:
        return []


def get_ligand_by_name(name):
    """Returns the ligand which matches the name given. Will raise a
    NoSuchLigandError if no ligand by that ID exists.

    :param str name: The name of the ligand to search for. Note that synonyms \
    will not be searched.
    :rtype: :py:class:`Ligand`
    :raises:  :class:`.NoSuchLigandError`: if no such ligand exists in the database."""

    ligands = get_ligands_by({"name": name})
    if ligands:
        return ligands[0]
    else:
        raise NoSuchLigandError



class Ligand:
    """A Guide to PHARMACOLOGY ligand object.

    .. py:attribute:: ligand_id (int):

        The ligand's GtoP ID.

    .. py:attribute:: abbreviation:

        The ligand's SMILES string.

    .. py:attribute:: inn:

        The ligand's INN name.

    .. py:attribute:: ligand_type:

        The ligand type.

    .. py:attribute:: species:<commit_msg>Remove argument from ligand docs<commit_after>"""Contains ligand-specific objects and functions."""

import requests
import json
import random
from .gtop import *
from .exceptions import *
from .shared import *

def get_ligand_by_id(ligand_id):
    """Returns a Ligand object of the ligand with the given ID.

    :param int ligand_id: The GtoP IDof the Ligand desired.
    :rtype: :py:class:`Ligand`
    :raises: :class:`.NoSuchLigandError`: if no such ligand exists in the database"""

    json_data = get_json_from_gtop("ligands/%i" % ligand_id)
    if json_data:
        return Ligand(json_data)
    else:
        raise NoSuchLigandError


def get_random_ligand(ligand_type=None):
    """Returns a random ligand, with the option to specify the ligand type.
    This can take a few seconds as it must first request *all* ligands.

    :param str ligand_type: If not None, the function will pick a ligand from\
     this category only.

    :rtype: :py:class:`Ligand`
    :raises: :class:`.NoSuchTypeError`: if a ligand type is supplied which doesn't exist
    """

    if ligand_type:
        json_data = get_json_from_gtop("ligands?type=%s" % ligand_type.lower())
        if not json_data:
            raise NoSuchTypeError("There are no ligands of type %s" % ligand_type)
    else:
        json_data = get_json_from_gtop("ligands")
    return Ligand(random.choice(json_data))


def get_all_ligands():
    """Returns a list of all ligands in the Guide to PHARMACOLOGY database. This
    can take a few seconds.

    :returns: list of :py:class:`Ligand` objects"""

    json_data = get_json_from_gtop("ligands")
    return [Ligand(l) for l in json_data]


def get_ligands_by(criteria):
    """Get all ligands which specify the criteria dictionary.

    :param dict criteria: A dictionary of `field=value` pairs. See the\
     `GtoP web services page <http://www.guidetopharmacology.org/\
     webServices.jsp#ligands>`_ for key/value pairs which can be supplied.
    :returns: list of :py:class:`Ligand` objects."""

    search_string = "&".join(["%s=%s" % (key, criteria[key]) for key in criteria])
    json_data = get_json_from_gtop("ligands?%s" % search_string)
    if json_data:
        return [Ligand(l) for l in json_data]
    else:
        return []


def get_ligand_by_name(name):
    """Returns the ligand which matches the name given. Will raise a
    NoSuchLigandError if no ligand by that ID exists.

    :param str name: The name of the ligand to search for. Note that synonyms \
    will not be searched.
    :rtype: :py:class:`Ligand`
    :raises:  :class:`.NoSuchLigandError`: if no such ligand exists in the database."""

    ligands = get_ligands_by({"name": name})
    if ligands:
        return ligands[0]
    else:
        raise NoSuchLigandError



class Ligand:
    """A Guide to PHARMACOLOGY ligand object.

    .. py:attribute:: ligand_id:

        The ligand's GtoP ID.

    .. py:attribute:: abbreviation:

        The ligand's SMILES string.

    .. py:attribute:: inn:

        The ligand's INN name.

    .. py:attribute:: ligand_type:

        The ligand type.

    .. py:attribute:: species:
{'fraction_non_alphanumeric': 0.10065715659560827, 'fraction_numerical': 0.00032056419297964416, 'mean_word_length': 3.193548387096774, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7440992', 'n_tokens_mistral': 2095, 'n_tokens_neox': 1944, 'n_words': 660}