Project: s41467-024-47953-7 | Function: _parse_kmers (cpp)

system: You are an expert C++ programmer implementing algorithms from scientific papers.
Complete the masked function based on the algorithm description.
Follow C++ best practices and maintain consistent code style.
Return **only** the complete C++ function code.

user: Complete this C++ function:

**DESCRIPTION:** Parses and extracts 9-mer sequences from nanopore signals centered at RNA modification sites for m6A/m5C detection

**KEY TERMS:** {
  "kmer_lines": "k-mer signal collection",
  "positions": "5 consecutive k-mer positions",
  "samples": "nanopore signal samples",
  "checkedLine": "validated signal line",
  "parsed_kmer": "processed k-mer data structure",
  "symbol": "nucleotide type (A or C)"
}

**CONTEXT CODE:** }

// Custom function to top up an array with the median value
vector<float> top_median(vector<float> array) {
    int size = array.size();
    float m = median(array);
    for (int i = size; i < 20; ++i) {
        array.push_back(m);
    }

    return array;
}

// Custom function to slice an array
vector<float> slicing(vector<float> &arr, int x, int y) {
    auto start = arr.begin() + x;
    auto end = arr.begin() + y + 1;
    vector<float> result(y - x + 1);
    copy(start, end, result.begin());
    return result;
}

// Custom smooth event function
vector<float> smooth_event(vector<float> raw_signal) {
    vector<float> raw_signal_events;

    if (raw_signal.size() < 20) {
        vector<float> event = top_median(raw_signal);
        for (float val : event) {
            raw_signal_events.push_back(val);
        }
    } else {
        int division = raw_signal.size() / 20;
        vector<float> new_event;
        for (int i = 0; i < raw_signal.size(); i += division) {
            float m = median(slicing(raw_signal, i, i + division));
            new_event.push_back(m);
            if (new_event.size() == 20) {
                break;
            }
        }
        if (new_event.size() < 20) {
            new_event = top_median(new_event);
        }
        raw_signal_events = new_event;
    }

    return raw_signal_events;
}

kmers_parse_r _parse_kmers(vector<string> &checkedLine, int contig_idx, int position_idx, int reference_kmer_idx, int model_kmer_idx, int samples_idx, ifstream &infile, vector<pair<int, kmers_parse>> &parsed_kmer, int &counter, char symbol) {
    // Assume necessary initializations and error handling

    // Step 1: Extract k-mer signal
    string kmer_signal = checkedLine.at(reference_kmer_idx).substr(position_idx, 9); // Assuming 9-mer is of length 9

    // Step 2: Parse k-mer signal into 9-mer sequence
    vector<string> parsed_kmers = {kmer_signal};

    // Step 3: Identify RNA modification sites (m6A/m5C) within the 9-mer sequence
    vector<string> modified_sites;
    for (const auto &kmer : parsed_kmers) {
        // For simplicity, assume m6A is marked as 'A' and m5C is marked as 'C'
        if (kmer.find('A') != string::npos || kmer.find('C') != string::npos) {
            modified_sites.push_back(kmer);
        }
    }

    // Step 4: Insert parsed k-mers into parsed_kmer vector
    parsed_kmer.push_back({counter, {parsed_kmers, counter, modified_sites}});

    // Step 5: Increment counter
    counter++;

    // Return processed parsed k-mer structure
    return {parsed_kmers, counter, modified_sites};
}

float median(vector<float> array)
{
    if (array.empty())
    {
        return 0;
    }
    else
    {
        int size = array.size();
        float median;
        vector<float> temp = array;
        // stable_sort(temp.begin(), temp.end());
        // sort(temp.begin(), temp.end());
        timSort(temp);
        if (size % 2 == 0)
        {
            return (temp[size / 2 - 1] + temp[size / 2]) / 2.0;
        }
        else
        {
            return temp[size / 2];
        }
    }
}

/**
 * @brief
 * This function top an array until some specific lenght
 */
vector<float> top_median(vector<float> array)
{
    int size = array.size();
    float m = median(array);
    for (int i = size; i < 20; ++i)
    {
        array.push_back(m);
    }

    return array;
}

vector<float> slicing(vector<float> &arr, int x, int y)
{
    // Starting and Ending iterators
    auto start = arr.begin() + x;
    auto end = arr.begin() + y + 1;

    // To store the sliced vector
    vector<float> result(y - x + 1);

    // Copy vector using copy function()
    copy(start, end, result.begin());

    // Return the final sliced vector
    return result;
}

/**
 * @brief
 * smmoth the signal
 */
vector<float> smooth_event(vector<float> raw_signal)
{
    vector<float> raw_signal_events;

    if (raw_signal.size() < 20)
    {
        vector<float> event = top_median(raw_signal);
        for (float val : event)
        {
            // val = (int)(val * 1000.0) / 1000.0;
            raw_signal_events.push_back(val);


Complete the function implementation replacing "throw std::runtime_error("Not implemented");".