예제 #1
0
def ResultsPage(request):
    """Renders the search results page for a given query."""
    form = SearchForm(request.GET)
    if not form.is_valid():
        raise Http404
    logging.debug('Generating a search result page')
    query_parser = service_config.Get().query_parser
    reaction_matcher = service_config.Get().reaction_matcher
    matcher = service_config.Get().search_matcher
    query = form.cleaned_query
    logging.debug('Query: "%s"', query)
    if not query.strip():
        response = render(request, 'main.html', {})
        return response
    # Check if we should parse and process the input as a reaction.
    if query_parser.IsReactionQuery(query):
        logging.info('Parsing the query as a reaction')
        try:
            parsed_reaction = query_parser.ParseReactionQuery(query)
        except Exception:
            return render(request, 'parse_error_page.html')

        reaction_matches = reaction_matcher.MatchReaction(parsed_reaction)
        best_reaction = reaction_matches.GetBestMatch()
        if not best_reaction:
            return render(request, 'search_error_page.html', {'query': query})

        logging.debug('Generating a reaction from the matched KEGG IDs')
        aq_params = conditions.AqueousParams.FromForm(form, request.COOKIES)
        rxn = apps.get_model('gibbs.Reaction').FromIds(best_reaction,
                                                       aq_params)
        response = render(request, 'reaction_page.html',
                          rxn.GetTemplateData(query))
        return response

    else:
        # Otherwise we try to parse it as a single compound.
        logging.debug('Parsing the query as a single compound/enzyme')
        results = matcher.Match(query)
        template_data = {}
        template_data['compound_results'] = \
            [m for m in results if m.IsCompound()]
        template_data['enzyme_results'] = [m for m in results if m.IsEnzyme()]
        template_data['enzymes_first'] = results and results[0].IsEnzyme()
        template_data['query'] = query
        response = render(request, 'search_results.html', template_data)
        return response

    raise Http404
예제 #2
0
def main():
    config = service_config.Get()
    matcher = config.compound_matcher

    while True:
        print 'query:',
        query = raw_input()
        matches = matcher.Match(query)
        for m in matches:
            print m.key, m.value.kegg_id, m.score
예제 #3
0
def SuggestJson(request):
    """Renders the suggest JSON."""
    form = search_form.SearchForm(request.GET)
    if not form.is_valid():
        logging.error(form.errors)
        raise Http404
    
    matcher = service_config.Get().compound_matcher
    query = str(form.cleaned_query)
    matches = matcher.Match(query)
    results = [unicode(m.key) for m in matches]
    types = [m.TypeStr() for m in matches]
    json_data = json.dumps({'query': query,
                            'suggestions': results,
                            'data': types})
    
    return HttpResponse(json_data, mimetype='application/json')
예제 #4
0
def SuggestJson(request):
    """Renders the suggest JSON."""
    form = SuggestForm(request.GET)
    if not form.is_valid():
        logging.error(form.errors)
        raise Http404
    query = str(form.cleaned_query)
    suggestions = []
    if query:
        matcher = service_config.Get().search_matcher
        matches = matcher.Match(query)
        suggestions = [{
            'value': str(m.key),
            'data': {
                'cat': m.key.TypeStr()
            }
        } for m in matches]
    output = {'query': query, 'suggestions': suggestions}
    json_data = json.dumps(output)
    return HttpResponse(json_data, content_type='application/json')
def main():
    config = service_config.Get()
    exact_matcher = matcher.Matcher(min_score=0.5)

    field_names = [
        'Compound Name', 'Compound KEGG ID', 'Match Name', 'Match KEGG ID',
        'Match Score'
    ]
    writer = csv.DictWriter(open('name_matches.csv', 'w'), field_names)

    min_match_score = 0.85
    for compound in models.Compound.objects.all():
        logging.info('Finding matches for KEGG ID %s', compound.kegg_id)
        curr_id = compound.kegg_id
        for cname in compound.common_names.all():
            logging.info('Finding matches for name %s', cname.name)

            # Only find exact matches and stereoisomer names.
            queries = (cname.name, 'D-%s' % cname.name, 'L-%s' % cname.name)
            for query in queries:
                matches = exact_matcher.Match(query)
                for m in matches:
                    match_id = m.value.kegg_id
                    if curr_id == match_id:
                        continue

                    if m.score < min_match_score:
                        continue

                    row_dict = {
                        'Compound Name': cname.name,
                        'Compound KEGG ID': curr_id,
                        'Match Name': m.key,
                        'Match KEGG ID': match_id,
                        'Match Score': m.score
                    }
                    writer.writerow(row_dict)
예제 #6
0
def ResultsPage(request):
    """Renders the search results page for a given query."""
    form = search_form.SearchForm(request.GET)
    if not form.is_valid():
        raise Http404

    query_parser = service_config.Get().query_parser
    reaction_matcher = service_config.Get().reaction_matcher
    matcher = service_config.Get().compound_matcher

    query = form.cleaned_query
    ph = form.cleaned_ph
    pmg = form.cleaned_pmg
    ionic_strength = form.cleaned_ionic_strength
    template_data = {
        'query': query,
        'ph': ph,
        'pmg': pmg,
        'ionic_strength': ionic_strength
    }

    # Check if we should parse and process the input as a reaction.
    if query_parser.IsReactionQuery(query):
        try:
            parsed_reaction = query_parser.ParseReactionQuery(query)
        except Exception:
            return render_to_response('parse_error_page.html', template_data)

        reaction_matches = reaction_matcher.MatchReaction(parsed_reaction)
        best_reaction = reaction_matches.GetBestMatch()

        if not best_reaction:
            return render_to_response('search_error_page.html', template_data)

        reactants, products = best_reaction
        cprofile = concentration_profile.GetProfile()
        rxn = reaction.Reaction.FromIds(reactants,
                                        products,
                                        concentration_profile=cprofile,
                                        pH=ph,
                                        pMg=pmg,
                                        ionic_strength=ionic_strength)

        balance_with_water_link = rxn.GetBalanceWithWaterLink(query)
        balance_electrons_link = rxn.GetBalanceElectronsLink(query)
        replace_co2_link = rxn.GetReplaceCO2Link(query)
        half_reaction_link = rxn.GetHalfReactionLink(query)
        template_data.update({
            'reaction': rxn,
            'balance_with_water_link': balance_with_water_link,
            'balance_electrons_link': balance_electrons_link,
            'replace_co2_link': replace_co2_link,
            'half_reaction_link': half_reaction_link
        })
        return render_to_response('reaction_page.html', template_data)

    else:
        # Otherwise we try to parse it as a single compound.
        results = matcher.Match(query)
        compound_matches = [m for m in results if m.IsCompound()]
        enzyme_matches = [m for m in results if m.IsEnzyme()]
        template_data['compound_results'] = compound_matches
        template_data['enzyme_results'] = enzyme_matches

        enzymes_first = False
        if results and results[0].IsEnzyme():
            enzymes_first = True
        template_data['enzymes_first'] = enzymes_first

        return render_to_response('search_results.html', template_data)

    raise Http404