<commit_before>#include <Python.h>
#include "numpy/arrayobject.h"
#include <string.h>
#include <stdio.h>

struct align_settings {
    int is_global;
    int v;  // gap opening penalty
    int u;  // gap extension penalty
    int l;  // alphabet length
    const char * alphabet;
    int * d;  // weighting function
};

struct align_output {
    const char * aligned_seq1;
    const char * aligned_seq2;
    int alignment_score;
};


void map_ascii_to_alphabet (int * map, const char * alphabet) {
    for (int i = 0; i < 256; i++) {
        map[i] = -1;  // if left at -1, indicates non-alphabet character in sequence
    }
    for (int i = 0; i < (int) strlen(alphabet); i++) {
        map[(int)alphabet[i]] = i;  // e.g., "A" in sequence indexes into map[65] = 0
    }
}

int * encode_sequence (const char * seq, int * map) {
    int seqlen = (int) strlen(seq);
    int * encoded[seqlen];
    for (int i=0; i < seqlen; i++) {
        encoded[i] = map[(int)seq[i]];
    }
}



// main wrapper function
struct align_output align(const char * seq1, const char * seq2, struct align_settings m) {
    int map[256] = {0};
    struct align_output o;

    // 1. convert sequences into integer indices into alphabet
    map_ascii_to_alphabet(map, m.alphabet);

    int * sA = encode_sequence(seq1, map);
    int * sB = encode_sequence(seq2, map);

    // 2. generate D matrix


    // X. decode aligned integer sequences into alphabet sequences
    o.aligned_seq1 = seq1;
    o.aligned_seq2 = seq2;
    o.alignment_score = 0;
    return o;
}

static PyObject * align_wrapper(PyObject * self, PyObject * args) {
    const char * alphabet;
    const char * seq1;
    const char * seq2;
    int gop, gep, is_global;

    struct align_settings my_settings;<commit_msg>I think sequence encoding is working, now starting to implement matrix computation<commit_after>#include <Python.h>
#include "numpy/arrayobject.h"
#include <string.h>
#include <stdio.h>

struct align_settings {
    int is_global;
    int v;  // gap opening penalty
    int u;  // gap extension penalty
    int l;  // alphabet length
    const char * alphabet;
    int * d;  // weighting function
};

struct align_output {
    const char * aligned_seq1;
    const char * aligned_seq2;
    int alignment_score;
};


void map_ascii_to_alphabet (int * map, const char * alphabet) {
    for (int i = 0; i < 256; i++) {
        map[i] = -1;  // if left at -1, indicates non-alphabet character in sequence
    }
    for (int i = 0; i < (int) strlen(alphabet); i++) {
        map[(int)alphabet[i]] = i;  // e.g., "A" in sequence indexes into map[65] = 0
    }
}

void encode_sequence (const char * seq, int * map, int * encoded) {
    int seqlen = (int) strlen(seq);
    for (int i=0; i < seqlen; i++) {
        encoded[i] = map[(int)seq[i]];
    }
}


void populate_D_matrix(int * d, int nrows, int ncols, struct align_settings s) {
    int max_dim = nrows;
    if (nrows < ncols) {
        max_dim = ncols;
    }

    // D(0,0) = 0
    d[0] = 0;

    // linearize cache matrices so that (m,n) = (m * ncols + n)
    int p[nrows*ncols];
    int q[nrows*ncols];

    // initialize left column
    for (int m = 0; m < nrows; m++) {
        d[m*ncols] = p[m*ncols] = s.is_global ? (s.v+s.u*m) : 0;
    }

    // initialize the top row
    for (int n = 0; n < ncols; n++) {
        d[n] = q[n] = s.is_global ? (s.v+s.u*n) : 0;
    }

    // iterate through D matrix by diagonals
    // http://stackoverflow.com/questions/1779199/traverse-matrix-in-diagonal-strips
    for (int z, slice=0; slice<(2*max_dim-1); slice++) {
        z = (slice < max_dim) ? 0 : (slice-max_dim+1);  // which side are we on?
        for (int j=z; j<=slice-z; )
    }

    for (int m = 1; m < nrows; m++) {
        for (int n = 1; n < ncols; n++) {
            d[m*ncols+n] = 0;
        }
    }

}


// main wrapper function
struct align_output align(const char * seq1, const char * seq2, struct align_settings m) {
    int map[256] = {0};
    int l1 = (int) strlen(seq1);
    int l2 = (int) strlen(seq2);
    int sA[l1];
    int sB[l2];

    int d[(l1+1)*(l2+1)];
    struct align_output o;

    // 1. convert sequences into integer indices into alphabet
    map_ascii_to_alphabet(map, m.alphabet);

    encode_sequence(seq1, map, sA);
    encode_sequence(seq2, map, sB);

    // 2. generate D matrix
    populate_D_matrix(d, l1+1, l2+1, m);

    for (int i=0; i<l1+1; i++) {
        for (int j=0; j<l2+1; j++) {
            fprintf(stdout, "%d ", d[i*(l2+1)+j]);
        }
        fprintf(stdout, "\n");
    }

    // TODO: decode aligned integer sequences into alphabet sequences
    o.aligned_seq1 = seq1;
    o.aligned_seq2 = seq2;
    o.alignment_score = 0;
    return o;
}

static PyObject * align_wrapper(PyObject * self, PyObject * args) {
    const char * alphabet;
    const char * seq1;
    const char * seq2;
    int gop, gep, is_global;

    struct align_settings my_settings;
{'fraction_non_alphanumeric': 0.1394827937283649, 'fraction_numerical': 0.021584198737528, 'mean_word_length': 2.521146953405018, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2089164', 'n_tokens_mistral': 1840, 'n_tokens_neox': 1751, 'n_words': 524}