Exemple #1
0
def assemble_html(stmts_with_counts):
    for channel, (stmts, ev_counts, source_counts) in stmts_with_counts.items():
        ha = HtmlAssembler(stmts, ev_totals=ev_counts,
                           source_counts=source_counts,
                           title='INDRA statements for %s' % channel,
                           db_rest_url='https://db.indra.bio')
        ha.save_model('html/%s.html' % channel)
Exemple #2
0
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
Exemple #3
0
 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()
Exemple #4
0
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
Exemple #5
0
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')
Exemple #6
0
 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
Exemple #7
0
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
Exemple #8
0
def format_stmts(stmts, output_format, ev_counts=None, source_counts=None):
    if output_format == 'tsv':
        msg = ''
        for stmt in stmts:
            if not stmt.evidence:
                logger.warning('Statement %s without evidence' % stmt.uuid)
                txt = ''
                pmid = ''
            else:
                txt = '"%s"' % stmt.evidence[0].text if \
                    stmt.evidence[0].text else ''
                pmid = stmt.evidence[0].pmid if stmt.evidence[0].pmid else ''
            try:
                ea_txt = EnglishAssembler([stmt]).make_model()
            except Exception as e:
                ea_txt = ''
                logger.error('English assembly failed for %s' % stmt)
                logger.error(e)
            line = '%s\t%s\t%s\tPMID%s\n' % (stmt, ea_txt, txt, pmid)
            msg += line
        return msg
    elif output_format == 'pkl':
        fname = 'indrabot.pkl'
        with open(fname, 'wb') as fh:
            pickle.dump(stmts, fh)
        return fname
    elif output_format == 'pdf':
        fname = 'indrabot.pdf'
        ga = GraphAssembler(stmts)
        ga.make_model()
        ga.save_pdf(fname)
        return fname
    elif output_format == 'json':
        msg = json.dumps(stmts_to_json(stmts), indent=1)
        return msg
    elif output_format == 'html':
        ev_counts = {} if not ev_counts else ev_counts
        ha = HtmlAssembler(stmts, ev_totals=ev_counts,
                           source_counts=source_counts)
        fname = 'indrabot.html'
        ha.save_model(fname)
        return fname
    return None
Exemple #9
0
 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 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)
Exemple #11
0
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)
from indra.tools import assemble_corpus as ac
from indra.statements import stmts_to_json_file
from indra.assemblers.html import HtmlAssembler
from indra.sources import reach
tp = reach.process_pmc('PMC4455820', url=reach.local_nxml_url)
if tp:
    stmts = tp.statements
    print(stmts)
    stmts = ac.filter_grounded_only(stmts)  # Filter out ungrounded agents
    stmts = ac.run_preassembly(
        stmts,  # Run preassembly
        return_toplevel=False,
        normalize_equivalences=
        True,  # Optional: rewrite equivalent groundings to one standard
        normalize_opposites=
        True,  # Optional: rewrite opposite groundings to one standard
        normalize_ns='WM'
    )  # Use 'WM' namespace to normalize equivalences and opposites
    stmts = ac.filter_belief(stmts, 0.8)  # Apply belief cutoff of e.g., 0.8
    stmts_to_json_file(stmts, 'PMC4455820.json')
    ha = HtmlAssembler(stmts)
    ha.save_model('PMC4455820.html')

#
#
#
#
Exemple #13
0
#attempt to combine many statements

from indra.tools import assemble_corpus as ac
from indra.statements import stmts_to_json_file
from indra.assemblers.html import HtmlAssembler
from indra.sources import reach

pmcids = ["PMC3717945", "PMC5906628"]
stmts = []

for pmcid in pmcids:
    tp = reach.process_pmc(pmcid)
    stmts += tp.statements

stmts = ac.filter_grounded_only(stmts)  # Filter out ungrounded agents
stmts = ac.run_preassembly(
    stmts,  # Run preassembly
    return_toplevel=False,
    normalize_equivalences=
    True,  # Optional: rewrite equivalent groundings to one standard
    normalize_opposites=
    True,  # Optional: rewrite opposite groundings to one standard
    normalize_ns='WM'
)  # Use 'WM' namespace to normalize equivalences and opposites
stmts = ac.filter_belief(stmts, 0.8)  # Apply belief cutoff of e.g., 0.8
stmts_to_json_file(stmts, 'bigresults.json')
ha = HtmlAssembler(stmts)
ha.save_model('bigresults.html')
Exemple #14
0
    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)
Exemple #16
0
 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()