def migrate(uri: str, archive_uri: str, case_id: str, dry: bool, force: bool):
    """Update all information that was manually annotated from a old instance."""
    scout_client = MongoClient(uri)
    scout_database = scout_client[uri.rsplit('/', 1)[-1]]
    scout_adapter = MongoAdapter(database=scout_database)
    scout_case = scout_adapter.case(case_id)
    if not force and scout_case.get('is_migrated'):
        print("case already migrated")
        return

    archive_client = MongoClient(archive_uri)
    archive_database = archive_client[archive_uri.rsplit('/', 1)[-1]]
    archive_case = archive_database.case.find_one({
        'owner':
        scout_case['owner'],
        'display_name':
        scout_case['display_name']
    })

    archive_data = archive_info(archive_database, archive_case)

    if dry:
        print(ruamel.yaml.safe_dump(archive_data))
    else:
        #migrate_case(scout_adapter, scout_case, archive_data)
        pass
Exemple #2
0
def load_delivery_report(adapter: MongoAdapter,
                         report_path: str,
                         case_id: str,
                         update: bool = False):
    """Load a delivery report into a case in the database

    If the report already exists the function will exit.
    If the user want to load a report that is already in the database
    'update' has to be 'True'

    Args:
        adapter     (MongoAdapter): Connection to the database
        report_path (string):       Path to delivery report
        case_id     (string):       Optional case identifier
        update      (bool):         If an existing report should be replaced

    Returns:
        updated_case(dict)

    """

    case_obj = adapter.case(case_id=case_id)

    if case_obj is None:
        raise DataNotFoundError("no case found")

    if update or case_obj.get("delivery_report") is None:
        _update_report_path(case_obj, report_path, "delivery_report")
    else:
        raise IntegrityError("Existing report found, use update = True to "
                             "overwrite")

    LOG.info("Saving report for case {} in database".format(case_obj["_id"]))
    return adapter.replace_case(case_obj)
Exemple #3
0
def real_database(request, database_name, institute_obj, user_obj, genes,
                  parsed_panel):
    """Setup a real database with populated data"""
    mongo_client = pymongo.MongoClient()
    mongo_client.drop_database(database_name)

    database = mongo_client[database_name]
    adapter = MongoAdapter(database)

    # Populate the database
    adapter.add_institute(institute_obj)
    adapter.add_user(user_obj)

    load_hgnc_genes(adapter, genes)
    adapter.hgnc_collection.create_index([('build', pymongo.ASCENDING),
                                          ('hgnc_symbol', pymongo.ASCENDING)])
    adapter.load_panel(parsed_panel=parsed_panel)

    # load_hpo(adapter=gene_database, hpo_lines=hpo_terms_handle, disease_lines=genemap_handle)
    # adapter.add_case(case_obj)

    # for variant in variant_objs:
    #     adapter.load_variant(variant)

    def teardown():
        mongo_client.drop_database(database_name)

    request.addfinalizer(teardown)

    return adapter
def setup(context, institute, user_mail, user_name):
    """
    Setup scout instances: a demo database or a production database, according to the
    according to the subcommand specified by user.
    """
    setup_config = {
        "institute_name": institute,
        "user_name": user_name,
        "user_mail": user_mail,
    }

    mongodb_name = current_app.config["MONGO_DBNAME"]
    client = current_app.config["MONGO_CLIENT"]

    if context.invoked_subcommand == "demo":
        # Modify the name of the database that will be created
        LOG.debug("Change database name to scout-demo")
        mongodb_name = "scout-demo"

    mongo_database = client[mongodb_name]
    LOG.info("Test if mongod is running")
    try:
        mongo_database.test.find_one()
    except ServerSelectionTimeoutError as err:
        LOG.warning("Connection could not be established")
        LOG.warning("Please check if mongod is running")
        LOG.warning(err)
        raise click.Abort()

    setup_config["mongodb"] = mongodb_name
    setup_config["adapter"] = MongoAdapter(mongo_database)
    context.obj = setup_config
Exemple #5
0
def upload_research_variants(
    adapter: MongoAdapter,
    case_obj: dict,
    variant_type: str,
    category: str,
    rank_treshold: int,
):
    """Delete existing variants and upload new variants"""
    adapter.delete_variants(case_id=case_obj["_id"], variant_type=variant_type, category=category)

    LOG.info("Load %s %s for: %s", variant_type, category.upper(), case_obj["_id"])
    adapter.load_variants(
        case_obj=case_obj,
        variant_type=variant_type,
        category=category,
        rank_threshold=rank_treshold,
    )
Exemple #6
0
def load_delivery_report(adapter: MongoAdapter,
                         report_path: str,
                         case_id: str,
                         update: bool = False):
    """ Load a delivery report into a case in the database

    If the report already exists the function will exit.
    If the user want to load a report that is already in the database
    'update' has to be 'True'

    Args:
        adapter     (MongoAdapter): Connection to the database
        report_path (string):       Path to delivery report
        case_id     (string):       Optional case identifier
        update      (bool):         If an existing report should be replaced
        
    Returns:
        updated_case(dict)

    """

    case_obj = adapter.case(
        case_id=case_id,
    )

    if case_obj is None:
        raise DataNotFoundError("no case found")

    if not case_obj.get('delivery_report'):
        _put_report_in_case_root(case_obj, report_path)
    else:
        if update:
            _put_report_in_case_root(case_obj, report_path)
        else:
            raise IntegrityError('Existing delivery report found, use update = True to '
                                 'overwrite')

    logger.info('Saving report for case {} in database'.format(case_obj['_id']))
    return adapter.replace_case(case_obj)
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('*****@*****.**')
    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',
            )
Exemple #8
0
def get_case_and_institute(
    adapter: MongoAdapter, case_id: str, institute: Optional[str]
) -> (str, str):
    """Fetch the case_id and institute_id

    There was an old way to create case ids so we need a special case to handle this.
    For old case_id we assume institute-case combo
    We check if first part of the case_id is institute, then we know it is the old format
    """
    if institute:
        return case_id, institute
    split_case = case_id.split("-")
    institute_id = None

    if len(split_case) > 1:
        institute_id = split_case[0]
        institute_obj = adapter.institute(institute_id)
        if institute_obj:
            institute_id = institute_obj["_id"]
            case_id = split_case[1]
    return case_id, institute_id
Exemple #9
0
"""Code for extensions used by flask"""

from flask_bootstrap import Bootstrap
from flask_debugtoolbar import DebugToolbarExtension
from flask_ldap3_login import LDAP3LoginManager
from flask_login import LoginManager
from authlib.integrations.flask_client import OAuth
from flask_mail import Mail

from scout.adapter import MongoAdapter

from .loqus_extension import LoqusDB
from .mongo_extension import MongoDB

toolbar = DebugToolbarExtension()
bootstrap = Bootstrap()
store = MongoAdapter()
login_manager = LoginManager()
ldap_manager = LDAP3LoginManager()
oauth_client = OAuth()
mail = Mail()

loqusdb = LoqusDB()
mongo = MongoDB()