<commit_before>from contentbase import (
    calculated_property,
    collection,
    load_schema,
)
from .base import (
    Item,
    paths_filtered_by_status,
)
from .shared_calculated_properties import (
    CalculatedBiosampleSlims,
    CalculatedBiosampleSynonyms
)


@collection(
    name='biosamples',
    unique_key='accession',
    properties={
        'title': 'Biosamples',
        'description': 'Biosamples used in the ENCODE project',
    })
class Biosample(Item, CalculatedBiosampleSlims, CalculatedBiosampleSynonyms):
    item_type = 'biosample'
    schema = load_schema('encoded:schemas/biosample.json')
    name_key = 'accession'
    rev = {
        'characterizations': ('BiosampleCharacterization', 'characterizes'),
    }
    embedded = [
        'donor',
        'donor.mutated_gene',
        'donor.organism',
        'donor.characterizations',
        'donor.characterizations.award',
        'donor.characterizations.lab',
        'donor.characterizations.submitted_by',
        'donor.references',
        'model_organism_donor_constructs',
        'model_organism_donor_constructs.submitted_by',
        'model_organism_donor_constructs.target',
        'model_organism_donor_constructs.documents',
        'model_organism_donor_constructs.documents.award',
        'model_organism_donor_constructs.documents.lab',
        'model_organism_donor_constructs.documents.submitted_by',
        'submitted_by',
        'lab',
        'award',
        'award.pi.lab',
        'source',
        'treatments.protocols.submitted_by',
        'treatments.protocols.lab',
        'treatments.protocols.award',
        'constructs.documents.submitted_by',
        'constructs.documents.award',
        'constructs.documents.lab',
        'constructs.target',
        'protocol_documents.lab',
        'protocol_documents.award',
        'protocol_documents.submitted_by',
        'derived_from',
        'part_of',
        'pooled_from',
        'characterizations.submitted_by',
        'characterizations.award',
        'characterizations.lab',
        'rnais',
        'rnais.target.organism',
        'rnais.source',
        'rnais.documents.submitted_by',
        'rnais.documents.award',
        'rnais.documents.lab',
        'organism',
        'references',
        'talens',
        'talens.documents',
        'talens.documents.award',
        'talens.documents.lab',
        'talens.documents.submitted_by',
    ]

    @calculated_property(schema={
        "title": "Sex",
        "type": "string"
    })
    def sex(self, request, donor=None, model_organism_sex=None, organism=None):
        humanFlag = False
        if organism is not None:
            organismObject = request.embed(organism, '@@object')
            if organismObject['scientific_name'] == 'Homo sapiens':
                humanFlag = True

        if humanFlag == True:
            if donor is not None:# try to get the sex from the donor
                donorObject = request.embed(donor, '@@object')
                if 'sex' in donorObject:
                    return donorObject['sex']
                else:
                    return 'unknown'
            else:
                return 'unknown'
        else:
            if model_organism_sex is not None:
                return model_organism_sex
            else:
                return 'unknown'

    @calculated_property(schema={
        "title": "Age",
        "type": "string"
    })
    def age(self, request, donor=None, model_organism_age=None, organism=None):
        humanFlag = False
        if organism is not None:
            organismObject = request.embed(organism, '@@object')
            if organismObject['scientific_name']=='Homo sapiens':
                humanFlag = True

        if humanFlag == True:
            if donor is not None:  # try to get the age from the donor
                donorObject = request.embed(donor, '@@object')
                if 'age' in donorObject:
                    return donorObject['age']
                else:
                    return 'unknown'<commit_msg>added biosample summary calculated dictionary<commit_after>from contentbase import (
    calculated_property,
    collection,
    load_schema,
)
from .base import (
    Item,
    paths_filtered_by_status,
)
from .shared_calculated_properties import (
    CalculatedBiosampleSlims,
    CalculatedBiosampleSynonyms
)


@collection(
    name='biosamples',
    unique_key='accession',
    properties={
        'title': 'Biosamples',
        'description': 'Biosamples used in the ENCODE project',
    })
class Biosample(Item, CalculatedBiosampleSlims, CalculatedBiosampleSynonyms):
    item_type = 'biosample'
    schema = load_schema('encoded:schemas/biosample.json')
    name_key = 'accession'
    rev = {
        'characterizations': ('BiosampleCharacterization', 'characterizes'),
    }
    embedded = [
        'donor',
        'donor.mutated_gene',
        'donor.organism',
        'donor.characterizations',
        'donor.characterizations.award',
        'donor.characterizations.lab',
        'donor.characterizations.submitted_by',
        'donor.references',
        'model_organism_donor_constructs',
        'model_organism_donor_constructs.submitted_by',
        'model_organism_donor_constructs.target',
        'model_organism_donor_constructs.documents',
        'model_organism_donor_constructs.documents.award',
        'model_organism_donor_constructs.documents.lab',
        'model_organism_donor_constructs.documents.submitted_by',
        'submitted_by',
        'lab',
        'award',
        'award.pi.lab',
        'source',
        'treatments',
        'treatments.protocols.submitted_by',
        'treatments.protocols.lab',
        'treatments.protocols.award',
        'constructs',
        'constructs.documents.submitted_by',
        'constructs.documents.award',
        'constructs.documents.lab',
        'constructs.target',
        'protocol_documents.lab',
        'protocol_documents.award',
        'protocol_documents.submitted_by',
        'derived_from',
        'part_of',
        'pooled_from',
        'characterizations.submitted_by',
        'characterizations.award',
        'characterizations.lab',
        'rnais',
        'rnais.target',
        'rnais.target.organism',
        'rnais.source',
        'rnais.documents.submitted_by',
        'rnais.documents.award',
        'rnais.documents.lab',
        'organism',
        'references',
        'talens',
        'talens.documents',
        'talens.documents.award',
        'talens.documents.lab',
        'talens.documents.submitted_by',
    ]

    @calculated_property(schema={
        "title": "Summary",
        "type": "string",
    })
    def summary(self, request,
                organism=None,
                donor=None,
                age=None,
                age_units=None,
                life_stage=None,
                sex=None,
                biosample_term_name=None,
                biosample_type=None,
                depleted_in_term_name=None,
                phase=None,
                subcellular_fraction_term_name=None,
                post_synchronization_time=None,
                post_synchronization_time_units=None,
                treatments=None,
                part_of=None,
                derived_from=None,
                transfection_method=None,
                transfection_type=None,
                talens=None,
                constructs=None,
                model_organism_donor_constructs=None,
                rnais=None):

        dict_of_phrases = {}

        if organism is not None:
            organismObject = request.embed(organism, '@@object')
            dict_of_phrases['scientific_name'] = organismObject['scientific_name']

            if organismObject['scientific_name'] != 'Homo sapiens':  # model organism
                if donor is not None:
                    donorObject = request.embed(donor, '@@object')
                    if 'strain_name' in donorObject:
                        dict_of_phrases['strain_name'] = donorObject['strain_name']

        if age is not None and age_units is not None:
            dict_of_phrases['age_display'] = str(age) + ' ' + age_units

        if life_stage is not None and life_stage != 'unknown':
            dict_of_phrases['life_stage'] = life_stage

        if sex is not None and sex != 'unknown':
            dict_of_phrases['sex'] = sex

        if biosample_term_name is not None:
            dict_of_phrases['biosample_term_name'] = biosample_term_name

        if biosample_type is not None and biosample_type != 'whole organisms':
            dict_of_phrases['biosample_type'] = biosample_type

        if depleted_in_term_name is not None and len(depleted_in_term_name) > 0:
            dict_of_phrases['depleted_in'] = 'depleted in '+str(depleted_in_term_name)

        if phase is not None:
            dict_of_phrases['phase'] = 'in cell phase '+phase

        if subcellular_fraction_term_name is not None:
            dict_of_phrases['fractionated'] = 'fractionated to '+subcellular_fraction_term_name

        # relevant only in worms and flys

        if post_synchronization_time is not None and post_synchronization_time_units is not None:
            dict_of_phrases['synchronization'] = (post_synchronization_time +
                                                  ' ' + post_synchronization_time_units +
                                                  ' post synchronization')

        if treatments is not None and len(treatments) > 0:
            treatments_list = []
            for t in treatments:
                treatmentObject = request.embed(t, '@@object')
                if 'concentration' in treatmentObject and \
                   'concentration_units' in treatmentObject and \
                   'treatment_term_name' in treatmentObject and \
                   'duration' in treatmentObject and \
                   'duration_units' in treatmentObject:
                    treatments_list.append('treated with ' +
                                           str(treatmentObject['concentration']) + ' ' +
                                           treatmentObject['concentration_units'] + ' ' +
                                           treatmentObject['treatment_term_name'] + ' for ' +
                                           str(treatmentObject['duration']) + ' ' +
                                           treatmentObject['duration_units'])
            if len(treatments_list) > 0:
                dict_of_phrases['treatments'] = treatments_list

        if part_of is not None:
            part_ofObject = request.embed(part_of, '@@object')
            dict_of_phrases['part_of'] = 'part of biosample '+part_ofObject['accession']

        if derived_from is not None:
            derived_fromObject = request.embed(derived_from, '@@object')
            dict_of_phrases['derived_from'] = ('derived from biosample ' +
                                               derived_fromObject['accession'])

        if transfection_method is not None and transfection_type is not None:
            dict_of_phrases['transfection'] = (transfection_type + ' transfection via ' +
                                               transfection_method)  # will need to correct grammar

        if talens is not None and len(talens) > 0:
            talens_list = []
            for t in talens:
                talenObject = request.embed(t, '@@object')
                if 'name' in talenObject:
                    talens_list.append(talenObject['name'])
            dict_of_phrases['talens'] = 'modified with talens: '+str(talens_list)

        if constructs is not None and len(constructs) > 0:
            constructs_list = []
            for c in constructs:
                constructObject = request.embed(c, '@@object')
                if 'construct_type' in constructObject and \
                   'vector_backbone_name' in constructObject and \
                   'target' in constructObject:
                    targetObject = request.embed(constructObject['target'], '@@object')
                    constructs_list.append(constructObject['construct_type'] + ' ' +
                                           constructObject['vector_backbone_name'] + ' ' +
                                           targetObject['title'])
            dict_of_phrases['constructs'] = 'biosample constructs: '+str(constructs_list)

        if model_organism_donor_constructs is not None and len(model_organism_donor_constructs) > 0:
            constructs_list = []
            for c in model_organism_donor_constructs:
                constructObject = request.embed(c, '@@object')
                if 'construct_type' in constructObject and \
                   'vector_backbone_name' in constructObject and \
                   'target' in constructObject:
                    targetObject = request.embed(constructObject['target'], '@@object')
                    constructs_list.append(constructObject['construct_type'] + ' ' +
                                           constructObject['vector_backbone_name'] + ' ' +
                                           targetObject['title'])
            dict_of_phrases['model_organism_constructs'] = ('biosample model' +
                                                            'organism donor constructs: ' +
                                                            str(constructs_list))

        if rnais is not None and len(rnais) > 0:
            rnais_list = []
            for r in rnais:
                rnaiObject = request.embed(r, '@@object')
                if 'rnai_type' in rnaiObject and \
                   'target' in rnaiObject:
                    targetObject = request.embed(rnaiObject['target'], '@@object')
                    rnais_list.append(rnaiObject['rnai_type'] + ' ' +
                                      targetObject['title'])
            dict_of_phrases['rnais'] = 'rnais '+str(rnais_list)

        return dict_of_phrases

    @calculated_property(schema={
        "title": "Sex",
        "type": "string"
    })
    def sex(self, request, donor=None, model_organism_sex=None, organism=None):
        humanFlag = False
        if organism is not None:
            organismObject = request.embed(organism, '@@object')
            if organismObject['scientific_name'] == 'Homo sapiens':
                humanFlag = True

        if humanFlag is True:
            if donor is not None:  # try to get the sex from the donor
                donorObject = request.embed(donor, '@@object')
                if 'sex' in donorObject:
                    return donorObject['sex']
                else:
                    return 'unknown'
            else:
                return 'unknown'
        else:
            if model_organism_sex is not None:
                return model_organism_sex
            else:
                return 'unknown'

    @calculated_property(schema={
        "title": "Age",
        "type": "string"
    })
    def age(self, request, donor=None, model_organism_age=None, organism=None):
        humanFlag = False
        if organism is not None:
            organismObject = request.embed(organism, '@@object')
            if organismObject['scientific_name']=='Homo sapiens':
                humanFlag = True

        if humanFlag == True:
            if donor is not None:  # try to get the age from the donor
                donorObject = request.embed(donor, '@@object')
                if 'age' in donorObject:
                    return donorObject['age']
                else:
                    return 'unknown'
{'fraction_non_alphanumeric': 0.09514674851333206, 'fraction_numerical': 0.0004475989513395997, 'mean_word_length': 1.8824179874677478, 'pattern_counts': {'":': 10, '<': 3, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 35, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4867818', 'n_tokens_mistral': 4486, 'n_tokens_neox': 4165, 'n_words': 837}