Example #1
0
def _create_patient_if_missing(project, individual):
    """Create a new PhenoTips patient record with the given patient id.

    Args:
        project (Model): seqr Project - used to retrieve PhenoTips credentials
        individual (Model): seqr Individual
    Returns:
        True if patient created
    Raises:
        PhenotipsException: if unable to create patient record
    """
    if phenotips_patient_exists(individual):
        return False

    url = '/rest/patients'
    headers = {"Content-Type": "application/json"}
    data = json.dumps({'external_id': individual.guid})
    auth_tuple = get_phenotips_uname_and_pwd_for_project(project.phenotips_user_id)

    response_items = make_phenotips_api_call('POST', url, auth_tuple=auth_tuple, http_headers=headers, data=data, expected_status_code=201, parse_json_resonse=False)
    patient_id = response_items['Location'].split('/')[-1]
    logger.info("Created PhenoTips record with patient id {patient_id} and external id {external_id}".format(patient_id=patient_id, external_id=individual.guid))

    username_read_only, _ = get_phenotips_uname_and_pwd_for_project(project.phenotips_user_id, read_only=True)
    _add_user_to_patient(username_read_only, patient_id, allow_edit=False)
    logger.info("Added PhenoTips user {username} to {patient_id}".format(username=username_read_only, patient_id=patient_id))

    individual.phenotips_patient_id = patient_id
    individual.phenotips_eid = individual.guid
    individual.save()

    return True
Example #2
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.")