Example #1
0
def ajax_start_synth(request):
    '''Perform the forward synthesis'''
    data = {'err': False}

    reactants = request.GET.get('reactants', None)
    solvent = request.GET.get('solvent', None)
    temperature = request.GET.get('temperature', None)
    reagents = request.GET.get('reagents', None)
    mincount = int(request.GET.get('mincount', 25))
    maxreturn = int(request.GET.get('maxreturn', 100))
    forward_scorer = request.GET.get('forward_scorer', 'Template_Free')
    print('Conditions for forward synthesis:')
    print('reactants: {}'.format(reactants))
    print('solvent: {}'.format(solvent))
    print('temp: {}'.format(temperature))
    print('reagents: {}'.format(reagents))
    print('mincount: {}'.format(mincount))
    print('max return: {}'.format(maxreturn))
    print('forward scorer: {}'.format(forward_scorer))

    startTime = time.time()

    # context expected is (T1, slvt1, rgt1, cat1, t1, y1)
    if solvent == 'default':
        solvent = ''
        print('reset default solvent')

    res = evaluate.delay(
        reactants,
        '',
        contexts=[clean_context((temperature, solvent, reagents, '', -1, -1))],
        forward_scorer=forward_scorer,
        top_n=maxreturn,
        return_all_outcomes=True)
    outcomes = res.get(300)[0]['outcomes']

    print('Got top outcomes, length {}'.format(len(outcomes)))
    #print(outcomes)
    data['html_time'] = '{:.3f} seconds elapsed'.format(time.time() -
                                                        startTime)

    if outcomes:

        data['html'] = render_to_string('synth_outcomes_only.html',
                                        {'outcomes': outcomes})
    else:
        data['html'] = 'No outcomes found? That is weird...'

    # Save in session in case used wants to print
    request.session['last_synth_interactive'] = {
        'reactants': reactants,
        'temperature': temperature,
        'reagents': reagents,
        'solvent': solvent,
        'mincount': mincount,
        'outcomes': outcomes,
        'forward_scorer': forward_scorer
    }

    return JsonResponse(data)
Example #2
0
def template_free(request):
    resp = {}
    resp['request'] = dict(**request.GET)
    reactants = request.GET.get('reactants')
    solvent = request.GET.get('solvent', '')
    reagents = request.GET.get('reagents', '')
    num_results = int(request.GET.get('num_results', 100))
    contexts = [clean_context((None, solvent, reagents, '', -1, -1))]

    if not reactants:
        resp['error'] = 'Required parameter "reactants" missing'
        return JsonResponse(resp, status=400)

    rmol = Chem.MolFromSmiles(reactants)
    if not rmol:
        resp['error'] = 'Cannot parse reactants smiles with rdkit'
        return JsonResponse(resp, status=400)

    smol = Chem.MolFromSmiles(contexts[0][1])
    if not smol:
        resp['error'] = 'Cannot parse solvent smiles with rdkit'
        return JsonResponse(resp, status=400)

    remol = Chem.MolFromSmiles(contexts[0][2])
    if not remol:
        resp['error'] = 'Cannot parse reagents smiles with rdkit'
        return JsonResponse(resp, status=400)

    res = evaluate.delay(reactants,
                         '',
                         contexts=contexts,
                         forward_scorer='Template_Free',
                         top_n=num_results,
                         return_all_outcomes=True)

    try:
        outcomes = res.get(TIMEOUT)
    except TimeoutError:
        resp['error'] = 'API request timed out (limit {}s)'.format(TIMEOUT)
        res.revoke()
        return JsonResponse(resp, status=408)
    except Exception as e:
        resp['error'] = str(e)
        res.revoke()
        return JsonResponse(resp, status=400)

    outcomes = outcomes[0]['outcomes']
    for out in outcomes:
        o = out.pop('outcome')
        out['smiles'] = o['smiles']
    resp['outcomes'] = outcomes
    return JsonResponse(resp)