def post_change_log(change_log_record): db_session = orm.get_session() change_version = change_log_record.get('version') change_log_record['created'] = datetime.datetime.utcnow() try: orm_changelog = orm.models.ChangeLog(**change_log_record) except TypeError as e: err = _report_conversion_error('changelog', e, **change_log_record) return err, 400 try: db_session.add(orm_changelog) db_session.commit() except exc.IntegrityError: db_session.rollback() err = _report_object_exists( 'changelog: ' + change_log_record['version'], **change_log_record) return err, 405 except orm.ORMException as e: err = _report_write_error('changelog', e, **change_log_record) return err, 500 logger().info( struct_log(action='post_change_log', status='created', change_version=change_version, **change_log_record)) return change_log_record, 201, { 'Location': BasePath + '/changelog/' + change_version }
def _report_proxy_auth_error(key, **kwargs): """ Generate standard log message for warning: API access without :param **kwargs: arbitrary keyword parameters """ message = 'Attempt to access with invalid proxy/api key: ' + key logger().warning(struct_log(action=message, **kwargs))
def post_expression(expression_record): db_session = orm.get_session() if expression_record.get('__filepath__'): file_path = expression_record['__filepath__'] if not os.path.isfile(file_path): err = Error(message="Invalid file path: " + file_path, code=400) return err, 400 else: err = Error(message="An absolute __filepath__ is required", code=400) return err, 400 if not expression_record.get('id'): iid = uuid.uuid1() expression_record['id'] = iid else: iid = expression_record['id'] if not expression_record.get('version'): expression_record['version'] = Version if not expression_record.get('URL'): base_url = app.config.get('BASE_DL_URL') + BasePath expression_record[ 'URL'] = base_url + '/expressions/download/' + os.path.basename( file_path) expression_record['created'] = datetime.datetime.utcnow() try: orm_expression = orm.models.File(**expression_record) except TypeError as e: err = _report_conversion_error('file', e, **expression_record) return err, 400 try: db_session.add(orm_expression) db_session.commit() except exc.IntegrityError: db_session.rollback() err = _report_object_exists('expression: ' + expression_record['URL'], **expression_record) return err, 405 except orm.ORMException as e: db_session.rollback() err = _report_write_error('expression', e, **expression_record) return err, 500 logger().info( struct_log(action='post_expression', status='created', expression_id=str(iid), **expression_record)) return expression_record, 201, { 'Location': BasePath + '/expressions/' + str(iid) }
def _report_object_exists(typename, **kwargs): """ Generate standard log message + request error for warning: Trying to POST an object that already exists :param typename: name of type involved :param **kwargs: arbitrary keyword parameters :return: Connexion Error() type to return """ report = typename + 'already exists' logger().warning(struct_log(action=report, **kwargs)) return Error(message=report, code=405)
def _report_conversion_error(typename, exception, **kwargs): """ Generate standard log message + request error for warning: Trying to POST an object that already exists :param typename: name of type involved :param exception: exception thrown by ORM :param **kwargs: arbitrary keyword parameters :return: Connexion Error() type to return """ report = 'Could not convert ' + typename + ' to ORM model' message = typename + ': failed validation - could not convert to internal representation' logger().error( struct_log(action=report, exception=str(exception), **kwargs)) return Error(message=message, code=400)
def _report_search_failed(typename, exception, **kwargs): """ Generate standard log message + request error for error: Internal error performing search :param typename: name of type involved :param exception: exception thrown by ORM :param **kwargs: arbitrary keyword parameters :return: Connexion Error() type to return """ report = typename + ' search failed' message = 'Internal error searching for ' + typename + 's' logger().error( struct_log(action=report, exception=str(exception), **kwargs)) return Error(message=message, code=500)
def _report_write_error(typename, exception, **kwargs): """ Generate standard log message + request error for error: Error writing to DB :param typename: name of type involved :param exception: exception thrown by ORM :param **kwargs: arbitrary keyword parameters :return: Connexion Error() type to return """ report = 'Internal error writing ' + typename + ' to DB' message = typename + ': internal error saving ORM object to DB' logger().error( struct_log(action=report, exception=str(exception), **kwargs)) err = Error(message=message, code=500) return err
def slice_expression_data(expr, sampleIDList, featureIDList, featureNameList, minExpression, maxExpression, file_type): """ Performs the slicing on each expression file :param threshold_input_type: :return: temporary file response object """ tmp_dir = app.config.get('TMP_DIRECTORY') output_file_id = uuid.uuid1() output_filepath = tmp_dir + str(output_file_id) + '.' + file_type feature_map = pkg_resources.resource_filename( 'candig_rnaget', 'expression/feature_mapping_HGNC.tsv') try: h5query = ExpressionQueryTool(expr.__filepath__, output_filepath, include_metadata=False, output_type=file_type, feature_map=feature_map) if minExpression or maxExpression: q = h5query.search_threshold(min_ft=minExpression, max_ft=maxExpression, feature_id_list=featureIDList, feature_name_list=featureNameList, samples=sampleIDList) else: q = h5query.search(samples=sampleIDList, feature_id_list=featureIDList, feature_name_list=featureNameList) h5query.close() # HDF5 file error except OSError as err: logger().warning(struct_log(action=str(err))) return None # Given feature list or sample ID does not contain any valid entries except LookupError as err: logger().warning(struct_log(action=str(err))) return None return generate_file_response(q, file_type, output_file_id, expr.studyID, expr.units)
def post_project(project_record): db_session = orm.get_session() if not project_record.get('id'): iid = uuid.uuid1() project_record['id'] = iid else: iid = project_record['id'] if not project_record.get('version'): project_record['version'] = Version project_record['created'] = datetime.datetime.utcnow() try: orm_project = orm.models.Project(**project_record) except TypeError as e: err = _report_conversion_error('project', e, **project_record) return err, 400 try: db_session.add(orm_project) db_session.commit() except exc.IntegrityError: db_session.rollback() err = _report_object_exists('project: ' + project_record['id'], **project_record) return err, 405 except orm.ORMException as e: db_session.rollback() err = _report_write_error('project', e, **project_record) return err, 500 logger().info( struct_log(action='post_project', status='created', project_id=str(iid), **project_record)) return project_record, 201, { 'Location': BasePath + '/projects/' + str(iid) }
def post_study(body): db_session = orm.get_session() if not body.get('id'): iid = uuid.uuid1() body['id'] = iid else: iid = body['id'] if not body.get('version'): body['version'] = Version body['created'] = datetime.datetime.utcnow() try: orm_study = orm.models.Study(**body) except TypeError as e: err = _report_conversion_error('study', e, **body) return err, 400 try: db_session.add(orm_study) db_session.commit() except exc.IntegrityError: db_session.rollback() err = _report_object_exists('study: ' + body['id'], **body) return err, 405 except orm.ORMException as e: db_session.rollback() err = _report_write_error('study', e, **body) return err, 500 logger().info( struct_log(action='post_study', status='created', project_id=str(iid), **body)) return body, 201, {'Location': BasePath + '/studies/' + str(iid)}
def slice_expression_data(expr, sampleID, featureIDList, featureNameList, minExpression, maxExpression, file_type, threshold_label='name', threshold_input_type='array'): """ Performs the slicing on each expression file :param threshold_input_type: :return: temporary file response object """ tmp_dir = app.config.get('TMP_DIRECTORY') output_file_id = uuid.uuid1() output_filepath = tmp_dir + str(output_file_id) + '.' + file_type feature_map = pkg_resources.resource_filename( 'candig_rnaget', 'expression/feature_mapping_HGNC.tsv') try: h5query = ExpressionQueryTool(expr.__filepath__, output_filepath, include_metadata=False, output_type=file_type, feature_map=feature_map) if sampleID or featureIDList or featureNameList: q = h5query.search(sample_id=sampleID, feature_list_id=featureIDList, feature_list_name=featureNameList) elif minExpression: if threshold_input_type == 'object': if validate_threshold_object( minExpression[0])[0] == 'featureName': threshold_label = 'name' else: threshold_label = 'id' threshold_array = convert_threshold_array( minExpression, input_format=threshold_input_type) q = h5query.search_threshold(threshold_array, ft_type='min', feature_label=threshold_label) else: if threshold_input_type == 'object': if validate_threshold_object( maxExpression[0])[0] == 'featureName': threshold_label = 'name' else: threshold_label = 'id' threshold_array = convert_threshold_array( maxExpression, input_format=threshold_input_type) q = h5query.search_threshold(threshold_array, ft_type='max', feature_label=threshold_label) h5query.close() # HDF5 file error except OSError as err: logger().warning(struct_log(action=str(err))) return None # Given feature list or sample ID does not contain any valid entries except LookupError as err: logger().warning(struct_log(action=str(err))) return None return generate_file_response(q, file_type, output_file_id, expr.studyID, expr.units)