def calculateTask(task, sid, model):
    try:
        generate_mol_image(task)
        suite = SuiteTask.objects.get(sid=sid)
        map_model_name = get_model_name(model['model'])
        smile = task.file_obj.smiles.encode(
            'utf-8') if task.file_obj.file_type != 'mol' else ''

        # smile, mol_fpath 输入只选择一种方式(优先smile)
        mol_fpath = os.path.join(
            settings.SETTINGS_ROOT,
            task.file_obj.file_obj.path) if not smile else None

        try:
            temperature = float(model.get('temperature'))
        except:
            temperature = DEFAULT_TEMPERATURE_ARGS

        chemistry_logger.info(
            'PredictionModel calculating: model name(%s),'
            'smile(%s) mol path(%s) temperature(%s)', map_model_name, smile,
            mol_fpath, temperature)
        # 后台模型计算入口
        predict_results = prediction_model_calculate(map_model_name, smile,
                                                     mol_fpath, temperature)

        if task.file_obj.file_type == 'mol':
            name = os.path.basename(mol_fpath).split('.')[0]
        else:
            name = smile
        result = predict_results[name][map_model_name]
        chemistry_logger.info('[task]result %s' % result)
    except KeyError:
        chemistry_logger.exception('still cannot support this model')
        result = None
        task.result_state = "We don't support this model now"
        task.status = StatusCategory.objects.get(category=STATUS_FAILED)
        suite.status_id = StatusCategory.objects.get(category=STATUS_FAILED)
    except Exception as e:
        chemistry_logger.exception('failed to submit task to prediction model')
        result = None
        task.result_state = str(e)
        task.status = StatusCategory.objects.get(category=STATUS_FAILED)
        suite.status_id = StatusCategory.objects.get(category=STATUS_FAILED)
    else:
        chemistry_logger.info("calculate Successfully in celery queue!")
        task.result_state = "Calculate Successfully!"
        task.status = StatusCategory.objects.get(category=STATUS_SUCCESS)
        suite.status_id = StatusCategory.objects.get(category=STATUS_WORKING)

    task.end_time = utils.get_real_now()
    task.results = json.dumps(result)

    suite.save()
    task.save()

    add_counter(suite.sid)

    return result
Esempio n. 2
0
def get_model_category(model_name):
    try:
        category = ModelCategory.objects.get(category=model_name).\
            origin_type.get_category_display()
    except Exception:
        chemistry_logger.exception('failed to get model category')
        category = ""

    return category
Esempio n. 3
0
def calculateTask(task, sid, model):
    try:
        generate_mol_image(task)
        suite = SuiteTask.objects.get(sid=sid)
        map_model_name = get_model_name(model['model'])
        smile = task.file_obj.smiles.encode('utf-8') if task.file_obj.file_type != 'mol' else ''

        # smile, mol_fpath 输入只选择一种方式(优先smile)
        mol_fpath = os.path.join(settings.SETTINGS_ROOT, task.file_obj.file_obj.path) if not smile else None

        try:
            temperature = float(model.get('temperature'))
        except:
            temperature = DEFAULT_TEMPERATURE_ARGS

        chemistry_logger.info('PredictionModel calculating: model name(%s),'
                              'smile(%s) mol path(%s) temperature(%s)',
                              map_model_name, smile, mol_fpath, temperature)
        # 后台模型计算入口
        predict_results = prediction_model_calculate(map_model_name, smile,
                                                     mol_fpath, temperature)

        if task.file_obj.file_type == 'mol':
            name = os.path.basename(mol_fpath).split('.')[0]
        else:
            name = smile
        result = predict_results[name][map_model_name]
        chemistry_logger.info('[task]result %s' % result)
    except KeyError:
        chemistry_logger.exception('still cannot support this model')
        result = None
        task.result_state = "We don't support this model now"
        task.status = StatusCategory.objects.get(category=STATUS_FAILED)
        suite.status_id = StatusCategory.objects.get(category=STATUS_FAILED)
    except Exception as e:
        chemistry_logger.exception('failed to submit task to prediction model')
        result = None
        task.result_state = str(e)
        task.status = StatusCategory.objects.get(category=STATUS_FAILED)
        suite.status_id = StatusCategory.objects.get(category=STATUS_FAILED)
    else:
        chemistry_logger.info("calculate Successfully in celery queue!")
        task.result_state = "Calculate Successfully!"
        task.status = StatusCategory.objects.get(category=STATUS_SUCCESS)
        suite.status_id = StatusCategory.objects.get(category=STATUS_WORKING)

    task.end_time = utils.get_real_now()
    task.results = json.dumps(result)

    suite.save()
    task.save()

    add_counter(suite.sid)

    return result
Esempio n. 4
0
def suite_task_context(sid):
    try:
        suite_task = SuiteTask.objects.get(sid=sid)
    except Exception:
        chemistry_logger.exception('failed to get suite task: %s' % sid)
        suite_task = None
        single_task_lists = []
    else:
        single_task_lists = SingleTask.objects.filter(sid=sid)

    return dict(suite_task=suite_task,
                single_task_lists=single_task_lists)
Esempio n. 5
0
def single_task_context(pid):
    try:
        single_task = SingleTask.objects.get(pid=pid)
        local_search_id = single_task.file_obj.local_search_id
        if local_search_id and isinstance(local_search_id, int):
            local_search = ChemInfoLocal.objects.get(local_search_id)
        else:
            local_search = None
    except Exception:
        chemistry_logger.exception('failed to get single task: %s' % pid)
        single_task = None
        local_search = None

    return dict(single_task=single_task, search_engine=local_search)
Esempio n. 6
0
def generate_calculate_task(models, smile, draw_mol_data, files_id_list,
                            sid, local_search_id):
    from chemistry.tasks import send_email_task
    try:
        for model in models:
            handle_smile_task(smile, model, sid, local_search_id)
            handle_moldraw_task(draw_mol_data, model, sid)
            handle_files_task(files_id_list, model, sid)
    except Exception:
        chemistry_logger.exception('failed to generate suite_task:%s' % sid)
        s = SuiteTask.objects.get(sid=sid)
        s.end_time = utils.get_real_now() 
        s.status_id = StatusCategory.objects.get(category=STATUS_FAILED)
        s.save()
        send_email_task(s.email, s.sid)
Esempio n. 7
0
def submit_calculate_task(user, smile=None, draw_mol_data=None,
                          task_notes=None, task_name=None,
                          files_id_list=None, models=None,
                          local_search_id=None):

    chemistry_logger.info("smile: %s" % smile)
    chemistry_logger.info("draw_mol_data: %s" % draw_mol_data)
    chemistry_logger.info("files_id_list: %s" % files_id_list)
    chemistry_logger.info("models: %s" % models)

    tasks_num = calculate_tasks(files_id_list, smile, draw_mol_data, models)

    if tasks_num == 0:
        status = False
        info = "请至少选择一种输入方式和计算模型!"
        id = None
        return (status, info, id)

    try:
        s = SuiteTask()
        s.sid = id = str(uuid.uuid4())
        s.user = UserProfile.objects.get(user=user)
        s.total_tasks = tasks_num
        s.has_finished_tasks = 0
        s.start_time = utils.get_real_now()
        s.name = task_name
        s.notes = task_notes
        s.models_str, s.models_category_str = parse_models(models)
        s.status = StatusCategory.objects.get(category=STATUS_WORKING)
        s.email = user.email
        s.save()
        
        chemistry_logger.info('~~~~~~~~ s:%s' % s.start_time)

        generate_calculate_task.delay(models, smile, draw_mol_data,
                                      files_id_list, id, local_search_id)
    except:
        chemistry_logger.exception('failed to generate suite_task')
        s.delete()
        status = False
        info = "计算任务添加不成功,将重试或联系网站管理员!"
        id = None
    else:
        status = True
        info = "恭喜,计算任务已经提交!"

    return (status, info, id)
Esempio n. 8
0
def singletask_details(pid):
    from chemistry.tasks import has_temperature
    single_task = get_object_or_404(SingleTask, pid=pid)
    if not has_temperature(single_task.model.desc):
        single_task.temperature = '--'
    single_task.result_value, single_task.hi, single_task.hx, single_task.degrade = get_singletask_area(single_task.results)
    try:
        local_search_id = single_task.file_obj.local_search_id
        if local_search_id:
            local_search = ChemInfoLocal.objects.get(id=local_search_id)
        else:
            local_search = None
    except Exception:
        chemistry_logger.exception('failed to get cheminfo by local_search_id')
        local_search = None

    return dict(singletask=single_task,
                search_engine=local_search)
Esempio n. 9
0
def fetch_ehomo_by_mopac(name, model):
    path = os.path.join(config.MOPAC_PATH, model, name, '%s.out' % name)
    if not os.path.exists(path):
        chemistry_logger.error('Cannot fetch H**O %s by mopac' % path)
        return 0.0

    with open(path, 'r') as f:
        for line in f.readlines():
            if EHOMO_MOPAC_RE.search(line):
                try:
                    v = line.split('=')[1].strip().strip('\t').split()[0]
                    v = float(v)
                except:
                    chemistry_logger.exception('Cannot get EHOMO value %s ' % line)
                    v = 0.0
                finally:
                    return v

    chemistry_logger.error('%s no H**O' % path)
    return 0.0
Esempio n. 10
0
def fetch_ehomo(name, model):
    path = os.path.join(config.GAUSSIAN_PATH, model, name, '%s.log' % name)

    if not os.path.exists(path):
        chemistry_logger.error('Cannot fetch H**O %s' % path)
        return 0.0

    ret = []

    with open(path, 'r') as f:
        for line in f.readlines():
            if EHOMO_RE.search(line):
                chemistry_logger.info(line)
                ret.append(line)
    try:
        v = float(ret[-1].split()[-1])
    except Exception as e:
        chemistry_logger.exception('Cannot get EHOMO value %s ' % line)
        v = 0

    return v
Esempio n. 11
0
def fetch_ehomo_by_mopac(name, model):
    path = os.path.join(config.MOPAC_PATH, model, name, '%s.out' % name)
    if not os.path.exists(path):
        chemistry_logger.error('Cannot fetch H**O %s by mopac' % path)
        return 0.0

    with open(path, 'r') as f:
        for line in f.readlines():
            if EHOMO_MOPAC_RE.search(line):
                try:
                    v = line.split('=')[1].strip().strip('\t').split()[0]
                    v = float(v)
                except:
                    chemistry_logger.exception('Cannot get EHOMO value %s ' %
                                               line)
                    v = 0.0
                finally:
                    return v

    chemistry_logger.error('%s no H**O' % path)
    return 0.0
Esempio n. 12
0
def fetch_ehomo(name, model):
    path = os.path.join(config.GAUSSIAN_PATH, model, name, '%s.log' % name)

    if not os.path.exists(path):
        chemistry_logger.error('Cannot fetch H**O %s' % path)
        return 0.0

    ret = []

    with open(path, 'r') as f:
        for line in f.readlines():
            if EHOMO_RE.search(line):
                chemistry_logger.info(line)
                ret.append(line)
    try:
        v = float(ret[-1].split()[-1])
    except Exception as e:
        chemistry_logger.exception('Cannot get EHOMO value %s ' % line)
        v = 0

    return v