示例#1
0
文件: data.py 项目: AndersenLab/CeNDR
 def generate():
     r = report.get(report_slug = report_slug)
     t = trait.get(report = r, trait_slug = trait_slug)
     intervals = list(report.select(mapping) \
            .join(mapping) \
            .where(
                     (report.report_slug == report_slug)
                     & 
                     (mapping.trait == t)
                 ) \
            .dicts()
            .execute())
     yield "\t".join(["report", "trait", "CHROM_POS", "REF", "ALT",
                      "gene_id", "locus", "feature_id", "transcript_biotype",
                      "annotation", "putative_impact", "hgvs_p", 
                      "correlation"]) + "\n"
     for i in intervals:
         for cor in get_correlated_genes(r, t, i["chrom"], i["interval_start"], i["interval_end"]):
             for variant in cor["variant_set"]:
                 line = map(str, [r.report_slug,
                                  t.trait_slug,
                                  variant["CHROM_POS"], 
                                  variant["REF"],
                                  variant["ALT"],
                                  variant["gene_id"],
                                  cor["gene_name"],
                                  variant["feature_id"],
                                  cor["transcript_biotype"],
                                  variant["annotation"],
                                  variant["putative_impact"],
                                  variant["hgvs_p"],
                                  variant["correlation"]])
                 yield '\t'.join(line) + "\n"
示例#2
0
def trait_view(report_slug, trait_slug="", rerun=None):

    report_data = list(
        report.select(report, trait, mapping.trait_id)
        .join(trait)
        .where(
            ((report.report_slug == report_slug) & (report.release == 0))
            | ((report.report_hash == report_slug) & (report.release > 0))
        )
        .join(mapping, JOIN.LEFT_OUTER)
        .distinct()
        .dicts()
        .execute()
    )

    if not report_data:
        return render_template("404.html"), 404

    if report_data[0]["release"] == 0:
        report_url_slug = report_data[0]["report_slug"]
    else:
        report_url_slug = report_data[0]["report_hash"]

    if not trait_slug:
        return redirect(url_for("trait_view", report_slug=report_url_slug, trait_slug=report_data[0]["trait_slug"]))
    else:
        try:
            trait_data = [x for x in report_data if x["trait_slug"] == trait_slug][0]
        except:
            # Redirect user to first trait if it can't be found.
            return redirect(url_for("trait_view", report_slug=report_url_slug, trait_slug=report_data[0]["trait_slug"]))

    page_title = trait_data["report_name"] + " > " + trait_data["trait_name"]
    title = trait_data["report_name"]
    subtitle = trait_data["trait_name"]
    # Define report and trait slug
    report_slug = trait_data["report_slug"]  # don't remove
    trait_slug = trait_data["trait_slug"]  # don't remove

    r = report.get(report_slug=report_slug)
    t = trait.get(report=r, trait_slug=trait_slug)

    # phenotype data
    phenotype_data = list(
        trait_value.select(strain.strain, trait_value.value)
        .join(trait)
        .join(report)
        .switch(trait_value)
        .join(strain)
        .where(report.report_slug == r.report_slug)
        .where(trait.trait_slug == t.trait_slug)
        .dicts()
        .execute()
    )

    if rerun == "rerun":
        t.status = "queue"
        t.save()
        launch_mapping(verify_request=False)
        # Return user to current trait
        return redirect(url_for("trait_view", report_slug=report_url_slug, trait_slug=trait_slug))

    report_trait = "%s/%s" % (report_slug, trait_slug)
    base_url = "https://storage.googleapis.com/cendr/" + report_trait

    # Fetch significant mappings
    mapping_results = list(
        mapping.select(mapping, report, trait)
        .join(trait)
        .join(report)
        .filter((report.report_slug == report_slug), (trait.trait_slug == trait_slug))
        .dicts()
        .execute()
    )

    #######################
    # Variant Correlation #
    #######################
    var_corr = []
    for m in mapping_results:
        var_corr.append(correlation.get_correlated_genes(r, t, m["chrom"], m["interval_start"], m["interval_end"]))
    tbl_color = {"LOW": "success", "MODERATE": "warning", "HIGH": "danger"}

    #######################
    # Fetch geo locations #
    #######################
    geo_gt = {}
    for m in mapping_results:
        try:
            result = GT.fetch_geo_gt(m["chrom"], m["pos"])
            geo_gt[m["chrom"] + ":" + str(m["pos"])] = result
        except:
            pass
    geo_gt = json.dumps(geo_gt)

    status = trait_data["status"]

    # List available datasets
    report_files = list(storage.Client().get_bucket("cendr").list_blobs(prefix=report_trait + "/tables"))
    report_files = [os.path.split(x.name)[1] for x in report_files]

    # Fetch biotypes descriptions
    from cendr import biotypes

    return render_template("report.html", **locals())