Example #1
0
def donor(rec_id):
    """
    Backports a huma-donor record belonging to
    https://www.encodeproject.org/profiles/human_donor.json.

    The record will be checked for existence in Pulsar by doing a search on the field
    `donor.upstread_identifer`` using as a query value the record's accession on the ENCODE Portal,
    and also its aliases alias.

    Args:
        rec_id: `str`. An identifier (alias or uuid) for a human-donor record on the ENCODE Portal.

    Returns:
        `dict`: The JSON representation of the existing Donor if it already exists in
        in Pulsar, otherwise the POST response.
    """
    rec = ENC_CONN.get(rec_id, ignore404=False)
    accession = rec[ACCESSION_PROP]
    aliases = rec[ALIASES_PROP]
    # Check if upstream exists already in Pulsar:
    pulsar_rec = models.Donor.find_by({UPSTREAM_PROP: [accession, *aliases]})
    if pulsar_rec:
        return pulsar_rec
    payload = {}
    AGE_PROP = "age"
    if AGE_PROP in rec:
        payload[AGE_PROP] = rec[AGE_PROP]
    GENDER_PROP = "sex"
    if GENDER_PROP in rec:
        payload["gender"] = rec[GENDER_PROP]
    payload[UPSTREAM_PROP] = accession
    try:
        payload["name"] = euu.strip_alias_prefix(aliases[0])
    except IndexError:
        payload["name"] = rec_id
    return models.Donor.post(payload)
Example #2
0
 def test_strip_alias_prefix(self):
     """Tests the function ``strip_alias_prefix()`` for success.
     """
     alias = "michael-snyder:B-167"
     self.assertEqual(utils.strip_alias_prefix(alias), "B-167")
Example #3
0
def treatment(rec_id, patch=False):
    """
    Backports a treatement record belonging to
    https://www.encodeproject.org/profiles/treatment.json.
    The required properties in the ENCODE Portal are:

    1. treatment_term_name,
    2. treatment_type.

    An example on the Portal: https://www.encodeproject.org/treatments/933a1ff2-43a2-4a54-9c87-aad228d0033e/.
    Identifying properties on the Portal are 'aliases' and 'uuid'.

    Args:
        rec_id: `str`. An identifier (alias or uuid) for a treatment record on the ENCODE Portal.

    Returns:
        `dict`: The JSON representation of the existing Treatment if it already exists in
        in Pulsar, otherwise the POST response.
    """
    rec = ENC_CONN.get(rec_id, ignore404=False)
    aliases = rec[ALIASES_PROP]
    #check if upstream exists already in Pulsar:
    pulsar_rec = models.Treatment.find_by(
        {UPSTREAM_PROP: [*aliases, rec[UUID_PROP], rec["@id"]]})
    if pulsar_rec and not patch:
        return pulsar_rec

    payload = {}
    payload["aliases"] = aliases
    pulsar_document_ids = []
    for doc_id in rec["documents"]:
        pulsar_document_ids.append(document(doc_id))
    payload["document_ids"] = pulsar_document_ids
    payload[UPSTREAM_PROP] = rec[UUID_PROP]
    payload["concentration"] = rec["amount"]
    amount_units = rec["amount_units"]
    pulsar_amount_units_rec = models.ConcentrationUnit.find_by(
        payload={"name": amount_units})
    if not pulsar_amount_units_rec:
        raise Exception(
            "ConcentrationUnit '{}' couldn't be found.".format(amount_units))
    pulsar_amount_units_rec = pulsar_amount_units_rec["concentration_unit"]
    payload["concentration_unit_id"] = pulsar_amount_units_rec["id"]
    payload["duration"] = rec["duration"]
    payload["duration_units"] = rec["duration_units"]
    temp_prop_name = "temperature"
    if temp_prop_name in rec:
        temp = rec[temp_prop_name]
        temp_units = rec["temperature_units"]
        if temp_units == "Kelvin":
            temp = pulsarpy.utils.kelvin_to_celsius(temp)
        elif temp_units == "Fahrenheit":
            temp = pulsarpy.utils.fahrenheit_to_celsius(temp)
        payload[temp_prop_name] = rec[temp_prop_name]
    ttn = rec["treatment_term_name"]
    tti = rec["treatment_term_id"]
    #Create new TreatmentTermName if not found in Pulsar.
    pulsar_ttn_rec = treatment_term_name(ttn, tti)["treatment_term_name"]
    payload["treatment_term_name_id"] = pulsar_ttn_rec["id"]
    payload["treatment_type"] = rec["treatment_type"]
    #The Portal's treatment model doesn't include a name prop or a description prop.
    # Thus, 'name' shall be set to be equal to 'upstream_identifier'.
    payload["name"] = euu.strip_alias_prefix(upstream)
    if not pulsar_rec:
        return models.Treatment.post(payload)
    else:
        pulsar_obj = models.Treatment(pulsar_rec["id"])
        return pulsar_obj.patch(payload)