sg_align.c 15.9 KB
Newer Older
Hoogenboom, Jerry's avatar
Hoogenboom, Jerry committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
/*
 * Copyright (C) 2019 Jerry Hoogenboom
 *
 * This file is based on original code from the TSSV project: Targeted
 * characterisation of short structural variation, version 0.4.0, which was
 * originally made available under the below license.
 *
 * Copyright (c) 2012-2016 Jeroen F.J. Laros <j.f.j.laros@lumc.nl>
 * Copyright (c) 2016 Jerry Hoogenboom <j.hoogenboom@nfi.minvenj.nl>
 * Copyright (c) 2012 Jaap W.F. van der Heijden
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is furnished to do
 * so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
#include <Python.h>

#define METHOD_DOC "align(seq1, seq2, indel_socre)\n"\
                   "\n"\
                   "Return the minimum number of mismatches and the corresponding position by\n"\
                   "aligning seq2 against seq1. Return (len(seq2), 0) if len(seq1) < len(seq2).\n"\
                   "\n"\
                   "An insertion or deletion of one base is counted as indel_score mismatches."


/*
Return the minimum of a and b.
*/
static __inline unsigned char _min(const unsigned char a, const unsigned char b) {
    if (a < b) {
        return a;
    }
    return b;
}


/*
Saturating addition of a and b.
*/
static __inline unsigned char _sadd(const unsigned char a, const unsigned char b) {
    if (a > 255 - b) {
        return 255;
    }
    return a + b;
}


#ifdef DEBUG
#include <stdio.h>
#endif


#if defined(_MSC_VER) || defined(__SSE2__)
/**************************************************************************************************
  SSE2-enabled Implementation
**************************************************************************************************/
#include <xmmintrin.h>
#include <emmintrin.h>

#ifdef _MSC_VER
#define _cpuid(cpuInfo, function_id) __cpuidex(cpuInfo, function_id, 0)
#else
#include <cpuid.h>
static __inline void _cpuid(int cpuInfo[4], int function_id) {
    __cpuid_count(function_id, 0, cpuInfo[0], cpuInfo[1], cpuInfo[2], cpuInfo[3]);
}
#endif

#ifdef DEBUG
#define capitalsonly(x) ((x>= 0x41 && x<=0x5A)? x : 0x2D)
static void _print128seq(__m128i var, char* name) {
    char *val = (char*) &var;
    printf("%s: %c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n", name,
           capitalsonly(val[0]), capitalsonly(val[1]), capitalsonly(val[2]), capitalsonly(val[3]),
           capitalsonly(val[4]), capitalsonly(val[5]), capitalsonly(val[6]), capitalsonly(val[7]),
           capitalsonly(val[8]), capitalsonly(val[9]), capitalsonly(val[10]), capitalsonly(val[11]),
           capitalsonly(val[12]), capitalsonly(val[13]), capitalsonly(val[14]), capitalsonly(val[15]));
}
static void _print128num(__m128i var, char* name) {
    char *val = (char*) &var;
    printf("%s: %3u %3u %3u %3u %3u %3u %3u %3u %3u %3u %3u %3u %3u %3u %3u %3u\n", name,
           val[0] & 0x0FF, val[1] & 0x0FF, val[2] & 0x0FF, val[3] & 0x0FF,
           val[4] & 0x0FF, val[5] & 0x0FF, val[6] & 0x0FF, val[7] & 0x0FF,
           val[8] & 0x0FF, val[9] & 0x0FF, val[10] & 0x0FF, val[11] & 0x0FF,
           val[12] & 0x0FF, val[13] & 0x0FF, val[14] & 0x0FF, val[15] & 0x0FF);
}
#endif


/*
Allocates and returns a pointer to the alignment matrix.
The actual matrix starts at the first 16-byte aligned byte and is stored diagonally.
The seq1len argument MUST NOT be less than the seq2len argument (this is not checked).
*/
static unsigned char *_sse2_make_matrix(const unsigned int seq1len, const unsigned int seq2len,
                                        const unsigned char indel_score) {
    const unsigned int width = (seq2len+31) & ~0x0F,
                       height = seq1len + seq2len + 1;
    unsigned char *mem = malloc(width * height + 16),
                  *matrix = (unsigned char*)(((unsigned long int)mem + 15) & ~(unsigned long int)0x0F),
                  *cell,
                  score;
    unsigned int i, j;

    // Set the first column to 0.
    for (i = 0, cell = matrix; i <= seq1len; i++, cell += width) {
        *cell = 0;
    }

    // Set the first row to 0, 1, 2, 3, 4, ... times the indel_score
    for (i = 0, cell = matrix, score = 0; i <= seq2len; i++, cell += width + 1) {
        *cell = score;
        score = _sadd(score, indel_score);
        for (j = 1; j <= seq2len - i; j++) {
            *(cell + j) = 255;  // This protects the second row.
        }
    }

    return mem;
}


/*
Reverse a sequence. Copies seq to seqr, from right to left. Len is the length of the sequence,
excluding the terminating NUL byte.
*/
static void _revseq(const char *seq, char *seqr, const unsigned int len){
    const char *p = seq;
    char *q = seqr + len;
    *(q--) = 0;
    while (q >= seqr) {
        *(q--) = *(p++);
    }
}


/*
Fill the alignment matrix as created with _sse2_make_matrix().
*/
static void _sse2_align(unsigned char *mem, const unsigned int seq1len, const unsigned int seq2len,
                        const char *seq1, const char *seq2, const unsigned char indel_score) {
    unsigned int x = 1,
                 y = 1,
                 width = (seq2len+31) & ~0x0F,
                 end = seq1len + _min(16, seq2len),
                 limit;
    unsigned char *matrix = (unsigned char*)(((unsigned long int)mem + 15) & ~(unsigned long int)0x0F),
                  *d = matrix,
                  *l = matrix + width,
                  *i = l + width + 1;

    const __m128i ones = _mm_set1_epi8(1),
        indel_scores = _mm_set1_epi8(indel_score);
    __m128i md = _mm_load_si128((__m128i*)d),
            ml = _mm_load_si128((__m128i*)l),
            mu = _mm_loadu_si128((__m128i*)(l + 1)),
            mi,
            mx,
            my;

    // Get copy of seq2 and reverse of seq1, making sure
    // that we can read 16 bytes (of garbage) past the end.
    char *seq1r = malloc(seq1len + 16),
         *seq2f = malloc(seq2len + 16);
    strcpy(seq2f, seq2);
    _revseq(seq1, seq1r, seq1len);
    mx = _mm_loadu_si128((__m128i*)seq2f);
    my = _mm_loadu_si128((__m128i*)(seq1r + seq1len - 1));

#ifdef DEBUG
    printf("Matrix size %ux%u (%ux%u)\n", seq2len + 1, seq1len + 1, width, seq1len + seq2len + 1);
    _print128seq(mx, "start x");
    _print128seq(my, "start y");
#endif

    while (y < end) {
        mi = _mm_min_epu8(_mm_adds_epu8(_mm_min_epu8(ml, mu), indel_scores),
             _mm_adds_epu8(md, _mm_add_epi8(_mm_cmpeq_epi8(mx, my), ones)));
        _mm_storeu_si128((__m128i*)i, mi);

#ifdef DEBUG
        printf("(%2u,%2u) d=%u l=%u i=%u end=%u\n", x, y, d-matrix, l-matrix, i-matrix, end);
        _print128num(md, "md");
        _print128num(ml, "ml");
        _print128num(mu, "mu");
        _print128num(mi, "mi");
#endif

        if (++y < end) {
            // Move down one row.
            l += width;
            i += width;
            md = ml;
            mu = mi;
            ml = _mm_load_si128((__m128i*)l);

            // Move to the next base in seq1r.
            my = _mm_slli_si128(my, 1);

            limit = y - x;
            if (limit < seq1len) {
                my = _mm_insert_epi16(my, *(short*)(seq1r + seq1len - limit - 1), 0);
            }

#ifdef DEBUG
            _print128seq(my, "shift y");
#endif

        }
        else if ((x += 16) <= seq2len) {
            // Move right 16 columns.
            y = x;
            end += _min(16, seq2len + 1 - x);
            d += 16 * width + 16;
            l = d + width;
            i = l + width + 1;
            md = _mm_load_si128((__m128i*)d);
            ml = _mm_load_si128((__m128i*)l);
            mu = _mm_loadu_si128((__m128i*)(l + 1));
            mx = _mm_loadu_si128((__m128i*)(seq2f + x - 1));
            my = _mm_loadu_si128((__m128i*)(seq1r + seq1len - 1));

#ifdef DEBUG
            _print128seq(mx, "jump  x");
            _print128seq(my, "jump  y");
#endif

        }
        else break;
    }

    free(seq2f);
    free(seq1r);

#ifdef DEBUG
    // Print matrix.
    for (y = 0; y < seq1len + seq2len + 1; y++) {
        for (x = 0; x < width; x++) {
            printf("%4i ", *(mem++));
        }
        printf("\n");
    }
    printf("         ");
    i = matrix;
    for (x = 0; x <= seq2len - 1; x++) {
        printf("%4c ", seq2[x]);
    }
    for (y = 0; y <= seq1len; y++) {
        i = matrix + y * width;
        printf("\n");
        if (y == 0) printf("     ");
        else printf("%4c ", seq1[y-1]);
        for (x = 0; x <= seq2len; x++, i += width + 1) {
            printf("%4i ", *i);
        }
    }
    printf("\n");
#endif
}


/*
Return the smallest possible sequence distance, along with the ending position inside seq1.

Oprates on an alignment matrix as created with _sse2_make_matrix().
*/
static PyObject *_sse2_find_min(unsigned char *mem, const unsigned int seq1len, const unsigned int seq2len) {
    const unsigned int width = (seq2len+31) & ~0x0F;
    unsigned char *matrix = (unsigned char*)(((unsigned long int)mem + 15) & ~(unsigned long int)0x0F);
    unsigned int distance = seq2len;
    unsigned int position = 0;
    unsigned int i;

    unsigned char *cell = matrix + seq2len * width + seq2len;
    for (i = 0; i <= seq1len; i++, cell += width) {
        if (*cell < distance) {
            distance = *cell;
            position = i;
        }
    }

    return Py_BuildValue("II", distance, position);
}


/*
Align two sequences, finding one in the other.

Input arguments are two Python strings and a Python integer.
The second string will be searched inside the first.
The integer specifies the indel penalty score.

Return two python integers: the sequence distance, and the position of the match.
*/
static PyObject *sse2_align(PyObject *self, PyObject *args) {
    // Parse Python arguments.
    const char *seq1, *seq2;
    const unsigned int seq1len, seq2len;
    const unsigned char indel_score;
    unsigned char *matrix;
    PyObject *result;
    if (!PyArg_ParseTuple(args, "s#s#b", &seq1, &seq1len, &seq2, &seq2len, &indel_score)) {
        return NULL;
    }

    // This algorithm is designed to find seq2 in seq1.
    // Give the most pessimistic possible score if seq1 is shorter.
    if (seq1len < seq2len) {
        return Py_BuildValue("II", seq2len, 0);
    }

    matrix = _sse2_make_matrix(seq1len, seq2len, indel_score);
    _sse2_align(matrix, seq1len, seq2len, seq1, seq2, indel_score);
    result = _sse2_find_min(matrix, seq1len, seq2len);
    free(matrix);
    return result;
}


#endif

/**************************************************************************************************
  Plain Implementation
**************************************************************************************************/

/*
Allocates and returns a pointer to the alignment matrix.
*/
static unsigned char *_make_matrix(const unsigned int rows, const unsigned int columns,
                                   const unsigned char indel_score) {
    unsigned char *matrix = malloc(rows * columns * sizeof(char));
    unsigned char score = 0;
    unsigned int i;
    for (i = 1; i < rows; i++) {
        *(matrix + i*columns) = 0;
    }
    for (i = 0; i < columns; i++) {
        *(matrix + i) = score;
        score = _sadd(score, indel_score);
    }
    return matrix;
}


/*
Fill the alignment matrix as created with _make_matrix().
*/
static void _align(unsigned char *matrix, const unsigned int rows, const unsigned int columns,
                   const char *seq1, const char *seq2, const unsigned char indel_score) {
    unsigned int r, c;
    unsigned char *d = matrix,
                  *l = d + 1,
                  *u = d + columns,
                  *i = l + columns;
    for (r = 1; r < rows; r++, d++, l++, u++, i++) {
        for (c = 1; c < columns; c++, d++, l++, u++, i++) {
            *i = _min(
                _sadd(_min(*l, *u), indel_score),
                _sadd(*d, seq1[r - 1] != seq2[c - 1]));
        }
    }

#ifdef DEBUG
    // Print matrix.
    printf("         ");
    for (c = 0; c < columns - 1; c++) {
        printf("%4c ", seq2[c]);
    }
    for (r = 0; r < rows; r++) {
        printf("\n");
        if (r == 0) printf("     ");
        else printf("%4c ", seq1[r - 1]);
        for (c = 0; c < columns; c++) {
            printf("%4i ", *(matrix + r*columns + c));
        }
    }
    printf("\n");
#endif

}


/*
Return the smallest possible sequence distance, along with the ending position inside seq1.

Oprates on an alignment matrix as created with _make_matrix().
*/
static PyObject *_find_min(unsigned char *matrix, const unsigned int rows, const unsigned int columns) {
    unsigned int distance = columns - 1;
    unsigned int position = 0;
    unsigned int r;

    for (r = 0; r < rows; r++) {
        if (*(matrix + r*columns + columns-1) < distance) {
            distance = *(matrix + r*columns + columns-1);
            position = r;
        }
    }

    return Py_BuildValue("II", distance, position);
}


/*
Align two sequences, finding one in the other.

Input arguments are two Python strings and a Python integer.
The second string will be searched inside the first.
The integer specifies the indel penalty score.

Return two python integers: the sequence distance, and the position of the match.
*/
static PyObject *align(PyObject *self, PyObject *args) {
    // Parse Python arguments.
    const char *seq1, *seq2;
    const unsigned int seq1len, seq2len;
    const unsigned char indel_score;
    unsigned int rows, columns;
    unsigned char *matrix;
    PyObject *result;
    if (!PyArg_ParseTuple(args, "s#s#b", &seq1, &seq1len, &seq2, &seq2len, &indel_score)) {
        return NULL;
    }

    // This algorithm is designed to find seq2 in seq1.
    // Give the most pessimistic possible score if seq1 is shorter.
    if (seq1len < seq2len) {
        return Py_BuildValue("II", seq2len, 0);
    }

    // Allocate memory for the matrix.
    rows = seq1len + 1;
    columns = seq2len + 1;
    matrix = _make_matrix(rows, columns, indel_score);

    // Perform the alignment.
    _align(matrix, rows, columns, seq1, seq2, indel_score);
    result = _find_min(matrix, rows, columns);

    // Free the matrix memory and return the result.
    free(matrix);
    return result;
}


/*************************************************************************************************/


static PyMethodDef AlignerMethods[] = {
    {"align",  align, METH_VARARGS, METHOD_DOC},
    {"_simple_align",  align, METH_VARARGS, METHOD_DOC},
#if defined(_MSC_VER) || defined(__SSE2__)
    {"_sse2_align",  sse2_align, METH_VARARGS, METHOD_DOC},
#endif
    {NULL, NULL, 0, NULL}  /* Sentinel */
};


PyMODINIT_FUNC initsg_align(void) {
#if defined(_MSC_VER) || defined(__SSE2__)
    int cpuInfo[4];
    _cpuid(cpuInfo, 1);
#ifdef DEBUG
    printf("CPUID: %02x %02x %02x %02x\n", cpuInfo[0], cpuInfo[1], cpuInfo[2], cpuInfo[3]);
#endif
    if (cpuInfo[3] & (1 << 26)) {
        AlignerMethods[0].ml_meth = sse2_align;
    }
#endif

    (void) Py_InitModule("sg_align", AlignerMethods);
}