Instruction:
Given the following Python 3 function, write the documentation
def genes_by_alias(hgnc_genes):
    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

Response:
Return a dictionary with the hgnc symbols as keys
    Value of the dictionaries are information about the hgnc ids for a gene.


Instruction:
Can you tell what is the following Python 3 function doing
def add_ensembl_info(genes, ensembl_lines):

    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']

Response: Add the coordinates from the ensembl lines to the base ensembl record set.


Instruction:
How would you code a function in Python 3 to
add information from the exac lines to the gene level

Response:
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


Instruction:
Can you create a Python 3 function that
adds omim info to the list of genes that are associated with a gene.

Response:
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', [])


Instruction:
Can you generate a brief explanation for the following Python 3 code
def add_incomplete_penetrance(genes, alias_genes, hpo_lines):
    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

Response: Add incomplete penetrance info to the given genes.


Instruction:
How would you code a function in Python 3 to
try to get the correct gene based on a resource HGNC symbol.

Response:
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


Instruction:
Create a Python 3 function to
give a list of lines of ensembl and exac lines and a dictionary of gene ids and a dictionary of gene ids and their corresponding genotypes return a dictionary of gene ids and their respective gene IDs.

Response:
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


Instruction:
Can you generate a brief explanation for the following Python 3 code
def matchmaker_request(url, token, method, content_type=None, accept=None, data=None):
    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

Response: Send a request to MatchMaker and return the response


Instruction:
Write a Python 3 script to
return the available nodes for the given MME service.

Response:
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


Instruction:
Create a Python 3 function for
getting the cytoband coordinate for a position in a given set of chromosomes.

Response:
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


Instruction:
Implement a function in Python 3 to
get the subcategory for a VCF variant

Response:
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


Instruction:
Write a Python 3 script to
return the length of a variant in the sequence alt_len ref_len category pos end svtype svlen end Returns - 1 if there is no length

Response:
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


Instruction:
Make a summary of the following Python 3 code
def get_end(pos, alt, category, snvend=None, svend=None, svlen=None):
    # 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

Response: Return the end coordinate for a variant in a sequence.


Instruction:
Given the following Python 3 function, write the documentation
def parse_coordinates(variant, category):
    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

Response: Parse the coordinates of a single variant into a dictionary.


Instruction:
How would you code a function in Python 3 to
parse iterable with cytoband coordinates and return a dictionary with chromosome names as keys and interval trees as values

Response:
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


Instruction:
Given the following Python 3 function, write the documentation
def update_panel(adapter, panel_name, panel_version, new_version=None, new_date=None):
    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

Response: Update a single panel in the database.


Instruction:
Make a summary of the following Python 3 code
def cli(context, mongodb, username, password, authdb, host, port, loglevel, config, demo):
    # 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

Response: Manage interactions with a scout instance.


Instruction:
Write a Python 3 script for
parsing an exac formated line into a dict with the relevant info

Response:
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


Instruction:
Can you implement a function in Python 3 that
parses lines with exac formated genes Shipment

Response:
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


Instruction:
Can you generate a brief explanation for the following Python 3 code
def panels():
    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)

Response: Show all panels for a case.


Instruction:
Make a summary of the following Python 3 code
def panel(panel_id):
    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

Response: Display and add pending updates to a specific gene panel.


Instruction:
Implement a Python 3 function for
updating a panel to a new version.

Response:
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))


Instruction:
How would you code a function in Python 3 to
export panel to PDF file

Response:
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')


Instruction:
Can you implement a function in Python 3 that
edits additional information about a panel gene.

Response:
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)


Instruction:
Can you implement a function in Python 3 that
adds delivery report to an existing case.

Response:
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()


Instruction:
Can you implement a function in Python 3 that
parses a peddy. ped file containing a list of dicts.

Response:
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


Instruction:
Make a summary of the following Python 3 code
def parse_peddy_ped_check(lines):
    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

Response: Parse a. ped_check. csv file and return a list of dicts.


Instruction:
Here you have a function in Python 3, explain what it does
def parse_peddy_sex_check(lines):
    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

Response: Parse a. ped_check. csv file and return a list of dicts.


Instruction:
Can you tell what is the following Python 3 function doing
def hpo_terms(store, query = None, limit = None):
    hpo_phenotypes = {}
    if limit:
        limit=int(limit)

    hpo_phenotypes['phenotypes'] = list(store.hpo_terms(text=query, limit=limit))
    return hpo_phenotypes

Response: Retrieves a list of HPO terms from the scout database


Instruction:
Write a Python 3 script for
showing all objects in the whitelist collection

Response:
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'])


Instruction:
Can you write a function in Python 3 where it
builds a small phenotype object with phenotype_id and description

Response:
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


Instruction:
Can you generate the documentation for the following Python 3 function
def build_case(case_data, adapter):
    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

Response: Build a case object that is to be inserted into the database.


Instruction:
Can you generate the documentation for the following Python 3 function
def gene(store, hgnc_id):
    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

Response: Parse information about a gene.


Instruction:
Write a Python 3 script for
fetching matching genes and convert to JSON.

Response:
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


Instruction:
Can you write a function in Python 3 where it
displays the Scout dashboard.

Response:
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)


Instruction:
Implement a function in Python 3 to
return a requests response from url

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


Instruction:
Can you generate the documentation for the following Python 3 function
def fetch_resource(url):
    try:
        data = get_request(url)
        lines = data.split('\n')
    except Exception as err:
        raise err

    return lines

Response:
Fetch a resource and return the resulting lines in a list
    Send file_name to get more clean log messages


Instruction:
Write a Python 3 function for
fetching the necessary files from the OMIM API.

Response:
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


Instruction:
Can you create a Python 3 function that
fetches the ensembl genes from HSAPIEN

Response:
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


Instruction:
How would you implement a function in Python 3 that
fetches the ensembl exons from HSAPIens.

Response:
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


Instruction:
Write a Python 3 script to
fetch the hgnc genes file from ftp://ftp. ebi. ac. uk

Response:
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


Instruction:
Here you have a function in Python 3, explain what it does
def fetch_exac_constraint():
    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

Response: Fetch the file with exac constraint scores


Instruction:
Can you generate the documentation for the following Python 3 function
def fetch_hpo_files(hpogenes=False, hpoterms=False, phenotype_to_terms=False, hpodisease=False):

    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

Response: Fetch the necessary mim files using a api key


Instruction:
Make a summary of the following Python 3 code
def transcripts(context, build, hgnc_id, json):
    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 '',
        ))

Response: Show all transcripts in the database


Instruction:
Write a Python 3 function that can
add an occurrence key to the event object w/ a list of occurrences

Response:
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)


Instruction:
Implement a Python 3 function for
returning the events that occur on the given day.

Response:
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


Instruction:
Can you generate the documentation for the following Python 3 function
def variants(store, institute_obj, case_obj, variants_query, page=1, per_page=50):
    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,
    }

Response: Pre - process list of variants.


Instruction:
How would you explain what the following Python 3 function does
def str_variants(store, institute_obj, case_obj, variants_query, page=1, per_page=50):
    # Nothing unique to STRs on this level. Inheritance?
    return variants(store, institute_obj, case_obj, variants_query, page, per_page)

Response: Retrieve a list of STR variants.


Instruction:
Here you have a function in Python 3, explain what it does
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
    }

Response: Pre - process an SV variant entry for detail page.


Instruction:
Explain what the following Python 3 code does
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

Response: Parse a single variant into a single object.


Instruction:
How would you code a function in Python 3 to
get variants info to be exported to file one list ( line per variant.

Response:
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


Instruction:
Can you write a function in Python 3 where it
returns a header for the CSV file with the filtered variants to be exported.

Response:
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


Instruction:
Write a Python 3 script to
get variant information from a list of genes.

Response:
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


Instruction:
Make a summary of the following Python 3 code
def get_predictions(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

Response: Get sift predictions from genes.


Instruction:
Given the following Python 3 function, write the documentation
def variant_case(store, case_obj, variant_obj):
    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")

Response: Pre - process the variant view.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def find_bai_file(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

Response: Find out BAI file by extension given the BAM file.


Instruction:
Given the following Python 3 function, write the documentation
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,
    }

Response: Pre - process a single variant for the detailed variant view.


Instruction:
Write a Python 3 script for
querying observations for a variant.

Response:
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


Instruction:
Implement a Python 3 function for
parsing a single gene.

Response:
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 [])


Instruction:
Given the following Python 3 function, write the documentation
def parse_transcript(gene_obj, tx_obj, build=None):
    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)

Response: Parse variant gene transcript.


Instruction:
Can you generate the documentation for the following Python 3 function
def transcript_str(transcript_obj, gene_name=None):
    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

Response: Generate amino acid change as a string.


Instruction:
Create a Python 3 function to
calculate the end position of a variant.

Response:
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)


Instruction:
Here you have a function in Python 3, explain what it does
def frequency(variant_obj):
    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'

Response: Returns a judgement on the overall frequency of the variant.


Instruction:
Make a summary of the following Python 3 code
def clinsig_human(variant_obj):
    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

Response: Convert to human readable version of CLINSIG evaluation.


Instruction:
How would you implement a function in Python 3 that
composes link to 1000G page for detailed information.

Response:
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)


Instruction:
Given the following Python 3 function, write the documentation
def cosmic_link(variant_obj):

    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)

Response: Compose link to COSMIIC database.


Instruction:
Explain what the following Python 3 code does
def beacon_link(variant_obj, build=None):
    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)

Response: Compose link to Beacon Network.


Instruction:
Write a Python 3 function for
composing link to UCSC.

Response:
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)


Instruction:
Here you have a function in Python 3, explain what it does
def spidex_human(variant_obj):
    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'

Response: Translate SPIDEX annotation to human readable string.


Instruction:
Here you have a function in Python 3, explain what it does
def expected_inheritance(variant_obj):
    manual_models = set()
    for gene in variant_obj.get('genes', []):
        manual_models.update(gene.get('manual_inheritance', []))
    return list(manual_models)

Response: Gather information from common gene information.


Instruction:
Here you have a function in Python 3, explain what it does
def callers(variant_obj, category='snv'):
    calls = set()
    for caller in CALLERS[category]:
        if variant_obj.get(caller['id']):
            calls.add((caller['name'], variant_obj[caller['id']]))

    return list(calls)

Response: Return info about callers.


Instruction:
Can you create a Python 3 function that
sands 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

Response:
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 = ["
{}: {}
".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("
{}
".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)


Instruction:
Explain what the following Python 3 code does
def verification_email_body(case_name, url, display_name, category, subcategory, breakpoint_1, breakpoint_2, hgnc_symbol, panels, gtcalls, tx_changes, name, comment):
    html = """

           Case {case_name}: {display_name}

Variant type: {category} ({subcategory})

Breakpoint 1: {breakpoint_1}

Breakpoint 2: {breakpoint_2}

HGNC symbols: {hgnc_symbol}

Gene panels: {panels}

GT call

         {gtcalls}

Amino acid changes

         {tx_changes}

Comment: {comment}

Ordered by: {name}

    """.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

Response: Builds the html body of the variant verification email


Instruction:
Can you generate the documentation for the following Python 3 function
def cancer_variants(store, request_args, institute_id, case_name):
    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

Response: Fetch data related to cancer variants for a given institute and case.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def clinvar_export(store, institute_id, case_name, variant_id):

    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
    )

Response: Return a dictionary with all the required data for creating the clinvar submission form for the given institute and case.


Instruction:
How would you explain what the following Python 3 function does
def get_clinvar_submission(store, institute_id, case_name, variant_id, submission_id):

    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
    )

Response: Returns a dictionary with all the data to display for a specific clinvar submission.


Instruction:
Can you generate the documentation for the following Python 3 function
def variant_acmg(store, institute_id, case_name, variant_id):
    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)

Response: Collect data relevant for rendering ACMG classification form.


Instruction:
Given the following Python 3 function, write the documentation
def variant_acmg_post(store, institute_id, case_name, variant_id, user_email, 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

Response: Calculate an ACMG classification based on a list of criteria.


Instruction:
How would you explain what the following Python 3 function does
def evaluation(store, evaluation_obj):
    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

Response: Fetch and fill - in evaluation object.


Instruction:
Explain what the following Python 3 code does
def upload_panel(store, institute_id, case_name, 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

Response: Parse out HGNC symbols from a stream.