def post_to_l1000cds2(sig_id): """POST signature to L1000CDS2 API, then redirect to the result page. """ # get GeneSignature instance gene_signature_fk = database.get(OptionalMetadata, sig_id, 'value').gene_signature_fk gene_signature = database.get(GeneSignature, gene_signature_fk, 'id') # retrieve up_genes, down_genes up_genes = [rg.gene.name for rg in gene_signature.up_genes] down_genes = [rg.gene.name for rg in gene_signature.down_genes] # POST to L1000CDS2 cds2_url = 'http://amp.pharm.mssm.edu/L1000CDS2/query' data = {'upGenes': up_genes, 'dnGenes': down_genes} config = { "aggravate": True, "searchMethod": "geneSet", "share": True, "combination": False, "db-version": "latest" } metadata = [{"key": "sig_id", "value": sig_id}] payload = {"data": data, "config": config, "meta": metadata} headers = {'content-type': 'application/json'} resp = requests.post(cds2_url, data=json.dumps(payload), headers=headers) if resp.status_code == 200: resp = resp.json() share_id = resp['shareId'] result_url = 'http://amp.pharm.mssm.edu/L1000CDS2/#/result/%s' % share_id return redirect(result_url) else: abort(404)
def get_binary(sig_id): """Get up/down gene sets. """ # get GeneSignature instance gene_signature_fk = database.get(OptionalMetadata, sig_id, 'value').gene_signature_fk gene_signature = database.get(GeneSignature, gene_signature_fk, 'id') # retrieve up_genes, down_genes up_genes = [rg.gene.name for rg in gene_signature.up_genes] down_genes = [rg.gene.name for rg in gene_signature.down_genes] return json.dumps({ 'up_genes': up_genes, 'down_genes': down_genes, 'sig_id': sig_id })
def view_gene_signature(extraction_id): """Modal for gene signature. """ gene_signature = database.get(GeneSignature, extraction_id, 'extraction_id') return render_template('pages/gene-signature.html', gene_signature=gene_signature)
def view_custom_report(report_id, tag_name): """Views a custom report by report ID. """ tag = database.get(Tag, tag_name, 'name') report = database.get(Report, report_id) print(report.category) print(report.gene_signatures) if not tag or not report: abort(404) if report.pca_plot: pca_json = report.pca_plot.data else: pca_json = None return render_template('pages/report.html', tag=tag, report=report, pca_json=pca_json)
def build_approved_report(tag_name): """Builds the an approved report for a tag. """ category = request.args.get('category') tag = database.get(Tag, tag_name, 'name') report_builder.build(tag, category=category) return redirect( url_for('report_pages.view_approved_report', tag_name=tag.name))
def reanalyze_approved_report(tag_name): """Reanalyze, i.e. requests new results from Enrichr and L1000CDS2, an approved report for a tag. """ category = request.args.get('category') tag = database.get(Tag, tag_name, 'name') report_builder.build(tag, category=category, reanalyze=True) return redirect( url_for('report_pages.view_approved_report', tag_name=tag.name))
def view_custom_tag(report_id): report = database.get(Report, report_id) if report is None: return abort(404) else: signatures = report.gene_signatures # The signatures for a custom report are a subset of signatures for # the main report. We show "tag-custom.html" because we do not want # users to build custom reports from these subsets. return render_template('pages/tag-custom.html', tag=report.tag, report=report, gene_signatures=signatures)
def view_approved_tag(tag_name, extraction_id=None): tag = database.get(Tag, tag_name, key='name') if tag is None: abort(404) else: signatures = tag.gene_signatures metadata = _get_metadata_names_and_percentages(signatures) return render_template('pages/tag.html', tag=tag, gene_signatures=signatures, extraction_id=extraction_id, metadata=metadata)
def view_approved_report(tag_name): """Renders approved report page. """ tag = database.get(Tag, tag_name, 'name') drug = database.get(Drug, tag_name, 'pert_id') if not tag: # abort(404) return render_template('pages/report-empty.html', drug=drug) else: report = tag.approved_report print tag.name print report.id if not report.complete(Config.SUPPORTED_ENRICHR_LIBRARIES): print 'Report for %s is not complete, building...' % tag.name print len(report.heat_maps) print len(report.enrichr_heat_maps) report_builder.rebuild(tag, category='cell') return render_template('pages/report.html', tag=tag, drug=drug, report=report)
def build_custom_report_from_all_signatures(tag_name): """Builds a custom report from all signatures. """ tag = database.get(Tag, tag_name, 'name') if not tag: abort(404) report_name = tag.name category = None report_id = report_builder.build_custom(tag, tag.gene_signatures, report_name, category) new_url = '%s/%s/%s' % (Config.REPORT_URL, report_id, tag.name) return redirect(new_url)
def download_gene_list(extraction_id, direction): """Generate txt file for gene lists in gene signatures. direction should be attr name in one of ('combined_genes', 'up_genes', 'down_genes') """ gene_signature = database.get(GeneSignature, extraction_id, 'extraction_id') gene_list = map(lambda x: x.gene.name, getattr(gene_signature, direction)) # Make a file on-the-fly gene_list = '\n'.join(gene_list) filename = '%s-%s.txt' % (extraction_id, direction) response = make_response(gene_list) response.headers[ "Content-Disposition"] = "attachment; filename=%s" % filename return response
def build_custom_report(tag_name): """Builds a custom report. """ tag = database.get(Tag, tag_name, 'name') if not tag: abort(404) category = request.json.get('category') report_name = request.json.get('report_name') extraction_ids = _get_extraction_ids(request) gene_signatures = database.get_signatures_by_ids(extraction_ids) report_id = report_builder.build_custom(tag, gene_signatures, report_name, category) # This endpoint is hit via an AJAX request. JavaScript must perform the # redirect. new_url = '%s/%s/%s' % (Config.REPORT_URL, report_id, tag.name) return jsonify({'new_url': new_url})
def create(type_, Session=None, **kwargs): """Returns link to hierarchical clustering. """ category_name = kwargs.get('category') TYPES = ['genes', 'enrichr', 'l1000cds2'] if type_ not in TYPES: raise ValueError('Invalid type_ argument. Must be in %s' % str(TYPES)) if 'extraction_ids' in kwargs: signatures = [] for extraction_id in kwargs.get('extraction_ids', []): signature = database.get(GeneSignature, extraction_id, 'extraction_id') signatures.append(signature) else: signatures = kwargs.get('signatures') diff_exp_method = kwargs.get('diff_exp_method') payload = {} if type_ == 'enrichr': library = kwargs.get('library') payload['is_up_down'] = True payload['columns'] = prepare_enriched_terms(Session, signatures, category_name, library) elif type_ == 'l1000cds2': payload['columns'] = prepare_perturbations(Session, signatures, category_name) payload['is_up_down'] = True elif type_ == 'genes': payload['columns'] = prepare_ranked_genes(diff_exp_method, signatures, category_name) payload['is_up_down'] = False net = Network() net.load_vect_post_to_net(payload) net.make_clust() result = net.export_net_json() return result
def test(tag_name): """Returns a zipped directory with one plain text file for each signature. """ tag = database.get(Tag, tag_name, 'name') # Write the contents of the signatures to file and get the filenames. filenames = _get_signature_files(tag) # Folder name in ZIP archive which contains the above files zip_subdir = tag.name zip_filename = '%s.zip' % zip_subdir # Open StringIO to grab in-memory ZIP contents s = StringIO.StringIO() # The zip compressor zf = zipfile.ZipFile(s, 'w') for fpath in filenames: # Calculate path for file in zip fdir, fname = os.path.split(fpath) zip_path = os.path.join(zip_subdir, fname) # Add file, at correct path zf.write(fpath, zip_path) zf.close() # Grab ZIP file from in-memory, make response with correct MIME-type resp = Response(s.getvalue(), mimetype='application/x-zip-compressed') resp.headers[ 'Content-Disposition'] = 'attachment; filename=%s' % zip_filename # Remove files from disc for f in filenames: os.remove(f) return resp