scripts/transfer-archive.py
def migrate_case(adapter: MongoAdapter, scout_case: dict, archive_data: dict):
    """Migrate case information from archive."""
    # update collaborators
    collaborators = list(set(scout_case['collaborators'] + archive_data['collaborators']))
    if collaborators != scout_case['collaborators']:
        LOG.info(f"set collaborators: {', '.join(collaborators)}")
        scout_case['collaborators'] = collaborators

    # update assignees
    if len(scout_case.get('assignees', [])) == 0:
        scout_user = adapter.user(archive_data['assignee'])
        if scout_user:
            scout_case['assignees'] = [archive_data['assignee']]
        else:
            LOG.warning(f"{archive_data['assignee']}: unable to find assigned user")

    # add/update suspected/causative variants
    for key in ['suspects', 'causatives']:
        scout_case[key] = scout_case.get(key, [])
        for archive_variant in archive_data[key]:
            variant_id = get_variantid(archive_variant, scout_case['_id'])
            scout_variant = adapter.variant(variant_id)
            if scout_variant:
                if scout_variant['_id'] in scout_case[key]:
                    LOG.info(f"{scout_variant['_id']}: variant already in {key}")
                else:
                    LOG.info(f"{scout_variant['_id']}: add to {key}")
                    scout_variant[key].append(scout_variant['_id'])
            else:
                LOG.warning(f"{scout_variant['_id']}: unable to find variant ({key})")
                scout_variant[key].append(variant_id)

    if not scout_case.get('synopsis'):
        # update synopsis
        scout_case['synopsis'] = archive_data['synopsis']

    scout_case['is_migrated'] = True
    adapter.case_collection.find_one_and_replace(
        {'_id': scout_case['_id']},
        scout_case,
    )

    # add/update phenotype groups/terms
    scout_institute = adapter.institute(scout_case['owner'])
    scout_user = adapter.user('mans.magnusson@scilifelab.se')
    for key in ['phenotype_terms', 'phenotype_groups']:
        for archive_term in archive_data[key]:
            adapter.add_phenotype(
                institute=scout_institute,
                case=scout_case,
                user=scout_user,
                link=f"/{scout_case['owner']}/{scout_case['display_name']}",
                hpo_term=archive_term['phenotype_id'],
                is_group=key == 'phenotype_groups',
            )
def migrate_case(adapter: MongoAdapter, scout_case: dict, archive_data: dict):
    """Migrate case information from archive."""
    # update collaborators
    collaborators = list(set(scout_case['collaborators'] + archive_data['collaborators']))
    if collaborators != scout_case['collaborators']:
        LOG.info(f"set collaborators: {', '.join(collaborators)}")
        scout_case['collaborators'] = collaborators

    # update assignees
    if len(scout_case.get('assignees', [])) == 0:
        scout_user = adapter.user(archive_data['assignee'])
        if scout_user:
            scout_case['assignees'] = [archive_data['assignee']]
        else:
            LOG.warning(f"{archive_data['assignee']}: unable to find assigned user")

    # add/update suspected/causative variants
    for key in ['suspects', 'causatives']:
        scout_case[key] = scout_case.get(key, [])
        for archive_variant in archive_data[key]:
            variant_id = get_variantid(archive_variant, scout_case['_id'])
            scout_variant = adapter.variant(variant_id)
            if scout_variant:
                if scout_variant['_id'] in scout_case[key]:
                    LOG.info(f"{scout_variant['_id']}: variant already in {key}")
                else:
                    LOG.info(f"{scout_variant['_id']}: add to {key}")
                    scout_variant[key].append(scout_variant['_id'])
            else:
                LOG.warning(f"{scout_variant['_id']}: unable to find variant ({key})")
                scout_variant[key].append(variant_id)

    if not scout_case.get('synopsis'):
        # update synopsis
        scout_case['synopsis'] = archive_data['synopsis']

    scout_case['is_migrated'] = True
    adapter.case_collection.find_one_and_replace(
        {'_id': scout_case['_id']},
        scout_case,
    )

    # add/update phenotype groups/terms
    scout_institute = adapter.institute(scout_case['owner'])
    scout_user = adapter.user('mans.magnusson@scilifelab.se')
    for key in ['phenotype_terms', 'phenotype_groups']:
        for archive_term in archive_data[key]:
            adapter.add_phenotype(
                institute=scout_institute,
                case=scout_case,
                user=scout_user,
                link=f"/{scout_case['owner']}/{scout_case['display_name']}",
                hpo_term=archive_term['phenotype_id'],
                is_group=key == 'phenotype_groups',
            )
Migrate case information from archive.