示例#1
0
 def bulk_create(cls, user, new_models):
     """Helper bulk create method that logs the creation"""
     for model in new_models:
         model.created_by = user
     models = cls.objects.bulk_create(new_models)
     log_model_bulk_update(logger, models, user, 'create')
     return models
示例#2
0
def _bulk_update_tags(tag_type, json, tag_model_q):
    tag_models = tag_model_q.filter(variant_tag_type=tag_type)
    log_model_bulk_update(logger,
                          tag_models,
                          user=None,
                          update_type='update',
                          update_fields=list(json.keys()))
    tag_models.update(**json)
    return tag_models
示例#3
0
def remove_unused_validation_tag_types(apps, schema_editor):
    VariantTagType = apps.get_model("seqr", "VariantTagType")
    db_alias = schema_editor.connection.alias
    tag_types = VariantTagType.objects.using(db_alias).filter(
        project__isnull=True, name__in=VALIDATION_TAGS.keys())
    if tag_types:
        log_model_bulk_update(logger,
                              tag_types,
                              user=None,
                              update_type='delete')
        tag_types.delete()
示例#4
0
    def bulk_update(cls, user, update_json, queryset=None, **filter_kwargs):
        """Helper bulk update method that logs the update"""
        if queryset is None:
            queryset = cls.objects.filter(**filter_kwargs)

        entity_ids = log_model_bulk_update(logger,
                                           queryset,
                                           user,
                                           'update',
                                           update_fields=update_json.keys())
        queryset.update(**update_json)
        return entity_ids
示例#5
0
 def bulk_delete(cls, user, queryset=None, **filter_kwargs):
     """Helper bulk delete method that logs the deletion"""
     if queryset is None:
         queryset = cls.objects.filter(**filter_kwargs)
     log_model_bulk_update(logger, queryset, user, 'delete')
     return queryset.delete()
示例#6
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