Exemplo n.º 1
0
def delete_individuals(project, individual_guids, user):
    """Delete one or more individuals

    Args:
        project (object): Django ORM model for project
        individual_guids (list): GUIDs of individuals to delete

    Returns:
        list: Family objects for families with deleted individuals
    """

    individuals_to_delete = Individual.objects.filter(
        family__project=project, guid__in=individual_guids)

    Sample.bulk_delete(user, individual__family__project=project, individual__guid__in=individual_guids)
    IgvSample.bulk_delete(user, individual__family__project=project, individual__guid__in=individual_guids)

    families = {individual.family for individual in individuals_to_delete}

    Individual.bulk_delete(user, queryset=individuals_to_delete)

    update_pedigree_images(families, user)

    families_with_deleted_individuals = list(families)

    return families_with_deleted_individuals
Exemplo n.º 2
0
def _update_variant_samples(matched_sample_id_to_sample_record, user, elasticsearch_index, loaded_date=None,
                            dataset_type=Sample.DATASET_TYPE_VARIANT_CALLS, sample_type=Sample.SAMPLE_TYPE_WES):
    if not loaded_date:
        loaded_date = timezone.now()
    updated_samples = [sample.id for sample in matched_sample_id_to_sample_record.values()]

    activated_sample_guids = Sample.bulk_update(user, {
        'elasticsearch_index': elasticsearch_index,
        'is_active': True,
        'loaded_date': loaded_date,
    }, id__in=updated_samples, is_active=False)

    matched_sample_id_to_sample_record.update({
        sample.sample_id: sample for sample in Sample.objects.filter(guid__in=activated_sample_guids)
    })

    inactivate_samples = Sample.objects.filter(
        individual_id__in={sample.individual_id for sample in matched_sample_id_to_sample_record.values()},
        is_active=True,
        dataset_type=dataset_type,
        sample_type=sample_type,
    ).exclude(id__in=updated_samples)

    inactivate_sample_guids = Sample.bulk_update(user, {'is_active': False}, queryset=inactivate_samples)

    return inactivate_sample_guids
Exemplo n.º 3
0
def _delete_project(project_guid, user):
    """Delete project.

    Args:
        project_guid (string): GUID of the project to delete
        user (object): Django ORM model for the user
    """
    project = Project.objects.get(guid=project_guid)
    check_user_created_object_permissions(project, user)

    IgvSample.bulk_delete(user, individual__family__project=project)
    Sample.bulk_delete(user, individual__family__project=project)

    Individual.bulk_delete(user, family__project=project)

    Family.bulk_delete(user, project=project)

    project.delete_model(user, user_can_delete=True)
Exemplo n.º 4
0
def match_sample_ids_to_sample_records(
    project,
    user,
    sample_ids,
    sample_type,
    dataset_type=Sample.DATASET_TYPE_VARIANT_CALLS,
    elasticsearch_index=None,
    create_sample_records=True,
    sample_id_to_individual_id_mapping=None,
    loaded_date=None,
):
    """Goes through the given list of sample_ids and finds existing Sample records of the given
    sample_type and dataset_type with ids from the list. For sample_ids that aren't found to have existing Sample
    records, it looks for Individual records that have an individual_id that either exactly or
    approximately equals one of the sample_ids in the list or is contained in the optional
    sample_id_to_individual_id_mapping and optionally creates new Sample records for these.

    Args:
        project (object): Django ORM project model
        user (object): Django ORM User model
        sample_ids (list): a list of sample ids for which to find matching Sample records
        sample_type (string): one of the Sample.SAMPLE_TYPE_* constants
        dataset_type (string): one of the Sample.DATASET_TYPE_* constants
        elasticsearch_index (string): an optional string specifying the index where the dataset is loaded
        max_edit_distance (int): max permitted edit distance for approximate matches
        create_sample_records (bool): whether to create new Sample records for sample_ids that
            don't match existing Sample records, but do match individual_id's of existing
            Individual records.
        sample_id_to_individual_id_mapping (object): Mapping between sample ids and their corresponding individual ids

    Returns:
        tuple:
            [0] dict: sample_id_to_sample_record containing the matching Sample records (including any
            newly-created ones)
            [1] array: array of the sample_ids of any samples that were created
    """

    sample_id_to_sample_record = find_matching_sample_records(
        project, sample_ids, sample_type, dataset_type, elasticsearch_index)
    logger.debug(
        str(len(sample_id_to_sample_record)) + " exact sample record matches",
        user)

    remaining_sample_ids = set(sample_ids) - set(
        sample_id_to_sample_record.keys())
    if len(remaining_sample_ids) > 0:
        already_matched_individual_ids = {
            sample.individual.individual_id
            for sample in sample_id_to_sample_record.values()
        }

        remaining_individuals_dict = {
            i.individual_id: i
            for i in Individual.objects.filter(
                family__project=project).exclude(
                    individual_id__in=already_matched_individual_ids)
        }

        # find Individual records with exactly-matching individual_ids
        sample_id_to_individual_record = {}
        for sample_id in remaining_sample_ids:
            individual_id = sample_id
            if sample_id_to_individual_id_mapping and sample_id in sample_id_to_individual_id_mapping:
                individual_id = sample_id_to_individual_id_mapping[sample_id]

            if individual_id not in remaining_individuals_dict:
                continue
            sample_id_to_individual_record[
                sample_id] = remaining_individuals_dict[individual_id]
            del remaining_individuals_dict[individual_id]

        logger.debug(
            str(len(sample_id_to_individual_record)) +
            " matched individual ids", user)

        # create new Sample records for Individual records that matches
        if create_sample_records:
            new_samples = [
                Sample(
                    guid='S{}_{}'.format(random.randint(10**9, 10**10),
                                         sample_id)[:Sample.MAX_GUID_SIZE],
                    sample_id=sample_id,
                    sample_type=sample_type,
                    dataset_type=dataset_type,
                    elasticsearch_index=elasticsearch_index,
                    individual=individual,
                    created_date=timezone.now(),
                    loaded_date=loaded_date or timezone.now(),
                ) for sample_id, individual in
                sample_id_to_individual_record.items()
            ]
            sample_id_to_sample_record.update({
                sample.sample_id: sample
                for sample in Sample.bulk_create(user, new_samples)
            })
            log_model_bulk_update(logger, new_samples, user, 'create')

    return sample_id_to_sample_record