Ejemplo n.º 1
0
def set_parents_for_individual(individual):
    """
    Sets family for an individual (creating if necessary)
    Saves both to db
    """
    project = individual.project
    if individual.paternal_id and individual.paternal_id != '.':
        father = get_or_create_xbrowse_model(Individual, project=project, indiv_id=individual.paternal_id)[0]
        update_xbrowse_model(father, family=individual.family)

    if individual.maternal_id and individual.maternal_id != '.':
        mother = get_or_create_xbrowse_model(Individual, project=project, indiv_id=individual.maternal_id)[0]
        update_xbrowse_model(mother, family=individual.family)
Ejemplo n.º 2
0
def set_parents_for_individual(individual):
    """
    Sets family for an individual (creating if necessary)
    Saves both to db
    """
    project = individual.project
    if individual.paternal_id and individual.paternal_id != '.':
        father = get_or_create_xbrowse_model(Individual, project=project, indiv_id=individual.paternal_id)[0]
        update_xbrowse_model(father, family=individual.family)

    if individual.maternal_id and individual.maternal_id != '.':
        mother = get_or_create_xbrowse_model(Individual, project=project, indiv_id=individual.maternal_id)[0]
        update_xbrowse_model(mother, family=individual.family)
Ejemplo n.º 3
0
def add_family_group_submit(request, project_id):
    project = get_object_or_404(Project, project_id=project_id)
    if not project.can_admin(request.user):
        raise PermissionDenied

    error = None

    form = AddFamilyGroupForm(project, request.POST)
    if form.is_valid():
        # todo: move to sample_anagement
        family_group, created = get_or_create_xbrowse_model(
            FamilyGroup,
            project=project,
            slug=form.cleaned_data['family_group_slug'],
        )
        update_xbrowse_model(family_group, name=form.cleaned_data['name'], description=form.cleaned_data['description'])

        seqr_analysis_group = find_matching_seqr_model(family_group)
        for family in form.cleaned_data['families']:
            family_group.families.add(family)
            seqr_analysis_group.families.add(find_matching_seqr_model(family))
    else:
        error = server_utils.form_error_string(form)

    if error:
        return server_utils.JSONResponse({'is_error': True, 'error': error})
    else:
        return server_utils.JSONResponse({'is_error': False, 'new_url': reverse('family_group_home', args=(project.project_id, family_group.slug))})
Ejemplo n.º 4
0
def update_project_from_individuals(project, xindividuals):
    individuals = []
    for xindividual in xindividuals:
        individual = get_or_create_xbrowse_model(Individual, project=project, indiv_id=xindividual.indiv_id)[0]
        individual.from_xindividual(xindividual)
        set_family_id_for_individual(individual, xindividual.family_id)

        individuals.append(individual)
        #set_parents_for_individual(individual)
    return individuals
Ejemplo n.º 5
0
def get_or_create_project_tag(project,
                              order,
                              category,
                              tag_name,
                              description,
                              color='#1f78b4',
                              original_names=None):
    """
    Updates or creates a particular ProjectTag in a given project.

    Args:
        project (object): The project that contains this tag
        category (string):
        tag_name (string): The name of the new tag (can contain spaces)   (eg. "Causal Variant")
        description (string):  Longer description of the tag
        color (string): hex color (eg. "#123456")
        original_names (list): if the tag appeaered under one or more different names, remap these to the new tag_name

    Returns:
        new ProjectTag model object (or an existing one if a match was found)
    """

    project_tag = None
    if original_names:
        for original_name in original_names:
            tags = ProjectTag.objects.filter(project=project,
                                             tag__icontains=original_name,
                                             order__isnull=True)
            if tags:
                project_tag = tags[0]

    if project_tag is None:
        tags = ProjectTag.objects.filter(project=project, tag__iexact=tag_name)
        if tags:
            project_tag = tags[0]

    if project_tag is None:
        tags = ProjectTag.objects.filter(project=project,
                                         tag__icontains=tag_name)
        if tags:
            project_tag = tags[0]

    if project_tag is None:
        project_tag, created = get_or_create_xbrowse_model(ProjectTag,
                                                           project=project,
                                                           tag=tag_name)
        if created:
            print("Created new tag: %s :  %s" % (project, tag_name))

    update_xbrowse_model(project_tag,
                         order=order,
                         category=category,
                         tag=tag_name,
                         title=description,
                         color=color)
Ejemplo n.º 6
0
def set_family_id_for_individual(individual, family_id):
    """
    Sets family for an individual (creating if necessary)
    Saves both to db
    """
    project = individual.project
    if family_id:
        family = get_or_create_xbrowse_model(Family, family_id=family_id, project=project)[0]
    else:
        family = None
    update_xbrowse_model(individual, family=family)
Ejemplo n.º 7
0
def save_individual_from_json_dict(project, indiv_dict):
    individual = get_or_create_xbrowse_model(Individual, indiv_id=indiv_dict['indiv_id'], project=project)[0]
    update_xbrowse_model(
        individual,
        gender = indiv_dict.get('gender'),
        affected = indiv_dict.get('affected'),
        nickname = indiv_dict.get('nickname', ''),
        paternal_id = indiv_dict.get('paternal_id', ''),
        maternal_id = indiv_dict.get('maternal_id', ''))

    sample_management.set_family_id_for_individual(individual, indiv_dict.get('family_id', ''))
    sample_management.set_individual_phenotypes_from_dict(individual, indiv_dict.get('phenotypes', {}))
Ejemplo n.º 8
0
def update_project_from_individuals(project, xindividuals):
    # make sure families exist
    families = {}
    for family_id in set([xindividual.family_id for xindividual in xindividuals]):
        family, _ = get_or_create_xbrowse_model(Family, family_id=family_id, project=project)
        families[family_id] = family

    individuals = []
    for xindividual in xindividuals:
        individual, _ = get_or_create_xbrowse_model(
            Individual,
            project=project,
            family=families[xindividual.family_id],
            indiv_id=xindividual.indiv_id)

        individual.from_xindividual(xindividual)
        individuals.append(individual)

        #set_parents_for_individual(individual)

    return individuals
Ejemplo n.º 9
0
def save_individual_from_json_dict(project, indiv_dict):
    individual = get_or_create_xbrowse_model(Individual, indiv_id=indiv_dict['indiv_id'], project=project)[0]
    update_xbrowse_model(
        individual,
        gender = indiv_dict.get('gender'),
        affected = indiv_dict.get('affected'),
        nickname = indiv_dict.get('nickname', ''),
        paternal_id = indiv_dict.get('paternal_id', ''),
        maternal_id = indiv_dict.get('maternal_id', ''))

    sample_management.set_family_id_for_individual(individual, indiv_dict.get('family_id', ''))
    sample_management.set_individual_phenotypes_from_dict(individual, indiv_dict.get('phenotypes', {}))
Ejemplo n.º 10
0
def add_family_group_submit(request, project_id):
    project = get_object_or_404(Project, project_id=project_id)
    if not project.can_admin(request.user):
        raise PermissionDenied

    error = None

    form = AddFamilyGroupForm(project, request.POST)
    if form.is_valid():
        # todo: move to sample_anagement
        family_group, created = get_or_create_xbrowse_model(
            FamilyGroup,
            project=project,
            slug=form.cleaned_data['family_group_slug'],
        )
        update_xbrowse_model(family_group,
                             name=form.cleaned_data['name'],
                             description=form.cleaned_data['description'])

        seqr_analysis_group = find_matching_seqr_model(family_group)
        for family in form.cleaned_data['families']:
            family_group.families.add(family)
            seqr_analysis_group.families.add(find_matching_seqr_model(family))
    else:
        error = server_utils.form_error_string(form)

    if error:
        return server_utils.JSONResponse({'is_error': True, 'error': error})
    else:
        return server_utils.JSONResponse({
            'is_error':
            False,
            'new_url':
            reverse('family_group_home',
                    args=(project.project_id, family_group.slug))
        })
Ejemplo n.º 11
0
def add_indiv_ids_to_project(project, indiv_id_list):
    """
    Add these individuals if they don't already exist
    """
    for indiv_id in indiv_id_list:
        get_or_create_xbrowse_model(Individual, project=project, indiv_id=indiv_id)
Ejemplo n.º 12
0
    def handle(self, *args, **options):
        to_project = BaseProject.objects.get(project_id=options['to_project'])
        from_project = BaseProject.objects.get(
            project_id=options['from_project'])

        to_families = BaseFamily.objects.filter(project=to_project).only(
            'family_id').prefetch_related('individual_set')

        from_families_map = {
            f.family_id: {
                'family': f,
                'individuals': f.individual_set.all()
            }
            for f in BaseFamily.objects.filter(
                project=from_project,
                family_id__in=[f.family_id for f in to_families]).only(
                    'family_id').prefetch_related('individual_set')
        }

        print('Transferring individuals from {} to {}:'.format(
            from_project.project_name, to_project.project_name))

        missing_family_individual_counts = defaultdict(int)
        missing_individual_sample_counts = {}
        created_vcf_count = 0
        for family in to_families:
            for from_individual in from_families_map[
                    family.family_id]['individuals']:
                to_individual, individual_created = get_or_create_xbrowse_model(
                    BaseIndividual,
                    project=to_project,
                    family=family,
                    indiv_id=from_individual.indiv_id,
                )
                if individual_created:
                    update_xbrowse_model(
                        to_individual, **{
                            field: getattr(from_individual, field)
                            for field in INDIVIDUAL_FIELDS
                        })
                _update_model(
                    to_individual, **{
                        field: getattr(from_individual, field)
                        for field in INDIVIDUAL_DATA_FIELDS
                    })
                missing_family_individual_counts[family] += (
                    1 if individual_created else 0)

                for from_vcf_file in from_individual.vcf_files.all():
                    to_vcf_file, vcf_created = VCFFile.objects.get_or_create(
                        project=to_project,
                        **{
                            field: getattr(from_vcf_file, field)
                            for field in VCF_FILE_FIELDS
                        })
                    if vcf_created:
                        created_vcf_count += 1
                    to_individual.vcf_files.add(to_vcf_file)

                counters = {'samples_created': 0}
                create_sample_records(to_individual, None,
                                      to_individual.seqr_individual, counters)
                missing_individual_sample_counts[to_individual] = counters[
                    'samples_created']

        missing_individual_counts = defaultdict(int)
        updated_families = set()
        for family, individual_count in missing_family_individual_counts.items(
        ):
            missing_individual_counts[individual_count] += 1
            if individual_count > 0:
                updated_families.add(family)

        missing_sample_counts = defaultdict(int)
        for individual, sample_count in missing_individual_sample_counts.items(
        ):
            missing_sample_counts[sample_count] += 1
            if sample_count > 0:
                updated_families.add(individual.family)

        for family in updated_families:
            update_xbrowse_model(
                family,
                pedigree_image=from_families_map[
                    family.family_id]['family'].pedigree_image)

        print("Done.")
        print("----------------------------------------------")
        for num_individuals, num_families in missing_individual_counts.items():
            print('Added {} individuals to {} families'.format(
                num_individuals, num_families))
        print("----------------------------------------------")
        print('Added {} VCF files'.format(created_vcf_count))
        print("----------------------------------------------")
        for num_samples, num_individuals in missing_sample_counts.items():
            print('Added {} samples to {} individuals'.format(
                num_samples, num_individuals))
    def handle(self, *args, **options):
        to_project = BaseProject.objects.get(project_id=options['to_project'])
        from_project = BaseProject.objects.get(project_id=options['from_project'])

        to_families = BaseFamily.objects.filter(project=to_project).only('family_id').prefetch_related('individual_set')

        from_families_map = {
            f.family_id: {'family': f, 'individuals': f.individual_set.all()}
            for f in BaseFamily.objects.filter(
                project=from_project, family_id__in=[f.family_id for f in to_families]
            ).only('family_id').prefetch_related('individual_set')
        }

        print('Transferring individuals from {} to {}:'.format(from_project.project_name, to_project.project_name))

        missing_family_individual_counts = defaultdict(int)
        missing_individual_sample_counts = {}
        created_vcf_count = 0
        for family in to_families:
            for from_individual in from_families_map[family.family_id]['individuals']:
                to_individual, individual_created = get_or_create_xbrowse_model(
                    BaseIndividual,
                    project=to_project,
                    family=family,
                    indiv_id=from_individual.indiv_id,
                )
                if individual_created:
                    update_xbrowse_model(
                        to_individual,
                        **{field: getattr(from_individual, field) for field in INDIVIDUAL_FIELDS}
                    )
                _update_model(
                    to_individual,
                    **{field: getattr(from_individual, field) for field in INDIVIDUAL_DATA_FIELDS}
                )
                missing_family_individual_counts[family] += (1 if individual_created else 0)

                for from_vcf_file in from_individual.vcf_files.all():
                    to_vcf_file, vcf_created = VCFFile.objects.get_or_create(
                        project=to_project,
                        **{field: getattr(from_vcf_file, field) for field in VCF_FILE_FIELDS}
                    )
                    if vcf_created:
                        created_vcf_count += 1
                    to_individual.vcf_files.add(to_vcf_file)

                counters = {'samples_created': 0}
                create_sample_records(to_individual, None, to_individual.seqr_individual, counters)
                missing_individual_sample_counts[to_individual] = counters['samples_created']

        missing_individual_counts = defaultdict(int)
        updated_families = set()
        for family, individual_count in missing_family_individual_counts.items():
            missing_individual_counts[individual_count] += 1
            if individual_count > 0:
                updated_families.add(family)

        missing_sample_counts = defaultdict(int)
        for individual, sample_count in missing_individual_sample_counts.items():
            missing_sample_counts[sample_count] += 1
            if sample_count > 0:
                updated_families.add(individual.family)

        for family in updated_families:
            update_xbrowse_model(family, pedigree_image=from_families_map[family.family_id]['family'].pedigree_image)

        print("Done.")
        print("----------------------------------------------")
        for num_individuals, num_families in missing_individual_counts.items():
            print('Added {} individuals to {} families'.format(num_individuals, num_families))
        print("----------------------------------------------")
        print('Added {} VCF files'.format(created_vcf_count))
        print("----------------------------------------------")
        for num_samples, num_individuals in missing_sample_counts.items():
            print('Added {} samples to {} individuals'.format(num_samples, num_individuals))
Ejemplo n.º 14
0
def add_indiv_ids_to_project(project, indiv_id_list):
    """
    Add these individuals if they don't already exist
    """
    for indiv_id in indiv_id_list:
        get_or_create_xbrowse_model(Individual, project=project, indiv_id=indiv_id)
    def handle(self, *args, **options):
        from_project = BaseProject.objects.get(project_id=options['from_project'])
        to_project = BaseProject.objects.get(project_id=options['to_project'])
        to_seqr_project = find_matching_seqr_model(to_project)
        family_ids = options['family_ids']
        families = BaseFamily.objects.filter(project=from_project, family_id__in=family_ids)
        print('Found {} out of {} families. No match for: {}.'.format(len(families), len(set(family_ids)), set(family_ids) - set([f.family_id for f in families])))

        for f in families:
            print("==> Moving {}".format(f))
            for individual in f.individual_set.all():
                individual.project = to_project
                individual.save()
                # Update individuals in phenotips
                if _phenotips_patient_exists(individual.seqr_individual):
                    # make sure phenotips_patient_id is up to date
                    seqr_individual = individual.seqr_individual
                    data_json = _get_patient_data(
                        from_project.seqr_project,
                        seqr_individual,
                    )

                    seqr_individual.phenotips_patient_id = data_json["id"]
                    seqr_individual.save()

                    # update permissions
                    phenotips_readonly_username, _ = _get_phenotips_uname_and_pwd_for_project(to_project.seqr_project.phenotips_user_id, read_only=True)
                    _add_user_to_patient(phenotips_readonly_username, seqr_individual.phenotips_patient_id, allow_edit=False)

                    phenotips_readwrite_username, _ = _get_phenotips_uname_and_pwd_for_project(to_project.seqr_project.phenotips_user_id, read_only=False)
                    _add_user_to_patient(phenotips_readwrite_username, seqr_individual.phenotips_patient_id, allow_edit=True)

                # Update individuals samples/ VCFs
                for from_vcf_file in individual.vcf_files.all():
                    to_vcf_file, _ = VCFFile.objects.get_or_create(
                        project=to_project,
                        elasticsearch_index=from_vcf_file.elasticsearch_index,
                        file_path=from_vcf_file.file_path,
                        dataset_type=from_vcf_file.dataset_type,
                        sample_type=from_vcf_file.sample_type,
                        loaded_date=from_vcf_file.loaded_date,
                    )
                    individual.vcf_files.add(to_vcf_file)
                    individual.vcf_files.remove(from_vcf_file)

            # Update variant tags/ notes
            saved_variants = set()
            for note in BaseVariantNote.objects.filter(family=f):
                seqr_note = find_matching_seqr_model(note)
                if seqr_note:
                    saved_variants.add(seqr_note.saved_variant)
                note.project = to_project
                note.save()

            for variant_tag in BaseVariantTag.objects.filter(family=f):
                to_project_tag, created = get_or_create_xbrowse_model(
                    ProjectTag, project=to_project, tag=variant_tag.project_tag.tag
                )
                if created:
                    update_xbrowse_model(
                        to_project_tag,
                        category=variant_tag.project_tag.category,
                        title=variant_tag.project_tag.title,
                        color=variant_tag.project_tag.color,
                        order=variant_tag.project_tag.order,
                    )
                update_xbrowse_model(variant_tag, project_tag=to_project_tag)
                seqr_variant_tag = find_matching_seqr_model(variant_tag)
                if seqr_variant_tag:
                    saved_variants.add(seqr_variant_tag.saved_variant)

            for saved_variant in saved_variants:
                saved_variant.project = to_seqr_project
                saved_variant.save()

            # Update families
            update_xbrowse_model(f, project=to_project)

        print("Done.")