def make_html(stmts, fname): ha = HtmlAssembler( stmts, title='Small molecule inhibitors of %s assembled by INDRA' % target, db_rest_url='http://db.indra.bio/latest') ha.make_model() ha.save_model(fname)
def make_html(stmts, ev_counts, source_counts, fname, drug): ha = HtmlAssembler(stmts, ev_totals=ev_counts, source_counts=source_counts, title='Effects of %s assembled by INDRA' % drug, db_rest_url='http://db.indra.bio/latest') ha.make_model() ha.save_model(fname)
def html_assembler(indra_stmts, fname): """Assemble INDRA statements into a HTML report""" html_assembler = HtmlAssembler(indra_stmts, db_rest_url='https://db.indra.bio') assembled_html_report = html_assembler.make_model(no_redundancy=True) html_assembler.save_model(fname) return assembled_html_report
def _make_evidence_html(stmts, ev_counts=None, source_counts=None, title='Results from the INDRA database'): "Make html from a set of statements." ha = HtmlAssembler(stmts, db_rest_url='https://db.indra.bio', title=title, ev_totals=ev_counts, source_counts=source_counts) return ha.make_model()
def dump_to_s3(stmts, ev_counts, source_counts): s3 = boto3.client('s3') bucket = 'indrabot-results' fname = '%s.html' % uuid.uuid4() ha = HtmlAssembler(stmts, db_rest_url=db_rest_url, ev_totals=ev_totals, source_counts=source_counts) html_str = ha.make_model() url = 'https://s3.amazonaws.com/%s/%s' % (bucket, fname) logger.info('Dumping to %s' % url) s3.put_object(Key=fname, Body=html_str.encode('utf-8'), Bucket=bucket, ContentType='text/html') logger.info('Dumped to %s' % url) return url
def dump_html_to_s3(kinase, stmts): s3 = boto3.client('s3') bucket = 'dark-kinases' fname = f'{kinase}.html' sts_sorted = sorted(stmts, key=lambda x: len(x.evidence), reverse=True) ha = HtmlAssembler(sts_sorted, db_rest_url='https://db.indra.bio') html_str = ha.make_model(no_redundancy=True, show_belief=True) url = 'https://s3.amazonaws.com/%s/%s' % (bucket, fname) print('Dumping to %s' % url) s3.put_object(Key=fname, Body=html_str.encode('utf-8'), Bucket=bucket, ContentType='text/html')
def get_html(self): """Get html for these statements.""" import boto3 html_assembler = HtmlAssembler(self.get_statements(), db_rest_url=DB_REST_URL) html = html_assembler.make_model() s3 = boto3.client('s3') bucket = 'indrabot-results' key = '%s.html' % uuid.uuid4() link = 'https://s3.amazonaws.com/%s/%s' % (bucket, key) s3.put_object(Bucket=bucket, Key=key, Body=html.encode('utf-8'), ContentType='text/html') return link
def load(name): assert WORKING_DIR is not None, "WORKING_DIR is not defined." print(f"Attempting to load {name}") # Select the correct file is_html = False file_path = None for option in _list_files(name): if option.endswith('.html'): file_path = option is_html = True break elif option.endswith('.pkl'): file_path = option if file_path is None: print(f"ERROR: Invalid name: {name}") abort(400, (f"Invalid name: neither {name}.pkl nor {name}.html " f"exists in {WORKING_DIR}. If using s3 directory, " f"remember to add the '/' to the end for your working " f"directory.")) return raw_content = _get_file(file_path) # If the file is HTML, just return it. if is_html: print("Returning with cached HTML file.") return raw_content # Get the pickle file. stmts = pickle.loads(raw_content) # Build the HTML file html_assembler = HtmlAssembler(stmts, title='INDRA Curation', db_rest_url=request.url_root[:-1]) template = env.get_template('curation_service/cur_stmts_view.html') content = html_assembler.make_model(template, with_grouping=False) # Save the file to s3 html_file_path = file_path.replace('.pkl', '.html') print(f"Saved HTML file to {html_file_path}") _put_file(html_file_path, content) # Return the result. print("Returning with newly generated HTML file.") return content
def get_html(self): """Get html for these statements.""" logger.info('Generating HTML') import boto3 from botocore import UNSIGNED from botocore.client import Config s3 = boto3.client('s3', config=Config(signature_version=UNSIGNED)) ev_totals = self.get_ev_totals() source_counts = self.get_source_counts() html_assembler = HtmlAssembler(self.get_statements(), ev_counts=ev_totals, source_counts=source_counts, db_rest_url=DB_REST_URL) html = html_assembler.make_model() bucket = 'indrabot-results' key = '%s.html' % uuid.uuid4() link = 'https://s3.amazonaws.com/%s/%s' % (bucket, key) logger.info('Uploading to S3') s3.put_object(Bucket=bucket, Key=key, Body=html.encode('utf-8'), ContentType='text/html', ACL='public-read') return link
def decorator(*args, **kwargs): tracker = LogTracker() start_time = datetime.now() logger.info("Got query for %s at %s!" % (f.__name__, start_time)) query = request.args.copy() offs = query.pop('offset', None) ev_lim = query.pop('ev_limit', None) best_first_str = query.pop('best_first', 'true') best_first = True if best_first_str.lower() == 'true' \ or best_first_str else False do_stream_str = query.pop('stream', 'false') do_stream = True if do_stream_str == 'true' else False max_stmts = min(int(query.pop('max_stmts', MAX_STATEMENTS)), MAX_STATEMENTS) format = query.pop('format', 'json') api_key = query.pop('api_key', None) logger.info("Running function %s after %s seconds." % (f.__name__, sec_since(start_time))) result = f(query, offs, max_stmts, ev_lim, best_first, *args, **kwargs) logger.info("Finished function %s after %s seconds." % (f.__name__, sec_since(start_time))) # Redact elsevier content for those without permission. if api_key is None or not _has_elsevier_auth(api_key): for stmt_json in result['statements'].values(): for ev_json in stmt_json['evidence']: if get_source(ev_json) == 'elsevier': text = ev_json['text'] if len(text) > 200: ev_json['text'] = text[:200] + REDACT_MESSAGE logger.info("Finished redacting evidence for %s after %s seconds." % (f.__name__, sec_since(start_time))) result['offset'] = offs result['evidence_limit'] = ev_lim result['statement_limit'] = MAX_STATEMENTS result['statements_returned'] = len(result['statements']) if format == 'html': stmts_json = result.pop('statements') ev_totals = result.pop('evidence_totals') stmts = stmts_from_json(stmts_json.values()) html_assembler = HtmlAssembler(stmts, result, ev_totals, title='INDRA DB REST Results', db_rest_url=request.url_root[:-1]) content = html_assembler.make_model() if tracker.get_messages(): level_stats = [ '%d %ss' % (n, lvl.lower()) for lvl, n in tracker.get_level_stats().items() ] msg = ' '.join(level_stats) content = html_assembler.append_warning(msg) mimetype = 'text/html' else: # Return JSON for all other values of the format argument result.update(tracker.get_level_stats()) content = json.dumps(result) mimetype = 'application/json' if do_stream: # Returning a generator should stream the data. resp_json_bts = content gen = batch_iter(resp_json_bts, 10000) resp = Response(gen, mimetype=mimetype) else: resp = Response(content, mimetype=mimetype) logger.info("Exiting with %d statements with %d evidence of size " "%f MB after %s seconds." % (result['statements_returned'], result['total_evidence'], sys.getsizeof(resp.data) / 1e6, sec_since(start_time))) return resp
def assemble_html(stmts, fname_key): ha = HtmlAssembler(stmts) ha.make_model() ha.save_model('%s.html' % fname_key)
def _make_evidence_html(self, stmts, ev_counts=None, title='Results from the INDRA database'): "Make html from a set of statements." ha = HtmlAssembler(stmts, db_rest_url='db.indra.bio', title=title, ev_totals=ev_counts) return ha.make_model()