INSTRUCTION
stringlengths
1
8.43k
RESPONSE
stringlengths
75
104k
Display a specific structural variant.
def sv_variant(institute_id, case_name, variant_id): """Display a specific structural variant.""" data = controllers.sv_variant(store, institute_id, case_name, variant_id) return data
Display a specific STR variant.
def str_variant(institute_id, case_name, variant_id): """Display a specific STR variant.""" data = controllers.str_variant(store, institute_id, case_name, variant_id) return data
Update user - defined information about a variant: manual rank & ACMG.
def variant_update(institute_id, case_name, variant_id): """Update user-defined information about a variant: manual rank & ACMG.""" institute_obj, case_obj = institute_and_case(store, institute_id, case_name) variant_obj = store.variant(variant_id) user_obj = store.user(current_user.email) link = request.referrer manual_rank = request.form.get('manual_rank') if manual_rank: new_manual_rank = int(manual_rank) if manual_rank != '-1' else None store.update_manual_rank(institute_obj, case_obj, user_obj, link, variant_obj, new_manual_rank) if new_manual_rank: flash("updated variant tag: {}".format(new_manual_rank), 'info') else: flash("reset variant tag: {}".format(variant_obj.get('manual_rank', 'NA')), 'info') elif request.form.get('acmg_classification'): new_acmg = request.form['acmg_classification'] acmg_classification = variant_obj.get('acmg_classification') if isinstance(acmg_classification, int) and (new_acmg == ACMG_MAP[acmg_classification]): new_acmg = None store.update_acmg(institute_obj, case_obj, user_obj, link, variant_obj, new_acmg) flash("updated ACMG classification: {}".format(new_acmg), 'info') new_dismiss = request.form.getlist('dismiss_variant') if request.form.getlist('dismiss_variant'): store.update_dismiss_variant(institute_obj, case_obj, user_obj, link, variant_obj, new_dismiss) if new_dismiss: flash("Dismissed variant: {}".format(new_dismiss), 'info') if variant_obj.get('dismiss_variant') and not new_dismiss: if 'dismiss' in request.form: store.update_dismiss_variant(institute_obj, case_obj, user_obj, link, variant_obj, new_dismiss) flash("Reset variant dismissal: {}".format(variant_obj.get('dismiss_variant')), 'info') else: log.debug("DO NOT reset variant dismissal: {}".format(variant_obj.get('dismiss_variant')), 'info') mosaic_tags = request.form.getlist('mosaic_tags') if mosaic_tags: store.update_mosaic_tags(institute_obj, case_obj, user_obj, link, variant_obj, mosaic_tags) if new_dismiss: flash("Added mosaic tags: {}".format(mosaic_tags), 'info') if variant_obj.get('mosaic_tags') and not mosaic_tags: if 'mosaic' in request.form: store.update_mosaic_tags(institute_obj, case_obj, user_obj, link, variant_obj, mosaic_tags) flash("Reset mosaic tags: {}".format(variant_obj.get('mosaic_tags')), 'info') return redirect(request.referrer)
Start procedure to validate variant using other techniques.
def verify(institute_id, case_name, variant_id, variant_category, order): """Start procedure to validate variant using other techniques.""" institute_obj, case_obj = institute_and_case(store, institute_id, case_name) variant_obj = store.variant(variant_id) user_obj = store.user(current_user.email) comment = request.form.get('verification_comment') try: controllers.variant_verification(store=store, mail=mail, institute_obj=institute_obj, case_obj=case_obj, user_obj=user_obj, comment=comment, variant_obj=variant_obj, sender=current_app.config['MAIL_USERNAME'], variant_url=request.referrer, order=order, url_builder=url_for) except controllers.MissingVerificationRecipientError: flash('No verification recipients added to institute.', 'danger') return redirect(request.referrer)
Build a clinVar submission form for a variant.
def clinvar(institute_id, case_name, variant_id): """Build a clinVar submission form for a variant.""" data = controllers.clinvar_export(store, institute_id, case_name, variant_id) if request.method == 'GET': return data else: #POST form_dict = request.form.to_dict() submission_objects = set_submission_objects(form_dict) # A tuple of submission objects (variants and casedata objects) # Add submission data to an open clinvar submission object, # or create a new if no open submission is found in database open_submission = store.get_open_clinvar_submission(current_user.email, institute_id) updated_submission = store.add_to_submission(open_submission['_id'], submission_objects) # Redirect to clinvar submissions handling page, and pass it the updated_submission_object return redirect(url_for('cases.clinvar_submissions', institute_id=institute_id))
Show cancer variants overview.
def cancer_variants(institute_id, case_name): """Show cancer variants overview.""" data = controllers.cancer_variants(store, request.args, institute_id, case_name) return data
ACMG classification form.
def variant_acmg(institute_id, case_name, variant_id): """ACMG classification form.""" if request.method == 'GET': data = controllers.variant_acmg(store, institute_id, case_name, variant_id) return data else: criteria = [] criteria_terms = request.form.getlist('criteria') for term in criteria_terms: criteria.append(dict( term=term, comment=request.form.get("comment-{}".format(term)), links=[request.form.get("link-{}".format(term))], )) acmg = controllers.variant_acmg_post(store, institute_id, case_name, variant_id, current_user.email, criteria) flash("classified as: {}".format(acmg), 'info') return redirect(url_for('.variant', institute_id=institute_id, case_name=case_name, variant_id=variant_id))
Show or delete an ACMG evaluation.
def evaluation(evaluation_id): """Show or delete an ACMG evaluation.""" evaluation_obj = store.get_evaluation(evaluation_id) controllers.evaluation(store, evaluation_obj) if request.method == 'POST': link = url_for('.variant', institute_id=evaluation_obj['institute']['_id'], case_name=evaluation_obj['case']['display_name'], variant_id=evaluation_obj['variant_specific']) store.delete_evaluation(evaluation_obj) return redirect(link) return dict(evaluation=evaluation_obj, institute=evaluation_obj['institute'], case=evaluation_obj['case'], variant=evaluation_obj['variant'], CRITERIA=ACMG_CRITERIA)
Calculate an ACMG classification from submitted criteria.
def acmg(): """Calculate an ACMG classification from submitted criteria.""" criteria = request.args.getlist('criterion') classification = get_acmg(criteria) return jsonify(dict(classification=classification))
Parse gene panel file and fill in HGNC symbols for filter.
def upload_panel(institute_id, case_name): """Parse gene panel file and fill in HGNC symbols for filter.""" file = form.symbol_file.data if file.filename == '': flash('No selected file', 'warning') return redirect(request.referrer) try: stream = io.StringIO(file.stream.read().decode('utf-8'), newline=None) except UnicodeDecodeError as error: flash("Only text files are supported!", 'warning') return redirect(request.referrer) category = request.args.get('category') if(category == 'sv'): form = SvFiltersForm(request.args) else: form = FiltersForm(request.args) hgnc_symbols = set(form.hgnc_symbols.data) new_hgnc_symbols = controllers.upload_panel(store, institute_id, case_name, stream) hgnc_symbols.update(new_hgnc_symbols) form.hgnc_symbols.data = ','.join(hgnc_symbols) # reset gene panels form.gene_panels.data = '' # HTTP redirect code 307 asks that the browser preserves the method of request (POST). if(category == 'sv'): return redirect(url_for('.sv_variants', institute_id=institute_id, case_name=case_name, **form.data), code=307) else: return redirect(url_for('.variants', institute_id=institute_id, case_name=case_name, **form.data), code=307)
Download all verified variants for user s cases
def download_verified(): """Download all verified variants for user's cases""" user_obj = store.user(current_user.email) user_institutes = user_obj.get('institutes') temp_excel_dir = os.path.join(variants_bp.static_folder, 'verified_folder') os.makedirs(temp_excel_dir, exist_ok=True) written_files = controllers.verified_excel_file(store, user_institutes, temp_excel_dir) if written_files: today = datetime.datetime.now().strftime('%Y-%m-%d') # zip the files on the fly and serve the archive to the user data = io.BytesIO() with zipfile.ZipFile(data, mode='w') as z: for f_name in pathlib.Path(temp_excel_dir).iterdir(): zipfile.ZipFile z.write(f_name, os.path.basename(f_name)) data.seek(0) # remove temp folder with excel files in it shutil.rmtree(temp_excel_dir) return send_file( data, mimetype='application/zip', as_attachment=True, attachment_filename='_'.join(['scout', 'verified_variants', today])+'.zip' ) else: flash("No verified variants could be exported for user's institutes", 'warning') return redirect(request.referrer)
Return a dictionary with hgnc symbols as keys
def genes_by_alias(hgnc_genes): """Return a dictionary with hgnc symbols as keys Value of the dictionaries are information about the hgnc ids for a symbol. If the symbol is primary for a gene then 'true_id' will exist. A list of hgnc ids that the symbol points to is in ids. Args: hgnc_genes(dict): a dictionary with hgnc_id as key and gene info as value Returns: alias_genes(dict): { 'hgnc_symbol':{ 'true_id': int, 'ids': list(int) } } """ alias_genes = {} for hgnc_id in hgnc_genes: gene = hgnc_genes[hgnc_id] # This is the primary symbol: hgnc_symbol = gene['hgnc_symbol'] for alias in gene['previous_symbols']: true_id = None if alias == hgnc_symbol: true_id = hgnc_id if alias in alias_genes: alias_genes[alias.upper()]['ids'].add(hgnc_id) if true_id: alias_genes[alias.upper()]['true_id'] = hgnc_id else: alias_genes[alias.upper()] = { 'true': true_id, 'ids': set([hgnc_id]) } return alias_genes
Add the coordinates from ensembl Args: genes ( dict ): Dictionary with all genes ensembl_lines ( iteable ): Iteable with raw ensembl info
def add_ensembl_info(genes, ensembl_lines): """Add the coordinates from ensembl Args: genes(dict): Dictionary with all genes ensembl_lines(iteable): Iteable with raw ensembl info """ LOG.info("Adding ensembl coordinates") # Parse and add the ensembl gene info if isinstance(ensembl_lines, DataFrame): ensembl_genes = parse_ensembl_gene_request(ensembl_lines) else: ensembl_genes = parse_ensembl_genes(ensembl_lines) for ensembl_gene in ensembl_genes: gene_obj = genes.get(ensembl_gene['hgnc_id']) if not gene_obj: continue gene_obj['chromosome'] = ensembl_gene['chrom'] gene_obj['start'] = ensembl_gene['gene_start'] gene_obj['end'] = ensembl_gene['gene_end'] # ensembl ids can differ between builds. There is one stated in HGNC # that is true for build 38. So we add information from ensembl gene_obj['ensembl_gene_id'] = ensembl_gene['ensembl_gene_id']
Add information from the exac genes Currently we only add the pLi score on gene level The exac resource only use HGNC symbol to identify genes so we need our alias mapping. Args: genes ( dict ): Dictionary with all genes alias_genes ( dict ): Genes mapped to all aliases ensembl_lines ( iteable ): Iteable with raw ensembl info
def add_exac_info(genes, alias_genes, exac_lines): """Add information from the exac genes Currently we only add the pLi score on gene level The exac resource only use HGNC symbol to identify genes so we need our alias mapping. Args: genes(dict): Dictionary with all genes alias_genes(dict): Genes mapped to all aliases ensembl_lines(iteable): Iteable with raw ensembl info """ LOG.info("Add exac pli scores") for exac_gene in parse_exac_genes(exac_lines): hgnc_symbol = exac_gene['hgnc_symbol'].upper() pli_score = exac_gene['pli_score'] for hgnc_id in get_correct_ids(hgnc_symbol, alias_genes): genes[hgnc_id]['pli_score'] = pli_score
Add omim information We collect information on what phenotypes that are associated with a gene what inheritance models that are associated and the correct omim id. Args: genes ( dict ): Dictionary with all genes alias_genes ( dict ): Genes mapped to all aliases genemap_lines ( iterable ): Iterable with raw omim info mim2gene_lines ( iterable ): Iterable with raw omim info
def add_omim_info(genes, alias_genes, genemap_lines, mim2gene_lines): """Add omim information We collect information on what phenotypes that are associated with a gene, what inheritance models that are associated and the correct omim id. Args: genes(dict): Dictionary with all genes alias_genes(dict): Genes mapped to all aliases genemap_lines(iterable): Iterable with raw omim info mim2gene_lines(iterable): Iterable with raw omim info """ LOG.info("Add omim info") omim_genes = get_mim_genes(genemap_lines, mim2gene_lines) for hgnc_symbol in omim_genes: omim_info = omim_genes[hgnc_symbol] inheritance = omim_info.get('inheritance', set()) for hgnc_id in get_correct_ids(hgnc_symbol, alias_genes): gene_info = genes[hgnc_id] # Update the omim id to the one found in omim gene_info['omim_id'] = omim_info['mim_number'] gene_info['inheritance_models'] = list(inheritance) gene_info['phenotypes'] = omim_info.get('phenotypes', [])
Add information of incomplete penetrance
def add_incomplete_penetrance(genes, alias_genes, hpo_lines): """Add information of incomplete penetrance""" LOG.info("Add incomplete penetrance info") for hgnc_symbol in get_incomplete_penetrance_genes(hpo_lines): for hgnc_id in get_correct_ids(hgnc_symbol, alias_genes): genes[hgnc_id]['incomplete_penetrance'] = True
Try to get the correct gene based on hgnc_symbol The HGNC symbol is unfortunately not a persistent gene identifier. Many of the resources that are used by Scout only provides the hgnc symbol to identify a gene. We need a way to guess what gene is pointed at. Args: hgnc_symbol ( str ): The symbol used by a resource alias_genes ( dict ): A dictionary with all the alias symbols ( including the current symbol ) for all genes Returns: hgnc_ids ( iterable ( int )): Hopefully only one but a symbol could map to several ids
def get_correct_ids(hgnc_symbol, alias_genes): """Try to get the correct gene based on hgnc_symbol The HGNC symbol is unfortunately not a persistent gene identifier. Many of the resources that are used by Scout only provides the hgnc symbol to identify a gene. We need a way to guess what gene is pointed at. Args: hgnc_symbol(str): The symbol used by a resource alias_genes(dict): A dictionary with all the alias symbols (including the current symbol) for all genes Returns: hgnc_ids(iterable(int)): Hopefully only one but a symbol could map to several ids """ hgnc_ids = set() hgnc_symbol = hgnc_symbol.upper() if hgnc_symbol in alias_genes: hgnc_id_info = alias_genes[hgnc_symbol] if hgnc_id_info['true']: return set([hgnc_id_info['true']]) else: return set(hgnc_id_info['ids']) return hgnc_ids
Gather information from different sources and return a gene dict
def link_genes(ensembl_lines, hgnc_lines, exac_lines, mim2gene_lines, genemap_lines, hpo_lines): """Gather information from different sources and return a gene dict Extract information collected from a number of sources and combine them into a gene dict with HGNC symbols as keys. hgnc_id works as the primary symbol and it is from this source we gather as much information as possible (hgnc_complete_set.txt) Coordinates are gathered from ensemble and the entries are linked from hgnc to ensembl via ENSGID. From exac the gene intolerance scores are collected, genes are linked to hgnc via hgnc symbol. This is a unstable symbol since they often change. Args: ensembl_lines(iterable(str)): Strings with ensembl gene information hgnc_lines(iterable(str)): Strings with hgnc gene information exac_lines(iterable(str)): Strings with exac PLi score info mim2gene_lines(iterable(str)) genemap_lines(iterable(str)) hpo_lines(iterable(str)): Strings with hpo gene information Yields: gene(dict): A dictionary with gene information """ genes = {} LOG.info("Linking genes") # HGNC genes are the main source, these define the gene dataset to use # Try to use as much information as possible from hgnc for hgnc_gene in parse_hgnc_genes(hgnc_lines): hgnc_id = hgnc_gene['hgnc_id'] genes[hgnc_id] = hgnc_gene add_ensembl_info(genes, ensembl_lines) symbol_to_id = genes_by_alias(genes) add_exac_info(genes, symbol_to_id, exac_lines) add_omim_info(genes, symbol_to_id, genemap_lines, mim2gene_lines) add_incomplete_penetrance(genes, symbol_to_id, hpo_lines) return genes
Send a request to MatchMaker and return its response
def matchmaker_request(url, token, method, content_type=None, accept=None, data=None): """Send a request to MatchMaker and return its response Args: url(str): url to send request to token(str): MME server authorization token method(str): 'GET', 'POST' or 'DELETE' content_type(str): MME request Content-Type accept(str): accepted response data(dict): eventual data to send in request Returns: json_response(dict): server response """ headers = Headers() headers = { 'X-Auth-Token': token} if content_type: headers['Content-Type'] = content_type if accept: headers['Accept'] = accept #sending data anyway so response will not be cached req_data = data or {'timestamp' : datetime.datetime.now().timestamp()} json_response = None try: LOG.info('Sending {} request to MME url {}. Data sent: {}'.format( method, url, req_data)) resp = requests.request( method = method, url = url, headers = headers, data = json.dumps(req_data) ) json_response = resp.json() LOG.info('MME server response was:{}'.format(json_response)) if isinstance(json_response, str): json_response = { 'message' : json_response, } elif isinstance(json_response, list): #asking for connected nodes return json_response json_response['status_code'] = resp.status_code except Exception as err: LOG.info('An error occurred while sending HTTP request to server ({})'.format(err)) json_response = { 'message' : str(err) } return json_response
Return the available MatchMaker nodes
def mme_nodes(mme_base_url, token): """Return the available MatchMaker nodes Args: mme_base_url(str): base URL of MME service token(str): MME server authorization token Returns: nodes(list): a list of node disctionaries """ nodes = [] if not mme_base_url or not token: return nodes url = ''.join([mme_base_url, '/nodes']) nodes = matchmaker_request(url=url, token=token, method='GET') LOG.info('Matchmaker has the following connected nodes:{}'.format(nodes)) return nodes
Get the cytoband coordinate for a position
def get_cytoband_coordinates(chrom, pos): """Get the cytoband coordinate for a position Args: chrom(str) pos(int) Returns: coordinate(str) """ coordinate = "" if chrom in CYTOBANDS: for interval in CYTOBANDS[chrom][pos]: coordinate = interval.data return coordinate
Get the subcategory for a VCF variant
def get_sub_category(alt_len, ref_len, category, svtype=None): """Get the subcategory for a VCF variant The sub categories are: 'snv', 'indel', 'del', 'ins', 'dup', 'bnd', 'inv' Args: alt_len(int) ref_len(int) category(str) svtype(str) Returns: subcategory(str) """ subcategory = '' if category in ('snv', 'indel', 'cancer'): if ref_len == alt_len: subcategory = 'snv' else: subcategory = 'indel' elif category == 'sv': subcategory = svtype return subcategory
Return the length of a variant
def get_length(alt_len, ref_len, category, pos, end, svtype=None, svlen=None): """Return the length of a variant Args: alt_len(int) ref_len(int) category(str) svtype(str) svlen(int) """ # -1 would indicate uncertain length length = -1 if category in ('snv', 'indel', 'cancer'): if ref_len == alt_len: length = alt_len else: length = abs(ref_len - alt_len) elif category == 'sv': if svtype == 'bnd': length = int(10e10) else: if svlen: length = abs(int(svlen)) # Some software does not give a length but they give END elif end: if end != pos: length = end - pos return length
Return the end coordinate for a variant
def get_end(pos, alt, category, snvend=None, svend=None, svlen=None): """Return the end coordinate for a variant Args: pos(int) alt(str) category(str) snvend(str) svend(int) svlen(int) Returns: end(int) """ # If nothing is known we set end to be same as start end = pos # If variant is snv or indel we know that cyvcf2 can handle end pos if category in ('snv', 'indel', 'cancer'): end = snvend # With SVs we have to be a bit more careful elif category == 'sv': # The END field from INFO usually works fine end = svend # For some cases like insertions the callers set end to same as pos # In those cases we can hope that there is a svlen... if svend == pos: if svlen: end = pos + svlen # If variant is 'BND' they have ':' in alt field # Information about other end is in the alt field if ':' in alt: match = BND_ALT_PATTERN.match(alt) if match: end = int(match.group(2)) return end
Find out the coordinates for a variant
def parse_coordinates(variant, category): """Find out the coordinates for a variant Args: variant(cyvcf2.Variant) Returns: coordinates(dict): A dictionary on the form: { 'position':<int>, 'end':<int>, 'end_chrom':<str>, 'length':<int>, 'sub_category':<str>, 'mate_id':<str>, 'cytoband_start':<str>, 'cytoband_end':<str>, } """ ref = variant.REF if variant.ALT: alt = variant.ALT[0] if category=="str" and not variant.ALT: alt = '.' chrom_match = CHR_PATTERN.match(variant.CHROM) chrom = chrom_match.group(2) svtype = variant.INFO.get('SVTYPE') if svtype: svtype = svtype.lower() mate_id = variant.INFO.get('MATEID') svlen = variant.INFO.get('SVLEN') svend = variant.INFO.get('END') snvend = int(variant.end) position = int(variant.POS) ref_len = len(ref) alt_len = len(alt) sub_category = get_sub_category(alt_len, ref_len, category, svtype) end = get_end(position, alt, category, snvend, svend) length = get_length(alt_len, ref_len, category, position, end, svtype, svlen) end_chrom = chrom if sub_category == 'bnd': if ':' in alt: match = BND_ALT_PATTERN.match(alt) # BND will often be translocations between different chromosomes if match: other_chrom = match.group(1) match = CHR_PATTERN.match(other_chrom) end_chrom = match.group(2) cytoband_start = get_cytoband_coordinates(chrom, position) cytoband_end = get_cytoband_coordinates(end_chrom, end) coordinates = { 'position': position, 'end': end, 'length': length, 'sub_category': sub_category, 'mate_id': mate_id, 'cytoband_start': cytoband_start, 'cytoband_end': cytoband_end, 'end_chrom': end_chrom, } return coordinates
Parse iterable with cytoband coordinates Args: lines ( iterable ): Strings on format chr1 \ t2300000 \ t5400000 \ tp36. 32 \ tgpos25 Returns: cytobands ( dict ): Dictionary with chromosome names as keys and interval trees as values
def parse_cytoband(lines): """Parse iterable with cytoband coordinates Args: lines(iterable): Strings on format "chr1\t2300000\t5400000\tp36.32\tgpos25" Returns: cytobands(dict): Dictionary with chromosome names as keys and interval trees as values """ cytobands = {} for line in lines: line = line.rstrip() splitted_line = line.split('\t') chrom = splitted_line[0].lstrip('chr') start = int(splitted_line[1]) stop = int(splitted_line[2]) name = splitted_line[3] if chrom in cytobands: # Add interval to existing tree cytobands[chrom][start:stop] = name else: # Create a new interval tree new_tree = intervaltree.IntervalTree() # create the interval new_tree[start:stop] = name # Add the interval tree cytobands[chrom] = new_tree return cytobands
docstring for cli
def cli(infile): """docstring for cli""" lines = get_file_handle(infile) cytobands = parse_cytoband(lines) print("Check some coordinates:") print("checking chrom 1 pos 2") intervals = cytobands['1'][2] for interval in intervals: print(interval) print(interval.begin) print(interval.end) print(interval.data) # print(interval.__dict__) print(cytobands['1'][2]) print("checking chrom 8 pos 101677777") print(cytobands['8'][101677777]) print("checking chrom X pos 4200000 - 6000000") print(cytobands['X'][4200000:6000000])
Update a gene panel in the database We need to update the actual gene panel and then all cases that refers to the panel. Args: adapter ( scout. adapter. MongoAdapter ) panel_name ( str ): Unique name for a gene panel panel_version ( float ) new_version ( float ) new_date ( datetime. datetime ) Returns: updated_panel ( scout. models. GenePanel ): The updated gene panel object
def update_panel(adapter, panel_name, panel_version, new_version=None, new_date=None): """Update a gene panel in the database We need to update the actual gene panel and then all cases that refers to the panel. Args: adapter(scout.adapter.MongoAdapter) panel_name(str): Unique name for a gene panel panel_version(float) new_version(float) new_date(datetime.datetime) Returns: updated_panel(scout.models.GenePanel): The updated gene panel object """ panel_obj = adapter.gene_panel(panel_name, panel_version) if not panel_obj: raise IntegrityError("Panel %s version %s does not exist" % (panel_name, panel_version)) updated_panel = adapter.update_panel(panel_obj, new_version, new_date) panel_id = updated_panel['_id'] # We need to alter the embedded panels in all affected cases update = {'$set': {}} if new_version: update['$set']['panels.$.version'] = updated_panel['version'] if new_date: update['$set']['panels.$.updated_at'] = updated_panel['date'] LOG.info('Updating affected cases with {0}'.format(update)) query = {'panels': { '$elemMatch': {'panel_name': panel_name}}} adapter.case_collection.update_many(query, update) return updated_panel
scout: manage interactions with a scout instance.
def cli(context, mongodb, username, password, authdb, host, port, loglevel, config, demo): """scout: manage interactions with a scout instance.""" # log_format = "%(message)s" if sys.stdout.isatty() else None log_format = None coloredlogs.install(level=loglevel, fmt=log_format) LOG.info("Running scout version %s", __version__) LOG.debug("Debug logging enabled.") mongo_config = {} cli_config = {} if config: LOG.debug("Use config file %s", config) with open(config, 'r') as in_handle: cli_config = yaml.load(in_handle) mongo_config['mongodb'] = (mongodb or cli_config.get('mongodb') or 'scout') if demo: mongo_config['mongodb'] = 'scout-demo' mongo_config['host'] = (host or cli_config.get('host') or 'localhost') mongo_config['port'] = (port or cli_config.get('port') or 27017) mongo_config['username'] = username or cli_config.get('username') mongo_config['password'] = password or cli_config.get('password') mongo_config['authdb'] = authdb or cli_config.get('authdb') or mongo_config['mongodb'] mongo_config['omim_api_key'] = cli_config.get('omim_api_key') if context.invoked_subcommand in ('setup', 'serve'): mongo_config['adapter'] = None else: LOG.info("Setting database name to %s", mongo_config['mongodb']) LOG.debug("Setting host to %s", mongo_config['host']) LOG.debug("Setting port to %s", mongo_config['port']) try: client = get_connection(**mongo_config) except ConnectionFailure: context.abort() database = client[mongo_config['mongodb']] LOG.info("Setting up a mongo adapter") mongo_config['client'] = client adapter = MongoAdapter(database) mongo_config['adapter'] = adapter LOG.info("Check if authenticated...") try: for ins_obj in adapter.institutes(): pass except OperationFailure as err: LOG.info("User not authenticated") context.abort() context.obj = mongo_config
Parse an exac formated line Args: line ( list ): A list with exac gene info header ( list ): A list with the header info Returns: exac_info ( dict ): A dictionary with the relevant info
def parse_exac_line(line, header): """Parse an exac formated line Args: line(list): A list with exac gene info header(list): A list with the header info Returns: exac_info(dict): A dictionary with the relevant info """ exac_gene = {} splitted_line = line.rstrip().split('\t') exac_gene = dict(zip(header, splitted_line)) exac_gene['hgnc_symbol'] = exac_gene['gene'] exac_gene['pli_score'] = float(exac_gene['pLI']) exac_gene['raw'] = line return exac_gene
Parse lines with exac formated genes This is designed to take a dump with genes from exac. This is downloaded from: ftp. broadinstitute. org/ pub/ ExAC_release// release0. 3/ functional_gene_constraint/ fordist_cleaned_exac_r03_march16_z_pli_rec_null_data. txt Args: lines ( iterable ( str )): An iterable with ExAC formated genes Yields: exac_gene ( dict ): A dictionary with the relevant information
def parse_exac_genes(lines): """Parse lines with exac formated genes This is designed to take a dump with genes from exac. This is downloaded from: ftp.broadinstitute.org/pub/ExAC_release//release0.3/functional_gene_constraint/ fordist_cleaned_exac_r03_march16_z_pli_rec_null_data.txt Args: lines(iterable(str)): An iterable with ExAC formated genes Yields: exac_gene(dict): A dictionary with the relevant information """ header = [] logger.info("Parsing exac genes...") for index, line in enumerate(lines): if index == 0: header = line.rstrip().split('\t') elif len(line) > 0: exac_gene = parse_exac_line(line, header) yield exac_gene
Show all panels for a case.
def panels(): """Show all panels for a case.""" if request.method == 'POST': # update an existing panel csv_file = request.files['csv_file'] content = csv_file.stream.read() lines = None try: if b'\n' in content: lines = content.decode('utf-8', 'ignore').split('\n') else: lines = content.decode('windows-1252').split('\r') except Exception as err: flash('Something went wrong while parsing the panel CSV file! ({})'.format(err), 'danger') return redirect(request.referrer) new_panel_name = request.form.get('new_panel_name') if new_panel_name: #create a new panel new_panel_id = controllers.new_panel( store=store, institute_id=request.form['institute'], panel_name=new_panel_name, display_name=request.form['display_name'], csv_lines=lines, ) if new_panel_id is None: flash('Something went wrong and the panel list was not updated!','warning') return redirect(request.referrer) else: flash("new gene panel added, {}!".format(new_panel_name),'success') return redirect(url_for('panels.panel', panel_id=new_panel_id)) else: # modify an existing panel update_option = request.form['modify_option'] panel_obj= controllers.update_panel( store=store, panel_name=request.form['panel_name'], csv_lines=lines, option=update_option ) if panel_obj is None: return abort(404, "gene panel not found: {}".format(request.form['panel_name'])) else: return redirect(url_for('panels.panel', panel_id=panel_obj['_id'])) institutes = list(user_institutes(store, current_user)) panel_names = [name for institute in institutes for name in store.gene_panels(institute_id=institute['_id']).distinct('panel_name')] panel_versions = {} for name in panel_names: panel_versions[name]=store.gene_panels(panel_id=name) panel_groups = [] for institute_obj in institutes: institute_panels = store.latest_panels(institute_obj['_id']) panel_groups.append((institute_obj, institute_panels)) return dict(panel_groups=panel_groups, panel_names=panel_names, panel_versions=panel_versions, institutes=institutes)
Display ( and add pending updates to ) a specific gene panel.
def panel(panel_id): """Display (and add pending updates to) a specific gene panel.""" panel_obj = store.gene_panel(panel_id) or store.panel(panel_id) if request.method == 'POST': raw_hgnc_id = request.form['hgnc_id'] if '|' in raw_hgnc_id: raw_hgnc_id = raw_hgnc_id.split(' | ', 1)[0] hgnc_id = 0 try: hgnc_id = int(raw_hgnc_id) except: flash("Provided HGNC is not valid : '{}'". format(raw_hgnc_id), 'danger') return redirect(request.referrer) action = request.form['action'] gene_obj = store.hgnc_gene(hgnc_id) if gene_obj is None: flash("HGNC id not found: {}".format(hgnc_id), 'warning') return redirect(request.referrer) if action == 'add': panel_gene = controllers.existing_gene(store, panel_obj, hgnc_id) if panel_gene: flash("gene already in panel: {}".format(panel_gene['symbol']), 'warning') else: # ask user to fill-in more information about the gene return redirect(url_for('.gene_edit', panel_id=panel_id, hgnc_id=hgnc_id)) elif action == 'delete': log.debug("marking gene to be deleted: %s", hgnc_id) panel_obj = store.add_pending(panel_obj, gene_obj, action='delete') data = controllers.panel(store, panel_obj) if request.args.get('case_id'): data['case'] = store.case(request.args['case_id']) if request.args.get('institute_id'): data['institute'] = store.institute(request.args['institute_id']) return data
Update panel to a new version.
def panel_update(panel_id): """Update panel to a new version.""" panel_obj = store.panel(panel_id) update_version = request.form.get('version', None) new_panel_id = store.apply_pending(panel_obj, update_version) return redirect(url_for('panels.panel', panel_id=new_panel_id))
Export panel to PDF file
def panel_export(panel_id): """Export panel to PDF file""" panel_obj = store.panel(panel_id) data = controllers.panel_export(store, panel_obj) data['report_created_at'] = datetime.datetime.now().strftime("%Y-%m-%d") html_report = render_template('panels/panel_pdf_simple.html', **data) return render_pdf(HTML(string=html_report), download_filename=data['panel']['panel_name']+'_'+str(data['panel']['version'])+'_'+datetime.datetime.now().strftime("%Y-%m-%d")+'_scout.pdf')
Edit additional information about a panel gene.
def gene_edit(panel_id, hgnc_id): """Edit additional information about a panel gene.""" panel_obj = store.panel(panel_id) hgnc_gene = store.hgnc_gene(hgnc_id) panel_gene = controllers.existing_gene(store, panel_obj, hgnc_id) form = PanelGeneForm() transcript_choices = [] for transcript in hgnc_gene['transcripts']: if transcript.get('refseq_id'): refseq_id = transcript.get('refseq_id') transcript_choices.append((refseq_id, refseq_id)) form.disease_associated_transcripts.choices = transcript_choices if form.validate_on_submit(): action = 'edit' if panel_gene else 'add' info_data = form.data.copy() if 'csrf_token' in info_data: del info_data['csrf_token'] store.add_pending(panel_obj, hgnc_gene, action=action, info=info_data) return redirect(url_for('.panel', panel_id=panel_id)) if panel_gene: for field_key in ['disease_associated_transcripts', 'reduced_penetrance', 'mosaicism', 'inheritance_models', 'database_entry_version', 'comment']: form_field = getattr(form, field_key) if not form_field.data: panel_value = panel_gene.get(field_key) if panel_value is not None: form_field.process_data(panel_value) return dict(panel=panel_obj, form=form, gene=hgnc_gene, panel_gene=panel_gene)
Add delivery report to an existing case.
def delivery_report(context, case_id, report_path, update): """Add delivery report to an existing case.""" adapter = context.obj['adapter'] try: load_delivery_report(adapter=adapter, case_id=case_id, report_path=report_path, update=update) LOG.info("saved report to case!") except Exception as e: LOG.error(e) context.abort()
Parse a peddy. ped file Args: lines ( iterable ( str )) Returns: peddy_ped ( list ( dict ))
def parse_peddy_ped(lines): """Parse a peddy.ped file Args: lines(iterable(str)) Returns: peddy_ped(list(dict)) """ peddy_ped = [] header = [] for i,line in enumerate(lines): line = line.rstrip() if i == 0: # Header line header = line.lstrip('#').split('\t') else: ind_info = dict(zip(header, line.split('\t'))) # PC1/PC2/PC3/PC4: the first 4 values after this sample was # projected onto the thousand genomes principle components. ind_info['PC1'] = convert_number(ind_info['PC1']) ind_info['PC2'] = convert_number(ind_info['PC2']) ind_info['PC3'] = convert_number(ind_info['PC3']) # ancestry-prediction one of AFR AMR EAS EUR SAS UNKNOWN ind_info['het_call_rate'] = convert_number(ind_info['het_call_rate']) # idr_baf: inter-decile range (90th percentile - 10th percentile) # of b-allele frequency. We make a distribution of all sites of # alts / (ref + alts) and then report the difference between the # 90th and the 10th percentile. # Large values indicated likely sample contamination. ind_info['het_idr_baf'] = convert_number(ind_info['het_idr_baf']) ind_info['het_mean_depth'] = convert_number(ind_info['het_mean_depth']) peddy_ped.append(ind_info) return peddy_ped
Parse a. ped_check. csv file Args: lines ( iterable ( str )) Returns: ped_check ( list ( dict ))
def parse_peddy_ped_check(lines): """Parse a .ped_check.csv file Args: lines(iterable(str)) Returns: ped_check(list(dict)) """ ped_check = [] header = [] for i,line in enumerate(lines): line = line.rstrip() if i == 0: # Header line header = line.lstrip('#').split(',') else: pair_info = dict(zip(header, line.split(','))) # the number of sites at which sample_a was heterozygous pair_info['hets_a'] = convert_number(pair_info['hets_a']) # the number of sites at which sample_b was heterozygous pair_info['hets_b'] = convert_number(pair_info['hets_b']) # the number of sites at which the 2 samples shared no alleles # (should approach 0 for parent-child pairs). pair_info['ibs0'] = convert_number(pair_info['ibs0']) # the number of sites and which the 2 samples where both # hom-ref, both het, or both hom-alt. pair_info['ibs2'] = convert_number(pair_info['ibs2']) # the number of sites that was used to predict the relatedness. pair_info['n'] = convert_number(pair_info['n']) # the relatedness reported in the ped file. pair_info['rel'] = convert_number(pair_info['rel']) # the relatedness reported in the ped file. pair_info['pedigree_relatedness'] = convert_number(pair_info['pedigree_relatedness']) # difference between the preceding 2 colummns. pair_info['rel_difference'] = convert_number(pair_info['rel_difference']) # the number of sites at which both samples were hets. pair_info['shared_hets'] = convert_number(pair_info['shared_hets']) # boolean indicating that this pair is a parent-child pair # according to the ped file. pair_info['pedigree_parents'] = make_bool(pair_info.get('pedigree_parents')) # boolean indicating that this pair is expected to be a parent-child # pair according to the ibs0 (< 0.012) calculated from the genotypes. pair_info['predicted_parents'] = make_bool(pair_info.get('predicted_parents')) # boolean indicating that the preceding 2 columns do not match pair_info['parent_error'] = make_bool(pair_info.get('parent_error')) # boolean indicating that rel > 0.75 and ibs0 < 0.012 pair_info['sample_duplication_error'] = make_bool(pair_info.get('sample_duplication_error')) ped_check.append(pair_info) return ped_check
Parse a. ped_check. csv file Args: lines ( iterable ( str )) Returns: sex_check ( list ( dict ))
def parse_peddy_sex_check(lines): """Parse a .ped_check.csv file Args: lines(iterable(str)) Returns: sex_check(list(dict)) """ sex_check = [] header = [] for i,line in enumerate(lines): line = line.rstrip() if i == 0: # Header line header = line.lstrip('#').split(',') else: ind_info = dict(zip(header, line.split(','))) # boolean indicating wether there is a mismatch between X # genotypes and ped sex. ind_info['error'] = make_bool(ind_info.get('error')) # number of homozygous-alternate calls ind_info['hom_alt_count'] = convert_number(ind_info['hom_alt_count']) #number of homozygous-reference calls ind_info['hom_ref_count'] = convert_number(ind_info['hom_ref_count']) # number of heterozygote calls ind_info['het_count'] = convert_number(ind_info['het_count']) # ratio of het_count / hom_alt_count. Low for males, high for females ind_info['het_ratio'] = convert_number(ind_info['het_ratio']) sex_check.append(ind_info) return sex_check
Retrieves a list of HPO terms from scout database
def hpo_terms(store, query = None, limit = None): """Retrieves a list of HPO terms from scout database Args: store (obj): an adapter to the scout database query (str): the term to search in the database limit (str): the number of desired results Returns: hpo_phenotypes (dict): the complete list of HPO objects stored in scout """ hpo_phenotypes = {} if limit: limit=int(limit) hpo_phenotypes['phenotypes'] = list(store.hpo_terms(text=query, limit=limit)) return hpo_phenotypes
Show all objects in the whitelist collection
def whitelist(context): """Show all objects in the whitelist collection""" LOG.info("Running scout view users") adapter = context.obj['adapter'] ## TODO add a User interface to the adapter for whitelist_obj in adapter.whitelist_collection.find(): click.echo(whitelist_obj['_id'])
Build a small phenotype object
def build_phenotype(phenotype_id, adapter): """Build a small phenotype object Build a dictionary with phenotype_id and description Args: phenotype_id (str): The phenotype id adapter (scout.adapter.MongoAdapter) Returns: phenotype_obj (dict): dict( phenotype_id = str, feature = str, # description of phenotype ) """ phenotype_obj = {} phenotype = adapter.hpo_term(phenotype_id) if phenotype: phenotype_obj['phenotype_id'] = phenotype['hpo_id'] phenotype_obj['feature'] = phenotype['description'] return phenotype
Build a case object that is to be inserted to the database
def build_case(case_data, adapter): """Build a case object that is to be inserted to the database Args: case_data (dict): A dictionary with the relevant case information adapter (scout.adapter.MongoAdapter) Returns: case_obj (dict): A case object dict( case_id = str, # required=True, unique display_name = str, # If not display name use case_id owner = str, # required # These are the names of all the collaborators that are allowed to view the # case, including the owner collaborators = list, # List of institute_ids assignee = str, # _id of a user individuals = list, # list of dictionaries with individuals created_at = datetime, updated_at = datetime, suspects = list, # List of variants referred by there _id causatives = list, # List of variants referred by there _id synopsis = str, # The synopsis is a text blob status = str, # default='inactive', choices=STATUS is_research = bool, # default=False research_requested = bool, # default=False rerun_requested = bool, # default=False analysis_date = datetime, analyses = list, # list of dict # default_panels specifies which panels that should be shown when # the case is opened panels = list, # list of dictionaries with panel information dynamic_gene_list = list, # List of genes genome_build = str, # This should be 37 or 38 genome_version = float, # What version of the build rank_model_version = str, rank_score_threshold = int, # default=8 phenotype_terms = list, # List of dictionaries with phenotype information phenotype_groups = list, # List of dictionaries with phenotype information madeline_info = str, # madeline info is a full xml file multiqc = str, # path to dir with multiqc information vcf_files = dict, # A dictionary with vcf files diagnosis_phenotypes = list, # List of references to diseases diagnosis_genes = list, # List of references to genes has_svvariants = bool, # default=False is_migrated = bool # default=False ) """ log.info("build case with id: {0}".format(case_data['case_id'])) case_obj = { '_id': case_data['case_id'], 'display_name': case_data.get('display_name', case_data['case_id']), } # Check if institute exists in database try: institute_id = case_data['owner'] except KeyError as err: raise ConfigError("Case has to have a institute") institute_obj = adapter.institute(institute_id) if not institute_obj: raise IntegrityError("Institute %s not found in database" % institute_id) case_obj['owner'] = case_data['owner'] # Owner allways has to be part of collaborators collaborators = set(case_data.get('collaborators', [])) collaborators.add(case_data['owner']) case_obj['collaborators'] = list(collaborators) if case_data.get('assignee'): case_obj['assignees'] = [case_data['assignee']] # Individuals ind_objs = [] try: for individual in case_data.get('individuals', []): ind_objs.append(build_individual(individual)) except Exception as error: ## TODO add some action here raise error # sort the samples to put the affected individual first sorted_inds = sorted(ind_objs, key=lambda ind: -ind['phenotype']) case_obj['individuals'] = sorted_inds now = datetime.now() case_obj['created_at'] = now case_obj['updated_at'] = now if case_data.get('suspects'): case_obj['suspects'] = case_data['suspects'] if case_data.get('causatives'): case_obj['causatives'] = case_data['causatives'] case_obj['synopsis'] = case_data.get('synopsis', '') case_obj['status'] = 'inactive' case_obj['is_research'] = False case_obj['research_requested'] = False case_obj['rerun_requested'] = False analysis_date = case_data.get('analysis_date') if analysis_date: case_obj['analysis_date'] = analysis_date # We store some metadata and references about gene panels in 'panels' case_panels = case_data.get('gene_panels', []) default_panels = case_data.get('default_panels', []) panels = [] for panel_name in case_panels: panel_obj = adapter.gene_panel(panel_name) if not panel_obj: raise IntegrityError("Panel %s does not exist in database" % panel_name) panel = { 'panel_id': panel_obj['_id'], 'panel_name': panel_obj['panel_name'], 'display_name': panel_obj['display_name'], 'version': panel_obj['version'], 'updated_at': panel_obj['date'], 'nr_genes': len(panel_obj['genes']) } if panel_name in default_panels: panel['is_default'] = True else: panel['is_default'] = False panels.append(panel) case_obj['panels'] = panels case_obj['dynamic_gene_list'] = {} # Meta data genome_build = case_data.get('genome_build', '37') if not genome_build in ['37', '38']: pass ##TODO raise exception if invalid genome build was used case_obj['genome_build'] = genome_build case_obj['genome_version'] = case_data.get('genome_version') if case_data.get('rank_model_version'): case_obj['rank_model_version'] = str(case_data['rank_model_version']) if case_data.get('sv_rank_model_version'): case_obj['sv_rank_model_version'] = str(case_data['sv_rank_model_version']) if case_data.get('rank_score_threshold'): case_obj['rank_score_threshold'] = float(case_data['rank_score_threshold']) # phenotype information phenotypes = [] for phenotype in case_data.get('phenotype_terms', []): phenotype_obj = build_phenotype(phenotype, adapter) if phenotype_obj: phenotypes.append(phenotype_obj) if phenotypes: case_obj['phenotype_terms'] = phenotypes # phenotype groups phenotype_groups = [] for phenotype in case_data.get('phenotype_groups', []): phenotype_obj = build_phenotype(phenotype, adapter) if phenotype_obj: phenotype_groups.append(phenotype_obj) if phenotype_groups: case_obj['phenotype_groups'] = phenotype_groups # Files case_obj['madeline_info'] = case_data.get('madeline_info') if 'multiqc' in case_data: case_obj['multiqc'] = case_data.get('multiqc') case_obj['vcf_files'] = case_data.get('vcf_files', {}) case_obj['delivery_report'] = case_data.get('delivery_report') case_obj['has_svvariants'] = False if (case_obj['vcf_files'].get('vcf_sv') or case_obj['vcf_files'].get('vcf_sv_research')): case_obj['has_svvariants'] = True case_obj['has_strvariants'] = False if (case_obj['vcf_files'].get('vcf_str')): case_obj['has_strvariants'] = True case_obj['is_migrated'] = False case_obj['track'] = case_data.get('track', 'rare') return case_obj
Parse information about a gene.
def gene(store, hgnc_id): """Parse information about a gene.""" res = {'builds': {'37': None, '38': None}, 'symbol': None, 'description': None, 'ensembl_id': None, 'record': None} for build in res['builds']: record = store.hgnc_gene(hgnc_id, build=build) if record: record['position'] = "{this[chromosome]}:{this[start]}-{this[end]}".format(this=record) res['aliases'] = record['aliases'] res['hgnc_id'] = record['hgnc_id'] res['description'] = record['description'] res['builds'][build] = record res['symbol'] = record['hgnc_symbol'] res['description'] = record['description'] res['entrez_id'] = record.get('entrez_id') res['pli_score'] = record.get('pli_score') add_gene_links(record, int(build)) res['omim_id'] = record.get('omim_id') res['incomplete_penetrance'] = record.get('incomplete_penetrance',False) res['inheritance_models'] = record.get('inheritance_models',[]) for transcript in record['transcripts']: transcript['position'] = ("{this[chrom]}:{this[start]}-{this[end]}" .format(this=transcript)) add_tx_links(transcript, build) for phenotype in record.get('phenotypes',[]): phenotype['omim_link'] = omim(phenotype.get('mim_number')) if not res['record']: res['record'] = record # If none of the genes where found if not any(res.values()): raise ValueError return res
Fetch matching genes and convert to JSON.
def genes_to_json(store, query): """Fetch matching genes and convert to JSON.""" gene_query = store.hgnc_genes(query, search=True) json_terms = [{'name': "{} | {} ({})".format(gene['hgnc_id'], gene['hgnc_symbol'], ', '.join(gene['aliases'])), 'id': gene['hgnc_id']} for gene in gene_query] return json_terms
Display the Scout dashboard.
def index(): """Display the Scout dashboard.""" accessible_institutes = current_user.institutes if not 'admin' in current_user.roles: accessible_institutes = current_user.institutes if not accessible_institutes: flash('Not allowed to see information - please visit the dashboard later!') return redirect(url_for('cases.dahboard_general.html')) LOG.debug('User accessible institutes: {}'.format(accessible_institutes)) institutes = [inst for inst in store.institutes(accessible_institutes)] # Insert a entry that displays all institutes in the beginning of the array institutes.insert(0, {'_id': None, 'display_name': 'All institutes'}) institute_id = None slice_query = None panel=1 if request.method=='POST': institute_id = request.form.get('institute') slice_query = request.form.get('query') panel=request.form.get('pane_id') elif request.method=='GET': institute_id = request.args.get('institute') slice_query = request.args.get('query') # User should be restricted to their own institute if: #1) Their default institute when the page is first loaded #2) if they ask for an institute that they don't belong to #3) if they want perform a query on all institutes if not institute_id: institute_id = accessible_institutes[0] elif (not current_user.is_admin) and (slice_query and institute_id == 'None'): institute_id = accessible_institutes[0] elif (not institute_id in accessible_institutes) and not (institute_id == 'None'): institute_id = accessible_institutes[0] LOG.info("Fetch all cases with institute: %s", institute_id) data = get_dashboard_info(store, institute_id, slice_query) data['institutes'] = institutes data['choice'] = institute_id total_cases = data['total_cases'] LOG.info("Found %s cases", total_cases) if total_cases == 0: flash('no cases found for institute {} (with that query) - please visit the dashboard later!'.format(institute_id), 'info') # return redirect(url_for('cases.index')) return render_template( 'dashboard/dashboard_general.html', institute=institute_id, query=slice_query, panel=panel, **data)
Simple tag - returns the weekday of the given ( year month day ) or of given ( weekday_number ).
def weekday(year_or_num, month=None, day=None, full=False): """Simple tag - returns the weekday of the given (year, month, day) or of given (weekday_number). Usage (in template): {% weekday 2014 3 3 %} Result: Mon Return abbreviation by default. To return full name: pass full=True {% weekday 2014 3 3 full=True %} Result: Monday When only number of weekday is given then 0 is considered as "Monday" {% weekday 0 full=True %} Result: Monday """ if any([month, day]) and not all([month, day]): raise TemplateSyntaxError("weekday accepts 1 or 3 arguments plus optional 'full' argument") try: if all([year_or_num, month, day]): weekday_num = date(*map(int, (year_or_num, month, day))).weekday() else: weekday_num = year_or_num if full: return WEEKDAYS[weekday_num] else: return WEEKDAYS_ABBR[weekday_num] except Exception: return
Return a requests response from url Args: url ( str ) Returns: decoded_data ( str ): Decoded response
def get_request(url): """Return a requests response from url Args: url(str) Returns: decoded_data(str): Decoded response """ try: LOG.info("Requesting %s", url) response = urllib.request.urlopen(url) if url.endswith('.gz'): LOG.info("Decompress zipped file") data = gzip.decompress(response.read()) # a `bytes` object else: data = response.read() # a `bytes` object decoded_data = data.decode('utf-8') except HTTPError as err: LOG.warning("Something went wrong, perhaps the api key is not valid?") raise err except URLError as err: LOG.warning("Something went wrong, are you connected to internet?") raise err if 'Error' in decoded_data: raise URLError("Seems like url {} does not exist".format(url)) return decoded_data
Fetch a resource and return the resulting lines in a list Send file_name to get more clean log messages Args: url ( str ) Returns: lines ( list ( str ))
def fetch_resource(url): """Fetch a resource and return the resulting lines in a list Send file_name to get more clean log messages Args: url(str) Returns: lines(list(str)) """ try: data = get_request(url) lines = data.split('\n') except Exception as err: raise err return lines
Fetch the necessary mim files using a api key Args: api_key ( str ): A api key necessary to fetch mim data Returns: mim_files ( dict ): A dictionary with the neccesary files
def fetch_mim_files(api_key, mim2genes=False, mimtitles=False, morbidmap=False, genemap2=False): """Fetch the necessary mim files using a api key Args: api_key(str): A api key necessary to fetch mim data Returns: mim_files(dict): A dictionary with the neccesary files """ LOG.info("Fetching OMIM files from https://omim.org/") mim2genes_url = 'https://omim.org/static/omim/data/mim2gene.txt' mimtitles_url= 'https://data.omim.org/downloads/{0}/mimTitles.txt'.format(api_key) morbidmap_url = 'https://data.omim.org/downloads/{0}/morbidmap.txt'.format(api_key) genemap2_url = 'https://data.omim.org/downloads/{0}/genemap2.txt'.format(api_key) mim_files = {} mim_urls = {} if mim2genes is True: mim_urls['mim2genes'] = mim2genes_url if mimtitles is True: mim_urls['mimtitles'] = mimtitles_url if morbidmap is True: mim_urls['morbidmap'] = morbidmap_url if genemap2 is True: mim_urls['genemap2'] = genemap2_url for file_name in mim_urls: url = mim_urls[file_name] mim_files[file_name] = fetch_resource(url) return mim_files
Fetch the ensembl genes Args: build ( str ): [ 37 38 ]
def fetch_ensembl_genes(build='37'): """Fetch the ensembl genes Args: build(str): ['37', '38'] """ if build == '37': url = 'http://grch37.ensembl.org' else: url = 'http://www.ensembl.org' LOG.info("Fetching ensembl genes from %s", url) dataset_name = 'hsapiens_gene_ensembl' dataset = pybiomart.Dataset(name=dataset_name, host=url) attributes = [ 'chromosome_name', 'start_position', 'end_position', 'ensembl_gene_id', 'hgnc_symbol', 'hgnc_id', ] filters = { 'chromosome_name': CHROMOSOMES, } result = dataset.query( attributes = attributes, filters = filters, use_attr_names=True, ) return result
Fetch the ensembl genes Args: build ( str ): [ 37 38 ]
def fetch_ensembl_exons(build='37'): """Fetch the ensembl genes Args: build(str): ['37', '38'] """ LOG.info("Fetching ensembl exons build %s ...", build) if build == '37': url = 'http://grch37.ensembl.org' else: url = 'http://www.ensembl.org' dataset_name = 'hsapiens_gene_ensembl' dataset = pybiomart.Dataset(name=dataset_name, host=url) attributes = [ 'chromosome_name', 'ensembl_gene_id', 'ensembl_transcript_id', 'ensembl_exon_id', 'exon_chrom_start', 'exon_chrom_end', '5_utr_start', '5_utr_end', '3_utr_start', '3_utr_end', 'strand', 'rank' ] filters = { 'chromosome_name': CHROMOSOMES, } result = dataset.query( attributes = attributes, filters = filters ) return result
Fetch the hgnc genes file from ftp:// ftp. ebi. ac. uk/ pub/ databases/ genenames/ new/ tsv/ hgnc_complete_set. txt Returns: hgnc_gene_lines ( list ( str ))
def fetch_hgnc(): """Fetch the hgnc genes file from ftp://ftp.ebi.ac.uk/pub/databases/genenames/new/tsv/hgnc_complete_set.txt Returns: hgnc_gene_lines(list(str)) """ file_name = "hgnc_complete_set.txt" url = 'ftp://ftp.ebi.ac.uk/pub/databases/genenames/new/tsv/{0}'.format(file_name) LOG.info("Fetching HGNC genes") hgnc_lines = fetch_resource(url) return hgnc_lines
Fetch the file with exac constraint scores Returns: exac_lines ( iterable ( str ))
def fetch_exac_constraint(): """Fetch the file with exac constraint scores Returns: exac_lines(iterable(str)) """ file_name = 'fordist_cleaned_exac_r03_march16_z_pli_rec_null_data.txt' url = ('ftp://ftp.broadinstitute.org/pub/ExAC_release/release0.3/functional_gene_constraint' '/{0}').format(file_name) LOG.info("Fetching ExAC genes") try: exac_lines = fetch_resource(url) except URLError as err: LOG.info("Failed to fetch exac constraint scores file from ftp server") LOG.info("Try to fetch from google bucket...") url = ("https://storage.googleapis.com/gnomad-public/legacy/exacv1_downloads/release0.3.1" "/manuscript_data/forweb_cleaned_exac_r03_march16_z_data_pLI.txt.gz") exac_lines = fetch_resource(url) return exac_lines
Fetch the necessary mim files using a api key Args: api_key ( str ): A api key necessary to fetch mim data Returns: mim_files ( dict ): A dictionary with the neccesary files
def fetch_hpo_files(hpogenes=False, hpoterms=False, phenotype_to_terms=False, hpodisease=False): """Fetch the necessary mim files using a api key Args: api_key(str): A api key necessary to fetch mim data Returns: mim_files(dict): A dictionary with the neccesary files """ LOG.info("Fetching HPO information from http://compbio.charite.de") base_url = ('http://compbio.charite.de/jenkins/job/hpo.annotations.monthly/' 'lastStableBuild/artifact/annotation/{}') hpogenes_url = base_url.format('ALL_SOURCES_ALL_FREQUENCIES_genes_to_phenotype.txt') hpoterms_url= base_url.format('ALL_SOURCES_ALL_FREQUENCIES_phenotype_to_genes.txt') hpo_phenotype_to_terms_url = base_url.format('ALL_SOURCES_ALL_FREQUENCIES_diseases_to_genes_to_phenotypes.txt') hpodisease_url = base_url.format('diseases_to_genes.txt') hpo_files = {} hpo_urls = {} if hpogenes is True: hpo_urls['hpogenes'] = hpogenes_url if hpoterms is True: hpo_urls['hpoterms'] = hpoterms_url if phenotype_to_terms is True: hpo_urls['phenotype_to_terms'] = hpo_phenotype_to_terms_url if hpodisease is True: hpo_urls['hpodisease'] = hpodisease_url for file_name in hpo_urls: url = hpo_urls[file_name] hpo_files[file_name] = request_file(url) return hpo_files
Show all transcripts in the database
def transcripts(context, build, hgnc_id, json): """Show all transcripts in the database""" LOG.info("Running scout view transcripts") adapter = context.obj['adapter'] if not json: click.echo("Chromosome\tstart\tend\ttranscript_id\thgnc_id\trefseq\tis_primary") for tx_obj in adapter.transcripts(build=build, hgnc_id=hgnc_id): if json: pp(tx_obj) continue click.echo("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}".format( tx_obj['chrom'], tx_obj['start'], tx_obj['end'], tx_obj['ensembl_transcript_id'], tx_obj['hgnc_id'], tx_obj.get('refseq_id', ''), tx_obj.get('is_primary') or '', ))
Adds an occurrence key to the event object w/ a list of occurrences and adds a popover ( for use with twitter bootstrap ). The occurrence is added so that each event can be aware of what day ( s ) it occurs in the month.
def add_occurrences(events, count): """ Adds an occurrence key to the event object w/ a list of occurrences and adds a popover (for use with twitter bootstrap). The occurrence is added so that each event can be aware of what day(s) it occurs in the month. """ for day in count: for item in count[day]: for event in events: if event.pk == item[1]: try: event.occurrence.append(day) except AttributeError: event.occurrence = [] event.occurrence.append(day)
A function that returns an html calendar for the given month in the given year with the number of events for that month shown on the generated calendar. Start_day is the day the calendar should start on ( default is Monday ).
def month_display(year, month, all_month_events, start_day, net, qs, mini=False, request=None, context=None): """ A function that returns an html calendar for the given month in the given year, with the number of events for that month shown on the generated calendar. Start_day is the day the calendar should start on (default is Monday). """ # count the number of times events happen on a given day count = CountHandler(year, month, all_month_events).get_count() # sort count by start date using all_month_events (which is already sorted) for event in all_month_events[::-1]: for l in count.values(): for item in l: if item[1] == event.pk: l.insert(0, l.pop(l.index(item))) args = (year, month, count, all_month_events, start_day) if not mini: html_cal = EventCalendar(request=request, context=context, *args).formatmonth(year, month, net=net, qs=qs) else: html_cal = MiniEventCalendar(request=request, context=context, *args).formatmonth(year, month, net=net, qs=qs) nxt, prev = get_next_and_prev(net) extra_qs = ('&' + '&'.join(qs)) if qs else '' # inject next/prev querystring urls and make them aware of any querystrings # already present in the url html_cal = html_cal.replace( 'class="month">\n<tr>', 'class="month">\n<tr><th colspan="1" class="month-arrow-left">\ <a href="?cal_prev=%d%s">&larr;</a></th>' % (prev, extra_qs) ).replace( '%d</th>' % year, '%d</th><th colspan="1" class="month-arrow-right">\ <a href="?cal_next=%d%s">&rarr;</a></th>' % (year, nxt, extra_qs) ) add_occurrences(all_month_events, count) return html_cal
Returns the events that occur on the given day. Works by getting all occurrences for the month then drilling down to only those occurring on the given day.
def day_display(year, month, all_month_events, day): """ Returns the events that occur on the given day. Works by getting all occurrences for the month, then drilling down to only those occurring on the given day. """ # Get a dict with all of the events for the month count = CountHandler(year, month, all_month_events).get_count() pks = [x[1] for x in count[day]] # list of pks for events on given day # List enables sorting. # See the comments in EventMonthView in views.py for more info day_events = list(Event.objects.filter(pk__in=pks).order_by( 'start_date').prefetch_related('cancellations')) day_events.sort(key=lambda x: x.l_start_date.hour) return day_events
Pre - process list of variants.
def variants(store, institute_obj, case_obj, variants_query, page=1, per_page=50): """Pre-process list of variants.""" variant_count = variants_query.count() skip_count = per_page * max(page - 1, 0) more_variants = True if variant_count > (skip_count + per_page) else False variant_res = variants_query.skip(skip_count).limit(per_page) genome_build = case_obj.get('genome_build', '37') if genome_build not in ['37','38']: genome_build = '37' variants = [] for variant_obj in variant_res: overlapping_svs = [sv for sv in store.overlapping(variant_obj)] variant_obj['overlapping'] = overlapping_svs or None variants.append(parse_variant(store, institute_obj, case_obj, variant_obj, update=True, genome_build=genome_build)) return { 'variants': variants, 'more_variants': more_variants, }
Pre - process list of SV variants.
def sv_variants(store, institute_obj, case_obj, variants_query, page=1, per_page=50): """Pre-process list of SV variants.""" skip_count = (per_page * max(page - 1, 0)) more_variants = True if variants_query.count() > (skip_count + per_page) else False genome_build = case_obj.get('genome_build', '37') if genome_build not in ['37','38']: genome_build = '37' return { 'variants': (parse_variant(store, institute_obj, case_obj, variant, genome_build=genome_build) for variant in variants_query.skip(skip_count).limit(per_page)), 'more_variants': more_variants, }
Pre - process list of STR variants.
def str_variants(store, institute_obj, case_obj, variants_query, page=1, per_page=50): """Pre-process list of STR variants.""" # Nothing unique to STRs on this level. Inheritance? return variants(store, institute_obj, case_obj, variants_query, page, per_page)
Pre - process an STR variant entry for detail page.
def str_variant(store, institute_id, case_name, variant_id): """Pre-process an STR variant entry for detail page. Adds information to display variant Args: store(scout.adapter.MongoAdapter) institute_id(str) case_name(str) variant_id(str) Returns: detailed_information(dict): { 'institute': <institute_obj>, 'case': <case_obj>, 'variant': <variant_obj>, 'overlapping_snvs': <overlapping_snvs>, 'manual_rank_options': MANUAL_RANK_OPTIONS, 'dismiss_variant_options': DISMISS_VARIANT_OPTIONS } """ institute_obj, case_obj = institute_and_case(store, institute_id, case_name) variant_obj = store.variant(variant_id) # fill in information for pilup view variant_case(store, case_obj, variant_obj) variant_obj['callers'] = callers(variant_obj, category='str') # variant_obj['str_ru'] # variant_obj['str_repid'] # variant_obj['str_ref'] variant_obj['comments'] = store.events(institute_obj, case=case_obj, variant_id=variant_obj['variant_id'], comments=True) return { 'institute': institute_obj, 'case': case_obj, 'variant': variant_obj, 'overlapping_snvs': overlapping_snvs, 'manual_rank_options': MANUAL_RANK_OPTIONS, 'dismiss_variant_options': DISMISS_VARIANT_OPTIONS }
Pre - process an SV variant entry for detail page.
def sv_variant(store, institute_id, case_name, variant_id=None, variant_obj=None, add_case=True, get_overlapping=True): """Pre-process an SV variant entry for detail page. Adds information to display variant Args: store(scout.adapter.MongoAdapter) institute_id(str) case_name(str) variant_id(str) variant_obj(dcit) add_case(bool): If information about case files should be added Returns: detailed_information(dict): { 'institute': <institute_obj>, 'case': <case_obj>, 'variant': <variant_obj>, 'overlapping_snvs': <overlapping_snvs>, 'manual_rank_options': MANUAL_RANK_OPTIONS, 'dismiss_variant_options': DISMISS_VARIANT_OPTIONS } """ institute_obj, case_obj = institute_and_case(store, institute_id, case_name) if not variant_obj: variant_obj = store.variant(variant_id) if add_case: # fill in information for pilup view variant_case(store, case_obj, variant_obj) # frequencies variant_obj['frequencies'] = [ ('1000G', variant_obj.get('thousand_genomes_frequency')), ('1000G (left)', variant_obj.get('thousand_genomes_frequency_left')), ('1000G (right)', variant_obj.get('thousand_genomes_frequency_right')), ('ClinGen CGH (benign)', variant_obj.get('clingen_cgh_benign')), ('ClinGen CGH (pathogenic)', variant_obj.get('clingen_cgh_pathogenic')), ('ClinGen NGI', variant_obj.get('clingen_ngi')), ('SweGen', variant_obj.get('swegen')), ('Decipher', variant_obj.get('decipher')), ] variant_obj['callers'] = callers(variant_obj, category='sv') overlapping_snvs = [] if get_overlapping: overlapping_snvs = (parse_variant(store, institute_obj, case_obj, variant) for variant in store.overlapping(variant_obj)) # parse_gene function is not called for SVs, but a link to ensembl gene is required for gene_obj in variant_obj['genes']: if gene_obj.get('common'): ensembl_id = gene_obj['common']['ensembl_id'] try: build = int(gene_obj['common'].get('build','37')) except Exception: build = 37 gene_obj['ensembl_link'] = ensembl(ensembl_id, build=build) variant_obj['comments'] = store.events(institute_obj, case=case_obj, variant_id=variant_obj['variant_id'], comments=True) case_clinvars = store.case_to_clinVars(case_obj.get('display_name')) if variant_id in case_clinvars: variant_obj['clinvar_clinsig'] = case_clinvars.get(variant_id)['clinsig'] if not 'end_chrom' in variant_obj: variant_obj['end_chrom'] = variant_obj['chromosome'] return { 'institute': institute_obj, 'case': case_obj, 'variant': variant_obj, 'overlapping_snvs': overlapping_snvs, 'manual_rank_options': MANUAL_RANK_OPTIONS, 'dismiss_variant_options': DISMISS_VARIANT_OPTIONS }
Parse information about variants.
def parse_variant(store, institute_obj, case_obj, variant_obj, update=False, genome_build='37', get_compounds = True): """Parse information about variants. - Adds information about compounds - Updates the information about compounds if necessary and 'update=True' Args: store(scout.adapter.MongoAdapter) institute_obj(scout.models.Institute) case_obj(scout.models.Case) variant_obj(scout.models.Variant) update(bool): If variant should be updated in database genome_build(str) """ has_changed = False compounds = variant_obj.get('compounds', []) if compounds and get_compounds: # Check if we need to add compound information # If it is the first time the case is viewed we fill in some compound information if 'not_loaded' not in compounds[0]: new_compounds = store.update_variant_compounds(variant_obj) variant_obj['compounds'] = new_compounds has_changed = True # sort compounds on combined rank score variant_obj['compounds'] = sorted(variant_obj['compounds'], key=lambda compound: -compound['combined_score']) # Update the hgnc symbols if they are incorrect variant_genes = variant_obj.get('genes') if variant_genes is not None: for gene_obj in variant_genes: # If there is no hgnc id there is nothin we can do if not gene_obj['hgnc_id']: continue # Else we collect the gene object and check the id if gene_obj.get('hgnc_symbol') is None: hgnc_gene = store.hgnc_gene(gene_obj['hgnc_id'], build=genome_build) if not hgnc_gene: continue has_changed = True gene_obj['hgnc_symbol'] = hgnc_gene['hgnc_symbol'] # We update the variant if some information was missing from loading # Or if symbold in reference genes have changed if update and has_changed: variant_obj = store.update_variant(variant_obj) variant_obj['comments'] = store.events(institute_obj, case=case_obj, variant_id=variant_obj['variant_id'], comments=True) if variant_genes: variant_obj.update(get_predictions(variant_genes)) if variant_obj.get('category') == 'cancer': variant_obj.update(get_variant_info(variant_genes)) for compound_obj in compounds: compound_obj.update(get_predictions(compound_obj.get('genes', []))) if isinstance(variant_obj.get('acmg_classification'), int): acmg_code = ACMG_MAP[variant_obj['acmg_classification']] variant_obj['acmg_classification'] = ACMG_COMPLETE_MAP[acmg_code] # convert length for SV variants variant_length = variant_obj.get('length') variant_obj['length'] = {100000000000: 'inf', -1: 'n.d.'}.get(variant_length, variant_length) if not 'end_chrom' in variant_obj: variant_obj['end_chrom'] = variant_obj['chromosome'] return variant_obj
Get variants info to be exported to file one list ( line ) per variant.
def variant_export_lines(store, case_obj, variants_query): """Get variants info to be exported to file, one list (line) per variant. Args: store(scout.adapter.MongoAdapter) case_obj(scout.models.Case) variants_query: a list of variant objects, each one is a dictionary Returns: export_variants: a list of strings. Each string of the list corresponding to the fields of a variant to be exported to file, separated by comma """ export_variants = [] for variant in variants_query: variant_line = [] position = variant['position'] change = variant['reference']+'>'+variant['alternative'] variant_line.append(variant['rank_score']) variant_line.append(variant['chromosome']) variant_line.append(position) variant_line.append(change) variant_line.append('_'.join([str(position), change])) # gather gene info: gene_list = variant.get('genes') #this is a list of gene objects gene_ids = [] gene_names = [] hgvs_c = [] # if variant is in genes if len(gene_list) > 0: for gene_obj in gene_list: hgnc_id = gene_obj['hgnc_id'] gene_name = gene(store, hgnc_id)['symbol'] gene_ids.append(hgnc_id) gene_names.append(gene_name) hgvs_nucleotide = '-' # gather HGVS info from gene transcripts transcripts_list = gene_obj.get('transcripts') for transcript_obj in transcripts_list: if transcript_obj.get('is_canonical') and transcript_obj.get('is_canonical') is True: hgvs_nucleotide = str(transcript_obj.get('coding_sequence_name')) hgvs_c.append(hgvs_nucleotide) variant_line.append(';'.join( str(x) for x in gene_ids)) variant_line.append(';'.join( str(x) for x in gene_names)) variant_line.append(';'.join( str(x) for x in hgvs_c)) else: while i < 4: variant_line.append('-') # instead of gene ids i = i+1 variant_gts = variant['samples'] # list of coverage and gt calls for case samples for individual in case_obj['individuals']: for variant_gt in variant_gts: if individual['individual_id'] == variant_gt['sample_id']: # gather coverage info variant_line.append(variant_gt['allele_depths'][0]) # AD reference variant_line.append(variant_gt['allele_depths'][1]) # AD alternate # gather genotype quality info variant_line.append(variant_gt['genotype_quality']) variant_line = [str(i) for i in variant_line] export_variants.append(",".join(variant_line)) return export_variants
Returns a header for the CSV file with the filtered variants to be exported.
def variants_export_header(case_obj): """Returns a header for the CSV file with the filtered variants to be exported. Args: case_obj(scout.models.Case) Returns: header: includes the fields defined in scout.constants.variants_export EXPORT_HEADER + AD_reference, AD_alternate, GT_quality for each sample analysed for a case """ header = [] header = header + EXPORT_HEADER # Add fields specific for case samples for individual in case_obj['individuals']: display_name = str(individual['display_name']) header.append('AD_reference_'+display_name) # Add AD reference field for a sample header.append('AD_alternate_'+display_name) # Add AD alternate field for a sample header.append('GT_quality_'+display_name) # Add Genotype quality field for a sample return header
Get variant information
def get_variant_info(genes): """Get variant information""" data = {'canonical_transcripts': []} for gene_obj in genes: if not gene_obj.get('canonical_transcripts'): tx = gene_obj['transcripts'][0] tx_id = tx['transcript_id'] exon = tx.get('exon', '-') c_seq = tx.get('coding_sequence_name', '-') else: tx_id = gene_obj['canonical_transcripts'] exon = gene_obj.get('exon', '-') c_seq = gene_obj.get('hgvs_identifier', '-') if len(c_seq) > 20: c_seq = c_seq[:20] + '...' if len(genes) == 1: value = ':'.join([tx_id,exon,c_seq]) else: gene_id = gene_obj.get('hgnc_symbol') or str(gene_obj['hgnc_id']) value = ':'.join([gene_id, tx_id,exon,c_seq]) data['canonical_transcripts'].append(value) return data
Get sift predictions from genes.
def get_predictions(genes): """Get sift predictions from genes.""" data = { 'sift_predictions': [], 'polyphen_predictions': [], 'region_annotations': [], 'functional_annotations': [] } for gene_obj in genes: for pred_key in data: gene_key = pred_key[:-1] if len(genes) == 1: value = gene_obj.get(gene_key, '-') else: gene_id = gene_obj.get('hgnc_symbol') or str(gene_obj['hgnc_id']) value = ':'.join([gene_id, gene_obj.get(gene_key, '-')]) data[pred_key].append(value) return data
Pre - process case for the variant view.
def variant_case(store, case_obj, variant_obj): """Pre-process case for the variant view. Adds information about files from case obj to variant Args: store(scout.adapter.MongoAdapter) case_obj(scout.models.Case) variant_obj(scout.models.Variant) """ case_obj['bam_files'] = [] case_obj['mt_bams'] = [] case_obj['bai_files'] = [] case_obj['mt_bais'] = [] case_obj['sample_names'] = [] for individual in case_obj['individuals']: bam_path = individual.get('bam_file') mt_bam = individual.get('mt_bam') case_obj['sample_names'].append(individual.get('display_name')) if bam_path and os.path.exists(bam_path): case_obj['bam_files'].append(individual['bam_file']) case_obj['bai_files'].append(find_bai_file(individual['bam_file'])) if mt_bam and os.path.exists(mt_bam): case_obj['mt_bams'].append(individual['mt_bam']) case_obj['mt_bais'].append(find_bai_file(individual['mt_bam'])) else: LOG.debug("%s: no bam file found", individual['individual_id']) try: genes = variant_obj.get('genes', []) if len(genes) == 1: hgnc_gene_obj = store.hgnc_gene(variant_obj['genes'][0]['hgnc_id']) if hgnc_gene_obj: vcf_path = store.get_region_vcf(case_obj, gene_obj=hgnc_gene_obj) case_obj['region_vcf_file'] = vcf_path else: case_obj['region_vcf_file'] = None elif len(genes) > 1: chrom = variant_obj['genes'][0]['common']['chromosome'] start = min(gene['common']['start'] for gene in variant_obj['genes']) end = max(gene['common']['end'] for gene in variant_obj['genes']) # Create a reduced VCF with variants in the region vcf_path = store.get_region_vcf(case_obj, chrom=chrom, start=start, end=end) case_obj['region_vcf_file'] = vcf_path except (SyntaxError, Exception): LOG.warning("skip VCF region for alignment view")
Find out BAI file by extension given the BAM file.
def find_bai_file(bam_file): """Find out BAI file by extension given the BAM file.""" bai_file = bam_file.replace('.bam', '.bai') if not os.path.exists(bai_file): # try the other convention bai_file = "{}.bai".format(bam_file) return bai_file
Pre - process a single variant for the detailed variant view.
def variant(store, institute_obj, case_obj, variant_id=None, variant_obj=None, add_case=True, add_other=True, get_overlapping=True): """Pre-process a single variant for the detailed variant view. Adds information from case and institute that is not present on the variant object Args: store(scout.adapter.MongoAdapter) institute_obj(scout.models.Institute) case_obj(scout.models.Case) variant_id(str) variant_obj(dict) add_case(bool): If info about files case should be added add_other(bool): If information about other causatives should be added get_overlapping(bool): If overlapping svs should be collected Returns: variant_info(dict): { 'variant': <variant_obj>, 'causatives': <list(other_causatives)>, 'events': <list(events)>, 'overlapping_svs': <list(overlapping svs)>, 'manual_rank_options': MANUAL_RANK_OPTIONS, 'dismiss_variant_options': DISMISS_VARIANT_OPTIONS, 'ACMG_OPTIONS': ACMG_OPTIONS, 'evaluations': <list(evaluations)>, } """ # If the variant is already collected we skip this part if not variant_obj: default_panels = [] # Add default panel information to variant for panel in case_obj['panels']: if not panel.get('is_default'): continue panel_obj = store.gene_panel(panel['panel_name'], panel.get('version')) if not panel: LOG.warning("Panel {0} version {1} could not be found".format( panel['panel_name'], panel.get('version'))) continue default_panels.append(panel_obj) # NOTE this will query with variant_id == document_id, not the variant_id. variant_obj = store.variant(variant_id, gene_panels=default_panels) genome_build = case_obj.get('genome_build', '37') if genome_build not in ['37','38']: genome_build = '37' if variant_obj is None: return None # Add information to case_obj if add_case: variant_case(store, case_obj, variant_obj) # Collect all the events for the variant events = list(store.events(institute_obj, case=case_obj, variant_id=variant_obj['variant_id'])) for event in events: event['verb'] = VERBS_MAP[event['verb']] other_causatives = [] # Adds information about other causative variants if add_other: for other_variant in store.other_causatives(case_obj, variant_obj): # This should work with old and new ids case_id = other_variant['case_id'] other_case = store.case(case_id) if not other_case: continue other_variant['case_display_name'] = other_case.get('display_name', case_id) other_causatives.append(other_variant) variant_obj = parse_variant(store, institute_obj, case_obj, variant_obj, genome_build=genome_build) variant_obj['end_position'] = end_position(variant_obj) variant_obj['frequency'] = frequency(variant_obj) variant_obj['clinsig_human'] = (clinsig_human(variant_obj) if variant_obj.get('clnsig') else None) variant_obj['thousandg_link'] = thousandg_link(variant_obj, genome_build) variant_obj['exac_link'] = exac_link(variant_obj) variant_obj['gnomad_link'] = gnomad_link(variant_obj) variant_obj['swegen_link'] = swegen_link(variant_obj) variant_obj['cosmic_link'] = cosmic_link(variant_obj) variant_obj['beacon_link'] = beacon_link(variant_obj, genome_build) variant_obj['ucsc_link'] = ucsc_link(variant_obj, genome_build) variant_obj['alamut_link'] = alamut_link(variant_obj) variant_obj['spidex_human'] = spidex_human(variant_obj) variant_obj['expected_inheritance'] = expected_inheritance(variant_obj) variant_obj['callers'] = callers(variant_obj, category='snv') individuals = {individual['individual_id']: individual for individual in case_obj['individuals']} for sample_obj in variant_obj['samples']: individual = individuals[sample_obj.get('sample_id')] if not individual: return None sample_obj['is_affected'] = True if individual['phenotype'] == 2 else False gene_models = set() variant_obj['disease_associated_transcripts'] = [] # Parse the gene models, both from panels and genes for gene_obj in variant_obj.get('genes', []): # Adds gene level links parse_gene(gene_obj, genome_build) omim_models = set() for disease_term in gene_obj.get('disease_terms', []): omim_models.update(disease_term.get('inheritance', [])) gene_obj['omim_inheritance'] = list(omim_models) # Build strings for the disease associated transcripts from gene panel for refseq_id in gene_obj.get('disease_associated_transcripts', []): hgnc_symbol = (gene_obj['common']['hgnc_symbol'] if gene_obj.get('common') else gene_obj['hgnc_id']) transcript_str = "{}:{}".format(hgnc_symbol, refseq_id) variant_obj['disease_associated_transcripts'].append(transcript_str) gene_models = gene_models | omim_models if variant_obj.get('genetic_models'): variant_models = set(model.split('_', 1)[0] for model in variant_obj['genetic_models']) variant_obj['is_matching_inheritance'] = variant_models & gene_models evaluations = [] for evaluation_obj in store.get_evaluations(variant_obj): evaluation(store, evaluation_obj) evaluations.append(evaluation_obj) case_clinvars = store.case_to_clinVars(case_obj.get('display_name')) if variant_id in case_clinvars: variant_obj['clinvar_clinsig'] = case_clinvars.get(variant_id)['clinsig'] svs = [] if get_overlapping: svs = (parse_variant(store, institute_obj, case_obj, variant_obj) for variant_obj in store.overlapping(variant_obj)) return { 'variant': variant_obj, 'causatives': other_causatives, 'events': events, 'overlapping_svs': svs, 'manual_rank_options': MANUAL_RANK_OPTIONS, 'dismiss_variant_options': DISMISS_VARIANT_OPTIONS, 'mosaic_variant_options': MOSAICISM_OPTIONS, 'ACMG_OPTIONS': ACMG_OPTIONS, 'evaluations': evaluations, }
Query observations for a variant.
def observations(store, loqusdb, case_obj, variant_obj): """Query observations for a variant.""" composite_id = ("{this[chromosome]}_{this[position]}_{this[reference]}_" "{this[alternative]}".format(this=variant_obj)) obs_data = loqusdb.get_variant({'_id': composite_id}) or {} obs_data['total'] = loqusdb.case_count() obs_data['cases'] = [] institute_id = variant_obj['institute'] for case_id in obs_data.get('families', []): if case_id != variant_obj['case_id'] and case_id.startswith(institute_id): other_variant = store.variant(variant_obj['variant_id'], case_id=case_id) other_case = store.case(case_id) obs_data['cases'].append(dict(case=other_case, variant=other_variant)) return obs_data
Parse variant genes.
def parse_gene(gene_obj, build=None): """Parse variant genes.""" build = build or 37 if gene_obj.get('common'): add_gene_links(gene_obj, build) refseq_transcripts = [] for tx_obj in gene_obj['transcripts']: parse_transcript(gene_obj, tx_obj, build) # select refseq transcripts as "primary" if not tx_obj.get('refseq_id'): continue refseq_transcripts.append(tx_obj) gene_obj['primary_transcripts'] = (refseq_transcripts if refseq_transcripts else [])
Parse variant gene transcript ( VEP ).
def parse_transcript(gene_obj, tx_obj, build=None): """Parse variant gene transcript (VEP).""" build = build or 37 add_tx_links(tx_obj, build) if tx_obj.get('refseq_id'): gene_name = (gene_obj['common']['hgnc_symbol'] if gene_obj['common'] else gene_obj['hgnc_id']) tx_obj['change_str'] = transcript_str(tx_obj, gene_name)
Generate amino acid change as a string.
def transcript_str(transcript_obj, gene_name=None): """Generate amino acid change as a string.""" if transcript_obj.get('exon'): gene_part, part_count_raw = 'exon', transcript_obj['exon'] elif transcript_obj.get('intron'): gene_part, part_count_raw = 'intron', transcript_obj['intron'] else: # variant between genes gene_part, part_count_raw = 'intergenic', '0' part_count = part_count_raw.rpartition('/')[0] change_str = "{}:{}{}:{}:{}".format( transcript_obj.get('refseq_id', ''), gene_part, part_count, transcript_obj.get('coding_sequence_name', 'NA'), transcript_obj.get('protein_sequence_name', 'NA'), ) if gene_name: change_str = "{}:".format(gene_name) + change_str return change_str
Calculate end position for a variant.
def end_position(variant_obj): """Calculate end position for a variant.""" alt_bases = len(variant_obj['alternative']) num_bases = max(len(variant_obj['reference']), alt_bases) return variant_obj['position'] + (num_bases - 1)
Returns a judgement on the overall frequency of the variant.
def frequency(variant_obj): """Returns a judgement on the overall frequency of the variant. Combines multiple metrics into a single call. """ most_common_frequency = max(variant_obj.get('thousand_genomes_frequency') or 0, variant_obj.get('exac_frequency') or 0) if most_common_frequency > .05: return 'common' elif most_common_frequency > .01: return 'uncommon' else: return 'rare'
Convert to human readable version of CLINSIG evaluation.
def clinsig_human(variant_obj): """Convert to human readable version of CLINSIG evaluation.""" for clinsig_obj in variant_obj['clnsig']: # The clinsig objects allways have a accession if isinstance(clinsig_obj['accession'], int): # New version link = "https://www.ncbi.nlm.nih.gov/clinvar/variation/{}" else: # Old version link = "https://www.ncbi.nlm.nih.gov/clinvar/{}" human_str = 'not provided' if clinsig_obj.get('value'): try: # Old version int(clinsig_obj['value']) human_str = CLINSIG_MAP.get(clinsig_obj['value'], 'not provided') except ValueError: # New version human_str = clinsig_obj['value'] clinsig_obj['human'] = human_str clinsig_obj['link'] = link.format(clinsig_obj['accession']) yield clinsig_obj
Compose link to 1000G page for detailed information.
def thousandg_link(variant_obj, build=None): """Compose link to 1000G page for detailed information.""" dbsnp_id = variant_obj.get('dbsnp_id') build = build or 37 if not dbsnp_id: return None if build == 37: url_template = ("http://grch37.ensembl.org/Homo_sapiens/Variation/Explore" "?v={};vdb=variation") else: url_template = ("http://www.ensembl.org/Homo_sapiens/Variation/Explore" "?v={};vdb=variation") return url_template.format(dbsnp_id)
Compose link to COSMIC Database.
def cosmic_link(variant_obj): """Compose link to COSMIC Database. Args: variant_obj(scout.models.Variant) Returns: url_template(str): Link to COSMIIC database if cosmic id is present """ cosmic_ids = variant_obj.get('cosmic_ids') if not cosmic_ids: return None else: cosmic_id = cosmic_ids[0] url_template = ("https://cancer.sanger.ac.uk/cosmic/mutation/overview?id={}") return url_template.format(cosmic_id)
Compose link to Beacon Network.
def beacon_link(variant_obj, build=None): """Compose link to Beacon Network.""" build = build or 37 url_template = ("https://beacon-network.org/#/search?pos={this[position]}&" "chrom={this[chromosome]}&allele={this[alternative]}&" "ref={this[reference]}&rs=GRCh37") # beacon does not support build 38 at the moment # if build == '38': # url_template = ("https://beacon-network.org/#/search?pos={this[position]}&" # "chrom={this[chromosome]}&allele={this[alternative]}&" # "ref={this[reference]}&rs=GRCh38") return url_template.format(this=variant_obj)
Compose link to UCSC.
def ucsc_link(variant_obj, build=None): """Compose link to UCSC.""" build = build or 37 url_template = ("http://genome.ucsc.edu/cgi-bin/hgTracks?db=hg19&" "position=chr{this[chromosome]}:{this[position]}" "-{this[position]}&dgv=pack&knownGene=pack&omimGene=pack") if build == 38: url_template = ("http://genome.ucsc.edu/cgi-bin/hgTracks?db=hg20&" "position=chr{this[chromosome]}:{this[position]}" "-{this[position]}&dgv=pack&knownGene=pack&omimGene=pack") return url_template.format(this=variant_obj)
Translate SPIDEX annotation to human readable string.
def spidex_human(variant_obj): """Translate SPIDEX annotation to human readable string.""" if variant_obj.get('spidex') is None: return 'not_reported' elif abs(variant_obj['spidex']) < SPIDEX_HUMAN['low']['pos'][1]: return 'low' elif abs(variant_obj['spidex']) < SPIDEX_HUMAN['medium']['pos'][1]: return 'medium' else: return 'high'
Gather information from common gene information.
def expected_inheritance(variant_obj): """Gather information from common gene information.""" manual_models = set() for gene in variant_obj.get('genes', []): manual_models.update(gene.get('manual_inheritance', [])) return list(manual_models)
Return info about callers.
def callers(variant_obj, category='snv'): """Return info about callers.""" calls = set() for caller in CALLERS[category]: if variant_obj.get(caller['id']): calls.add((caller['name'], variant_obj[caller['id']])) return list(calls)
Sand a verification email and register the verification in the database
def variant_verification(store, mail, institute_obj, case_obj, user_obj, variant_obj, sender, variant_url, order, comment, url_builder=url_for): """Sand a verification email and register the verification in the database Args: store(scout.adapter.MongoAdapter) mail(scout.server.extensions.mail): an instance of flask_mail.Mail institute_obj(dict): an institute object case_obj(dict): a case object user_obj(dict): a user object variant_obj(dict): a variant object (snv or sv) sender(str): current_app.config['MAIL_USERNAME'] variant_url(str): the complete url to the variant (snv or sv), a link that works from outside scout domain. order(str): False == cancel order, True==order verification comment(str): sender's entered comment from form url_builder(flask.url_for): for testing purposes, otherwise test verification email fails because out of context """ recipients = institute_obj['sanger_recipients'] if len(recipients) == 0: raise MissingSangerRecipientError() view_type = None email_subject = None category = variant_obj.get('category') display_name = None chromosome = variant_obj['chromosome'] end_chrom = variant_obj.get('end_chrom', chromosome) breakpoint_1 = ':'.join([chromosome, str(variant_obj['position'])]) breakpoint_2 = ':'.join([end_chrom, str(variant_obj.get('end'))]) variant_size = variant_obj.get('length') panels = ', '.join(variant_obj.get('panels', [])) hgnc_symbol = ', '.join(variant_obj['hgnc_symbols']) email_subj_gene_symbol = None if len(variant_obj['hgnc_symbols']) > 3: email_subj_gene_symbol = ' '.join([ str(len(variant_obj['hgnc_symbols'])) + 'genes']) else: email_subj_gene_symbol = hgnc_symbol gtcalls = ["<li>{}: {}</li>".format(sample_obj['display_name'], sample_obj['genotype_call']) for sample_obj in variant_obj['samples']] tx_changes = [] if category == 'snv': #SNV view_type = 'variants.variant' display_name = variant_obj.get('display_name') tx_changes = [] for gene_obj in variant_obj.get('genes', []): for tx_obj in gene_obj['transcripts']: parse_transcript(gene_obj, tx_obj) # select refseq transcripts as "primary" if not tx_obj.get('refseq_id'): continue for refseq_id in tx_obj.get('refseq_identifiers'): transcript_line = [] if "common" in gene_obj: transcript_line.append(gene_obj['common']['hgnc_symbol']) else: transcript_line.append(gene_obj['hgnc_id']) transcript_line.append('-'.join([refseq_id, tx_obj['transcript_id']])) if "exon" in tx_obj: transcript_line.append(''.join([ "exon", tx_obj["exon"]])) elif "intron" in tx_obj: transcript_line.append(''.join([ "intron", tx_obj["intron"]])) else: transcript_line.append('intergenic') if "coding_sequence_name" in tx_obj: transcript_line.append(urllib.parse.unquote(tx_obj['coding_sequence_name'])) else: transcript_line.append('') if "protein_sequence_name" in tx_obj: transcript_line.append(urllib.parse.unquote(tx_obj['protein_sequence_name'])) else: transcript_line.append('') tx_changes.append("<li>{}</li>".format(':'.join(transcript_line))) else: #SV view_type = 'variants.sv_variant' display_name = '_'.join([breakpoint_1, variant_obj.get('sub_category').upper()]) # body of the email html = verification_email_body( case_name = case_obj['display_name'], url = variant_url, #this is the complete url to the variant, accessible when clicking on the email link display_name = display_name, category = category.upper(), subcategory = variant_obj.get('sub_category').upper(), breakpoint_1 = breakpoint_1, breakpoint_2 = breakpoint_2, hgnc_symbol = hgnc_symbol, panels = panels, gtcalls = ''.join(gtcalls), tx_changes = ''.join(tx_changes) or 'Not available', name = user_obj['name'].encode('utf-8'), comment = comment ) # build a local the link to the variant to be included in the events objects (variant and case) created in the event collection. local_link = url_builder(view_type, institute_id=institute_obj['_id'], case_name=case_obj['display_name'], variant_id=variant_obj['_id']) if order == 'True': # variant verification should be ordered # pin variant if it's not already pinned if case_obj.get('suspects') is None or variant_obj['_id'] not in case_obj['suspects']: store.pin_variant(institute_obj, case_obj, user_obj, local_link, variant_obj) email_subject = "SCOUT: validation of {} variant {}, ({})".format( category.upper(), display_name, email_subj_gene_symbol) store.order_verification(institute=institute_obj, case=case_obj, user=user_obj, link=local_link, variant=variant_obj) else: # variant verification should be cancelled email_subject = "SCOUT: validation of {} variant {}, ({}), was CANCELLED!".format(category.upper(), display_name, email_subj_gene_symbol) store.cancel_verification(institute=institute_obj, case=case_obj, user=user_obj, link=local_link, variant=variant_obj) kwargs = dict(subject=email_subject, html=html, sender=sender, recipients=recipients, # cc the sender of the email for confirmation cc=[user_obj['email']]) message = Message(**kwargs) # send email using flask_mail mail.send(message)
Builds the html code for the variant verification emails ( order verification and cancel verification )
def verification_email_body(case_name, url, display_name, category, subcategory, breakpoint_1, breakpoint_2, hgnc_symbol, panels, gtcalls, tx_changes, name, comment): """ Builds the html code for the variant verification emails (order verification and cancel verification) Args: case_name(str): case display name url(str): the complete url to the variant, accessible when clicking on the email link display_name(str): a display name for the variant category(str): category of the variant subcategory(str): sub-category of the variant breakpoint_1(str): breakpoint 1 (format is 'chr:start') breakpoint_2(str): breakpoint 2 (format is 'chr:stop') hgnc_symbol(str): a gene or a list of genes separated by comma panels(str): a gene panel of a list of panels separated by comma gtcalls(str): genotyping calls of any sample in the family tx_changes(str): amino acid changes caused by the variant, only for snvs otherwise 'Not available' name(str): user_obj['name'], uft-8 encoded comment(str): sender's comment from form Returns: html(str): the html body of the variant verification email """ html = """ <ul> <li> <strong>Case {case_name}</strong>: <a href="{url}">{display_name}</a> </li> <li><strong>Variant type</strong>: {category} ({subcategory}) <li><strong>Breakpoint 1</strong>: {breakpoint_1}</li> <li><strong>Breakpoint 2</strong>: {breakpoint_2}</li> <li><strong>HGNC symbols</strong>: {hgnc_symbol}</li> <li><strong>Gene panels</strong>: {panels}</li> <li><strong>GT call</strong></li> {gtcalls} <li><strong>Amino acid changes</strong></li> {tx_changes} <li><strong>Comment</strong>: {comment}</li> <li><strong>Ordered by</strong>: {name}</li> </ul> """.format( case_name=case_name, url=url, display_name=display_name, category=category, subcategory=subcategory, breakpoint_1=breakpoint_1, breakpoint_2=breakpoint_2, hgnc_symbol=hgnc_symbol, panels=panels, gtcalls=gtcalls, tx_changes=tx_changes, name=name, comment=comment) return html
Fetch data related to cancer variants for a case.
def cancer_variants(store, request_args, institute_id, case_name): """Fetch data related to cancer variants for a case.""" institute_obj, case_obj = institute_and_case(store, institute_id, case_name) form = CancerFiltersForm(request_args) variants_query = store.variants(case_obj['_id'], category='cancer', query=form.data).limit(50) data = dict( institute=institute_obj, case=case_obj, variants=(parse_variant(store, institute_obj, case_obj, variant, update=True) for variant in variants_query), form=form, variant_type=request_args.get('variant_type', 'clinical'), ) return data
Gather the required data for creating the clinvar submission form
def clinvar_export(store, institute_id, case_name, variant_id): """Gather the required data for creating the clinvar submission form Args: store(scout.adapter.MongoAdapter) institute_id(str): Institute ID case_name(str): case ID variant_id(str): variant._id Returns: a dictionary with all the required data (case and variant level) to pre-fill in fields in the clinvar submission form """ institute_obj, case_obj = institute_and_case(store, institute_id, case_name) pinned = [store.variant(variant_id) or variant_id for variant_id in case_obj.get('suspects', [])] variant_obj = store.variant(variant_id) return dict( today = str(date.today()), institute=institute_obj, case=case_obj, variant=variant_obj, pinned_vars=pinned )
Collects all variants from the clinvar submission collection with a specific submission_id
def get_clinvar_submission(store, institute_id, case_name, variant_id, submission_id): """Collects all variants from the clinvar submission collection with a specific submission_id Args: store(scout.adapter.MongoAdapter) institute_id(str): Institute ID case_name(str): case ID variant_id(str): variant._id submission_id(str): clinvar submission id, i.e. SUB76578 Returns: A dictionary with all the data to display the clinvar_update.html template page """ institute_obj, case_obj = institute_and_case(store, institute_id, case_name) pinned = [store.variant(variant_id) or variant_id for variant_id in case_obj.get('suspects', [])] variant_obj = store.variant(variant_id) clinvar_submission_objs = store.clinvars(submission_id=submission_id) return dict( today = str(date.today()), institute=institute_obj, case=case_obj, variant=variant_obj, pinned_vars=pinned, clinvars = clinvar_submission_objs )
Collect data relevant for rendering ACMG classification form.
def variant_acmg(store, institute_id, case_name, variant_id): """Collect data relevant for rendering ACMG classification form.""" institute_obj, case_obj = institute_and_case(store, institute_id, case_name) variant_obj = store.variant(variant_id) return dict(institute=institute_obj, case=case_obj, variant=variant_obj, CRITERIA=ACMG_CRITERIA, ACMG_OPTIONS=ACMG_OPTIONS)
Calculate an ACMG classification based on a list of criteria.
def variant_acmg_post(store, institute_id, case_name, variant_id, user_email, criteria): """Calculate an ACMG classification based on a list of criteria.""" institute_obj, case_obj = institute_and_case(store, institute_id, case_name) variant_obj = store.variant(variant_id) user_obj = store.user(user_email) variant_link = url_for('variants.variant', institute_id=institute_id, case_name=case_name, variant_id=variant_id) classification = store.submit_evaluation( institute_obj=institute_obj, case_obj=case_obj, variant_obj=variant_obj, user_obj=user_obj, link=variant_link, criteria=criteria, ) return classification
Fetch and fill - in evaluation object.
def evaluation(store, evaluation_obj): """Fetch and fill-in evaluation object.""" evaluation_obj['institute'] = store.institute(evaluation_obj['institute_id']) evaluation_obj['case'] = store.case(evaluation_obj['case_id']) evaluation_obj['variant'] = store.variant(evaluation_obj['variant_specific']) evaluation_obj['criteria'] = {criterion['term']: criterion for criterion in evaluation_obj['criteria']} evaluation_obj['classification'] = ACMG_COMPLETE_MAP[evaluation_obj['classification']] return evaluation_obj
Parse out HGNC symbols from a stream.
def upload_panel(store, institute_id, case_name, stream): """Parse out HGNC symbols from a stream.""" institute_obj, case_obj = institute_and_case(store, institute_id, case_name) raw_symbols = [line.strip().split('\t')[0] for line in stream if line and not line.startswith('#')] # check if supplied gene symbols exist hgnc_symbols = [] for raw_symbol in raw_symbols: if store.hgnc_genes(raw_symbol).count() == 0: flash("HGNC symbol not found: {}".format(raw_symbol), 'warning') else: hgnc_symbols.append(raw_symbol) return hgnc_symbols
Collect all verified variants in a list on institutes and save them to file
def verified_excel_file(store, institute_list, temp_excel_dir): """Collect all verified variants in a list on institutes and save them to file Args: store(adapter.MongoAdapter) institute_list(list): a list of institute ids temp_excel_dir(os.Path): folder where the temp excel files are written to Returns: written_files(int): the number of files written to temp_excel_dir """ document_lines = [] written_files = 0 today = datetime.datetime.now().strftime('%Y-%m-%d') LOG.info('Creating verified variant document..') for cust in institute_list: verif_vars = store.verified(institute_id=cust) LOG.info('Found {} verified variants for customer {}'.format(len(verif_vars), cust)) if not verif_vars: continue unique_callers = set() for var_type, var_callers in CALLERS.items(): for caller in var_callers: unique_callers.add(caller.get('id')) cust_verified = export_verified_variants(verif_vars, unique_callers) document_name = '.'.join([cust, '_verified_variants', today]) + '.xlsx' workbook = Workbook(os.path.join(temp_excel_dir,document_name)) Report_Sheet = workbook.add_worksheet() # Write the column header row = 0 for col,field in enumerate(VERIFIED_VARIANTS_HEADER + list(unique_callers)): Report_Sheet.write(row,col,field) # Write variant lines, after header (start at line 1) for row, line in enumerate(cust_verified,1): # each line becomes a row in the document for col, field in enumerate(line): # each field in line becomes a cell Report_Sheet.write(row,col,field) workbook.close() if os.path.exists(os.path.join(temp_excel_dir,document_name)): written_files += 1 return written_files
Build a hpo_term object Check that the information is correct and add the correct hgnc ids to the array of genes. Args: hpo_info ( dict ) Returns: hpo_obj ( scout. models. HpoTerm ): A dictionary with hpo information
def build_hpo_term(hpo_info): """Build a hpo_term object Check that the information is correct and add the correct hgnc ids to the array of genes. Args: hpo_info(dict) Returns: hpo_obj(scout.models.HpoTerm): A dictionary with hpo information """ try: hpo_id = hpo_info['hpo_id'] except KeyError: raise KeyError("Hpo terms has to have a hpo_id") LOG.debug("Building hpo term %s", hpo_id) # Add description to HPO term try: description = hpo_info['description'] except KeyError: raise KeyError("Hpo terms has to have a description") hpo_obj = HpoTerm( hpo_id = hpo_id, description = description ) # Add links to hgnc genes if any hgnc_ids = hpo_info.get('genes', set()) if hgnc_ids: hpo_obj['genes'] = list(hgnc_ids) return hpo_obj
Export all genes from the database
def export_genes(adapter, build='37'): """Export all genes from the database""" LOG.info("Exporting all genes to .bed format") for gene_obj in adapter.all_genes(build=build): yield gene_obj
Get the clnsig information
def parse_clnsig(acc, sig, revstat, transcripts): """Get the clnsig information Args: acc(str): The clnsig accession number, raw from vcf sig(str): The clnsig significance score, raw from vcf revstat(str): The clnsig revstat, raw from vcf transcripts(iterable(dict)) Returns: clnsig_accsessions(list): A list with clnsig accessions """ clnsig_accsessions = [] if acc: # New format of clinvar allways have integers as accession numbers try: acc = int(acc) except ValueError: pass # There are sometimes different separators so we need to check which # one to use if isinstance(acc, int): revstat_groups = [] if revstat: revstat_groups = [rev.lstrip('_') for rev in revstat.split(',')] sig_groups = [] if sig: for significance in sig.split('/'): splitted_word = significance.split('_') sig_groups.append(' '.join(splitted_word[:2])) for sign_term in sig_groups: clnsig_accsessions.append({ 'value': sign_term, 'accession': int(acc), 'revstat': ', '.join(revstat_groups), }) else: # There are sometimes different separators so we need to check which # one to use acc_groups = acc.split('|') sig_groups = sig.split('|') revstat_groups = revstat.split('|') for acc_group, sig_group, revstat_group in zip(acc_groups, sig_groups, revstat_groups): accessions = acc_group.split(',') significances = sig_group.split(',') revstats = revstat_group.split(',') for accession, significance, revstat in zip(accessions, significances, revstats): clnsig_accsessions.append({ 'value': int(significance), 'accession': accession, 'revstat': revstat, }) elif transcripts: clnsig = set() for transcript in transcripts: for annotation in transcript.get('clinsig', []): clnsig.add(annotation) for annotation in clnsig: clnsig_accsessions.append({'value': annotation}) return clnsig_accsessions