コード例 #1
0
    def handle(self, *args, **options):
        from_project = Project.objects.get(guid=options['from_project'])
        to_project = Project.objects.get(guid=options['to_project'])
        family_ids = options['family_ids']
        families = Family.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 phenotips for {}".format(f))
            for seqr_individual in f.individual_set.all():

                if phenotips_patient_exists(seqr_individual):
                    # make sure phenotips_patient_id is up to date
                    data_json = _get_patient_data(
                        from_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.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.phenotips_user_id, read_only=False)
                    _add_user_to_patient(phenotips_readwrite_username,
                                         seqr_individual.phenotips_patient_id,
                                         allow_edit=True)

        for variant_tag_type in VariantTagType.objects.filter(
                project=from_project):
            variant_tags = VariantTag.objects.filter(
                saved_variant__family__in=families,
                variant_tag_type=variant_tag_type)
            if variant_tags:
                print('Updating "{}" tags'.format(variant_tag_type.name))
                to_tag_type, created = VariantTagType.objects.get_or_create(
                    project=to_project, name=variant_tag_type.name)
                if created:
                    to_tag_type.category = variant_tag_type.category
                    to_tag_type.description = variant_tag_type.description
                    to_tag_type.color = variant_tag_type.color
                    to_tag_type.order = variant_tag_type.order
                    to_tag_type.save()
                variant_tags.update(variant_tag_type=to_tag_type)

        print("Updating families")
        families.update(project=to_project)

        print("Done.")
コード例 #2
0
def transfer_individual(source_individual, new_project, connect_to_phenotips):
    """Transfers the given Individual and returns the new Individual"""

    # get rid of '.' to signify 'unknown'
    if source_individual.paternal_id == "." or source_individual.maternal_id == "." or source_individual.gender == "." or source_individual.affected == ".":
        if source_individual.paternal_id == ".":
            source_individual.paternal_id = ""
        if source_individual.maternal_id == ".":
            source_individual.maternal_id = ""
        if source_individual.affected == ".":
            source_individual.affected = ""
        if source_individual.gender == ".":
            source_individual.gender = ""
        source_individual.save()

    new_individual, created = safe_get_or_create(source_individual)
    if created:
        print("Created SeqrSample", new_individual)

    update_model_fields(new_individual, source_individual, [
        'family',
        'indiv_id',
        'created_date',
        'maternal_id',
        'paternal_id',
        'gender',
        'affected',
        'nickname',
        'case_review_status',
        'phenotips_id',
        'phenotips_data',
    ])

    if new_individual.display_name == new_individual.individual_id:
        new_individual.display_name = ''
        new_individual.save()

    # update PhenoTips data
    phenotips_data_retrieved = False
    if connect_to_phenotips and new_project.is_phenotips_enabled:
        try:
            data_json = _get_patient_data(
                new_project,
                new_individual,
            )

            _update_individual_phenotips_data(new_individual, data_json)

            phenotips_data_retrieved = True
        except phenotips_api.PhenotipsException as e:
            print("Couldn't retrieve latest data from phenotips for %s: %s" %
                  (new_individual, e))

    return new_individual, created, phenotips_data_retrieved
コード例 #3
0
def _retrieve_and_update_individual_phenotips_data(project, individual):
    """Retrieve and update the phenotips_data and phenotips_patient_id fields for the given Individual

    Args:
        project (Model): Project model
        individual (Model): Individual model
    """
    try:
        latest_phenotips_json = phenotips_api._get_patient_data(
            project,
            individual,
        )

    except phenotips_api.PhenotipsException as e:
        print("Couldn't retrieve latest data from phenotips for %s: %s" % (individual, e))
        return

    _update_individual_phenotips_data(individual, latest_phenotips_json)
コード例 #4
0
def transfer_individual(source_individual, new_project, connect_to_phenotips):
    """Transfers the given Individual and returns the new Individual"""

    # get rid of '.' to signify 'unknown'
    if source_individual.paternal_id == "." or source_individual.maternal_id == "." or source_individual.gender == "." or source_individual.affected == ".":
        if source_individual.paternal_id == ".":
            source_individual.paternal_id = ""
        if source_individual.maternal_id == ".":
            source_individual.maternal_id = ""
        if source_individual.affected == ".":
            source_individual.affected = ""
        if source_individual.gender == ".":
            source_individual.gender = ""
        source_individual.save()

    new_individual, created = safe_get_or_create(source_individual)
    if created:
        print("Created SeqrSample", new_individual)

    update_model_fields(new_individual, source_individual, [
        'family', 'indiv_id', 'created_date', 'maternal_id', 'paternal_id', 'gender', 'affected', 'nickname',
        'case_review_status', 'phenotips_id', 'phenotips_data',
    ])

    if new_individual.display_name == new_individual.individual_id:
        new_individual.display_name = ''
        new_individual.save()

    # update PhenoTips data
    phenotips_data_retrieved = False
    if connect_to_phenotips and new_project.is_phenotips_enabled:
        try:
            data_json = _get_patient_data(
                new_project,
                new_individual,
            )

            _update_individual_phenotips_data(new_individual, data_json)

            phenotips_data_retrieved = True
        except phenotips_api.PhenotipsException as e:
            print("Couldn't retrieve latest data from phenotips for %s: %s" % (new_individual, e))

    return new_individual, created, phenotips_data_retrieved
コード例 #5
0
def transfer_individual(source_individual, new_project, connect_to_phenotips):
    """Transfers the given Individual and returns the new Individual"""

    # get rid of '.' to signify 'unknown'
    if source_individual.paternal_id == "." or source_individual.maternal_id == "." or source_individual.gender == "." or source_individual.affected == ".":
        if source_individual.paternal_id == ".":
            source_individual.paternal_id = ""
        if source_individual.maternal_id == ".":
            source_individual.maternal_id = ""
        if source_individual.affected == ".":
            source_individual.affected = ""
        if source_individual.gender == ".":
            source_individual.gender = ""
        source_individual.save()

    new_individual, created = safe_get_or_create(source_individual)
    if created:
        print("Created SeqrSample", new_individual)

    update_model_fields(new_individual, source_individual, [
        'family',
        'indiv_id',
        'created_date',
        'maternal_id',
        'paternal_id',
        'gender',
        'affected',
        'nickname',
        'case_review_status',
        'phenotips_id',
        'phenotips_data',
    ])

    if new_individual.display_name == new_individual.individual_id:
        new_individual.display_name = ''
        new_individual.save()

    # update PhenoTips data
    phenotips_data_retrieved = False
    if connect_to_phenotips and new_project.is_phenotips_enabled:
        try:
            data_json = _get_patient_data(
                new_project,
                new_individual,
            )

            _update_individual_phenotips_data(new_individual, data_json)

            phenotips_data_retrieved = True
        except phenotips_api.PhenotipsException as e:
            print("Couldn't retrieve latest data from phenotips for %s: %s" %
                  (new_individual, e))

    # transfer MME data
    if new_project.is_mme_enabled:
        mme_data_for_individual = list(
            settings.SEQR_ID_TO_MME_ID_MAP.find({
                'seqr_id':
                new_individual.individual_id
            }).sort('insertion_date', pymongo.DESCENDING))

        if mme_data_for_individual:
            submitted_data = mme_data_for_individual[0]['submitted_data']
            if submitted_data:
                new_individual.mme_submitted_data = json.dumps(
                    submitted_data, default=json_util.default)
                new_individual.mme_id = submitted_data['patient']['id']
                new_individual.save()

    return new_individual, created, phenotips_data_retrieved
コード例 #6
0
    def handle(self, *args, **options):
        from_project = Project.objects.get(guid=options['from_project'])
        to_project = Project.objects.get(guid=options['to_project'])
        to_base_project = find_matching_xbrowse_model(to_project)
        family_ids = options['family_ids']
        families = Family.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 seqr_individual in f.individual_set.all():
                base_individual = find_matching_xbrowse_model(seqr_individual)
                base_individual.project = to_base_project
                base_individual.save()
                # Update individuals in phenotips
                if _phenotips_patient_exists(seqr_individual):
                    # make sure phenotips_patient_id is up to date
                    data_json = _get_patient_data(
                        from_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.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.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 base_individual.vcf_files.all():
                    to_vcf_file, _ = VCFFile.objects.get_or_create(
                        project=to_base_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,
                    )
                    base_individual.vcf_files.add(to_vcf_file)
                    base_individual.vcf_files.remove(from_vcf_file)

            # Update variant tags/ notes
            saved_variants = SavedVariant.objects.filter(family=f)
            for variant_tag in VariantTag.objects.filter(
                    saved_variant__in=saved_variants).select_related(
                        'variant_tag_type'):
                if variant_tag.variant_tag_type.project:
                    to_tag_type, created = get_or_create_seqr_model(
                        VariantTagType,
                        project=to_project,
                        name=variant_tag.variant_tag_type.name)
                    if created:
                        update_seqr_model(
                            to_tag_type,
                            category=variant_tag.variant_tag_type.category,
                            description=variant_tag.variant_tag_type.
                            description,
                            color=variant_tag.variant_tag_type.color,
                            order=variant_tag.variant_tag_type.order,
                        )
                    update_seqr_model(variant_tag,
                                      variant_tag_type=to_tag_type)
                else:
                    to_project_tag, created = ProjectTag.objects.get_or_create(
                        project=to_base_project,
                        tag=variant_tag.variant_tag_type.name)
                    if created:
                        to_project_tag.category = variant_tag.variant_tag_type.category
                        to_project_tag.title = variant_tag.variant_tag_type.description
                        to_project_tag.color = variant_tag.variant_tag_type.color
                        to_project_tag.order = variant_tag.variant_tag_type.order
                        to_project_tag.save()
                    variant_tag.project_tag = to_project_tag
                    variant_tag.save()

            base_family = find_matching_xbrowse_model(f)
            for note in BaseVariantNote.objects.filter(family=base_family):
                note.project = to_base_project
                note.save()

            # Update families
            update_seqr_model(f, project=to_project)

        print("Done.")
コード例 #7
0
    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.")