def get_predictions(db, result_uuid, sort_max_value, limit, offset): """ Get predictions of a result with specified properties. :param db: Database Connection :param result_uuid: str: uuid of the custom_result to search. :param sort_max_value: boolean: true if we should sort by max prediction else sort by seq idx. :param limit: int: how many predictions to return :param offset: int: offset into results to allow paging :return: [dict]: list of prediction properties """ result = [] query, params = CustomResultData.get_prediction_query_and_params(result_uuid, sort_max_value, limit, offset) for row in read_database(db, query, params): name, max_value, pred, sequence = row if not sequence: sequence = SEQUENCE_NOT_FOUND if CustomResultData.is_none_prediction_values(pred): pred = [] result.append({ 'name': name, 'max': str(max_value), 'values': pred, 'sequence': sequence }) return result
def find(db, sequence_id, model_name): """ Find custom results with the sequence_id and optionally model_name. :param db: Database Connection :param sequence_id: str: uuid of the custom sequence to search for :param model_name: str: name of the model to search for, None if for all model names :return: [dict]: array of custom result info """ try: val = uuid.UUID(sequence_id, version=4) except ValueError: raise ClientException(message="Sequence id is not a valid uuid", error_type=ErrorType.INVALID_SEQUENCE_ID) select_sql = "select custom_result.id, custom_result.model_name from custom_result " \ " inner join job on job.id = job_id " \ " where seq_id = %s" params = [sequence_id] if model_name: select_sql += " and custom_result.model_name = %s" params.append(model_name) result = [] for row in read_database(db, select_sql, params): values = { 'resultId': row[0], 'modelName': row[1], 'sequenceId': sequence_id, } result.append(values) return result
def bed_file_contents(db, result_id): select_sql = "select name, start, stop, value from custom_result_row " \ " where result_id = %s" result = "" for row in read_database(db, select_sql, [result_id]): name, start, stop, value = row line = '\t'.join([name, str(start), str(stop), str(value)]) result += line + '\n' return result
def load(self, db): """ Load self.contents from the database based on self.seq_uuid. :param db: database connection """ rows = read_database(db, "select data, created, title from sequence_list where id = %s", [self.seq_uuid]) if not rows: raise KeyError("Unable to find sequence for {}".format(self.seq_uuid)) first_row = rows[0] self.content = first_row[0] self.created = first_row[1] self.title = first_row[2]
def determine_last_page(db, result_uuid, per_page): """ Determine what the last page in for the specified result_uuid given per_page :param db: database connection :param result_uuid: str: uuid of the custom result to query :param per_page: int: how many items are there per page :return: int: which page is the last """ query, params = CustomResultData.last_page_query_and_params(result_uuid) rows = read_database(db, query, params) first_row = rows[0] items = float(first_row[0]) return int(math.ceil(items / per_page))
def find_one(db, sequence_id, model_name): """ Find a single custom result for the specified sequence :param db: Database Connection :param sequence_id: str: uuid of the custom sequence to search for :param model_name: str: name of the model to search for :return: int value of custom_result or None if not found """ select_sql = "select custom_result.id from custom_result " \ " inner join job on job.id = job_id " \ " where seq_id = %s and custom_result.model_name = %s" for row in read_database(db, select_sql, [sequence_id, model_name]): return row[0] return None
def find_one(db, sequence_id, model_name): """ Find a single custom result for the specified sequence and model :param db: Database Connection :param sequence_id: str: uuid of the custom sequence to search for :param model_name: str: name of the model to search for :return: int value of custom_result or None if not found """ select_sql = "select custom_result.id from custom_result " \ " inner join job on job.id = job_id " \ " where seq_id = %s and custom_result.model_name = %s" for row in read_database(db, select_sql, [sequence_id, model_name]): return row[0] return None
def find_existing_job(db, job_type, sequence_list, model_name): """ Find a single job for the specified properties. :param db: DatabaseConnection: database to read :param job_type: str: type of job we are looking for :param sequence_list: str: uuid of sequence :param model_name: str: name of the model :return: CustomJob or None if not found """ select_sql = "select id, {} from job " \ " WHERE seq_id = %s and type = %s and model_name = %s".format(CustomJob.NON_KEY_FIELDS) params = [sequence_list, job_type, model_name] for row in read_database(db, select_sql, params): job = CustomJob(row[0]) job._load_non_key(row[1:]) return job return None
def custom_result_sequence_lookup(db, result_id): """ Returns name to DNA Sequence dictionary based on the sequence used in a custom result :param db: Database Connection :param result_id: uuid of the result we want to lookup :return: dict: name -> DNA sequence(str) """ select_sql = "select sequence_list_item.name, sequence_list_item.sequence " \ "from custom_result " \ "inner join job on job.id = job_id " \ "inner join sequence_list_item on sequence_list_item.seq_id = job.seq_id " \ "where custom_result.id = %s;" name_to_dna_seq = {} for row in read_database(db, select_sql, [result_id]): name, sequence = row name_to_dna_seq[name] = sequence return name_to_dna_seq
def bed_file_contents(db, result_id): """ Returns bed file contents given a custom result id. Bed file is just tsv with columns name, start, stop, value, dna sequence :param db: Database Connection :param result_id: uuid of the result we want to lookup :return: str: bed file contents """ sequence_lookup = CustomResultData.custom_result_sequence_lookup(db, result_id) select_sql = "select name, start, stop, value from custom_result_row " \ " where result_id = %s" result = "" for row in read_database(db, select_sql, [result_id]): name, start, stop, value = row full_dna_sequence = sequence_lookup.get(name) dna_sequence = full_dna_sequence[start:stop] line = '\t'.join([name, str(start), str(stop), str(value), dna_sequence]) result += line + '\n' return result
def find_jobs(db, job_status): """ Find jobs with optional job_status filter. :param db: DatabaseConnection: database to read :param job_status: str: JobStatus property to filter by :return: [CustomJob] jobs found """ result = [] select_sql = "select id, {} from job".format(CustomJob.NON_KEY_FIELDS) params = [] if job_status: select_sql += " WHERE status = %s" params.append(job_status) select_sql += " order by created " for row in read_database(db, select_sql, params): job = CustomJob(row[0]) job._load_non_key(row[1:]) result.append(job) return result
def load(self, db): select_sql = "select {} from job where id = %s".format( self.NON_KEY_FIELDS) rows = read_database(db, select_sql, [self.uuid]) self._load_non_key(rows[0])
def load(self, db): select_sql = "select {} from job where id = %s".format(self.NON_KEY_FIELDS) rows = read_database(db, select_sql, [self.uuid]) self._load_non_key(rows[0])