def docking(): """Renders the predict page and makes predictions if the method is POST.""" if request.method == 'GET': return render_docking() # Create and modify args parser = ArgumentParser() add_predict_args(parser) args = parser.parse_args([]) # Get arguments ckpt_id = request.form['checkpointName'] app.config[ 'TEMP_FOLDER'] = '/work01/home/yjxu/documents/A1_IIPharam_project/PKUMDL_ChemAI/test_space' args.test_path = app.config['TEMP_FOLDER'] if request.form['textSmiles'] != '': smiles = request.form['textSmiles'].split() elif request.form['drawSmiles'] != '': smiles = [request.form['drawSmiles']] else: print(" GOT HERE") # Upload data file with SMILES data = request.files['data'] data_name = secure_filename(data.filename) data_path = os.path.join(app.config['TEMP_FOLDER'], "raw", data_name) data.save(data_path) args.preds_path = os.path.join("prediction", data_name) args.data_name = data_name.split(".")[0] app.config['PREDICTIONS_FILENAME'] = os.path.join( "prediction", data_name) # Check if header is smiles possible_smiles = get_header(data_path)[0] smiles = [possible_smiles ] if Chem.MolFromSmiles(possible_smiles) is not None else [] print(data_path) # Get remaining smiles smiles.extend(get_smiles(data_path)) print(smiles) models = db.get_models(ckpt_id) model_paths = [ os.path.join(app.config['CHECKPOINT_FOLDER'], f'{model["id"]}.pt') for model in models ] task_names = load_task_names(model_paths[0]) num_tasks = len(task_names) gpu = request.form.get('gpu') args.checkpoint_paths = model_paths if gpu is not None: if gpu == 'None': args.no_cuda = True else: args.gpu = int(gpu) modify_predict_args(args) # Run predictions preds = make_predictions(args) if all(p is None for p in preds): return render_docking(errors=['All SMILES are invalid']) # Replace invalid smiles with message invalid_smiles_warning = "Invalid SMILES String" preds = [ pred if pred is not None else [invalid_smiles_warning] * num_tasks for pred in preds ] return render_docking( predicted=True, smiles=smiles, num_smiles=len(smiles), show_more=0, task_names=task_names, num_tasks=len(task_names), preds=preds, warnings=["List contains invalid SMILES strings"] if None in preds else None, errors=["No SMILES strings given"] if len(preds) == 0 else None)
def predict_old(): """Renders the predict page and makes predictions if the method is POST. @@@ #### example ``` curl -X POST url -F 'checkpointName=<checkpointName option>' -F 'textSmiles=<smiles>' curl -X POST url -F 'checkpointName=<checkpointName option>' -F 'textSmiles=' -F 'drawSmiles=<drawSmilesInput>' curl -X POST url -F 'checkpointName=<checkpointName option>' -F 'textSmiles=' -F 'drawSmiles=' -F 'data=@<fileSmilesInput>' ``` #### return - ##### predict.html @@@ """ if request.method == 'GET': return render_predict() ckpt_id = request.form['checkpointName'] app.config['TEMP_FOLDER'] = './web/web_log' if request.form['textSmiles'] != '': smiles = request.form['textSmiles'].split() elif request.form['drawSmiles'] != '': smiles = [request.form['drawSmiles']] else: # Upload data file with SMILES data = request.files['data'] data_name = secure_filename(data.filename) data_path = os.path.join(app.config['TEMP_FOLDER'], data_name) data.save(data_path) # Check if header is smiles possible_smiles = get_header(data_path)[0] smiles = [possible_smiles ] if Chem.MolFromSmiles(possible_smiles) is not None else [] # Get remaining smiles smiles.extend(get_smiles(data_path)) # Create and modify args parser = ArgumentParser() add_predict_args(parser) args = parser.parse_args([]) args.test_path = app.config['TEMP_FOLDER'] args.data_name = data_name.split(".")[0] gpu = request.form.get('gpu') model = db.get_models(ckpt_id)[0] model_path = os.path.join(app.config['CHECKPOINT_FOLDER'], f'{model["id"]}.pt') task_names = load_task_names(model_path) num_tasks = len(task_names) preds_path = os.path.join(app.config['TEMP_FOLDER'], "predict", app.config['PREDICTIONS_FILENAME']) args.preds_path = preds_path args.checkpoint_paths = model_path if gpu is not None: if gpu == 'None': args.no_cuda = True else: args.gpu = int(gpu) train_args = load_args(model_path) for key, value in vars(train_args).items(): if not hasattr(args, key): setattr(args, key, value) modify_predict_args(args) # Run predictions preds = make_predictions(args) if all(p is None for p in preds): return render_predict(errors=['All SMILES are invalid']) # Replace invalid smiles with message invalid_smiles_warning = "Invalid SMILES String" preds = [ pred if pred is not None else [invalid_smiles_warning] * num_tasks for pred in preds ] print(preds) return render_predict( predicted=True, smiles=smiles, num_smiles=min(10, len(smiles)), show_more=max(0, len(smiles) - 10), task_names=task_names, num_tasks=len(task_names), preds=preds, warnings=["List contains invalid SMILES strings"] if None in preds else None, errors=["No SMILES strings given"] if len(preds) == 0 else None)
def generate(): """Renders the generate page Content-Type: application/json args: {"checkpointName": "igm_char_rnn, igm_atom_rnn, igm_frag_vae","n_sample_per_model" : "1000", "filter": "None" or "MW_0_1, logP_0_4, SAS_0_5, QED_0_1" } return: json {"igm_char_rnn": "path_to_charrnn_file", "igm_atom_rnn": "path_to_atomrnn_file","igm_frag_vae": "path_to_fragvae_file", "filtered": "None" or "path_to_filter_file"} or generate.html @@@ #### example ``` curl -H "Content-Type: application/json" -X POST --data 'args' ``` """ if request.method == 'GET': return render_generate() # Get arguments print(request.is_json) content = request.get_json() print(content) model_names = content["checkpointName"] num_sample = content["n_sample_per_model"] isfilter = content["filter"] return jsonify({ "igm_char_rnn": "path_to_charrnn_file", "igm_atom_rnn": "path_to_atomrnn_file", "igm_frag_vae": "path_to_fragvae_file", "filtered": "None or path_to_filter_file" }) ckpt_id = request.form['checkpointName'] app.config['TEMP_FOLDER'] = 'tmp_log_web/generate' args.test_path = app.config['TEMP_FOLDER'] if request.form['textSmiles'] != '': smiles = request.form['textSmiles'].split() elif request.form['drawSmiles'] != '': smiles = [request.form['drawSmiles']] else: print(" GOT HERE") # Upload data file with SMILES data = request.files['data'] data_name = secure_filename(data.filename) data_path = os.path.join(app.config['TEMP_FOLDER'], "raw", data_name) data.save(data_path) args.preds_path = os.path.join("prediction", data_name) args.data_name = data_name.split(".")[0] app.config['PREDICTIONS_FILENAME'] = os.path.join( "prediction", data_name) # Check if header is smiles possible_smiles = get_header(data_path)[0] smiles = [possible_smiles ] if Chem.MolFromSmiles(possible_smiles) is not None else [] print(data_path) # Get remaining smiles smiles.extend(get_smiles(data_path)) print(smiles) models = db.get_models(ckpt_id) model_paths = [ os.path.join(app.config['CHECKPOINT_FOLDER'], f'{model["id"]}.pt') for model in models ] task_names = load_task_names(model_paths[0]) num_tasks = len(task_names) gpu = request.form.get('gpu') args.checkpoint_paths = model_paths if gpu is not None: if gpu == 'None': args.no_cuda = True else: args.gpu = int(gpu) modify_predict_args(args) # Run predictions preds = make_predictions(args) if all(p is None for p in preds): return render_generate(errors=['All SMILES are invalid']) # Replace invalid smiles with message invalid_smiles_warning = "Invalid SMILES String" preds = [ pred if pred is not None else [invalid_smiles_warning] * num_tasks for pred in preds ] return render_generate( predicted=True, smiles=smiles, num_smiles=len(smiles), show_more=0, task_names=task_names, num_tasks=len(task_names), preds=preds, warnings=["List contains invalid SMILES strings"] if None in preds else None, errors=["No SMILES strings given"] if len(preds) == 0 else None)
def predict(): """Renders the predict page and makes predictions if the method is POST. Content-Type: multipart/form-data @@@ #### example ``` curl -X POST url -F 'checkpointName=<checkpointName option>' -F 'textSmiles=<smiles>' curl -X POST url -F 'checkpointName=<checkpointName option>' -F 'textSmiles=' -F 'drawSmiles=<drawSmilesInput>' curl -X POST url -F 'checkpointName=<checkpointName option>' -F 'textSmiles=' -F 'drawSmiles=' -F 'data=@<fileSmilesInput>' or -F "usejson=True" ``` #### return - ##### predict.html - ##### json data if usejson is True @@@ """ if request.method == 'GET': return render_predict() # Create and modify args parser = ArgumentParser() add_predict_args(parser) args = parser.parse_args([]) # Get arguments ckpt_id = request.form['checkpointName'] print(ckpt_id) args.test_path = app.config['TEMP_FOLDER'] if request.form['textSmiles'] != '': smiles = request.form['textSmiles'].split() elif request.form['drawSmiles'] != '': smiles = [request.form['drawSmiles']] else: print(" GOT HERE") # Upload data file with SMILES data = request.files['data'] # print(data) data_name = secure_filename(data.filename) data_path = os.path.join(app.config['TEMP_FOLDER'], "raw", data_name) data.save(data_path) args.preds_path = os.path.join("prediction", data_name) args.data_name = data_name.split(".")[0] app.config['PREDICTIONS_FILENAME'] = args.preds_path # Check if header is smiles possible_smiles = get_header(data_path)[0] smiles = [possible_smiles ] if Chem.MolFromSmiles(possible_smiles) is not None else [] # print(data_path) # Get remaining smiles smiles.extend(get_smiles(data_path)) # print(smiles) models = db.get_models(ckpt_id) model_paths = [ os.path.join(app.config['CHECKPOINT_FOLDER'], f'{model["id"]}.pt') for model in models ] task_names = load_task_names(model_paths[0]) num_tasks = len(task_names) gpu = request.form.get('gpu') args.checkpoint_paths = model_paths if gpu is not None: if gpu == 'None': args.no_cuda = True else: args.gpu = int(gpu) modify_predict_args(args) # Run predictions preds = make_predictions(args) if request.form['usejson']: preds_arr = np.array(preds) jsondata = {} jsondata["smiles"] = smiles for i, task_name in enumerate(task_names): jsondata[task_name] = list(preds_arr[:, i]) return jsonify(jsondata) if all(p is None for p in preds): return render_predict(errors=['All SMILES are invalid']) # Replace invalid smiles with message invalid_smiles_warning = "Invalid SMILES String" preds = [ pred if pred is not None else [invalid_smiles_warning] * num_tasks for pred in preds ] return render_predict( predicted=True, smiles=smiles, num_smiles=len(smiles), show_more=0, task_names=task_names, num_tasks=len(task_names), preds=preds, warnings=["List contains invalid SMILES strings"] if None in preds else None, errors=["No SMILES strings given"] if len(preds) == 0 else None)