def region_page(region_id): db = get_db() try: region = region_id.split('-') cache_key = 't-region-{}'.format(region_id) t = cache.get(cache_key) if t is None: chrom = region[0] start = None stop = None if len(region) == 3: chrom, start, stop = region start = int(start) stop = int(stop) if start is None or stop - start > REGION_LIMIT or stop < start: return render_template( 'region.html', genes_in_region=None, variants_in_region=None, chrom=chrom, start=start, stop=stop, coverage=None ) if start == stop: start -= 20 stop += 20 genes_in_region = lookups.get_genes_in_region(db, chrom, start, stop) variants_in_region = lookups.get_variants_in_region(db, chrom, start, stop) xstart = get_xpos(chrom, start) xstop = get_xpos(chrom, stop) coverage_stats_exomes = lookups.get_coverage_for_bases(db, 'exome_coverage', xstart, xstop) coverage_stats_genomes = lookups.get_coverage_for_bases(db, 'genome_coverage', xstart, xstop) coverage_stats = { "exomes": coverage_stats_exomes, "genomes": coverage_stats_genomes } t = render_template( 'region.html', genes_in_region=genes_in_region, variants_in_region=variants_in_region, chrom=chrom, start=start, stop=stop, coverage=coverage_stats ) print 'Rendering region: %s' % region_id return t except Exception, e: print 'Failed on region:', region_id, ';Error=', traceback.format_exc() abort(404)
def region_page(region_id): dbname = session['db_selected'] db = get_db() try: region = region_id.split('-') cache_key = 't-region-{}'.format(region_id) t = cache.get(cache_key) print 'Rendering %sregion: %s' % ('' if t is None else 'cached ', region_id) if t is None: chrom = region[0] start = None stop = None if len(region) == 3: chrom, start, stop = region start = int(start) stop = int(stop) if start is None or stop - start > REGION_LIMIT or stop < start: return render_template( 'region.html', genes_in_region=None, variants_in_region=None, chrom=chrom, start=start, stop=stop, coverage=None, csq_order=csq_order, dbname=dbname, ) if start == stop: start -= 20 stop += 20 genes_in_region = lookups.get_genes_in_region( db, chrom, start, stop) variants_in_region = lookups.get_variants_in_region( db, chrom, start, stop) xstart = get_xpos(chrom, start) xstop = get_xpos(chrom, stop) coverage_array = lookups.get_coverage_for_bases(db, xstart, xstop) t = render_template( 'region.html', genes_in_region=genes_in_region, variants_in_region=variants_in_region, chrom=chrom, start=start, stop=stop, coverage=coverage_array, csq_order=csq_order, dbname=dbname, ) cache.set(cache_key, t) return t except Exception, e: print 'Failed on region:', region_id, ';Error=', traceback.format_exc() abort(404)
def variant_page(variant_str): db = get_db() try: chrom, pos, ref, alt = variant_str.split('-') pos = int(pos) # pos, ref, alt = get_minimal_representation(pos, ref, alt) xpos = xbrowse.get_xpos(chrom, pos) variant = lookups.get_variant(db, xpos, ref, alt) if variant is None: variant = { 'chrom': chrom, 'pos': pos, 'xpos': xpos, 'ref': ref, 'alt': alt } consequences = None ordered_csqs = None if 'vep_annotations' in variant: variant['vep_annotations'] = order_vep_by_csq( variant['vep_annotations']) # Adds major_consequence ordered_csqs = [ x['major_consequence'] for x in variant['vep_annotations'] ] ordered_csqs = reduce( lambda x, y: ','.join([x, y]) if y not in x else x, ordered_csqs, '').split(',') # Close but not quite there consequences = defaultdict(lambda: defaultdict(list)) for annotation in variant['vep_annotations']: annotation['HGVS'] = get_proper_hgvs(annotation) consequences[annotation['major_consequence']][ annotation['Gene']].append(annotation) base_coverage = lookups.get_coverage_for_bases(db, xpos, xpos + len(ref) - 1) any_covered = any([x['has_coverage'] for x in base_coverage]) metrics = lookups.get_metrics(db, variant) print 'Rendering variant: %s' % variant_str return render_template('variant.html', variant=variant, base_coverage=base_coverage, consequences=consequences, any_covered=any_covered, ordered_csqs=ordered_csqs, metrics=metrics) except Exception, e: print 'Failed on variant:', variant_str, '; Error=', traceback.format_exc( ) abort(404)
def region_page(region_id): db = get_db() try: region = region_id.split('-') cache_key = 't-region-{}'.format(region_id) t = cache.get(cache_key) print 'Rendering %sregion: %s' % ('' if t is None else 'cached ', region_id) if t is None: chrom = region[0] start = None stop = None if len(region) == 3: chrom, start, stop = region start = int(start) stop = int(stop) if start is None or stop - start > REGION_LIMIT or stop < start: return render_template( 'region.html', genes_in_region=None, variants_in_region=None, chrom=chrom, start=start, stop=stop, coverage=None, csq_order=csq_order, ) if start == stop: start -= 20 stop += 20 genes_in_region = lookups.get_genes_in_region(db, chrom, start, stop) variants_in_region = lookups.get_variants_in_region(db, chrom, start, stop) xstart = get_xpos(chrom, start) xstop = get_xpos(chrom, stop) coverage_array = lookups.get_coverage_for_bases(db, xstart, xstop) t = render_template( 'region.html', genes_in_region=genes_in_region, variants_in_region=variants_in_region, chrom=chrom, start=start, stop=stop, coverage=coverage_array, csq_order=csq_order, ) cache.set(cache_key, t) return t except Exception, e: print 'Failed on region:', region_id, ';Error=', traceback.format_exc() abort(404)
def variant_data(variant_str, source): db = get_db() chrom, pos, ref, alt = variant_str.split('-') pos = int(pos) # pos, ref, alt = get_minimal_representation(pos, ref, alt) xpos = get_xpos(chrom, pos) variant = lookups.get_variant(db, source, xpos, ref, alt) if variant is None: variant = { 'chrom': chrom, 'pos': pos, 'xpos': xpos, 'ref': ref, 'alt': alt } consequences = OrderedDict() if 'vep_annotations' in variant: add_consequence_to_variant(variant) variant['vep_annotations'] = remove_extraneous_vep_annotations(variant['vep_annotations']) variant['vep_annotations'] = order_vep_by_csq(variant['vep_annotations']) # Adds major_consequence for annotation in variant['vep_annotations']: annotation['HGVS'] = get_proper_hgvs(annotation) consequences.setdefault(annotation['major_consequence'], {}).setdefault(annotation['Gene'], []).append(annotation) base_coverage = lookups.get_coverage_for_bases(db, 'base_coverage', xpos, xpos + len(ref) - 1) any_covered = any([x['has_coverage'] for x in base_coverage]) metrics = lookups.get_metrics(db, variant) # check the appropriate sqlite db to get the *expected* number of # available bams and *actual* number of available bams for this variant sqlite_db_path = os.path.join( app.config["READ_VIZ_DIR"], "combined_bams", chrom, "combined_chr%s_%03d.db" % (chrom, pos % 1000)) logging.info(sqlite_db_path) try: read_viz_db = sqlite3.connect(sqlite_db_path) n_het = read_viz_db.execute("select n_expected_samples, n_available_samples from t " "where chrom=? and pos=? and ref=? and alt=? and het_or_hom_or_hemi=?", (chrom, pos, ref, alt, 'het')).fetchone() n_hom = read_viz_db.execute("select n_expected_samples, n_available_samples from t " "where chrom=? and pos=? and ref=? and alt=? and het_or_hom_or_hemi=?", (chrom, pos, ref, alt, 'hom')).fetchone() n_hemi = read_viz_db.execute("select n_expected_samples, n_available_samples from t " "where chrom=? and pos=? and ref=? and alt=? and het_or_hom_or_hemi=?", (chrom, pos, ref, alt, 'hemi')).fetchone() read_viz_db.close() except Exception, e: logging.error("Error when accessing sqlite db: %s - %s", sqlite_db_path, e) n_het = n_hom = n_hemi = None
def region_page(region_id): db = get_db() try: region = region_id.split("-") cache_key = "t-region-{}".format(region_id) t = cache.get(cache_key) if t is None: chrom = region[0] start = None stop = None if len(region) == 3: chrom, start, stop = region start = int(start) stop = int(stop) if start is None or stop - start > REGION_LIMIT or stop < start: return render_template( "region.html", genes_in_region=None, variants_in_region=None, chrom=chrom, start=start, stop=stop, coverage=None, ) if start == stop: start -= 20 stop += 20 genes_in_region = lookups.get_genes_in_region(db, chrom, start, stop) variants_in_region = lookups.get_variants_in_region(db, chrom, start, stop) xstart = get_xpos(chrom, start) xstop = get_xpos(chrom, stop) coverage_array = lookups.get_coverage_for_bases(db, xstart, xstop) t = render_template( "region.html", genes_in_region=genes_in_region, variants_in_region=variants_in_region, chrom=chrom, start=start, stop=stop, coverage=coverage_array, ) print "Rendering region: %s" % region_id return t except Exception, e: print "Failed on region:", region_id, ";Error=", e abort(404)
def variant_page(variant_str): db = get_db() try: chrom, pos, ref, alt = variant_str.split('-') pos = int(pos) # pos, ref, alt = get_minimal_representation(pos, ref, alt) xpos = xbrowse.get_xpos(chrom, pos) variant = lookups.get_variant(db, xpos, ref, alt) if variant is None: variant = { 'chrom': chrom, 'pos': pos, 'xpos': xpos, 'ref': ref, 'alt': alt } consequences = None ordered_csqs = None if 'vep_annotations' in variant: variant['vep_annotations'] = order_vep_by_csq(variant['vep_annotations']) # Adds major_consequence ordered_csqs = [x['major_consequence'] for x in variant['vep_annotations']] ordered_csqs = reduce(lambda x, y: ','.join([x, y]) if y not in x else x, ordered_csqs, '').split(',') # Close but not quite there consequences = defaultdict(lambda: defaultdict(list)) for annotation in variant['vep_annotations']: annotation['HGVS'] = get_proper_hgvs(annotation) consequences[annotation['major_consequence']][annotation['Gene']].append(annotation) base_coverage = lookups.get_coverage_for_bases(db, xpos, xpos + len(ref) - 1) any_covered = any([x['has_coverage'] for x in base_coverage]) metrics = lookups.get_metrics(db, variant) print 'Rendering variant: %s' % variant_str return render_template( 'variant.html', variant=variant, base_coverage=base_coverage, consequences=consequences, any_covered=any_covered, ordered_csqs=ordered_csqs, metrics=metrics ) except Exception, e: print 'Failed on variant:', variant_str, '; Error=', traceback.format_exc() abort(404)
def region_page(region_id): db = get_db() try: region = region_id.split('-') cache_key = 't-region-{}'.format(region_id) t = cache.get(cache_key) if t is None: chrom = region[0] start = None stop = None if len(region) == 3: chrom, start, stop = region start = int(start) stop = int(stop) if start is None or stop - start > REGION_LIMIT: return render_template( 'region.html', genes_in_region=None, variants_in_region=None, chrom=chrom, start=start, stop=stop, coverage=None ) genes_in_region = lookups.get_genes_in_region(db, chrom, start, stop) variants_in_region = lookups.get_variants_in_region(db, chrom, start, stop) xstart = xbrowse.get_xpos(chrom, start) xstop = xbrowse.get_xpos(chrom, stop) coverage_array = lookups.get_coverage_for_bases(db, xstart, xstop) t = render_template( 'region.html', genes_in_region=genes_in_region, variants_in_region=variants_in_region, chrom=chrom, start=start, stop=stop, coverage=coverage_array ) print 'Rendering region: %s' % region_id return t except Exception, e: print 'Failed on region:', region_id, ';Error=', e abort(404)
def region_page(region_id): db = get_db() try: region = region_id.split('-') cache_key = 't-region-{}'.format(region_id) t = cache.get(cache_key) if t is None: chrom = region[0] start = None stop = None if len(region) == 3: chrom, start, stop = region start = int(start) stop = int(stop) if start is None or stop - start > REGION_LIMIT: return render_template('region.html', genes_in_region=None, variants_in_region=None, chrom=chrom, start=start, stop=stop, coverage=None) genes_in_region = lookups.get_genes_in_region( db, chrom, start, stop) variants_in_region = lookups.get_variants_in_region( db, chrom, start, stop) xstart = xbrowse.get_xpos(chrom, start) xstop = xbrowse.get_xpos(chrom, stop) coverage_array = lookups.get_coverage_for_bases(db, xstart, xstop) t = render_template('region.html', genes_in_region=genes_in_region, variants_in_region=variants_in_region, chrom=chrom, start=start, stop=stop, coverage=coverage_array) print 'Rendering region: %s' % region_id return t except Exception, e: print 'Failed on region:', region_id, ';Error=', e abort(404)
def variant_page(variant_str): db = get_db() try: chrom, pos, ref, alt = variant_str.split("-") pos = int(pos) # pos, ref, alt = get_minimal_representation(pos, ref, alt) xpos = get_xpos(chrom, pos) variant = lookups.get_variant(db, xpos, ref, alt) if variant is None: variant = {"chrom": chrom, "pos": pos, "xpos": xpos, "ref": ref, "alt": alt} consequences = None ordered_csqs = None if "vep_annotations" in variant: variant["vep_annotations"] = order_vep_by_csq(variant["vep_annotations"]) # Adds major_consequence ordered_csqs = [x["major_consequence"] for x in variant["vep_annotations"]] ordered_csqs = reduce(lambda x, y: ",".join([x, y]) if y not in x else x, ordered_csqs, "").split( "," ) # Close but not quite there consequences = defaultdict(lambda: defaultdict(list)) for annotation in variant["vep_annotations"]: annotation["HGVS"] = get_proper_hgvs(annotation) consequences[annotation["major_consequence"]][annotation["Gene"]].append(annotation) base_coverage = lookups.get_coverage_for_bases(db, xpos, xpos + len(ref) - 1) any_covered = any([x["has_coverage"] for x in base_coverage]) metrics = lookups.get_metrics(db, variant) print "Rendering variant: %s" % variant_str return render_template( "variant.html", variant=variant, base_coverage=base_coverage, consequences=consequences, any_covered=any_covered, ordered_csqs=ordered_csqs, metrics=metrics, ) except Exception, e: print "Failed on variant:", variant_str, "; Error=", traceback.format_exc() abort(404)
def variant_page(variant_str): db = get_db() try: chrom, pos, ref, alt = variant_str.split('-') pos = int(pos) # pos, ref, alt = get_minimal_representation(pos, ref, alt) xpos = get_xpos(chrom, pos) variant = lookups.get_variant(db, xpos, ref, alt) if variant is None: variant = { 'chrom': chrom, 'pos': pos, 'xpos': xpos, 'ref': ref, 'alt': alt } consequences = OrderedDict() if 'vep_annotations' in variant: add_consequence_to_variant(variant) variant['vep_annotations'] = remove_extraneous_vep_annotations(variant['vep_annotations']) variant['vep_annotations'] = order_vep_by_csq(variant['vep_annotations']) # Adds major_consequence for annotation in variant['vep_annotations']: annotation['HGVS'] = get_proper_hgvs(annotation) consequences.setdefault(annotation['major_consequence'], {}).setdefault(annotation['Gene'], []).append(annotation) base_coverage = lookups.get_coverage_for_bases(db, xpos, xpos + len(ref) - 1) any_covered = any([x['has_coverage'] for x in base_coverage]) metrics = lookups.get_metrics(db, variant) # check the appropriate sqlite db to get the *expected* number of # available bams and *actual* number of available bams for this variant sqlite_db_path = os.path.join( app.config["READ_VIZ_DIR"], "combined_bams", chrom, "combined_chr%s_%03d.db" % (chrom, pos % 1000)) logging.info(sqlite_db_path) try: read_viz_db = sqlite3.connect(sqlite_db_path) n_het = read_viz_db.execute("select n_expected_samples, n_available_samples from t " "where chrom=? and pos=? and ref=? and alt=? and het_or_hom_or_hemi=?", (chrom, pos, ref, alt, 'het')).fetchone() n_hom = read_viz_db.execute("select n_expected_samples, n_available_samples from t " "where chrom=? and pos=? and ref=? and alt=? and het_or_hom_or_hemi=?", (chrom, pos, ref, alt, 'hom')).fetchone() n_hemi = read_viz_db.execute("select n_expected_samples, n_available_samples from t " "where chrom=? and pos=? and ref=? and alt=? and het_or_hom_or_hemi=?", (chrom, pos, ref, alt, 'hemi')).fetchone() read_viz_db.close() except Exception, e: logging.error("Error when accessing sqlite db: %s - %s", sqlite_db_path, e) n_het = n_hom = n_hemi = None read_viz_dict = { 'het': {'n_expected': n_het[0] if n_het is not None and n_het[0] is not None else 0, 'n_available': n_het[1] if n_het is not None and n_het[1] is not None else 0,}, 'hom': {'n_expected': n_hom[0] if n_hom is not None and n_hom[0] is not None else 0, 'n_available': n_hom[1] if n_hom is not None and n_hom[1] is not None else 0,}, 'hemi': {'n_expected': n_hemi[0] if n_hemi is not None and n_hemi[0] is not None else 0, 'n_available': n_hemi[1] if n_hemi is not None and n_hemi[1] is not None else 0,}, } total_available = 0 total_expected = 0 for het_or_hom_or_hemi in ('het', 'hom', 'hemi'): total_available += read_viz_dict[het_or_hom_or_hemi]['n_available'] total_expected += read_viz_dict[het_or_hom_or_hemi]['n_expected'] read_viz_dict[het_or_hom_or_hemi]['readgroups'] = [ '%(chrom)s-%(pos)s-%(ref)s-%(alt)s_%(het_or_hom_or_hemi)s%(i)s' % locals() for i in range(read_viz_dict[het_or_hom_or_hemi]['n_available']) ] #eg. '1-157768000-G-C_hom1', read_viz_dict[het_or_hom_or_hemi]['urls'] = [ #os.path.join('combined_bams', chrom, 'combined_chr%s_%03d.bam' % (chrom, pos % 1000)) os.path.join('combined_bams', chrom, 'combined_chr%s_%03d.bam' % (chrom, pos % 1000)) for i in range(read_viz_dict[het_or_hom_or_hemi]['n_available']) ] read_viz_dict['total_available'] = total_available read_viz_dict['total_expected'] = total_expected print 'Rendering variant: %s' % variant_str return render_template( 'variant.html', variant=variant, base_coverage=base_coverage, consequences=consequences, any_covered=any_covered, metrics=metrics, read_viz=read_viz_dict, )
def variant_page(variant_str): db = get_db() try: chrom, pos, ref, alt = variant_str.split('-') pos = int(pos) # pos, ref, alt = get_minimal_representation(pos, ref, alt) xpos = get_xpos(chrom, pos) variant = lookups.get_variant(db, xpos, ref, alt) if variant is None: variant = { 'chrom': chrom, 'pos': pos, 'xpos': xpos, 'ref': ref, 'alt': alt } consequences = OrderedDict() if 'vep_annotations' in variant: add_consequence_to_variant(variant) variant['vep_annotations'] = remove_extraneous_vep_annotations( variant['vep_annotations']) variant['vep_annotations'] = order_vep_by_csq( variant['vep_annotations']) # Adds major_consequence for annotation in variant['vep_annotations']: annotation['HGVS'] = get_proper_hgvs(annotation) consequences.setdefault(annotation['major_consequence'], {}).setdefault(annotation['Gene'], []).append(annotation) base_coverage = lookups.get_coverage_for_bases(db, xpos, xpos + len(ref) - 1) any_covered = any([x['has_coverage'] for x in base_coverage]) metrics = lookups.get_metrics(db, variant) # check the appropriate sqlite db to get the *expected* number of # available bams and *actual* number of available bams for this variant sqlite_db_path = os.path.join( app.config["READ_VIZ_DIR"], "combined_bams", chrom, "combined_chr%s_%03d.db" % (chrom, pos % 1000)) logging.info(sqlite_db_path) try: read_viz_db = sqlite3.connect(sqlite_db_path) n_het = read_viz_db.execute( "select n_expected_samples, n_available_samples from t " "where chrom=? and pos=? and ref=? and alt=? and het_or_hom_or_hemi=?", (chrom, pos, ref, alt, 'het')).fetchone() n_hom = read_viz_db.execute( "select n_expected_samples, n_available_samples from t " "where chrom=? and pos=? and ref=? and alt=? and het_or_hom_or_hemi=?", (chrom, pos, ref, alt, 'hom')).fetchone() n_hemi = read_viz_db.execute( "select n_expected_samples, n_available_samples from t " "where chrom=? and pos=? and ref=? and alt=? and het_or_hom_or_hemi=?", (chrom, pos, ref, alt, 'hemi')).fetchone() read_viz_db.close() except Exception, e: logging.error("Error when accessing sqlite db: %s - %s", sqlite_db_path, e) n_het = n_hom = n_hemi = None read_viz_dict = { 'het': { 'n_expected': n_het[0] if n_het is not None and n_het[0] is not None else 0, 'n_available': n_het[1] if n_het is not None and n_het[1] is not None else 0, }, 'hom': { 'n_expected': n_hom[0] if n_hom is not None and n_hom[0] is not None else 0, 'n_available': n_hom[1] if n_hom is not None and n_hom[1] is not None else 0, }, 'hemi': { 'n_expected': n_hemi[0] if n_hemi is not None and n_hemi[0] is not None else 0, 'n_available': n_hemi[1] if n_hemi is not None and n_hemi[1] is not None else 0, }, } total_available = 0 total_expected = 0 for het_or_hom_or_hemi in ('het', 'hom', 'hemi'): total_available += read_viz_dict[het_or_hom_or_hemi]['n_available'] total_expected += read_viz_dict[het_or_hom_or_hemi]['n_expected'] read_viz_dict[het_or_hom_or_hemi]['readgroups'] = [ '%(chrom)s-%(pos)s-%(ref)s-%(alt)s_%(het_or_hom_or_hemi)s%(i)s' % locals() for i in range(read_viz_dict[het_or_hom_or_hemi] ['n_available']) ] #eg. '1-157768000-G-C_hom1', read_viz_dict[het_or_hom_or_hemi]['urls'] = [ #os.path.join('combined_bams', chrom, 'combined_chr%s_%03d.bam' % (chrom, pos % 1000)) os.path.join('combined_bams', chrom, 'combined_chr%s_%03d.bam' % (chrom, pos % 1000)) for i in range(read_viz_dict[het_or_hom_or_hemi] ['n_available']) ] read_viz_dict['total_available'] = total_available read_viz_dict['total_expected'] = total_expected print 'Rendering variant: %s' % variant_str return render_template( 'variant.html', variant=variant, base_coverage=base_coverage, consequences=consequences, any_covered=any_covered, metrics=metrics, read_viz=read_viz_dict, )
def variant_page(variant_str): db = get_db() try: chrom, pos, ref, alt = variant_str.split('-') pos = int(pos) # pos, ref, alt = get_minimal_representation(pos, ref, alt) xpos = get_xpos(chrom, pos) variant = lookups.get_variant(db, xpos, ref, alt) if variant is None: variant = { 'chrom': chrom, 'pos': pos, 'xpos': xpos, 'ref': ref, 'alt': alt } consequences = None ordered_csqs = None if 'vep_annotations' in variant: variant['vep_annotations'] = order_vep_by_csq( variant['vep_annotations']) # Adds major_consequence ordered_csqs = [ x['major_consequence'] for x in variant['vep_annotations'] ] ordered_csqs = reduce( lambda x, y: ','.join([x, y]) if y not in x else x, ordered_csqs, '').split(',') # Close but not quite there consequences = defaultdict(lambda: defaultdict(list)) for annotation in variant['vep_annotations']: annotation['HGVS'] = get_proper_hgvs(annotation) consequences[annotation['major_consequence']][ annotation['Gene']].append(annotation) base_coverage = lookups.get_coverage_for_bases(db, xpos, xpos + len(ref) - 1) any_covered = any([x['has_coverage'] for x in base_coverage]) metrics = lookups.get_metrics(db, variant) # check the appropriate sqlite db to get the *expected* number of # available bams and *actual* number of available bams for this variant sqlite_db_path = os.path.join( app.config["READ_VIZ_DIR"], "combined_bams", chrom, "combined_chr%s_%03d.db" % (chrom, pos % 1000)) print(sqlite_db_path) try: read_viz_db = sqlite3.connect(sqlite_db_path) n_het = read_viz_db.execute( "select n_expected_samples, n_available_samples from t " "where chrom=? and pos=? and ref=? and alt=? and het_or_hom=?", (chrom, pos, ref, alt, 'het')).fetchone() n_hom = read_viz_db.execute( "select n_expected_samples, n_available_samples from t " "where chrom=? and pos=? and ref=? and alt=? and het_or_hom=?", (chrom, pos, ref, alt, 'hom')).fetchone() read_viz_db.close() except Exception, e: logging.debug("Error when accessing sqlite db: %s - %s", sqlite_db_path, e) n_het = n_hom = None read_viz_dict = { 'het': { 'n_expected': n_het[0] if n_het is not None and n_het[0] is not None else -1, 'n_available': n_het[1] if n_het and n_het[1] else 0, }, 'hom': { 'n_expected': n_hom[0] if n_hom is not None and n_hom[0] is not None else -1, 'n_available': n_hom[1] if n_hom and n_hom[1] else 0, }, } for het_or_hom in ( 'het', 'hom', ): #read_viz_dict[het_or_hom]['some_samples_missing'] = (read_viz_dict[het_or_hom]['n_expected'] > 0) and (read_viz_dict[het_or_hom]['n_expected'] - read_viz_dict[het_or_hom]['n_available'] > 0) read_viz_dict[het_or_hom]['all_samples_missing'] = ( read_viz_dict[het_or_hom]['n_expected'] != 0) and (read_viz_dict[het_or_hom]['n_available'] == 0) read_viz_dict[het_or_hom]['readgroups'] = [ '%(chrom)s-%(pos)s-%(ref)s-%(alt)s_%(het_or_hom)s%(i)s' % locals() for i in range(read_viz_dict[het_or_hom]['n_available']) ] #eg. '1-157768000-G-C_hom1', read_viz_dict[het_or_hom]['urls'] = [ os.path.join('combined_bams', chrom, 'combined_chr%s_%03d.bam' % (chrom, pos % 1000)) for i in range(read_viz_dict[het_or_hom]['n_available']) ] print 'Rendering variant: %s' % variant_str return render_template( 'variant.html', variant=variant, base_coverage=base_coverage, consequences=consequences, any_covered=any_covered, ordered_csqs=ordered_csqs, metrics=metrics, read_viz=read_viz_dict, )
def variant_page(variant_str): db = get_db() try: chrom, pos, ref, alt = variant_str.split("-") pos = int(pos) # pos, ref, alt = get_minimal_representation(pos, ref, alt) xpos = get_xpos(chrom, pos) variant = lookups.get_variant(db, xpos, ref, alt) if variant is None: variant = {"chrom": chrom, "pos": pos, "xpos": xpos, "ref": ref, "alt": alt} consequences = None ordered_csqs = None if "vep_annotations" in variant: variant["vep_annotations"] = order_vep_by_csq(variant["vep_annotations"]) # Adds major_consequence ordered_csqs = [x["major_consequence"] for x in variant["vep_annotations"]] ordered_csqs = reduce(lambda x, y: ",".join([x, y]) if y not in x else x, ordered_csqs, "").split( "," ) # Close but not quite there consequences = defaultdict(lambda: defaultdict(list)) for annotation in variant["vep_annotations"]: annotation["HGVS"] = get_proper_hgvs(annotation) consequences[annotation["major_consequence"]][annotation["Gene"]].append(annotation) base_coverage = lookups.get_coverage_for_bases(db, xpos, xpos + len(ref) - 1) any_covered = any([x["has_coverage"] for x in base_coverage]) metrics = lookups.get_metrics(db, variant) # check the appropriate sqlite db to get the *expected* number of # available bams and *actual* number of available bams for this variant sqlite_db_path = os.path.join( app.config["READ_VIZ_DIR"], "combined_bams", chrom, "combined_chr%s_%03d.db" % (chrom, pos % 1000) ) print (sqlite_db_path) try: read_viz_db = sqlite3.connect(sqlite_db_path) n_het = read_viz_db.execute( "select n_expected_samples, n_available_samples from t " "where chrom=? and pos=? and ref=? and alt=? and het_or_hom=?", (chrom, pos, ref, alt, "het"), ).fetchone() n_hom = read_viz_db.execute( "select n_expected_samples, n_available_samples from t " "where chrom=? and pos=? and ref=? and alt=? and het_or_hom=?", (chrom, pos, ref, alt, "hom"), ).fetchone() read_viz_db.close() except Exception, e: logging.debug("Error when accessing sqlite db: %s - %s", sqlite_db_path, e) n_het = n_hom = None read_viz_dict = { "het": { "n_expected": n_het[0] if n_het is not None and n_het[0] is not None else -1, "n_available": n_het[1] if n_het and n_het[1] else 0, }, "hom": { "n_expected": n_hom[0] if n_hom is not None and n_hom[0] is not None else -1, "n_available": n_hom[1] if n_hom and n_hom[1] else 0, }, } for het_or_hom in ("het", "hom"): # read_viz_dict[het_or_hom]['some_samples_missing'] = (read_viz_dict[het_or_hom]['n_expected'] > 0) and (read_viz_dict[het_or_hom]['n_expected'] - read_viz_dict[het_or_hom]['n_available'] > 0) read_viz_dict[het_or_hom]["all_samples_missing"] = (read_viz_dict[het_or_hom]["n_expected"] != 0) and ( read_viz_dict[het_or_hom]["n_available"] == 0 ) read_viz_dict[het_or_hom]["readgroups"] = [ "%(chrom)s-%(pos)s-%(ref)s-%(alt)s_%(het_or_hom)s%(i)s" % locals() for i in range(read_viz_dict[het_or_hom]["n_available"]) ] # eg. '1-157768000-G-C_hom1', read_viz_dict[het_or_hom]["urls"] = [ os.path.join("combined_bams", chrom, "combined_chr%s_%03d.bam" % (chrom, pos % 1000)) for i in range(read_viz_dict[het_or_hom]["n_available"]) ] print "Rendering variant: %s" % variant_str return render_template( "variant.html", variant=variant, base_coverage=base_coverage, consequences=consequences, any_covered=any_covered, ordered_csqs=ordered_csqs, metrics=metrics, read_viz=read_viz_dict, )
def variant_page(variant_str): db = get_db() try: chrom, pos, ref, alt = variant_str.split('-') pos = int(pos) # pos, ref, alt = get_minimal_representation(pos, ref, alt) xpos = get_xpos(chrom, pos) variant = lookups.get_variant(db, xpos, ref, alt) if variant is None: variant = { 'chrom': chrom, 'pos': pos, 'xpos': xpos, 'ref': ref, 'alt': alt } consequences = None ordered_csqs = None if 'vep_annotations' in variant: variant['vep_annotations'] = order_vep_by_csq(variant['vep_annotations']) # Adds major_consequence ordered_csqs = [x['major_consequence'] for x in variant['vep_annotations']] ordered_csqs = reduce(lambda x, y: ','.join([x, y]) if y not in x else x, ordered_csqs, '').split(',') # Close but not quite there consequences = defaultdict(lambda: defaultdict(list)) for annotation in variant['vep_annotations']: annotation['HGVS'] = get_proper_hgvs(annotation) consequences[annotation['major_consequence']][annotation['Gene']].append(annotation) base_coverage = lookups.get_coverage_for_bases(db, xpos, xpos + len(ref) - 1) any_covered = any([x['has_coverage'] for x in base_coverage]) metrics = lookups.get_metrics(db, variant) # check the appropriate sqlite db to get the *expected* number of # available bams and *actual* number of available bams for this variant sqlite_db_path = os.path.join( app.config["READ_VIZ_DIR"], "combined_bams", chrom, "combined_chr%s_%03d.db" % (chrom, pos % 1000)) print(sqlite_db_path) try: read_viz_db = sqlite3.connect(sqlite_db_path) n_het = read_viz_db.execute("select n_expected_samples, n_available_samples from t " "where chrom=? and pos=? and ref=? and alt=? and het_or_hom=?", (chrom, pos, ref, alt, 'het')).fetchone() n_hom = read_viz_db.execute("select n_expected_samples, n_available_samples from t " "where chrom=? and pos=? and ref=? and alt=? and het_or_hom=?", (chrom, pos, ref, alt, 'hom')).fetchone() read_viz_db.close() except Exception, e: logging.debug("Error when accessing sqlite db: %s - %s", sqlite_db_path, e) n_het = n_hom = None read_viz_dict = { 'het': {'n_expected': n_het[0] if n_het is not None and n_het[0] is not None else -1, 'n_available': n_het[1] if n_het and n_het[1] else 0,}, 'hom': {'n_expected': n_hom[0] if n_hom is not None and n_hom[0] is not None else -1, 'n_available': n_hom[1] if n_hom and n_hom[1] else 0,}, } for het_or_hom in ('het', 'hom',): #read_viz_dict[het_or_hom]['some_samples_missing'] = (read_viz_dict[het_or_hom]['n_expected'] > 0) and (read_viz_dict[het_or_hom]['n_expected'] - read_viz_dict[het_or_hom]['n_available'] > 0) read_viz_dict[het_or_hom]['all_samples_missing'] = (read_viz_dict[het_or_hom]['n_expected'] != 0) and (read_viz_dict[het_or_hom]['n_available'] == 0) read_viz_dict[het_or_hom]['readgroups'] = [ '%(chrom)s-%(pos)s-%(ref)s-%(alt)s_%(het_or_hom)s%(i)s' % locals() for i in range(read_viz_dict[het_or_hom]['n_available']) ] #eg. '1-157768000-G-C_hom1', read_viz_dict[het_or_hom]['urls'] = [ os.path.join('combined_bams', chrom, 'combined_chr%s_%03d.bam' % (chrom, pos % 1000)) for i in range(read_viz_dict[het_or_hom]['n_available']) ] print 'Rendering variant: %s' % variant_str return render_template( 'variant.html', variant=variant, base_coverage=base_coverage, consequences=consequences, any_covered=any_covered, ordered_csqs=ordered_csqs, metrics=metrics, read_viz=read_viz_dict, )