Exemple #1
0
def add_or_update_individuals_and_families(project, individual_records):
    """Add or update individual and family records in the given project.

    Args:
        project (object): Django ORM model for the project to add families to
        individual_records (list): A list of JSON records representing individuals. See
            pedigree_info_utils#convert_fam_file_rows_to_json(..)
    """
    families = {}
    for record in individual_records:
        family, created = Family.objects.get_or_create(
            project=project, family_id=record['familyId'])
        if created:
            if not family.display_name:
                family.display_name = family.family_id
                family.save()

            logger.info("Created family: %s" % (family, ))

        individual, created = Individual.objects.get_or_create(
            family=family, individual_id=record['individualId'])
        update_individual_from_json(individual,
                                    record,
                                    allow_unknown_keys=True)

        # apply additional json fields which don't directly map to Individual model fields
        individual.phenotips_eid = individual.guid  # use this instead of individual_id to avoid chance of collisions

        if created:
            # create new PhenoTips patient record
            patient_record = create_patient(project, individual.phenotips_eid)
            individual.phenotips_patient_id = patient_record['id']

            logger.info(
                "Created PhenoTips record with patient id %s and external id %s"
                % (str(individual.phenotips_patient_id),
                   str(individual.phenotips_eid)))

        if record.get('hpoTerms'):
            # update phenotips hpo ids
            logger.info("Setting PhenoTips HPO Terms to: %s" %
                        (record.get('hpoTerms'), ))
            set_patient_hpo_terms(project,
                                  individual.phenotips_eid,
                                  record.get('hpoTerms'),
                                  is_external_id=True)

        if not individual.display_name:
            individual.display_name = individual.individual_id

        individual.save()

        _deprecated_update_original_individual_data(project, individual)

        families[family.family_id] = family

    # update pedigree images
    for family in families.values():
        update_pedigree_image(family)
Exemple #2
0
def add_or_update_individuals_and_families(project,
                                           individual_records,
                                           user=None):
    """Add or update individual and family records in the given project.

    Args:
        project (object): Django ORM model for the project to add families to
        individual_records (list): A list of JSON records representing individuals. See
            the return value of pedigree_info_utils#convert_fam_file_rows_to_json(..)

    Return:
        2-tuple: updated_families, updated_individuals containing Django ORM models
    """
    families = {}
    updated_individuals = []
    for i, record in enumerate(individual_records):
        # family id will be in different places in the json depending on whether it comes from a flat uploaded file or from the nested individual object
        family_id = record.get('familyId') or record.get('family',
                                                         {}).get('familyId')
        if not family_id:
            raise ValueError(
                "record #%s doesn't contain a 'familyId' key: %s" %
                (i, record))

        if 'individualId' not in record and 'individualGuid' not in record:
            raise ValueError(
                "record #%s doesn't contain an 'individualId' key: %s" %
                (i, record))

        family, created = get_or_create_seqr_model(Family,
                                                   project=project,
                                                   family_id=family_id)

        if created:
            logger.info("Created family: %s", family)
            if not family.display_name:
                update_seqr_model(family, display_name=family.family_id)

        # uploaded files do not have unique guid's so fall back to a combination of family and individualId
        if record.get('individualGuid'):
            individual_filters = {'guid': record['individualGuid']}
        else:
            individual_filters = {
                'family': family,
                'individual_id': record['individualId']
            }

        individual, created = get_or_create_seqr_model(Individual,
                                                       **individual_filters)
        record['family'] = family
        record.pop('familyId', None)
        update_individual_from_json(individual,
                                    record,
                                    allow_unknown_keys=True,
                                    user=user)

        updated_individuals.append(individual)

        # apply additional json fields which don't directly map to Individual model fields
        individual.phenotips_eid = individual.guid  # use guid instead of indiv_id to avoid collisions

        if created:
            # create new PhenoTips patient record
            patient_record = create_patient(project, individual)
            update_seqr_model(individual,
                              phenotips_patient_id=patient_record['id'],
                              case_review_status='I')

            logger.info(
                "Created PhenoTips record with patient id %s and external id %s"
                % (str(individual.phenotips_patient_id),
                   str(individual.phenotips_eid)))

        if record.get(JsonConstants.HPO_TERMS_PRESENT_COLUMN) or record.get(
                JsonConstants.FINAL_DIAGNOSIS_OMIM_COLUMN):
            # update phenotips hpo ids
            logger.info("Setting PhenoTips HPO Terms to: %s" %
                        (record.get(JsonConstants.HPO_TERMS_PRESENT_COLUMN), ))
            set_patient_hpo_terms(
                project,
                individual,
                hpo_terms_present=record.get(
                    JsonConstants.HPO_TERMS_PRESENT_COLUMN, []),
                hpo_terms_absent=record.get(
                    JsonConstants.HPO_TERMS_ABSENT_COLUMN, []),
                final_diagnosis_mim_ids=record.get(
                    JsonConstants.FINAL_DIAGNOSIS_OMIM_COLUMN, []))

        if not individual.display_name:
            update_seqr_model(individual,
                              display_name=individual.individual_id)

        families[family.family_id] = family

    updated_families = list(families.values())

    # update pedigree images
    update_pedigree_images(updated_families)

    return updated_families, updated_individuals
Exemple #3
0
def add_or_update_individuals_and_families(project, individual_records):
    """Add or update individual and family records in the given project.

    Args:
        project (object): Django ORM model for the project to add families to
        individual_records (list): A list of JSON records representing individuals. See
            the return value of pedigree_info_utils#convert_fam_file_rows_to_json(..)

    Return:
        2-tuple: updated_families, updated_individuals containing Django ORM models
    """
    families = {}
    updated_individuals = []
    for i, record in enumerate(individual_records):
        if 'familyId' not in record:
            raise ValueError(
                "record #%s doesn't contain a 'familyId' key: %s" %
                (i, record))
        if 'individualId' not in record:
            raise ValueError(
                "record #%s doesn't contain an 'individualId' key: %s" %
                (i, record))

        family, created = Family.objects.get_or_create(
            project=project, family_id=record['familyId'])

        if created:
            logger.info("Created family: %s", family)
            if not family.display_name:
                family.display_name = family.family_id
                family.save()

        individual, created = Individual.objects.get_or_create(
            family=family, individual_id=record['individualId'])

        update_individual_from_json(individual,
                                    record,
                                    allow_unknown_keys=True)

        updated_individuals.append(individual)

        # apply additional json fields which don't directly map to Individual model fields
        individual.phenotips_eid = individual.guid  # use guid instead of indiv_id to avoid collisions

        if created:
            # create new PhenoTips patient record
            patient_record = create_patient(project, individual.phenotips_eid)
            individual.phenotips_patient_id = patient_record['id']
            individual.case_review_status = 'I'

            logger.info(
                "Created PhenoTips record with patient id %s and external id %s"
                % (str(individual.phenotips_patient_id),
                   str(individual.phenotips_eid)))

        if record.get('hpoTerms'):
            # update phenotips hpo ids
            logger.info("Setting PhenoTips HPO Terms to: %s" %
                        (record.get('hpoTerms'), ))
            set_patient_hpo_terms(project,
                                  individual.phenotips_eid,
                                  record.get('hpoTerms'),
                                  is_external_id=True)

        if not individual.display_name:
            individual.display_name = individual.individual_id

        individual.save()

        _deprecated_update_original_individual_data(project, individual)

        families[family.family_id] = family

    updated_families = list(families.values())

    # update pedigree images
    update_pedigree_images(updated_families)

    return updated_families, updated_individuals