Example #1
0
def get_a_sequence_from_dataset():
    request_id = request.args.get('requestID', type=int)
    from_data_set = request.args.get('fromDataset', 1, type=int)
    q_seq = request.args.get('qSeq', type=int)
    q_start = request.args.get('qStart', -1, type=int)
    q_end = request.args.get('qEnd', -1, type=int)
    with lock:
        ds_index = current_ds_index if from_data_set else current_q_index

        ds_length = onex.getDatasetSeqCount(ds_index)
        if (q_seq < 0 or q_seq >= ds_length):
            raise InvalidUsage('Sequence index is out of bound')

        seq_length = onex.getDatasetSeqLength(ds_index)
        if (q_start < 0) or (q_end < 0):
            q_start = 0
            q_end = seq_length - 1

        app.logger.debug('Get sequence %d (%d, %d), fromDataSet = %s', q_seq,
                         q_start, q_end, from_data_set)

        query = _to_string(onex.getSubsequence(ds_index, q_seq, q_start,
                                               q_end))

        return jsonify(query=query, requestID=request_id)
Example #2
0
def api_dataset_init():
    global current_collection_index, current_ds_index
    request_id = request.args.get('requestID', type=int)
    ds_collection_index = request.args.get('dsCollectionIndex', type=int)
    st = request.args.get('st', 0.2, type=float)

    with lock:
        if ds_collection_index >= len(datasets) or ds_collection_index < 0:
            raise InvalidUsage('Dataset collection index out of bound')

        if st < 0:
            raise InvalidUsage('Invalid similarity threshold value')

        # Unload the current dataset in memory
        if current_ds_index != -1:
            onex.unloadDataset(current_ds_index)
            app.logger.debug('Unloaded dataset %d', current_collection_index)

        # Load the new dataset
        current_collection_index = ds_collection_index
        ds_path = str(datasets[current_collection_index].get('path'))
        ds_name = str(datasets[current_collection_index].get('name'))
        ds_metadata = datasets[current_collection_index].get('metadata')
        current_ds_index = onex.loadDataset(ds_path)

        metadata = None
        if ds_metadata:
            with open(ds_metadata) as metadata_file:
                metadata = json.load(metadata_file)
        else:
            app.logger.info('No metadata found for dataset %s', ds_name)

        app.logger.debug('Loaded dataset %d [%s]', current_collection_index,
                         ds_name)

        # Normalize the new dataset
        app.logger.debug('Normalizing dataset %d', current_collection_index)
        normalization = onex.normalizeDataset(current_ds_index)

        normalization = {'max': normalization[0], 'min': normalization[1]}
        app.logger.info('Normalized dataset %d', current_collection_index)

        # Group the new dataset
        app.logger.debug('Grouping dataset %d with st = %f',
                         current_collection_index, st)
        num_groups = onex.groupDataset(current_ds_index, st)
        app.logger.info('Grouped dataset %d with st = %f. Created %d groups',
                        current_collection_index, st, num_groups)

        # Return number of sequences in the dataset
        ds_length = onex.getDatasetSeqCount(current_ds_index)

        return jsonify(dsLength=ds_length,
                       metadata=metadata,
                       normalization=normalization,
                       numGroups=num_groups,
                       requestID=request_id)
Example #3
0
def api_find_best_match():
    request_id = request.args.get('requestID', type=int)
    ds_collection_index = request.args.get('dsCollectionIndex', type=int)
    q_find_with_custom_query = request.args.get('qFindWithCustomQuery',
                                                0,
                                                type=int)
    q_seq = request.args.get('qSeq', type=int)
    q_start = request.args.get('qStart', type=int)
    q_end = request.args.get('qEnd', type=int)

    if q_start > q_end or q_start < 0 or q_end < 0:
        raise InvalidUsage('Invalid starting and ending position')

    with lock:
        if not (ds_collection_index == current_collection_index):
            raise InvalidUsage(
                'Dataset {} is not loaded yet'.format(ds_collection_index))

        # Index of the dataset containing the query, by default set to the same dataset
        # where the best match will be searched from
        q_ds_index = current_ds_index
        if q_find_with_custom_query:
            if current_q_index == -1:
                raise InvalidUsage('No custom query is loaded')
            # If find with custom query, set to the dataset containing the custom query
            q_ds_index = current_q_index

        # Get number of sequences in the database containing the query
        q_ds_length = onex.getDatasetSeqCount(q_ds_index)
        if q_seq < 0 or q_seq >= q_ds_length:
            raise InvalidUsage('Sequence index is out of bound')

        seq_length = onex.getDatasetSeqLength(q_ds_index)
        if q_start >= seq_length or q_end >= seq_length:
            raise InvalidUsage('Invalid starting and ending position')

        if q_find_with_custom_query:
            app.logger.debug(
                'Look for best match with sequence %d (%d:%d) in the custom query',
                q_seq, q_start, q_end)
        else:
            app.logger.debug(
                'Look for best match with sequence %d (%d:%d) in dataset %d',
                q_seq, q_start, q_end, current_collection_index)

        r_dist, r_seq, r_start, r_end = \
          onex.findSimilar(current_ds_index, q_ds_index, q_seq, q_start, q_end, 0, -1)

        group_index = onex.getGroupIndex(current_ds_index, r_seq, r_start,
                                         r_end)
        app.logger.debug('Result group index: (%d, %d)', group_index[0],
                         group_index[1])

        result = _to_string(
            onex.getSubsequence(current_ds_index, r_seq, r_start, r_end))
        warpingPath = onex.getWarpingPath(q_ds_index, q_seq, q_start, q_end,
                                          current_ds_index, r_seq, r_start,
                                          r_end)

        return jsonify(result=result,
                       groupIndex=group_index,
                       warpingPath=warpingPath,
                       dist=r_dist,
                       dsName=datasets[current_collection_index],
                       seq=r_seq,
                       start=r_start,
                       end=r_end,
                       requestID=request_id)