示例#1
0
def transfer_individual_info(source_individual, destination_individual):
    """Transfers the given Individual and returns the new Individual"""

    assert source_individual.individual_id == destination_individual.individual_id

    destination_individual.maternal_id = choose_one(destination_individual, 'maternal_id', source_individual.maternal_id, destination_individual.maternal_id)
    destination_individual.paternal_id = choose_one(destination_individual, 'paternal_id', source_individual.paternal_id, destination_individual.paternal_id)

    destination_individual.sex = choose_one(destination_individual, 'sex', source_individual.sex, destination_individual.sex)
    destination_individual.affected = choose_one(destination_individual, 'affected', source_individual.affected, destination_individual.affected)

    destination_individual.display_name = choose_one(destination_individual, 'display_name', source_individual.display_name, destination_individual.display_name)

    destination_individual.case_review_status = choose_one(destination_individual, 'case_review_status', source_individual.case_review_status, destination_individual.case_review_status)

    # if PhenoTips HPO terms entered for both individuals, prompt user to choose
    destination_individual.phenotips_data = choose_one(
        destination_individual,
        'phenotips_data',
        source_individual.phenotips_data,
        destination_individual.phenotips_data
    )

    destination_individual.mme_id = choose_one(destination_individual, 'mme_id', source_individual.mme_id, destination_individual.mme_id)
    destination_individual.mme_submitted_data = choose_one(destination_individual, 'mme_submitted_data', source_individual.mme_submitted_data, destination_individual.mme_submitted_data)

    destination_individual.save()
示例#2
0
def transfer_project_info(source_project, destination_project):
    """Transfers the given project"""

    # destination_project.name - keep project name as is
    # destination_project.deprecated_project_id - keep as is
    destination_project.description = choose_one(
        destination_project, "description", source_project.description,
        destination_project.description)

    # update permissions - transfer source project users
    for user in source_project.can_edit_group.user_set.all():
        destination_project.can_edit_group.user_set.add(user)

    for user in source_project.can_view_group.user_set.all():
        destination_project.can_view_group.user_set.add(user)

    # update permissions - transfer source project gene lists
    for locus_list in get_objects_for_group(source_project.can_view_group,
                                            CAN_VIEW, LocusList):
        assign_perm(user_or_group=destination_project.can_view_group,
                    perm=CAN_VIEW,
                    obj=locus_list)

    # update permissions - transfer SampleBatches
    for sample_batch in SampleBatch.objects.filter(
            sample__individual__family__project=source_project):
        assign_perm(user_or_group=destination_project.can_edit_group,
                    perm=CAN_EDIT,
                    obj=sample_batch)
        assign_perm(user_or_group=destination_project.can_view_group,
                    perm=CAN_VIEW,
                    obj=sample_batch)

    # transfer custom reference populations
    for p in source_project.custom_reference_populations.all():
        destination_project.custom_reference_populations.add(p)

    # phenotips and MME
    destination_project.is_phenotips_enabled = True

    # TODO check for each individual whether the source project has phenotips data
    destination_project.phenotips_user_id = source_project.phenotips_user_id

    destination_project.is_mme_enabled = source_project.is_mme_enabled or destination_project.is_mme_enabled
    destination_project.mme_primary_data_owner = choose_one(
        destination_project, "mme_primary_data_owner",
        source_project.mme_primary_data_owner,
        destination_project.mme_primary_data_owner)

    destination_project.save()
示例#3
0
def transfer_family_info(source_family, destination_family):
    """Transfers the given family and returns the new family"""

    print("Transferring " + str(source_family) + " to " +
          str(destination_family))
    assert source_family.family_id == destination_family.family_id

    destination_family.description = choose_one(destination_family,
                                                'description',
                                                source_family.description,
                                                destination_family.description)

    #destination_family.project - keep this since source_family.project will be deleted
    destination_family.display_name = choose_one(
        destination_family, "display_name", source_family.display_name,
        destination_family.display_name)

    #new_family.pedigree_image - keep this as is, it will need to be regenerated anyway

    destination_family.analysis_notes = choose_one(
        destination_family, 'analysis_notes', source_family.analysis_notes,
        destination_family.analysis_notes)
    destination_family.analysis_summary = choose_one(
        destination_family, 'analysis_summary', source_family.analysis_summary,
        destination_family.analysis_summary)
    destination_family.causal_inheritance_mode = choose_one(
        destination_family, 'causal_inheritance_mode',
        source_family.causal_inheritance_mode,
        destination_family.causal_inheritance_mode)

    destination_family.analysis_status = choose_one(
        destination_family, 'analysis_status', source_family.analysis_status,
        destination_family.analysis_status)
    destination_family.internal_case_review_notes = choose_one(
        destination_family, 'internal_case_review_notes',
        source_family.internal_case_review_notes,
        destination_family.internal_case_review_notes)

    destination_family.internal_case_review_summary = choose_one(
        destination_family, 'internal_case_review_summary',
        source_family.internal_case_review_summary,
        destination_family.internal_case_review_summary)

    destination_family.internal_analysis_status = choose_one(
        destination_family, 'internal_analysis_status',
        source_family.internal_analysis_status,
        destination_family.internal_analysis_status)

    destination_family.save()
示例#4
0
    def handle(self, *args, **options):
        to_project_id = options["to_project"]
        from_project_id = options["from_project"]
        from_project_datatype = options["from_project_datatype"]
        assert from_project_id
        assert to_project_id

        print("=============================")
        print("Transferring metadata from project %s to %s" % (from_project_id, to_project_id))
        if not ask_yes_no_question("Continue?"):
            return

        """
        The following models are transfered between projects.

        ProjectCollaborator => user = models.ForeignKey(User), project = models.ForeignKey('base.Project'), collaborator_type = models.CharField(max_length=20, choices=COLLABORATOR_TYPES, default="collaborator")
        Project => (private_reference_populations = models.ManyToManyField(ReferencePopulation), gene_lists = models.ManyToManyField('gene_lists.GeneList', through='ProjectGeneList'))
        Family => Project,
        FamilyGroup => Project   (families = models.ManyToManyField(Family))
        FamilyImageSlide => Family
        Cohort => Project  (individuals = models.ManyToManyField('base.Individual'), vcf_files, bam_file)
        Individual => Project, Family  # vcf_files = models.ManyToManyField(VCFFile, null=True, blank=True), bam_file = models.ForeignKey('datasets.BAMFile', null=True, blank=True)
        FamilySearchFlag => User, Family
        CausalVariant => Family
        ProjectTag => Project
        VariantTag => ProjectTag, Family
        VariantNote => User, Project
        IndividualPhenotype => Individual, ProjectPhenotype
        ProjectPhenotype => Project
        """

        # Project
        to_project, created = Project.objects.get_or_create(project_id=to_project_id)

        if created:
            print("--> Created project: " + str(to_project))
            to_project.created_date = timezone.now()
        else:
            print("--> Transferring into existing project: " + str(to_project))

        from_project = Project.objects.get(project_id=from_project_id)

        to_project.project_name = choose_one(to_project, 'project_name', from_project.project_name, to_project.project_name)
        to_project.description = choose_one(to_project, 'description', from_project.description, to_project.description)

        # set combined_projects_info
        if to_project.combined_projects_info:
            combined_projects_info = json.loads(to_project.combined_projects_info)
        else:
            combined_projects_info = {}

        combined_projects_info.update({from_project_datatype: {'project_id': from_project.project_id}})

        to_project.combined_projects_info = json.dumps(combined_projects_info)

        # TODO update collaborators?

        # Copy gene list
        for project_gene_list in ProjectGeneList.objects.filter(project=from_project):
            print("-->   Adding gene list: " + project_gene_list.gene_list.slug)
            ProjectGeneList.objects.get_or_create(project=to_project, gene_list=project_gene_list.gene_list)

        # Reference Populations
        for reference_population in from_project.private_reference_populations.all():
            if not to_project.private_reference_populations.filter(pk=reference_population.pk).exists():
                print("-->   Adding private reference population: " + reference_population.slug)
                to_project.private_reference_populations.add(reference_population)

        to_project.save()

        # ProjectCollaborator
        collaborators = ProjectCollaborator.objects.filter(project=from_project)
        if len(collaborators) > 0 and ask_yes_no_question("Transfer the %s collaborators?" % len(collaborators)):
            print("Transferring the %s collaborators" % len(collaborators))
            for c in collaborators:
                _, created = ProjectCollaborator.objects.get_or_create(project=to_project, user=c.user, collaborator_type=c.collaborator_type)
                if created:
                    print("-----> Added %s %s" % (c.collaborator_type, c.user.email))

        # --- SeqrProject - update new schema
        seqr_project, seqr_project_created = transfer_project(to_project)

        to_project.seqr_project = seqr_project
        to_project.save()

        print("-->  Set project.combined_projects_info to %s" % (to_project.combined_projects_info, ))

        # Family
        to_family_id_to_family = {} # maps family_id to the to_family object
        from_families = Family.objects.filter(project=from_project)
        print("----> Transferring %s families" % len(from_families))
        for from_f in from_families:
            to_f, created = Family.objects.get_or_create(project=to_project, family_id=from_f.family_id)
            if created:
                print("----> Created family %s" % to_f.family_id)
            else:
                print("----> Transferring into existing family %s (%s)" % (to_f.family_id, to_f.short_description))

            to_family_id_to_family[to_f.family_id] = to_f

            to_f.family_name = choose_one(to_f, 'family_name', from_f.family_name, to_f.family_name)
            to_f.short_description = choose_one(to_f, 'short_description', from_f.short_description, to_f.short_description)

            to_f.about_family_content = choose_one(to_f, 'about_family_content', from_f.about_family_content, to_f.about_family_content)

            to_f.analysis_summary_content = choose_one(to_f, 'analysis_summary_content', from_f.analysis_summary_content, to_f.analysis_summary_content)

            to_f.pedigree_image = None         # choose_one(to_f, 'pedigree_image', from_f.pedigree_image, to_f.pedigree_image)
            to_f.pedigree_image_height = None  # choose_one(to_f, 'pedigree_image_height', from_f.pedigree_image_height, to_f.pedigree_image_height)
            to_f.pedigree_image_width = None   # choose_one(to_f, 'pedigree_image_width', from_f.pedigree_image_width, to_f.pedigree_image_width)

            to_f.analysis_status = choose_one(to_f, 'analysis_status', from_f.analysis_status, to_f.analysis_status, default_value='Q')
            to_f.analysis_status_date_saved = choose_one(to_f, 'analysis_status_date_saved', from_f.analysis_status_date_saved, to_f.analysis_status_date_saved)
            to_f.analysis_status_saved_by = choose_one(to_f, 'analysis_status_saved_by', from_f.analysis_status_saved_by, to_f.analysis_status_saved_by)
            to_f.causal_inheritance_mode = choose_one(to_f, 'causal_inheritance_mode', from_f.causal_inheritance_mode, to_f.causal_inheritance_mode)

            to_f.internal_case_review_notes = choose_one(to_f, 'internal_case_review_notes', from_f.internal_case_review_notes, to_f.internal_case_review_notes)
            to_f.internal_case_review_summary = choose_one(to_f, 'internal_case_review_summary', from_f.internal_case_review_summary, to_f.internal_case_review_summary)

            to_f.coded_phenotype = choose_one(to_f, 'coded_phenotype', from_f.coded_phenotype, to_f.coded_phenotype)
            to_f.post_discovery_omim_number = choose_one(to_f, 'post_discovery_omim_number', from_f.post_discovery_omim_number, to_f.post_discovery_omim_number)

            # combined families info
            if to_f.combined_families_info:
                combined_families_info = json.loads(to_f.combined_families_info)
            else:
                combined_families_info = {}

            combined_families_info.update({from_project_datatype: {'project_id': from_project.project_id, 'family_id': from_f.family_id}})

            to_f.combined_families_info = json.dumps(combined_families_info)
            to_f.save()

            # --- SeqrProject - update new schema
            seqr_family, seqr_family_created = transfer_family(to_f, seqr_project)

            to_f.seqr_family = seqr_family
            to_f.save()

            print("---->  Set family.combined_families_info to %s" % (to_f.combined_families_info, ))


        # FamilyGroup
        for from_fg in FamilyGroup.objects.filter(project=from_project):
            FamilyGroup.objects.get_or_create(project=to_project, slug=from_fg.slug, name=from_fg.name, description=from_fg.description)

        # FamilyImageSlide
        #for from_family in Family.objects.filter(project=from_project):
        # TODO - need to iterate over image slides of from_family, and link to image slides of to_family
        #        FamilyImageSlide.objects.get_or_create(family=to_family, )


        # Cohort
        #cohorts = list(Cohort.objects.filter(project=project))
        #output_obj += cohorts

        # Individual
        print("-------> Transferring %s individuals" % len(Individual.objects.filter(project=from_project)))
        for from_family in Family.objects.filter(project=from_project):
            to_family = to_family_id_to_family[from_family.family_id]

            for from_i in Individual.objects.filter(project=from_project, family=from_family):
                to_i, created = Individual.objects.get_or_create(project=to_project, family=to_family, indiv_id=from_i.indiv_id)
                if created:
                    print("-------> Created individual %s" % to_i.indiv_id)
                else:
                    print("-------> Transferring into existing individual %s" % (to_i.indiv_id))


                to_i.created_date = choose_one(to_i, 'created_date', from_i.created_date, to_i.created_date, use_lower_value=True)
                to_i.maternal_id = choose_one(to_i, 'maternal_id', from_i.maternal_id, to_i.maternal_id)
                to_i.paternal_id = choose_one(to_i, 'paternal_id', from_i.paternal_id, to_i.paternal_id)
                to_i.gender = choose_one(to_i, 'gender', from_i.gender, to_i.gender, default_value='U')
                to_i.affected = choose_one(to_i, 'affected', from_i.affected, to_i.affected, default_value='U')

                to_i.nickname = choose_one(to_i, 'nickname', from_i.nickname, to_i.nickname)
                to_i.other_notes = choose_one(to_i, 'other_notes', from_i.other_notes, to_i.other_notes)

                to_i.case_review_status = choose_one(to_i, 'case_review_status', from_i.case_review_status, to_i.case_review_status)

                to_i.phenotips_id = choose_one(to_i, 'phenotips_id', from_i.phenotips_id, to_i.phenotips_id)
                to_i.phenotips_data = choose_one(to_i, 'phenotips_data', from_i.phenotips_data, to_i.phenotips_data)
                # create phenotips patients and upload data


                to_i.mean_target_coverage = None
                to_i.coverage_status = choose_one(to_i, 'coverage_status', from_i.coverage_status, to_i.coverage_status, default_value='S')
                if from_i.bam_file_path:
                    if to_i.bam_file_path:
                        to_i.bam_file_path = to_i.bam_file_path + "," + from_i.bam_file_path
                    else:
                        to_i.bam_file_path = from_i.bam_file_path

                to_i.vcf_id = ''   # VCF ids will now be stored at the Sample not the Individual level

                for vcf_file in from_i.vcf_files.all():
                    if vcf_file not in to_i.vcf_files.all():
                        to_i.vcf_files.add(vcf_file)

                to_i.in_case_review = choose_one(to_i, 'in_case_review', from_i.in_case_review, to_i.in_case_review)

                # combined individuals info
                if to_i.combined_individuals_info:
                    combined_individuals_info = json.loads(to_i.combined_individuals_info)
                else:
                    combined_individuals_info = {}

                combined_individuals_info.update({from_project_datatype: {
                    'project_id': from_project.project_id,
                    'family_id': from_f.family_id,
                    'indiv_id': from_i.indiv_id}
                })

                to_i.combined_individuals_info = json.dumps(combined_individuals_info)
                to_i.save()

                seqr_individual, seqr_individual_created, phenotips_data_retrieved = transfer_individual(to_i, seqr_family, seqr_project, connect_to_phenotips=True)
                seqr_individual.seqr_individual = seqr_individual
                seqr_individual.save()

                print("------->  Set individual.combined_individuals_info to %s" % (to_i.combined_individuals_info, ))

            for from_v in CausalVariant.objects.filter(family=from_family):
                CausalVariant.objects.get_or_create(
                    family = to_family,
                    variant_type=from_v.variant_type,
                    xpos=from_v.xpos,
                    ref=from_v.ref,
                    alt=from_v.alt)

        # add this project to phenotips
        print("--> PHENOTIPS")
        create_user_in_phenotips(to_project.project_id, to_project.project_name)
        individuals_to_add = Individual.objects.filter(project=to_project, family=to_family)
        add_individuals_to_phenotips(to_project.project_id, [i.indiv_id for i in individuals_to_add])

        for i in individuals_to_add:
            if i.phenotips_data:
                to_project.phenotips_user_id = to_project.project_id
                try:
                    json_obj = json.loads(i.phenotips_data)
                    update_patient_data(to_project, i, patient_json=json_obj)
                except Exception as e:
                    logger.error("%s - error while updating phenotips for %s: %s", e, i.phenotips_id, json_obj)

        # TODO: merge MME?
        from_variant_notes = VariantNote.objects.filter(project=from_project)
        print("--> Transferring %s VariantNotes:" % len(from_variant_notes))
        for from_vn in from_variant_notes:
            if from_vn.family and from_vn.family.family_id:
                to_family = to_family_id_to_family.get(from_vn.family.family_id)
            else:
                to_family = None

            _, created = VariantNote.objects.get_or_create(
                project=to_project,
                family=to_family,
                user=from_vn.user,
                date_saved=from_vn.date_saved,
                note=from_vn.note,
                xpos=from_vn.xpos,
                ref=from_vn.ref,
                alt=from_vn.alt)

            if created:
                print("-----> Created variant note %s:%s>%s" % (from_vn.xpos, from_vn.ref, from_vn.alt))

        from_project_tags = ProjectTag.objects.filter(project=from_project)
        print("--> Transferring %s ProjectTags" % len(from_project_tags))
        for from_ptag in from_project_tags:
            to_ptag, created = ProjectTag.objects.get_or_create(project=to_project, tag=from_ptag.tag)
            to_ptag.title = choose_one(to_ptag, 'title', from_ptag.title, to_ptag.title)
            to_ptag.category = choose_one(to_ptag, 'category', from_ptag.category, to_ptag.category)
            to_ptag.color = choose_one(to_ptag, 'color', from_ptag.color, to_ptag.color)
            to_ptag.order = choose_one(to_ptag, 'order', from_ptag.order, to_ptag.order)
            to_ptag.save()

            from_variant_tags = VariantTag.objects.filter(project_tag=from_ptag)
            print("-----> Transferring %s VariantTags for %s" % (len(from_variant_tags), from_ptag.tag))
            for from_vtag in from_variant_tags:
                if from_vtag.family and from_vtag.family.family_id:
                    to_family = to_family_id_to_family.get(from_vtag.family.family_id)
                else:
                    to_family = None

                _, created = VariantTag.objects.get_or_create(
                    family=to_family,
                    project_tag=to_ptag,
                    xpos=from_vtag.xpos,
                    ref=from_vtag.ref,
                    alt=from_vtag.alt)

                if created:
                    print("-----> Created variant tag %s:%s>%s" % (from_vtag.xpos, from_vtag.ref, from_vtag.alt))
示例#5
0
    def handle(self, *args, **options):
        to_project_id = options["to_project"]
        from_project_id = options["from_project"]
        from_project_datatype = options["from_project_datatype"]
        assert from_project_id
        assert to_project_id

        print("=============================")
        print("Transferring metadata from project %s to %s" % (from_project_id, to_project_id))
        if not ask_yes_no_question("Continue?"):
            return

        """
        The following models are transfered between projects.

        ProjectCollaborator => user = models.ForeignKey(User), project = models.ForeignKey('base.Project'), collaborator_type = models.CharField(max_length=20, choices=COLLABORATOR_TYPES, default="collaborator")
        Project => (private_reference_populations = models.ManyToManyField(ReferencePopulation), gene_lists = models.ManyToManyField('gene_lists.GeneList', through='ProjectGeneList'))
        Family => Project,
        FamilyGroup => Project   (families = models.ManyToManyField(Family))
        FamilyImageSlide => Family
        Cohort => Project  (individuals = models.ManyToManyField('base.Individual'), vcf_files, bam_file)
        Individual => Project, Family  # vcf_files = models.ManyToManyField(VCFFile, null=True, blank=True), bam_file = models.ForeignKey('datasets.BAMFile', null=True, blank=True)
        FamilySearchFlag => User, Family
        CausalVariant => Family
        ProjectTag => Project
        VariantTag => ProjectTag, Family
        VariantNote => User, Project
        IndividualPhenotype => Individual, ProjectPhenotype
        ProjectPhenotype => Project
        """

        # Project
        to_project, created = Project.objects.get_or_create(project_id=to_project_id)

        if created:
            print("--> Created project: " + str(to_project))
            to_project.created_date = timezone.now()
        else:
            print("--> Transferring into existing project: " + str(to_project))

        from_project = Project.objects.get(project_id=from_project_id)

        to_project.project_name = choose_one(to_project, 'project_name', from_project.project_name, to_project.project_name)
        to_project.description = choose_one(to_project, 'description', from_project.description, to_project.description)

        # set combined_projects_info
        if to_project.combined_projects_info:
            combined_projects_info = json.loads(to_project.combined_projects_info)
        else:
            combined_projects_info = {}

        combined_projects_info.update({from_project_datatype: {'project_id': from_project.project_id}})

        to_project.combined_projects_info = json.dumps(combined_projects_info)

        # TODO update collaborators?

        # Copy gene list
        for project_gene_list in ProjectGeneList.objects.filter(project=from_project):
            print("-->   Adding gene list: " + project_gene_list.gene_list.slug)
            ProjectGeneList.objects.get_or_create(project=to_project, gene_list=project_gene_list.gene_list)

        # Reference Populations
        for reference_population in from_project.private_reference_populations.all():
            if not to_project.private_reference_populations.filter(pk=reference_population.pk).exists():
                print("-->   Adding private reference population: " + reference_population.slug)
                to_project.private_reference_populations.add(reference_population)

        to_project.save()

        # ProjectCollaborator
        collaborators = ProjectCollaborator.objects.filter(project=from_project)
        if len(collaborators) > 0 and ask_yes_no_question("Transfer the %s collaborators?" % len(collaborators)):
            print("Transferring the %s collaborators" % len(collaborators))
            for c in collaborators:
                _, created = ProjectCollaborator.objects.get_or_create(project=to_project, user=c.user, collaborator_type=c.collaborator_type)
                if created:
                    print("-----> Added %s %s" % (c.collaborator_type, c.user.email))

        # --- SeqrProject - update new schema
        seqr_project, seqr_project_created = transfer_project(to_project)

        to_project.seqr_project = seqr_project
        to_project.save()

        print("-->  Set project.combined_projects_info to %s" % (to_project.combined_projects_info, ))

        # Family
        to_family_id_to_family = {} # maps family_id to the to_family object
        from_families = Family.objects.filter(project=from_project)
        print("----> Transferring %s families" % len(from_families))
        for from_f in from_families:
            to_f, created = Family.objects.get_or_create(project=to_project, family_id=from_f.family_id)
            if created:
                print("----> Created family %s" % to_f.family_id)
            else:
                print("----> Transferring into existing family %s (%s)" % (to_f.family_id, to_f.short_description))

            to_family_id_to_family[to_f.family_id] = to_f

            to_f.family_name = choose_one(to_f, 'family_name', from_f.family_name, to_f.family_name)
            to_f.short_description = choose_one(to_f, 'short_description', from_f.short_description, to_f.short_description)

            to_f.about_family_content = choose_one(to_f, 'about_family_content', from_f.about_family_content, to_f.about_family_content)

            to_f.analysis_summary_content = choose_one(to_f, 'analysis_summary_content', from_f.analysis_summary_content, to_f.analysis_summary_content)

            to_f.pedigree_image = None         # choose_one(to_f, 'pedigree_image', from_f.pedigree_image, to_f.pedigree_image)
            to_f.pedigree_image_height = None  # choose_one(to_f, 'pedigree_image_height', from_f.pedigree_image_height, to_f.pedigree_image_height)
            to_f.pedigree_image_width = None   # choose_one(to_f, 'pedigree_image_width', from_f.pedigree_image_width, to_f.pedigree_image_width)

            to_f.analysis_status = choose_one(to_f, 'analysis_status', from_f.analysis_status, to_f.analysis_status, default_value='Q')
            to_f.analysis_status_date_saved = choose_one(to_f, 'analysis_status_date_saved', from_f.analysis_status_date_saved, to_f.analysis_status_date_saved)
            to_f.analysis_status_saved_by = choose_one(to_f, 'analysis_status_saved_by', from_f.analysis_status_saved_by, to_f.analysis_status_saved_by)
            to_f.causal_inheritance_mode = choose_one(to_f, 'causal_inheritance_mode', from_f.causal_inheritance_mode, to_f.causal_inheritance_mode)

            to_f.internal_case_review_notes = choose_one(to_f, 'internal_case_review_notes', from_f.internal_case_review_notes, to_f.internal_case_review_notes)
            to_f.internal_case_review_summary = choose_one(to_f, 'internal_case_review_summary', from_f.internal_case_review_summary, to_f.internal_case_review_summary)

            to_f.coded_phenotype = choose_one(to_f, 'coded_phenotype', from_f.coded_phenotype, to_f.coded_phenotype)
            to_f.post_discovery_omim_number = choose_one(to_f, 'post_discovery_omim_number', from_f.post_discovery_omim_number, to_f.post_discovery_omim_number)

            # combined families info
            if to_f.combined_families_info:
                combined_families_info = json.loads(to_f.combined_families_info)
            else:
                combined_families_info = {}

            combined_families_info.update({from_project_datatype: {'project_id': from_project.project_id, 'family_id': from_f.family_id}})

            to_f.combined_families_info = json.dumps(combined_families_info)
            to_f.save()

            # --- SeqrProject - update new schema
            seqr_family, seqr_family_created = transfer_family(to_f, seqr_project)

            to_f.seqr_family = seqr_family
            to_f.save()

            print("---->  Set family.combined_families_info to %s" % (to_f.combined_families_info, ))


        # FamilyGroup
        for from_fg in FamilyGroup.objects.filter(project=from_project):
            FamilyGroup.objects.get_or_create(project=to_project, slug=from_fg.slug, name=from_fg.name, description=from_fg.description)

        # FamilyImageSlide
        #for from_family in Family.objects.filter(project=from_project):
        # TODO - need to iterate over image slides of from_family, and link to image slides of to_family
        #        FamilyImageSlide.objects.get_or_create(family=to_family, )


        # Cohort
        #cohorts = list(Cohort.objects.filter(project=project))
        #output_obj += cohorts

        # Individual
        print("-------> Transferring %s individuals" % len(Individual.objects.filter(project=from_project)))
        for from_family in Family.objects.filter(project=from_project):
            to_family = to_family_id_to_family[from_family.family_id]

            for from_i in Individual.objects.filter(project=from_project, family=from_family):
                to_i, created = Individual.objects.get_or_create(project=to_project, family=to_family, indiv_id=from_i.indiv_id)
                if created:
                    print("-------> Created individual %s" % to_i.indiv_id)
                else:
                    print("-------> Transferring into existing individual %s" % (to_i.indiv_id))


                to_i.created_date = choose_one(to_i, 'created_date', from_i.created_date, to_i.created_date, use_lower_value=True)
                to_i.maternal_id = choose_one(to_i, 'maternal_id', from_i.maternal_id, to_i.maternal_id)
                to_i.paternal_id = choose_one(to_i, 'paternal_id', from_i.paternal_id, to_i.paternal_id)
                to_i.gender = choose_one(to_i, 'gender', from_i.gender, to_i.gender, default_value='U')
                to_i.affected = choose_one(to_i, 'affected', from_i.affected, to_i.affected, default_value='U')

                to_i.nickname = choose_one(to_i, 'nickname', from_i.nickname, to_i.nickname)
                to_i.other_notes = choose_one(to_i, 'other_notes', from_i.other_notes, to_i.other_notes)

                to_i.case_review_status = choose_one(to_i, 'case_review_status', from_i.case_review_status, to_i.case_review_status)
                to_i.case_review_status_accepted_for = choose_one(to_i, 'case_review_status_accepted_for', from_i.case_review_status_accepted_for, to_i.case_review_status_accepted_for)

                to_i.phenotips_id = choose_one(to_i, 'phenotips_id', from_i.phenotips_id, to_i.phenotips_id)
                to_i.phenotips_data = choose_one(to_i, 'phenotips_data', from_i.phenotips_data, to_i.phenotips_data)
                # create phenotips patients and upload data


                to_i.mean_target_coverage = None
                to_i.coverage_status = choose_one(to_i, 'coverage_status', from_i.coverage_status, to_i.coverage_status, default_value='S')
                if from_i.bam_file_path:
                    if to_i.bam_file_path:
                        to_i.bam_file_path = to_i.bam_file_path + "," + from_i.bam_file_path
                    else:
                        to_i.bam_file_path = from_i.bam_file_path

                to_i.vcf_id = ''   # VCF ids will now be stored at the Sample not the Individual level

                for vcf_file in from_i.vcf_files.all():
                    if vcf_file not in to_i.vcf_files.all():
                        to_i.vcf_files.add(vcf_file)

                to_i.in_case_review = choose_one(to_i, 'in_case_review', from_i.in_case_review, to_i.in_case_review)

                # combined individuals info
                if to_i.combined_individuals_info:
                    combined_individuals_info = json.loads(to_i.combined_individuals_info)
                else:
                    combined_individuals_info = {}

                combined_individuals_info.update({from_project_datatype: {
                    'project_id': from_project.project_id,
                    'family_id': from_f.family_id,
                    'indiv_id': from_i.indiv_id}
                })

                to_i.combined_individuals_info = json.dumps(combined_individuals_info)
                to_i.save()

                seqr_individual, seqr_individual_created, phenotips_data_retrieved = transfer_individual(to_i, seqr_family, seqr_project, connect_to_phenotips=True)
                seqr_individual.seqr_individual = seqr_individual
                seqr_individual.save()

                print("------->  Set individual.combined_individuals_info to %s" % (to_i.combined_individuals_info, ))

            for from_v in CausalVariant.objects.filter(family=from_family):
                CausalVariant.objects.get_or_create(
                    family = to_family,
                    variant_type=from_v.variant_type,
                    xpos=from_v.xpos,
                    ref=from_v.ref,
                    alt=from_v.alt)

        # add this project to phenotips
        print("--> PHENOTIPS")
        create_user_in_phenotips(to_project.project_id, to_project.project_name)
        individuals_to_add = Individual.objects.filter(project=to_project, family=to_family)
        add_individuals_to_phenotips(to_project.project_id, [i.indiv_id for i in individuals_to_add])

        for i in individuals_to_add:
            if i.phenotips_data:
                to_project.phenotips_user_id = to_project.project_id
                try:
                    json_obj = json.loads(i.phenotips_data)
                    update_patient_data(to_project, i.phenotips_id, patient_json=json_obj, is_external_id=True)
                except Exception as e:
                    logger.error("%s - error while updating phenotips for %s: %s", e, i.phenotips_id, json_obj)

        # TODO: merge MME?
        from_variant_notes = VariantNote.objects.filter(project=from_project)
        print("--> Transferring %s VariantNotes:" % len(from_variant_notes))
        for from_vn in from_variant_notes:
            if from_vn.family and from_vn.family.family_id:
                to_family = to_family_id_to_family.get(from_vn.family.family_id)
            else:
                to_family = None

            _, created = VariantNote.objects.get_or_create(
                project=to_project,
                family=to_family,
                user=from_vn.user,
                date_saved=from_vn.date_saved,
                note=from_vn.note,
                xpos=from_vn.xpos,
                ref=from_vn.ref,
                alt=from_vn.alt)

            if created:
                print("-----> Created variant note %s:%s>%s" % (from_vn.xpos, from_vn.ref, from_vn.alt))

        from_project_tags = ProjectTag.objects.filter(project=from_project)
        print("--> Transferring %s ProjectTags" % len(from_project_tags))
        for from_ptag in from_project_tags:
            to_ptag, created = ProjectTag.objects.get_or_create(project=to_project, tag=from_ptag.tag)
            to_ptag.title = choose_one(to_ptag, 'title', from_ptag.title, to_ptag.title)
            to_ptag.category = choose_one(to_ptag, 'category', from_ptag.category, to_ptag.category)
            to_ptag.color = choose_one(to_ptag, 'color', from_ptag.color, to_ptag.color)
            to_ptag.order = choose_one(to_ptag, 'order', from_ptag.order, to_ptag.order)
            to_ptag.save()

            from_variant_tags = VariantTag.objects.filter(project_tag=from_ptag)
            print("-----> Transferring %s VariantTags for %s" % (len(from_variant_tags), from_ptag.tag))
            for from_vtag in from_variant_tags:
                if from_vtag.family and from_vtag.family.family_id:
                    to_family = to_family_id_to_family.get(from_vtag.family.family_id)
                else:
                    to_family = None

                _, created = VariantTag.objects.get_or_create(
                    family=to_family,
                    project_tag=to_ptag,
                    xpos=from_vtag.xpos,
                    ref=from_vtag.ref,
                    alt=from_vtag.alt)

                if created:
                    print("-----> Created variant tag %s:%s>%s" % (from_vtag.xpos, from_vtag.ref, from_vtag.alt))
示例#6
0
    def handle(self, *args, **options):
        """transfer project"""

        raise NotImplementedError("This script is not complete. combine_base_projects.py and update_projects_in_new_schema.py are being used instead - likely until the transition to the new schema is complete")


        print("Starting")
        counter = defaultdict(int)

        destination_project, created = Project.objects.get_or_create(deprecated_project_id=options['project_id_a'])
        source_project = Project.objects.get(deprecated_project_id=options['project_id_b'])

        if created:
            print("Destination project created: " + str(destination_project))

        # transfer Project metadata
        transfer_project_info(source_project, destination_project)

        if options['project_name']:
            destination_project.name = options['project_name']
        if options['project_description']:
            destination_project.description = options['project_description']
        destination_project.save()

        # transfer Families and Individuals
        for source_family in Family.objects.filter(project=source_project):
            destination_families = Family.objects.filter(
                project=destination_project,
                family_id=source_family.family_id
            )

            if not destination_families:
                # just move the family to the destination project. Its descendent individuals, samples and sample batches will move along with it.
                source_family.project = destination_project
                source_family.save()
                counter['reparented source_family'] += 1
                continue

            # the destination project contains a family with the same family_id as the source_family
            destination_family = destination_families[0]
            transfer_family_info(source_family, destination_family)
            counter['copied attributes from source_family'] += 1

            for source_individual in Individual.objects.filter(family=source_family):
                destination_individuals = Individual.objects.filter(
                    family=destination_family,
                    individual_id=source_individual.individual_id
                )
                if not destination_individuals:
                    # just move the individual to the destination family. Its descendent samples and sample batches will move along with it.
                    source_individual.family = destination_family
                    source_individual.save()
                    counter['reparented source_individual'] += 1
                    continue

                # the destination family contains an individual with the same individual_id as the source project
                destination_individual = destination_individuals[0]
                transfer_individual_info(source_individual, destination_individual)
                counter['copied attributes from source_individual'] += 1

                # Assume samples and sample batches are not duplicated between the 2 projects

        for source_variant_tag_type in VariantTagType.objects.filter(project=source_project):
            # if the same VariantTagType doesn't already exist in the destination project,
            # move the tag type to the destination project. Its descendent VariantTags will move along with it.
            destination_variant_tag_types = VariantTagType.objects.filter(
                project=destination_project,
                name=source_variant_tag_type.name
            )
            if not destination_variant_tag_types:
                source_variant_tag_type.project = destination_project
                counter['reparented variant_tag_type'] += 1
                source_variant_tag_type.save()
                continue

            destination_variant_tag_type = destination_variant_tag_types[0]
            destination_variant_tag_type.description = choose_one(destination_variant_tag_type, 'description', source_variant_tag_type.description, destination_variant_tag_type.description)
            destination_variant_tag_type.color = choose_one(destination_variant_tag_type, 'color', source_variant_tag_type.color, destination_variant_tag_type.color)
            destination_variant_tag_type.save()
            counter['copied attributes from variant_tag_type'] += 1

            for source_variant_tag in VariantTag.objects.filter(variant_tag_type=source_variant_tag_type):
                destination_variant_tag, created = VariantTag.objects.get_or_create(
                    variant_tag_type=destination_variant_tag_type,
                    xpos_start=source_variant_tag.xpos_start,
                    xpos_end=source_variant_tag.xpos_end,
                    ref=source_variant_tag.ref,
                    alt=source_variant_tag.alt,
                )
                if created:
                    counter['VariantTag created'] += 1

                if source_variant_tag.family and not destination_variant_tag.family:
                    destination_variant_tag.family = source_variant_tag.family
                destination_variant_tag.search_parameters = choose_one(destination_variant_tag, 'search_parameters', source_variant_tag.search_parameters, destination_variant_tag.search_parameters)
                counter['updated VariantTag'] += 1
                destination_variant_tag.save()

        for source_variant_note in VariantNote.objects.filter(project=source_project):
            # if the same VariantNote doesn't already exist in the destination project,
            # move the note to the destination project
            if not VariantNote.objects.filter(
                    project=destination_project,
                    note=source_variant_note.note,
                    created_by=source_variant_note.created_by):
                source_variant_note.project = destination_project
                counter['saved VariantNote'] += 1
                source_variant_note.save()

        # all data has been transferred to the destination so delete the source project
        print("Deleting project " + str(source_project) )
        for source_family in Family.objects.filter(project=source_project):
            for source_individual in Individual.objects.filter(family=source_family):
                source_individual.delete()
            source_family.delete()
        source_project.delete()

        for source_variant_tag_type in VariantTagType.objects.filter(project=source_project):
            for source_variant_tag in VariantTag.objects.filter(variant_tag_type=source_variant_tag_type):
                source_variant_tag.delete()
            source_variant_tag_type.delete()

        for source_variant_note in VariantNote.objects.filter(project=source_project):
            source_variant_note.delete()

        for key, value in counter.items():
            print("%15s: %s" % (key, value))